class SY::Dimension

This class represents physical dimension of a metrological quantity.

Attributes

standard_quantities[R]

Public Class Methods

__new__(dim={})
Alias for: new
base(symbol) click to toggle source

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
Also aliased as: basic
basic(symbol)
Alias for: base
instances() click to toggle source

Presents class-owned instances (array).

# File lib/sy/dimension.rb, line 42
def instances; return @instances ||= [] end
new(dim={}) click to toggle source

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
Also aliased as: __new__
new(hash) click to toggle source

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
zero() click to toggle source

Constructor for zero dimension (“dimensionless”).

# File lib/sy/dimension.rb, line 55
def zero; new end

Public Instance Methods

*(number) click to toggle source

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
+(other) click to toggle source

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
-(other) click to toggle source

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
/(number) click to toggle source

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
==(other) click to toggle source

Two dimensions are equal, if their exponents are equal.

# File lib/sy/dimension.rb, line 82
def == other
  to_a == other.to_a
end
[](ß) click to toggle source

[] 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
base?() click to toggle source

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
Also aliased as: basic?
basic?()
Alias for: base?
inspect() click to toggle source

Produces the inspect string of the dimension.

# File lib/sy/dimension.rb, line 159
def inspect
  "#<SY::Dimension: #{self} >"
end
standard_quantity() click to toggle source

Returns dimension's standard quantity.

# File lib/sy/dimension.rb, line 165
def standard_quantity
  self.class.standard_quantities[ self ]
end
to_a() click to toggle source

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
to_composition() click to toggle source

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
to_hash() click to toggle source

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
to_s() click to toggle source

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
zero?() click to toggle source

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