polynomials
This commit is contained in:
@@ -17,12 +17,15 @@ module RubyAlgebra
|
||||
def op_assoc_type
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def diff(v)
|
||||
raise NotImplementedError, "#{self.class}#diff not implemented"
|
||||
end
|
||||
|
||||
def evaluate
|
||||
raise NotImplementedError, "#{self.class}#evaluate not implemented"
|
||||
end
|
||||
|
||||
def constant?
|
||||
raise NotImplementedError, "#{self.class}#constant? not implemented"
|
||||
end
|
||||
@@ -33,6 +36,7 @@ module RubyAlgebra
|
||||
|
||||
def initialize(lhs, rhs)
|
||||
raise ArgumentError unless lhs.is_a?(Expression) && rhs.is_a?(Expression)
|
||||
|
||||
@lhs = lhs
|
||||
@rhs = rhs
|
||||
end
|
||||
@@ -59,18 +63,22 @@ module RubyAlgebra
|
||||
|
||||
def ==(other)
|
||||
return false if other.type != type
|
||||
|
||||
lhs == other.lhs && rhs == other.rhs
|
||||
end
|
||||
|
||||
def diff(v)
|
||||
Addition.new(@lhs.diff(v), @rhs.diff(v))
|
||||
end
|
||||
|
||||
def evaluate
|
||||
lhs_val = @lhs.evaluate
|
||||
rhs_val = @rhs.evaluate
|
||||
return nil if lhs_val.nil? || rhs_val.nil?
|
||||
|
||||
lhs_val + rhs_val
|
||||
end
|
||||
|
||||
def constant?
|
||||
@lhs.constant? && @rhs.constant?
|
||||
end
|
||||
@@ -94,12 +102,15 @@ module RubyAlgebra
|
||||
def diff(v)
|
||||
Subtraction.new(@lhs.diff(v), @rhs.diff(v))
|
||||
end
|
||||
|
||||
def evaluate
|
||||
lhs_val = @lhs.evaluate
|
||||
rhs_val = @rhs.evaluate
|
||||
return nil if lhs_val.nil? || rhs_val.nil?
|
||||
|
||||
lhs_val - rhs_val
|
||||
end
|
||||
|
||||
def constant?
|
||||
@lhs.constant? && @rhs.constant?
|
||||
end
|
||||
@@ -110,6 +121,7 @@ module RubyAlgebra
|
||||
|
||||
def initialize(lhs, rhs)
|
||||
raise ArgumentError unless lhs.is_a?(Expression) && rhs.is_a?(Expression)
|
||||
|
||||
@lhs = lhs
|
||||
@rhs = rhs
|
||||
end
|
||||
@@ -121,12 +133,10 @@ module RubyAlgebra
|
||||
if lhs.type == :constant && lhs.value == 1
|
||||
need_parentheses_right ? "(#{rhs})" : rhs.to_s
|
||||
elsif lhs.type == :constant && lhs.value == -1
|
||||
need_parentheses_right ? "-(#{rhs})" : "-#{rhs.to_s}"
|
||||
need_parentheses_right ? "-(#{rhs})" : "-#{rhs}"
|
||||
elsif (need_parentheses_right || rhs.type == :variable) && (need_parentheses_left || lhs.type == :mult || lhs.type == :constant)
|
||||
result = need_parentheses_left ? "(#{lhs})" : lhs.to_s
|
||||
if lhs.type == :variable && rhs.type == :variable && lhs.single_letter? && rhs.single_letter?
|
||||
result += " "
|
||||
end
|
||||
result += ' ' if lhs.type == :variable && rhs.type == :variable && lhs.single_letter? && rhs.single_letter?
|
||||
result + (need_parentheses_right ? "(#{rhs})" : rhs.to_s)
|
||||
else
|
||||
result = need_parentheses_left ? "(#{lhs}) * " : "#{lhs} * "
|
||||
@@ -148,6 +158,7 @@ module RubyAlgebra
|
||||
|
||||
def ==(other)
|
||||
return false if other.type != type
|
||||
|
||||
lhs == other.lhs && rhs == other.rhs
|
||||
end
|
||||
|
||||
@@ -158,12 +169,15 @@ module RubyAlgebra
|
||||
term2 = Multiplication.new(@lhs, v_prime)
|
||||
Addition.new(term1, term2)
|
||||
end
|
||||
|
||||
def evaluate
|
||||
lhs_val = @lhs.evaluate
|
||||
rhs_val = @rhs.evaluate
|
||||
return nil if lhs_val.nil? || rhs_val.nil?
|
||||
|
||||
lhs_val * rhs_val
|
||||
end
|
||||
|
||||
def constant?
|
||||
@lhs.constant? && @rhs.constant?
|
||||
end
|
||||
@@ -181,6 +195,7 @@ module RubyAlgebra
|
||||
def type
|
||||
:div
|
||||
end
|
||||
|
||||
def diff(v)
|
||||
u_prime = @lhs.diff(v)
|
||||
v_prime = @rhs.diff(v)
|
||||
@@ -192,13 +207,16 @@ module RubyAlgebra
|
||||
denominator = Power.new(@rhs, Constant.new(2))
|
||||
Division.new(numerator, denominator)
|
||||
end
|
||||
|
||||
def evaluate
|
||||
lhs_val = @lhs.evaluate
|
||||
rhs_val = @rhs.evaluate
|
||||
return nil if lhs_val.nil? || rhs_val.nil?
|
||||
return nil if rhs_val == 0
|
||||
lhs_val / rhs_val
|
||||
|
||||
lhs_val.to_f / rhs_val
|
||||
end
|
||||
|
||||
def constant?
|
||||
@lhs.constant? && @rhs.constant?
|
||||
end
|
||||
@@ -209,6 +227,7 @@ module RubyAlgebra
|
||||
|
||||
def initialize(base, exponent)
|
||||
raise ArgumentError unless base.is_a?(Expression) && exponent.is_a?(Expression)
|
||||
|
||||
@base = base
|
||||
@exponent = exponent
|
||||
end
|
||||
@@ -235,12 +254,13 @@ module RubyAlgebra
|
||||
|
||||
def ==(other)
|
||||
return false if other.type != type
|
||||
|
||||
base == other.base && exponent == other.exponent
|
||||
end
|
||||
|
||||
def diff(v)
|
||||
unless @exponent.is_a?(Constant)
|
||||
raise NotImplementedError, "Дифференцирование степени с неконстантным показателем не реализовано"
|
||||
raise NotImplementedError, 'Дифференцирование степени с неконстантным показателем не реализовано'
|
||||
end
|
||||
|
||||
n = @exponent.value
|
||||
@@ -250,12 +270,15 @@ module RubyAlgebra
|
||||
coeff = Constant.new(n)
|
||||
Multiplication.new(coeff, Multiplication.new(new_power, base_diff))
|
||||
end
|
||||
|
||||
def evaluate
|
||||
base_val = @base.evaluate
|
||||
exp_val = @exponent.evaluate
|
||||
return nil if base_val.nil? || exp_val.nil?
|
||||
base_val ** exp_val
|
||||
|
||||
base_val**exp_val
|
||||
end
|
||||
|
||||
def constant?
|
||||
@base.constant? && @exponent.constant?
|
||||
end
|
||||
@@ -286,15 +309,18 @@ module RubyAlgebra
|
||||
|
||||
def ==(other)
|
||||
return false if other.type != type
|
||||
|
||||
value == other.value
|
||||
end
|
||||
|
||||
def diff(v)
|
||||
Constant.new(0)
|
||||
end
|
||||
|
||||
def evaluate
|
||||
@value
|
||||
end
|
||||
|
||||
def constant?
|
||||
true
|
||||
end
|
||||
@@ -330,15 +356,18 @@ module RubyAlgebra
|
||||
|
||||
def ==(other)
|
||||
return false if other.type != type
|
||||
|
||||
symbol == other.symbol
|
||||
end
|
||||
|
||||
def diff(v)
|
||||
@symbol == v ? Constant.new(1) : Constant.new(0)
|
||||
end
|
||||
|
||||
def evaluate
|
||||
nil
|
||||
end
|
||||
|
||||
def constant?
|
||||
false
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user