Class: Sum

Inherits:
Equation show all
Defined in:
lib/symcalc.rb

Overview

Implements sum operations in SymCalc

Instance Method Summary collapse

Methods inherited from Equation

#*, #**, #+, #-, #/, #coerce, #derivative, #eval, #inspect, #simplify, #to_s

Constructor Details

#initialize(lside, rside) ⇒ Sum

Returns a new instance of Sum.



339
340
341
342
# File 'lib/symcalc.rb', line 339

def initialize lside, rside
	@lside = lside
	@rside = rside
end

Instance Method Details

#__derivative__(variable: nil) ⇒ Object



352
353
354
# File 'lib/symcalc.rb', line 352

def __derivative__ variable: nil
	return @lside.derivative(variable: variable) + @rside.derivative(variable: variable)
end

#__eval__(var_hash) ⇒ Object



348
349
350
# File 'lib/symcalc.rb', line 348

def __eval__ var_hash
	return @lside.eval(var_hash) + @rside.eval(var_hash)
end

#__get_m_elements__(el_hash) ⇒ Object



370
371
372
373
374
375
376
377
# File 'lib/symcalc.rb', line 370

def __get_m_elements__ el_hash
	if el_hash.keys.include? self
		el_hash[self] += 1
	else
		el_hash[self] = 1
	end
	return el_hash
end

#__simplify__Object



356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/symcalc.rb', line 356

def __simplify__
	@lside = @lside.__simplify__
	@rside = @rside.__simplify__
	if (@lside.is_a? EquationValue) && (@rside.is_a? EquationValue)
		return EquationValue.new((@lside + @rside).eval({}))
	elsif @lside == EquationValue.new(0)
		return @rside
	elsif @rside == EquationValue.new(0)
		return @lside
	else
		return self
	end
end

#all_variablesObject



379
380
381
# File 'lib/symcalc.rb', line 379

def all_variables
	return (@lside.all_variables + @rside.all_variables).uniq
end

#displayObject



344
345
346
# File 'lib/symcalc.rb', line 344

def display
	return "(#{@lside.display}) + (#{@rside.display})"
end