Class: Symbolic::Expression

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

Direct Known Subclasses

Factors, Summands

Constant Summary

Constants included from Symbolic

OPERATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Symbolic

#*, #**, #+, #+@, #-, #-@, #/, #coerce, #factorial, #inspect, #operations, #subs, #taylor, #to_s

Constructor Details

#initialize(numeric, symbolic) ⇒ Expression

Returns a new instance of Expression.



61
62
63
# File 'lib/symbolic/expression.rb', line 61

def initialize(numeric, symbolic)
  @numeric, @symbolic = numeric.freeze, symbolic.freeze
end

Instance Attribute Details

#numericObject (readonly)

Returns the value of attribute numeric.



59
60
61
# File 'lib/symbolic/expression.rb', line 59

def numeric
  @numeric
end

#symbolicObject (readonly)

Returns the value of attribute symbolic.



59
60
61
# File 'lib/symbolic/expression.rb', line 59

def symbolic
  @symbolic
end

Class Method Details

.add(var1, var2) ⇒ Object



6
7
8
9
# File 'lib/symbolic/expression.rb', line 6

def add(var1, var2)
  simplify_expression! expression = unite(convert(var1), convert(var2))
  simplify(*expression) || new(*expression)
end

.convert(var) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/symbolic/expression.rb', line 29

def convert(var)
  case var
  when Summands then summands var
  when Factors  then factors var
  when Numeric  then numeric var
  else one var; end
end

.numeric(numeric) ⇒ Object



37
38
39
# File 'lib/symbolic/expression.rb', line 37

def numeric(numeric)
  new numeric, {}
end

.one(symbolic) ⇒ Object



41
42
43
# File 'lib/symbolic/expression.rb', line 41

def one(symbolic)
  new self::IDENTITY, symbolic => 1
end

.simple?(var) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/symbolic/expression.rb', line 45

def simple?(var)
  case var
  when Numeric, Variable, Function
    true
  when Summands
    false
  when Factors
    var.symbolic.all? {|k,v| simple? k }
  else
    false
  end
end

.subtract(var1, var2) ⇒ Object



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

def subtract(var1, var2)
  add var1, convert(var2).reverse
end

.unite(expr1, expr2) ⇒ Object



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

def unite(expr1, expr2)
  numeric = unite_numeric expr1.numeric, expr2.numeric
  symbolic = unite_symbolic expr1.symbolic, expr2.symbolic
  [numeric, symbolic]
end

.unite_numeric(numeric1, numeric2) ⇒ Object



25
26
27
# File 'lib/symbolic/expression.rb', line 25

def unite_numeric(numeric1, numeric2)
  numeric1.send self::OPERATION, numeric2
end

.unite_symbolic(symbolic1, symbolic2) ⇒ Object



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

def unite_symbolic(symbolic1, symbolic2)
  symbolic1.merge(symbolic2) {|base, coef1, coef2| coef1 + coef2 }
end

Instance Method Details

#==(object) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/symbolic/expression.rb', line 69

def ==(object)
  # We need to make sure the classes are the same because both Factors and
  # Summands have .numeric and .symbolic, but we can't say they're equal
  object.class == self.class and
  object.numeric == @numeric and
  # Make sure that we have the same number of elements, otherwise the
  # next step could give false positives
  object.symbolic.size == @symbolic.size and
  # hash's == function only checks that the object_ids are equal, but we
  # could have different instances of the same object (mathematically speaking). We
  # need to check that each thing in @symbolic appears in object.symbolic as well.
  object.symbolic.inject(true) do |memo,(key,value)| # go through each kv pair in object.symbolic
    memo and @symbolic.inject(false) do |memo2,(key2,value2)|# and make sure it appears in @symbolic
      memo2 or (key2 == key and value2 == value)
    end
  end
end

#variablesObject



65
66
67
# File 'lib/symbolic/expression.rb', line 65

def variables
  @symbolic.map {|k,v| [k.variables, v.variables] }.flatten.uniq
end