Class: Equation
- Inherits:
-
Object
show all
- Defined in:
- lib/symcalc.rb
Direct Known Subclasses
Cos, Division, EquationValue, Exp, Ln, Log, Multiplication, Power, Sin, Subtraction, Sum, Variable
Instance Method Summary
collapse
Instance Method Details
#*(eq) ⇒ Object
25
26
27
|
# File 'lib/symcalc.rb', line 25
def *(eq)
return Multiplication.new(self, to_equation(eq))
end
|
#**(eq) ⇒ Object
41
42
43
|
# File 'lib/symcalc.rb', line 41
def **(eq)
return Power.new(self, to_equation(eq))
end
|
#+(eq) ⇒ Object
33
34
35
|
# File 'lib/symcalc.rb', line 33
def +(eq)
return Sum.new(self, to_equation(eq))
end
|
#-(eq) ⇒ Object
37
38
39
|
# File 'lib/symcalc.rb', line 37
def -(eq)
return Subtraction.new(self, to_equation(eq))
end
|
#/(eq) ⇒ Object
29
30
31
|
# File 'lib/symcalc.rb', line 29
def /(eq)
return Division.new(self, to_equation(eq))
end
|
#__simplify__ ⇒ Object
76
77
78
|
# File 'lib/symcalc.rb', line 76
def __simplify__
return self
end
|
#coerce(other) ⇒ Object
21
22
23
|
# File 'lib/symcalc.rb', line 21
def coerce(other)
[to_equation(other), self]
end
|
#derivative(order: 1, variable: nil) ⇒ Object
Calculate the derivative of the given function Accepts parameters order and variable. If the function has more than one dimensional, the variable needs to be provided
Example: x = SymCalc.var(“x”) y = SymCalc.var(“y”) fx = x ** 2 fx.derivative order: 2
fxy = x ** 2 + 3 * y fxy.derivative variable: x
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/symcalc.rb', line 58
def derivative(order: 1, variable: nil)
if variable == nil && self.all_variables.size < 2
fx = self
order.times do
fx = fx.simplify.__derivative__.simplify
end
return fx
elsif variable == nil && self.all_variables.size > 1
raise "Expected a variable as input for a #{self.all_variables.size}-dimensional function"
else
fx = self
order.times do
fx = fx.simplify.__derivative__(variable: variable).simplify
end
return fx
end
end
|
#eval(var_hash) ⇒ Object
Evaluates the function at given variable values Accepts the hash of variables and their values to evalualte the function
Example: x = SymCalc.var(“x”) fx = x ** 2 puts fx.eval(x: 3)
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/symcalc.rb', line 187
def eval(var_hash)
if var_hash.values.size == 0
return self.__eval__(Hash.new)
elsif !var_hash.values[0].is_a?(Array)
return self.__eval__ var_hash
elsif var_hash.values[0].is_a? Array
computed = []
var_hash.values[0].size.times do |i|
hash = var_hash.map {|k, v| [k, v[i]]}.to_h
computed << self.__eval__(hash)
end
return computed
end
end
|
#inspect ⇒ Object
17
18
19
|
# File 'lib/symcalc.rb', line 17
def inspect
self.display()
end
|
#simplify ⇒ Object
Simplifies the given function Accepts no arguments
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/symcalc.rb', line 83
def simplify
simplified = self.__simplify__
if [Multiplication, Division].include? simplified.class
m_els = simplified.__get_m_elements__(Hash.new)
if m_els.keys.size == 0
return EquationValue.new(0)
elsif m_els.keys.size == 1
part = m_els.keys[0]
power = to_equation(m_els.values[0])
if part == "exp"
part = (power == to_equation(1)) ? BasicVars::E : Exp.new(power)
else
part = part ** power if power != to_equation(1)
end
return part
else
els_index = 0
eq = nil
coeff = to_equation(1)
m_els.size.times do |els_index|
base = m_els.keys[els_index]
power = m_els[base]
base = base.simplify if base.is_a? Equation
power = power.simplify if power.is_a? Equation
base = to_equation base if base != "exp"
power = to_equation power
if power == to_equation(0)
next
end
if base.is_a?(EquationValue) && power.is_a?(EquationValue)
case power
when EquationValue.new(1)
coeff *= base
else
coeff *= base ** power
end
next
end
if base == "exp"
case power
when to_equation(1)
part = BasicVars::E
else
part = Exp.new(power)
end
else
case power
when to_equation(1)
part = base
else
part = base ** power
end
end
if eq == nil
eq = part
else
eq *= part
end
end
coeff = coeff.__simplify__
if coeff == to_equation(1)
eq = eq
elsif coeff == to_equation(0)
eq = to_equation(0)
elsif eq == nil
eq = coeff
else
eq = coeff * eq
end
return eq
end
else
return simplified
end
end
|
#to_s ⇒ Object
13
14
15
|
# File 'lib/symcalc.rb', line 13
def to_s
self.display()
end
|