Class: Unitwise::Unit
Instance Method Summary
collapse
Methods included from Composable
#<=>, #composition, #composition_string, #dim, included, #similar_to?
Constructor Details
#initialize(input) ⇒ Unit
5
6
7
8
9
10
11
12
13
|
# File 'lib/unitwise/unit.rb', line 5
def initialize(input)
if input.respond_to?(:expression)
@expression = input.expression
elsif input.respond_to?(:each)
@terms = input
else
@expression = input.to_s
end
end
|
Instance Method Details
#*(other) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/unitwise/unit.rb', line 59
def *(other)
if other.respond_to?(:terms)
self.class.new(terms + other.terms)
else
raise TypeError, "Can't multiply #{inspect} by #{other}."
end
end
|
#**(number) ⇒ Object
75
76
77
|
# File 'lib/unitwise/unit.rb', line 75
def **(number)
self.class.new(terms.map{ |t| t ** number })
end
|
#/(other) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/unitwise/unit.rb', line 67
def /(other)
if other.respond_to?(:terms)
self.class.new(terms + other.terms.map{ |t| t ** -1})
else
raise TypeError, "Can't divide #{inspect} by #{other}."
end
end
|
#atoms ⇒ Object
23
24
25
|
# File 'lib/unitwise/unit.rb', line 23
def atoms
terms.map(&:atom)
end
|
#depth ⇒ Object
39
40
41
|
# File 'lib/unitwise/unit.rb', line 39
def depth
terms.map(&:depth).max + 1
end
|
#dup ⇒ Object
35
36
37
|
# File 'lib/unitwise/unit.rb', line 35
def dup
self.class.new(expression)
end
|
#expression ⇒ Object
15
16
17
|
# File 'lib/unitwise/unit.rb', line 15
def expression
@expression ||= (Expression.compose(@terms) if @terms)
end
|
#functional(x = scalar, direction = 1) ⇒ Object
31
32
33
|
# File 'lib/unitwise/unit.rb', line 31
def functional(x=scalar, direction=1)
terms.first.functional(x, direction)
end
|
#inspect ⇒ Object
83
84
85
|
# File 'lib/unitwise/unit.rb', line 83
def inspect
"<#{self.class} #{to_s}>"
end
|
#root_terms ⇒ Object
47
48
49
|
# File 'lib/unitwise/unit.rb', line 47
def root_terms
terms.flat_map(&:root_terms)
end
|
#scalar ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/unitwise/unit.rb', line 51
def scalar
if terms.empty?
1
else
terms.map(&:scalar).inject(&:*)
end
end
|
#special? ⇒ Boolean
27
28
29
|
# File 'lib/unitwise/unit.rb', line 27
def special?
terms.count == 1 && terms.all?(&:special?)
end
|
#terminal? ⇒ Boolean
43
44
45
|
# File 'lib/unitwise/unit.rb', line 43
def terminal?
depth <= 3
end
|
#terms ⇒ Object
19
20
21
|
# File 'lib/unitwise/unit.rb', line 19
def terms
@terms ||= (Expression.decompose(@expression) if @expression)
end
|
#to_s ⇒ Object
79
80
81
|
# File 'lib/unitwise/unit.rb', line 79
def to_s
expression
end
|