Class: Power

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

Overview

Implements the power operation in SymCalc

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Equation

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

Constructor Details

#initialize(base, power) ⇒ Power

Returns a new instance of Power.



647
648
649
650
# File 'lib/symcalc.rb', line 647

def initialize base, power
	@base = base
	@power = power
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



645
646
647
# File 'lib/symcalc.rb', line 645

def base
  @base
end

#powerObject

Returns the value of attribute power.



645
646
647
# File 'lib/symcalc.rb', line 645

def power
  @power
end

Instance Method Details

#==(eq) ⇒ Object



665
666
667
668
669
670
671
672
673
# File 'lib/symcalc.rb', line 665

def == eq
	if eq.is_a? Power
		return (eq.base == @base) && (eq.power == @power)
	elsif (eq == EquationValue.new(1))
		return @power == 0
	else
		return false
	end
end

#__derivative__(variable: nil) ⇒ Object



660
661
662
663
# File 'lib/symcalc.rb', line 660

def __derivative__ variable: nil
	# exp(@power * ln(@base)).derivative
	return @base ** @power * (@power.derivative(variable: variable)*Ln.new(@base) + @base.derivative(variable: variable)*@power/@base)
end

#__eval__(var_hash) ⇒ Object



656
657
658
# File 'lib/symcalc.rb', line 656

def __eval__ var_hash
	return @base.eval(var_hash) ** @power.eval(var_hash)
end

#__get_m_elements__(var_hash) ⇒ Object



675
676
677
678
679
680
681
682
683
684
685
# File 'lib/symcalc.rb', line 675

def __get_m_elements__ var_hash
	elms = @base.__get_m_elements__({})
	elms.each do |k, v|
		if var_hash.keys.include? k
			var_hash[k] += v * @power
		else
			var_hash[k] = @power
		end
	end
	return var_hash
end

#__simplify__Object



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/symcalc.rb', line 687

def __simplify__
	
	base = @base.simplify
	power = @power.simplify
	
	if power == EquationValue.new(0)
		return EquationValue.new(1)
	elsif power == EquationValue.new(1)
		return base
	elsif base.is_a?(EquationValue) && power.is_a?(EquationValue)
		computed = base.value ** power.value
		if computed.to_s.size <= 6
			return to_equation(computed)
		else
			return base ** power
		end
	elsif base.is_a?(Power)
		new_base = base.base
		new_power = base.power * power
		return (new_base ** new_power).simplify
	else
		return base ** power
	end
	
end

#__sub__(original, replacement) ⇒ Object



717
718
719
720
# File 'lib/symcalc.rb', line 717

def __sub__ original, replacement
	return to_equation(replacement) if self == to_equation(original)
	return @base.__sub__(original, replacement) ** @power.__sub__(original, replacement)
end

#all_variablesObject



713
714
715
# File 'lib/symcalc.rb', line 713

def all_variables
	return (@base.all_variables + @power.all_variables).uniq
end

#displayObject



652
653
654
# File 'lib/symcalc.rb', line 652

def display
	return "(#{@base.display})^(#{@power.display})"
end