Class: Unitwise::Term

Inherits:
Object
  • Object
show all
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

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]

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/unitwise/term.rb', line 97

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]

Returns:



123
124
125
126
127
128
129
# File 'lib/unitwise/term.rb', line 123

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]

Returns:



110
111
112
113
114
115
116
117
118
# File 'lib/unitwise/term.rb', line 110

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

Parameters:

  • value (String, Atom) —

    Either a string representing an Atom, or an



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.

Returns:

  • (Integer)


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.

Returns:



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.

Returns:



54
55
56
# File 'lib/unitwise/term.rb', line 54

def factor
  super || 1
end

#inverse_scalar(scalar = scalar) ⇒ Numeric

Calculate the magnitude for this term

Parameters:

  • scalar (Numeric) (defaults to: scalar) —

    The scalar for which you want the magnitude

Returns:

  • (Numeric) —

    The magnitude on this scale.



77
78
79
# File 'lib/unitwise/term.rb', line 77

def inverse_scalar(scalar = scalar)
  calculate(atom ? atom.inverse_scalar(scalar) : 1)
end

#prefix=(value) ⇒ Object

Set the prefix. a Prefix

Parameters:

  • value (String, Prefix) —

    Either a string representing a Prefix, or



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

Returns:

  • (Array) —

    An array of Unitwise::Term



84
85
86
87
88
89
90
91
92
# File 'lib/unitwise/term.rb', line 84

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(magnitude = 1) ⇒ Numeric

The unitless scalar value for this term.

Parameters:

  • magnitude (Numeric) (defaults to: 1) —

    The magnitude to calculate the scalar for.

Returns:

  • (Numeric) —

    The unitless linear scalar value.



69
70
71
# File 'lib/unitwise/term.rb', line 69

def scalar(magnitude = 1)
  calculate(atom ? atom.scalar(magnitude) : 1)
end

#special? ⇒ true, false

Is this term special?

Returns:

  • (true, false)


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

Returns:

  • (true, false)


47
48
49
# File 'lib/unitwise/term.rb', line 47

def terminal?
  depth <= 3
end

#to_s ⇒ String

String representation for this term.

Returns:

  • (String)


133
134
135
136
# File 'lib/unitwise/term.rb', line 133

def to_s
  [(factor if factor != 1), prefix.to_s,
    atom.to_s, (exponent if exponent != 1)].compact.join('')
end