Class: SY::Dimension
- Inherits:
-
Object
- Object
- SY::Dimension
- Defined in:
- lib/sy/dimension.rb
Overview
This class represents physical dimension of a metrological quantity.
Class Attribute Summary collapse
-
.standard_quantities ⇒ Object
readonly
Returns the value of attribute standard_quantities.
Class Method Summary collapse
- .__new__ ⇒ Object
-
.base(symbol) ⇒ Object
(also: basic)
Base dimension constructor.
-
.instances ⇒ Object
Presents class-owned instances (array).
-
.new(dim = {}) ⇒ Object
The #new constructor of SY::Dimension has been changed, so that the same instance is returned, if that dimension has already been created.
-
.zero ⇒ Object
Constructor for zero dimension (“dimensionless”).
Instance Method Summary collapse
-
#*(number) ⇒ Object
Dimension arithmetic: multiplication by a number.
-
#+(other) ⇒ Object
Dimension arithmetic: addition.
-
#-(other) ⇒ Object
Dimension arithmetic: subtraction.
-
#/(number) ⇒ Object
Dimension arithmetic: division by a number.
-
#==(other) ⇒ Object
Two dimensions are equal, if their exponents are equal.
-
#[](ß) ⇒ Object
#[] method provides access to the dimension components, such as d = Dimension.new( L: 1, T: -2 ), d #=> -2.
-
#base? ⇒ Boolean
(also: #basic?)
True if the dimension is basic, otherwise false.
-
#initialize(hash) ⇒ Dimension
constructor
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⁻²” ).
-
#inspect ⇒ Object
Produces the inspect string of the dimension.
-
#standard_quantity ⇒ Object
Returns dimension’s standard quantity.
-
#to_a ⇒ Object
Conversion to an array (of exponents of in the order of the basic dimension letters).
-
#to_composition ⇒ Object
Returns default quantity composition for this dimension.
-
#to_hash ⇒ Object
Conversion to a hash (eg. { L: 1, M: 0, T: -2, Q: 0, Θ: 0 } ).⁻³.
-
#to_s ⇒ Object
Converts the dimension into its SPS.
-
#zero? ⇒ Boolean
True if the dimension is zero (“dimensionless”), otherwise false.
Constructor Details
#initialize(hash) ⇒ Dimension
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.
66 67 68 69 70 |
# File 'lib/sy/dimension.rb', line 66 def initialize hash SY::BASE_DIMENSIONS.base_symbols.each { |ß| instance_variable_set "@#{ß}", hash[ß] } end |
Class Attribute Details
.standard_quantities ⇒ Object (readonly)
Returns the value of attribute standard_quantities.
21 22 23 |
# File 'lib/sy/dimension.rb', line 21 def standard_quantities @standard_quantities end |
Class Method Details
.__new__ ⇒ Object
20 |
# File 'lib/sy/dimension.rb', line 20 alias __new__ new |
.base(symbol) ⇒ Object Also known as: basic
Base dimension constructor. Base dimension symbol is expeced as argument.
46 47 48 49 50 |
# 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 |
.instances ⇒ Object
Presents class-owned instances (array).
42 |
# File 'lib/sy/dimension.rb', line 42 def instances; return @instances ||= [] end |
.new(dim = {}) ⇒ Object
The #new constructor of SY::Dimension has been changed, so that the same instance is returned, if that dimension has already been created.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |
.zero ⇒ Object
Constructor for zero dimension (“dimensionless”).
55 |
# File 'lib/sy/dimension.rb', line 55 def zero; new end |
Instance Method Details
#*(number) ⇒ Object
Dimension arithmetic: multiplication by a number.
104 105 106 107 108 |
# 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 |
#+(other) ⇒ Object
Dimension arithmetic: addition. (+, -, *, /).
88 89 90 91 92 |
# 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 |
#-(other) ⇒ Object
Dimension arithmetic: subtraction.
96 97 98 99 100 |
# 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 |
#/(number) ⇒ Object
Dimension arithmetic: division by a number.
112 113 114 115 116 117 118 119 |
# 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 |
#==(other) ⇒ Object
Two dimensions are equal, if their exponents are equal.
82 83 84 |
# File 'lib/sy/dimension.rb', line 82 def == other to_a == other.to_a end |
#[](ß) ⇒ Object
#[] method provides access to the dimension components, such as d = Dimension.new( L: 1, T: -2 ), d #=> -2
75 76 77 78 |
# File 'lib/sy/dimension.rb', line 75 def [] ß return send ß if SY::BASE_DIMENSIONS.letters.include? ß raise ArgumentError, "Unknown basic dimension: #{ß}" end |
#base? ⇒ Boolean Also known as: basic?
True if the dimension is basic, otherwise false.
144 145 146 147 |
# 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 |
#inspect ⇒ Object
Produces the inspect string of the dimension.
159 160 161 |
# File 'lib/sy/dimension.rb', line 159 def inspect "#<SY::Dimension: #{self} >" end |
#standard_quantity ⇒ Object
Returns dimension’s standard quantity.
165 166 167 |
# File 'lib/sy/dimension.rb', line 165 def standard_quantity self.class.standard_quantities[ self ] end |
#to_a ⇒ Object
Conversion to an array (of exponents of in the order of the basic dimension letters).
124 125 126 |
# File 'lib/sy/dimension.rb', line 124 def to_a SY::BASE_DIMENSIONS.base_symbols.map { |l| self.send l } end |
#to_composition ⇒ Object
Returns default quantity composition for this dimension.
171 172 173 174 175 176 177 |
# 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 |
#to_hash ⇒ Object
Conversion to a hash (eg. { L: 1, M: 0, T: -2, Q: 0, Θ: 0 } ).⁻³
130 131 132 133 134 |
# 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 |
#to_s ⇒ Object
Converts the dimension into its SPS.
152 153 154 155 |
# File 'lib/sy/dimension.rb', line 152 def to_s sps = SY::SPS.( SY::BASE_DIMENSIONS.base_symbols, to_a ) return sps == "" ? "∅" : sps end |
#zero? ⇒ Boolean
True if the dimension is zero (“dimensionless”), otherwise false.
138 139 140 |
# File 'lib/sy/dimension.rb', line 138 def zero? to_a.all? { |e| e.zero? } end |