class SY::Dimension
This class represents physical dimension of a metrological quantity.
Attributes
Public Class Methods
Base dimension constructor. Base dimension symbol is expeced as argument.
# File lib/sy/dimension.rb, line 46 def base symbol raise ArgumentError, "Unknown base dimension: #{symbol}" unless SY::BASE_DIMENSIONS.base_symbols.include? symbol return new( symbol => 1 ) end
Presents class-owned instances (array).
# File lib/sy/dimension.rb, line 42 def instances; return @instances ||= [] end
The new constructor of SY::Dimension
has been changed, so that the same instance is returned, if that dimension has already been created.
# File lib/sy/dimension.rb, line 26 def new dim={} ꜧ = case dim when Hash then dim when self then return dim # it is a dimension instance else # we'll treat dimension_specification as SPS SY::BASE_DIMENSIONS.parse_sps( dim ) end # Zeros by default: ꜧ.default! Hash[ SY::BASE_DIMENSIONS.base_symbols.map { |ß| [ß, 0] } ] # Now we can see whether the instance of this dimension already exists. return instances.find { |inst| inst.to_hash == ꜧ } || __new__( ꜧ ).tap { |inst| instances << inst } end
Dimension
can be initialized either by supplying a hash (such as Dimension.new
L: 1, T: -2) or by supplying a SPS (superscripted product string), such as Dimension.new
( “L.T⁻²” ). It is also possible to supply a Dimension
instance, which will result in a new Dimension
instance equal to the supplied one.
# File lib/sy/dimension.rb, line 66 def initialize hash SY::BASE_DIMENSIONS.base_symbols.each { |ß| instance_variable_set "@#{ß}", hash[ß] } end
Constructor for zero dimension (“dimensionless”).
# File lib/sy/dimension.rb, line 55 def zero; new end
Public Instance Methods
Dimension
arithmetic: multiplication by a number.
# File lib/sy/dimension.rb, line 104 def * number ç.new Hash[ SY::BASE_DIMENSIONS.base_symbols.map do |l| [ l, self.send( l ) * number ] end ] end
Dimension
arithmetic: addition. (+, -, *, /).
# File lib/sy/dimension.rb, line 88 def + other ç.new Hash[ SY::BASE_DIMENSIONS.base_symbols.map do |l| [ l, self.send( l ) + other.send( l ) ] end ] end
Dimension
arithmetic: subtraction.
# File lib/sy/dimension.rb, line 96 def - other ç.new Hash[ SY::BASE_DIMENSIONS.base_symbols.map do |l| [ l, self.send( l ) - other.send( l ) ] end ] end
Dimension
arithmetic: division by a number.
# File lib/sy/dimension.rb, line 112 def / number ç.new Hash[ SY::BASE_DIMENSIONS.base_symbols.map do |l| exp = send l raise TErr, "Dimensions with rational exponents " + "not implemented!" if exp % number != 0 [ l, exp / number ] end ] end
Two dimensions are equal, if their exponents are equal.
# File lib/sy/dimension.rb, line 82 def == other to_a == other.to_a end
[]
method provides access to the dimension components, such as d = Dimension.new
( L: 1, T: -2 ), d #=> -2
# File lib/sy/dimension.rb, line 75 def [] ß return send ß if SY::BASE_DIMENSIONS.letters.include? ß raise ArgumentError, "Unknown basic dimension: #{ß}" end
True if the dimension is basic, otherwise false.
# File lib/sy/dimension.rb, line 144 def base? to_a.count( 1 ) == 1 && to_a.count( 0 ) == SY::BASE_DIMENSIONS.base_symbols.size - 1 end
Produces the inspect string of the dimension.
# File lib/sy/dimension.rb, line 159 def inspect "#<SY::Dimension: #{self} >" end
Returns dimension's standard quantity.
# File lib/sy/dimension.rb, line 165 def standard_quantity self.class.standard_quantities[ self ] end
Conversion to an array (of exponents of in the order of the basic dimension letters).
# File lib/sy/dimension.rb, line 124 def to_a SY::BASE_DIMENSIONS.base_symbols.map { |l| self.send l } end
Returns default quantity composition for this dimension.
# File lib/sy/dimension.rb, line 171 def to_composition SY::Composition[ SY::BASE_DIMENSIONS.base_symbols .map { |l| self.class.base l } .map { |dim| self.class.standard_quantities[ dim ] } .map { |qnt| qnt.absolute } .zip( to_a ).delete_if { |qnt, exp| exp.zero? } ] end
Conversion to a hash (eg. { L: 1, M: 0, T: -2, Q: 0, Θ: 0 } ).⁻³
# File lib/sy/dimension.rb, line 130 def to_hash SY::BASE_DIMENSIONS.base_symbols.each_with_object Hash.new do |l, ꜧ| ꜧ[ l ] = self.send( l ) end end
Converts the dimension into its SPS.
# File lib/sy/dimension.rb, line 152 def to_s sps = SY::SPS.( SY::BASE_DIMENSIONS.base_symbols, to_a ) return sps == "" ? "∅" : sps end
True if the dimension is zero (“dimensionless”), otherwise false.
# File lib/sy/dimension.rb, line 138 def zero? to_a.all? { |e| e.zero? } end