Sha256: f861987712ebff318e250c476d002737adeddb2cca9c9b27902b62562883070a
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
module Unitwise class Unit include Unitwise::Composable attr_writer :terms, :expression 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 def expression @expression ||= (Expression.compose(@terms) if @terms) end def terms @terms ||= (Expression.decompose(@expression) if @expression) end def atoms terms.map(&:atom) end def special? terms.count == 1 && terms.all?(&:special?) end def functional(value, direction=1) terms.first.functional(value, direction) end def dup self.class.new(expression) end def depth terms.map(&:depth).max + 1 end def terminal? depth <= 3 end def root_terms terms.flat_map(&:root_terms) end def scalar if terms.empty? 1 else terms.map(&:scalar).inject(&:*) end end def *(other) if other.respond_to?(:terms) self.class.new(terms + other.terms) else raise TypeError, "Can't multiply #{inspect} by #{other}." end end 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 def **(number) self.class.new(terms.map{ |t| t ** number }) end def to_s expression end def inspect "<#{self.class} #{to_s}>" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
unitwise-0.1.0 | lib/unitwise/unit.rb |