Class: Unitwise::Term
- Inherits:
-
Object
- Object
- Unitwise::Term
- Includes:
- Compatible
- Defined in:
- lib/unitwise/term.rb
Overview
A Term is the combination of an atom, prefix, factor and annotation. Not all properties have to be present. Examples: 'g', 'mm', 'mi2', '4', 'kJPotential'
Instance Method Summary collapse
-
#*(other) ⇒ Term
Term multiplication.
-
#**(other) ⇒ Term
Term exponentiation.
-
#/(other) ⇒ Term
Term division.
-
#atom=(value) ⇒ Object
Set the atom.
-
#depth ⇒ Integer
Determine how far away a unit is from a base unit.
-
#exponent ⇒ Numeric
The exponent for this term.
-
#factor ⇒ Numeric
The multiplication factor for this term.
-
#functional(x = scalar, forward = true) ⇒ Numeric
Get the equivalent scalar value of a unit based on the atom's function.
-
#initialize(*args) ⇒ Term
constructor
Setup a new term.
-
#prefix=(value) ⇒ Object
Set the prefix.
-
#root_terms ⇒ Array
The base units this term is derived from.
-
#scalar ⇒ Numeric
The unitless value for this term.
-
#special? ⇒ true, false
Is this term special?.
-
#terminal? ⇒ true, false
Determine if this is the last term in the scale chain.
-
#to_s ⇒ String
String representation for this term.
Methods included from Compatible
#<=>, #compatible_with?, #composition, #composition_string, #dim, included
Constructor Details
#initialize(*args) ⇒ Term
Setup a new term. Send a hash of properties, or ordered property values.
11 12 13 14 |
# File 'lib/unitwise/term.rb', line 11 def initialize(*args) super(*args) freeze end |
Instance Method Details
#*(other) ⇒ Term
Term multiplication. Multiply by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric]
103 104 105 106 107 108 109 110 111 |
# File 'lib/unitwise/term.rb', line 103 def *(other) if other.respond_to?(:terms) Unit.new(other.terms << self) elsif other.respond_to?(:atom) Unit.new([self, other]) elsif other.is_a?(Numeric) self.class.new(to_hash.merge(factor: factor * other)) end end |
#**(other) ⇒ Term
Term exponentiation. Raise a term to a numeric power. params other [Numeric]
129 130 131 132 133 134 135 |
# File 'lib/unitwise/term.rb', line 129 def **(other) if other.is_a?(Numeric) self.class.new(to_hash.merge(exponent: exponent * other)) else fail TypeError, "Can't raise #{self} to #{other}." end end |
#/(other) ⇒ Term
Term division. Divide by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric]
116 117 118 119 120 121 122 123 124 |
# File 'lib/unitwise/term.rb', line 116 def /(other) if other.respond_to?(:terms) Unit.new(other.terms.map { |t| t ** -1 } << self) elsif other.respond_to?(:atom) Unit.new([self, other ** -1]) elsif other.is_a?(Numeric) self.class.new(to_hash.merge(factor: factor / other)) end end |
#atom=(value) ⇒ Object
Set the atom. Atom
20 21 22 |
# File 'lib/unitwise/term.rb', line 20 def atom=(value) value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s)) end |
#depth ⇒ Integer
Determine how far away a unit is from a base unit.
40 41 42 |
# File 'lib/unitwise/term.rb', line 40 def depth atom ? atom.depth + 1 : 0 end |
#exponent ⇒ Numeric
The exponent for this term. The default value is 1.
61 62 63 |
# File 'lib/unitwise/term.rb', line 61 def exponent super || 1 end |
#factor ⇒ Numeric
The multiplication factor for this term. The default value is 1.
54 55 56 |
# File 'lib/unitwise/term.rb', line 54 def factor super || 1 end |
#functional(x = scalar, forward = true) ⇒ Numeric
Get the equivalent scalar value of a unit based on the atom's function. when converting from the special, use false when converting to the special.
82 83 84 85 |
# File 'lib/unitwise/term.rb', line 82 def functional(x = scalar, forward = true) (factor * (prefix ? prefix.scalar : 1)) * (atom ? atom.functional(x, forward) : 1) ** exponent end |
#prefix=(value) ⇒ Object
Set the prefix. a Prefix
27 28 29 |
# File 'lib/unitwise/term.rb', line 27 def prefix=(value) value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s)) end |
#root_terms ⇒ Array
The base units this term is derived from
90 91 92 93 94 95 96 97 98 |
# File 'lib/unitwise/term.rb', line 90 def root_terms if terminal? [self] else atom.scale.root_terms.map do |t| self.class.new(atom: t.atom, exponent: t.exponent * exponent) end end end |
#scalar ⇒ Numeric
The unitless value for this term. This is the equivalent value of it's base atom
69 70 71 72 |
# File 'lib/unitwise/term.rb', line 69 def scalar (factor * (prefix ? prefix.scalar : 1) * (atom ? atom.scalar : 1)) ** exponent end |
#special? ⇒ true, false
Is this term special?
33 34 35 |
# File 'lib/unitwise/term.rb', line 33 def special? atom.special rescue false end |
#terminal? ⇒ true, false
Determine if this is the last term in the scale chain
47 48 49 |
# File 'lib/unitwise/term.rb', line 47 def terminal? depth <= 3 end |
#to_s ⇒ String
String representation for this term.
139 140 141 142 |
# File 'lib/unitwise/term.rb', line 139 def to_s [(factor if factor != 1), prefix.to_s, atom.to_s, (exponent if exponent != 1)].compact.join('') end |