Class: Variable
Overview
Implements the Variable class
Instance Method Summary
collapse
Methods inherited from Equation
#*, #**, #+, #-, #/, #__simplify__, #__sub__, #coerce, #derivative, #eval, #inspect, #simplify, #sub, #to_s
Constructor Details
#initialize(name, fixed_value = nil) ⇒ Variable
Create a new symbolic variable with a custom display name, and an optional fixed value Accepts the display name and an optional fixed_value Fixed_value works in the cases of evaluating the function
Example: x = Variable.new “x”, 5 fx = x ** 2 fx.eval # => 25
304
305
306
307
|
# File 'lib/symcalc.rb', line 304
def initialize name, fixed_value = nil
@name = name
@fixed_value = fixed_value
end
|
Instance Method Details
#__derivative__(variable: nil) ⇒ Object
324
325
326
327
328
329
330
|
# File 'lib/symcalc.rb', line 324
def __derivative__ variable: nil
if variable == nil || variable == self
return EquationValue.new 1
else
return to_equation(0)
end
end
|
#__eval__(var_hash) ⇒ Object
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/symcalc.rb', line 313
def __eval__ var_hash
if @fixed_value
return @fixed_value
elsif var_hash.keys.include?(@name.to_sym) or var_hash.keys.include?(@name.to_s)
return (var_hash[@name.to_sym] or var_hash[@name.to_s])
else
raise "No value provided for #{@name.to_s} in eval"
end
end
|
#__get_m_elements__(var_hash) ⇒ Object
332
333
334
335
336
337
338
339
|
# File 'lib/symcalc.rb', line 332
def __get_m_elements__ var_hash
if var_hash.keys.include? self
var_hash[self] += 1
else
var_hash[self] = 1
end
return var_hash
end
|
#all_variables ⇒ Object
341
342
343
|
# File 'lib/symcalc.rb', line 341
def all_variables
return [self]
end
|
#display ⇒ Object
309
310
311
|
# File 'lib/symcalc.rb', line 309
def display
return @name
end
|