Value objects that do the actual unit tracking.
Contains all the interesting power tracking and cancellation.
# File lib/mathviz.rb, line 127 def self.binop(other) alias_method other, :binary_operator end
With a symbol, creates a simple unit.
With a hash-of-powers, it simply copies those values.
Otherwise, it becomes a dimensionless unit.
# File lib/mathviz.rb, line 108 def initialize(h = nil) @unit = Hash.new(0) case h when Hash; @unit.merge!(h); normalize! when Symbol; @unit[h] = 1 end @unit.freeze freeze end
# File lib/mathviz.rb, line 141 def *(other) x = @unit.dup other.unit.each do |u,power| x[u] += power end MathViz::Unit.new(x) end
# File lib/mathviz.rb, line 149 def /(other) x = @unit.dup other.unit.each do |u,power| x[u] -= power end MathViz::Unit.new(x) end
Implement a simple binary operation. It verifies that the units match and returns the unit ERROR if not.
# File lib/mathviz.rb, line 119 def binary_operator(other) if (unit != other.unit) #p "#{to_s} !+- #{other.to_s}" return MathViz::Unit.new(:ERROR) end return self end
# File lib/mathviz.rb, line 161 def denominator unit.reject {|u,power| power > 0} end
# File lib/mathviz.rb, line 157 def numerator unit.reject {|u,power| power < 0} end
Generated with the Darkfish Rdoc Generator 2.