Class: SY::Dimension

Inherits:
Object
  • Object
show all
Defined in:
lib/sy/dimension.rb

Overview

This class represents physical dimension of a metrological quantity.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



67
68
69
70
71
# File 'lib/sy/dimension.rb', line 67

def initialize hash
  SY::BASE_DIMENSIONS.base_symbols.each { |

Class Attribute Details

.standard_quantitiesObject (readonly)

Returns the value of attribute standard_quantities.



20
21
22
# File 'lib/sy/dimension.rb', line 20

def standard_quantities
  @standard_quantities
end

Class Method Details

.__new__Object



19
# File 'lib/sy/dimension.rb', line 19

alias __new__ new

.base(symbol) ⇒ Object Also known as: basic

Base dimension constructor. Base dimension symbol is expeced as argument.

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/sy/dimension.rb', line 47

def base symbol
  raise ArgumentError, "Unknown base dimension: #{symbol}" unless
    SY::BASE_DIMENSIONS.base_symbols.include? symbol
  return new( symbol => 1 )
end

.instancesObject

Presents class-owned instances (array).



41
42
43
# File 'lib/sy/dimension.rb', line 41

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sy/dimension.rb', line 25

def new dim={}
  

.zeroObject

Constructor for zero dimension (“dimensionless”).



56
# File 'lib/sy/dimension.rb', line 56

def zero; new end

Instance Method Details

#*(number) ⇒ Object

Dimension arithmetic: multiplication by a number.



105
106
107
108
109
# File 'lib/sy/dimension.rb', line 105

def * number
  

#+(other) ⇒ Object

Dimension arithmetic: addition. (+, -, *, /).



89
90
91
92
93
# File 'lib/sy/dimension.rb', line 89

def + other
  

#-(other) ⇒ Object

Dimension arithmetic: subtraction.



97
98
99
100
101
# File 'lib/sy/dimension.rb', line 97

def - other
  

#/(number) ⇒ Object

Dimension arithmetic: division by a number.



113
114
115
116
117
118
119
120
# File 'lib/sy/dimension.rb', line 113

def / number
  

#==(other) ⇒ Object

Two dimensions are equal, if their exponents are equal.



83
84
85
# File 'lib/sy/dimension.rb', line 83

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

Raises:

  • (ArgumentError)


76
77
78
79
# File 'lib/sy/dimension.rb', line 76

def [] 

#base?Boolean Also known as: basic?

True if the dimension is basic, otherwise false.

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/sy/dimension.rb', line 145

def base?
  to_a.count( 1 ) == 1 &&
    to_a.count( 0 ) == SY::BASE_DIMENSIONS.base_symbols.size - 1
end

#inspectObject

Produces the inspect string of the dimension.



160
161
162
# File 'lib/sy/dimension.rb', line 160

def inspect
  "#<SY::Dimension: #{self} >"
end

#standard_quantityObject

Returns dimension’s standard quantity.



166
167
168
# File 'lib/sy/dimension.rb', line 166

def standard_quantity
  self.class.standard_quantities[ self ]
end

#to_aObject

Conversion to an array (of exponents of in the order of the basic dimension letters).



125
126
127
# File 'lib/sy/dimension.rb', line 125

def to_a
  SY::BASE_DIMENSIONS.base_symbols.map { |l| self.send l }
end

#to_compositionObject

Returns default quantity composition for this dimension.



172
173
174
175
176
177
178
# File 'lib/sy/dimension.rb', line 172

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_hashObject

Conversion to a hash (eg. { L: 1, M: 0, T: -2, Q: 0, Θ: 0 } ).⁻³



131
132
133
134
135
# File 'lib/sy/dimension.rb', line 131

def to_hash
  SY::BASE_DIMENSIONS.base_symbols.each_with_object Hash.new do |l, 

#to_sObject

Converts the dimension into its SPS.



153
154
155
156
# File 'lib/sy/dimension.rb', line 153

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.

Returns:

  • (Boolean)


139
140
141
# File 'lib/sy/dimension.rb', line 139

def zero?
  to_a.all? { |e| e.zero? }
end