Class: Symbolic::Factor

Inherits:
Object
  • Object
show all
Includes:
Symbolic
Defined in:
lib/symbolic/factor.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Symbolic

#*, #**, #+, #+@, #-, #-@, #/, #coerce, #operations, #undefined_variables

Constructor Details

#initialize(factors) ⇒ Factor

Returns a new instance of Factor.



63
64
65
# File 'lib/symbolic/factor.rb', line 63

def initialize(factors)
  @factors = factors
end

Class Method Details

.divide(var1, var2) ⇒ Object



15
16
17
18
# File 'lib/symbolic/factor.rb', line 15

def divide(var1, var2)
  simplify_exponents! factors = unite(factors(var1), reverse(var2))
  simplify(*factors) || new(factors)
end

.exponent(base, exponent) ⇒ Object



5
6
7
8
# File 'lib/symbolic/factor.rb', line 5

def exponent(base, exponent)
  simplify_exponents! factors = unite_exponents(base, exponent)
  simplify(*factors) || new(factors)
end

.factors(var) ⇒ Object



40
41
42
# File 'lib/symbolic/factor.rb', line 40

def factors(var)
  var.is_a?(Symbolic) ? var.send(:factors) : [var, {}]
end

.multiply(var1, var2) ⇒ Object



10
11
12
13
# File 'lib/symbolic/factor.rb', line 10

def multiply(var1, var2)
  simplify_exponents! factors = unite(factors(var1), factors(var2))
  simplify(*factors) || new(factors)
end

.reverse(var) ⇒ Object



33
34
35
36
37
38
# File 'lib/symbolic/factor.rb', line 33

def reverse(var)
  factors(var).dup.tap do |it|
    it[0] = it[0]**-1
    it[1] = Hash[*it[1].map {|b,e| [b,-e] }.flatten]
  end
end

.simplify(numeric, symbolic) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/symbolic/factor.rb', line 25

def simplify(numeric, symbolic)
  if numeric == 0 || symbolic.empty?
    (numeric.round == numeric) ? numeric.to_i : numeric.to_f
  elsif numeric == 1 && symbolic.size == 1 && symbolic.values.first == 1
    symbolic.keys.first
  end
end

.simplify_exponents!(factors) ⇒ Object



20
21
22
23
# File 'lib/symbolic/factor.rb', line 20

def simplify_exponents!(factors)
  factors[1].delete_if {|base, exp| (base == 1) || (exp == 0) }
  factors[0] = 0 if factors[1].any? {|base, exp| base == 0 }
end

.unite(factors1, factors2) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/symbolic/factor.rb', line 44

def unite(factors1, factors2)
  numeric1, symbolic1 = factors1
  numeric2, symbolic2 = factors2

  numeric = numeric1 * numeric2
  symbolic = symbolic1.merge(symbolic2) {|base, exp1, exp2| exp1 + exp2 }
  return numeric, symbolic
end

.unite_exponents(base, exponent) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/symbolic/factor.rb', line 53

def unite_exponents(base, exponent)
  if base.is_a? Symbolic::Factor
    numeric, symbolic = factors(base)
    return numeric**exponent, Hash[*symbolic.map {|base,exp| [base,exp*exponent] }.flatten]
  else
    [1, { base => exponent }]
  end
end

Instance Method Details

#==(object) ⇒ Object



79
80
81
# File 'lib/symbolic/factor.rb', line 79

def ==(object)
  object.send(:factors) == @factors rescue false
end

#to_sObject



75
76
77
# File 'lib/symbolic/factor.rb', line 75

def to_s
  simplify_output
end

#valueObject



67
68
69
# File 'lib/symbolic/factor.rb', line 67

def value
  @factors[1].inject(@factors[0]) {|value, (base, exp)| value * base.value ** exp.value }
end

#variablesObject



71
72
73
# File 'lib/symbolic/factor.rb', line 71

def variables
  @factors[1].map {|k,v| [variables_of(k), variables_of(v)] }.flatten.uniq
end