Class: Unitwise::Term

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

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

Constructor Details

#initialize(attributes) ⇒ Term

Returns a new instance of Term.



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

def initialize(attributes)
  attributes.each do |k,v|
    public_send :"#{k}=", v
  end
end

Instance Attribute Details

#annotationObject

Returns the value of attribute annotation.



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

def annotation
  @annotation
end

#atomObject



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

def atom
  @atom ||= (Atom.find(@atom_code) if @atom_code)
end

#atom_codeObject



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

def atom_code
  @atom_code ||= (@atom.primary_code if @atom)
end

#exponentObject



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

def exponent
  @exponent ||= 1
end

#factorObject



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

def factor
  @factor ||= 1
end

#prefixObject



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

def prefix
  @prefix ||= (Prefix.find(@prefix_code) if @prefix_code)
end

#prefix_codeObject



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

def prefix_code
  @prefix_code ||= (@prefix.primary_code if @prefix)
end

Instance Method Details

#*(other) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/unitwise/term.rb', line 76

def *(other)
  if other.respond_to?(:terms)
    Unit.new(other.terms << self)
  else
    Unit.new([self, other])
  end
end

#**(integer) ⇒ Object



92
93
94
# File 'lib/unitwise/term.rb', line 92

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

#/(other) ⇒ Object



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

def /(other)
  if other.respond_to?(:terms)
    Unit.new(other.terms.map{|t| t ** -1} << self)
  else
    Unit.new([self, other ** -1])
  end
end

#depthObject



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

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

#functional(x = scalar, direction = 1) ⇒ Object



56
57
58
# File 'lib/unitwise/term.rb', line 56

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

#inspectObject



101
102
103
# File 'lib/unitwise/term.rb', line 101

def inspect
  "<#{self.class} #{to_s}>"
end

#root_termsObject



60
61
62
63
64
65
66
67
68
# File 'lib/unitwise/term.rb', line 60

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

#scalarObject



52
53
54
# File 'lib/unitwise/term.rb', line 52

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

#special?Boolean

Returns:



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

def special?
  atom.special rescue false
end

#terminal?Boolean

Returns:



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

def terminal?
  depth <= 3
end

#to_hashObject



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

def to_hash
  [:prefix, :atom, :exponent, :factor, :annotation].inject({}) do |h, a|
    h[a] = send a; h
  end
end

#to_sObject



96
97
98
99
# File 'lib/unitwise/term.rb', line 96

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