Class: Subtraction

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

Overview

Implements the subtraction operation in SymCalc

Instance Method Summary collapse

Methods inherited from Equation

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

Constructor Details

#initialize(lside, rside) ⇒ Subtraction

Returns a new instance of Subtraction.



387
388
389
390
# File 'lib/symcalc.rb', line 387

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

Instance Method Details

#__derivative__(variable: nil) ⇒ Object



400
401
402
# File 'lib/symcalc.rb', line 400

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

#__eval__(var_hash) ⇒ Object



396
397
398
# File 'lib/symcalc.rb', line 396

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

#__get_m_elements__(el_hash) ⇒ Object



404
405
406
407
408
409
410
411
# File 'lib/symcalc.rb', line 404

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



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/symcalc.rb', line 413

def __simplify__
	@lside = @lside.__simplify__
	@rside = @rside.__simplify__
	
	if (@lside.is_a? EquationValue) && (@rside.is_a? EquationValue)
		return EquationValue.new((@lside - @rside).eval(Hash.new))
	elsif @rside == EquationValue.new(0)
		return @lside
	elsif @lside == EquationValue.new(0)
		return -1 * @rside
	elsif @lside == @rside
		return EquationValue.new(0)
	else
		return self
	end
end

#all_variablesObject



430
431
432
# File 'lib/symcalc.rb', line 430

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

#displayObject



392
393
394
# File 'lib/symcalc.rb', line 392

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