Class: Equation
- Inherits:
-
Object
show all
- Defined in:
- lib/symcalc.rb
Direct Known Subclasses
Abs, Cos, Division, EquationValue, Exp, Ln, Log, Multiplication, Power, Sin, Subtraction, Sum, Variable
Instance Method Summary
collapse
Instance Method Details
#*(eq) ⇒ Object
34
35
36
|
# File 'lib/symcalc.rb', line 34
def *(eq)
return Multiplication.new(self, to_equation(eq))
end
|
#**(eq) ⇒ Object
50
51
52
|
# File 'lib/symcalc.rb', line 50
def **(eq)
return Power.new(self, to_equation(eq))
end
|
#+(eq) ⇒ Object
42
43
44
|
# File 'lib/symcalc.rb', line 42
def +(eq)
return Sum.new(self, to_equation(eq))
end
|
#-(eq) ⇒ Object
46
47
48
|
# File 'lib/symcalc.rb', line 46
def -(eq)
return Subtraction.new(self, to_equation(eq))
end
|
#/(eq) ⇒ Object
38
39
40
|
# File 'lib/symcalc.rb', line 38
def /(eq)
return Division.new(self, to_equation(eq))
end
|
#__simplify__ ⇒ Object
85
86
87
|
# File 'lib/symcalc.rb', line 85
def __simplify__
return self
end
|
#__sub__(original, replacement) ⇒ Object
211
212
213
214
|
# File 'lib/symcalc.rb', line 211
def __sub__ original, replacement
return to_equation(replacement) if self == to_equation(original)
return self
end
|
#coerce(other) ⇒ Object
30
31
32
|
# File 'lib/symcalc.rb', line 30
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/symcalc.rb', line 67
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)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/symcalc.rb', line 194
def eval(var_hash)
if var_hash.values.size == 0
result = self.__eval__(Hash.new)
elsif !var_hash.values[0].is_a?(Array)
result = self.__eval__ var_hash
elsif var_hash.values[0].is_a? Array
result = []
var_hash.values[0].size.times do |i|
hash = var_hash.map {|k, v| [k, v[i]]}.to_h
result << self.__eval__(hash)
end
end
result
end
|
#inspect ⇒ Object
26
27
28
|
# File 'lib/symcalc.rb', line 26
def inspect
self.display()
end
|
#simplify ⇒ Object
Simplifies the given function Accepts no arguments
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
177
178
179
180
181
182
183
|
# File 'lib/symcalc.rb', line 92
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
|
#sub(original, replacement) ⇒ Object
Returns an equation with an expression substituted for the replacement
Example: a = SymCalc.var(“a”) f = a ** 2 x = SymCalc.var(“x”) puts f.sub(a, 3 * x) # => (3 * x) ** 2
224
225
226
|
# File 'lib/symcalc.rb', line 224
def sub original, replacement
self.__sub__(original, replacement).simplify
end
|
#to_s ⇒ Object
22
23
24
|
# File 'lib/symcalc.rb', line 22
def to_s
self.display()
end
|