Class: Unitwise::Term

Inherits:
Object
  • Object
show all
Includes:
Compatible
Defined in:
lib/unitwise/term.rb

Instance Method Summary collapse

Methods included from Compatible

#<=>, #compatible_with?, #composition, #composition_string, #dim, included

Constructor Details

#initialize(*args) ⇒ Term

Returns a new instance of Term.



6
7
8
9
# File 'lib/unitwise/term.rb', line 6

def initialize(*args)
  super(*args)
  freeze
end

Instance Method Details

#*(other) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/unitwise/term.rb', line 57

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

#**(integer) ⇒ Object



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

def **(integer)
  self.class.new(to_hash.merge(exponent: exponent * integer))
end

#/(other) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/unitwise/term.rb', line 67

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



11
12
13
# File 'lib/unitwise/term.rb', line 11

def atom=(value)
  value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s))
end

#depth ⇒ Object



23
24
25
# File 'lib/unitwise/term.rb', line 23

def depth
  atom ? atom.depth + 1 : 0
end

#exponent ⇒ Object



35
36
37
# File 'lib/unitwise/term.rb', line 35

def exponent
  super || 1
end

#factor ⇒ Object



31
32
33
# File 'lib/unitwise/term.rb', line 31

def factor
  super || 1
end

#functional(x = scalar, forward = true) ⇒ Object



43
44
45
# File 'lib/unitwise/term.rb', line 43

def functional(x=scalar, forward=true)
  (factor * (prefix ? prefix.scalar : 1)) * (atom ? atom.functional(x, forward) : 1) ** exponent
end

#prefix=(value) ⇒ Object



15
16
17
# File 'lib/unitwise/term.rb', line 15

def prefix=(value)
  value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s))
end

#root_terms ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/unitwise/term.rb', line 47

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 ⇒ Object



39
40
41
# File 'lib/unitwise/term.rb', line 39

def scalar
  (factor * (prefix ? prefix.scalar : 1) * (atom ? atom.scalar : 1)) ** exponent
end

#special? ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/unitwise/term.rb', line 19

def special?
  atom.special rescue false
end

#terminal? ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/unitwise/term.rb', line 27

def terminal?
  depth <= 3
end

#to_s ⇒ Object



81
82
83
84
# File 'lib/unitwise/term.rb', line 81

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