добавил производную
This commit is contained in:
@@ -17,6 +17,9 @@ module RubyAlgebra
|
|||||||
def op_assoc_type
|
def op_assoc_type
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
raise NotImplementedError, "#{self.class}#diff not implemented"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Addition < Expression
|
class Addition < Expression
|
||||||
@@ -47,6 +50,9 @@ module RubyAlgebra
|
|||||||
def op_assoc_type
|
def op_assoc_type
|
||||||
:left
|
:left
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
Addition.new(@lhs.diff(v), @rhs.diff(v))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Subtraction < Addition
|
class Subtraction < Addition
|
||||||
@@ -63,6 +69,9 @@ module RubyAlgebra
|
|||||||
def type
|
def type
|
||||||
:sub
|
:sub
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
Subtraction.new(@lhs.diff(v), @rhs.diff(v))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Multiplication < Expression
|
class Multiplication < Expression
|
||||||
@@ -105,6 +114,13 @@ module RubyAlgebra
|
|||||||
def op_assoc_type
|
def op_assoc_type
|
||||||
:left
|
:left
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
u_prime = @lhs.diff(v)
|
||||||
|
v_prime = @rhs.diff(v)
|
||||||
|
term1 = Multiplication.new(u_prime, @rhs)
|
||||||
|
term2 = Multiplication.new(@lhs, v_prime)
|
||||||
|
Addition.new(term1, term2)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Division < Multiplication
|
class Division < Multiplication
|
||||||
@@ -119,6 +135,17 @@ module RubyAlgebra
|
|||||||
def type
|
def type
|
||||||
:div
|
:div
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
u_prime = @lhs.diff(v)
|
||||||
|
v_prime = @rhs.diff(v)
|
||||||
|
|
||||||
|
numerator = Subtraction.new(
|
||||||
|
Multiplication.new(u_prime, @rhs),
|
||||||
|
Multiplication.new(@lhs, v_prime)
|
||||||
|
)
|
||||||
|
denominator = Power.new(@rhs, Constant.new(2))
|
||||||
|
Division.new(numerator, denominator)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Power < Expression
|
class Power < Expression
|
||||||
@@ -149,6 +176,18 @@ module RubyAlgebra
|
|||||||
def op_assoc_type
|
def op_assoc_type
|
||||||
:right
|
:right
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
unless @exponent.is_a?(Constant)
|
||||||
|
raise NotImplementedError, "Дифференцирование степени с неконстантным показателем не реализовано"
|
||||||
|
end
|
||||||
|
|
||||||
|
n = @exponent.value
|
||||||
|
# (u^n)' = n * u^(n-1) * u'
|
||||||
|
base_diff = @base.diff(v)
|
||||||
|
new_power = Power.new(@base, Constant.new(n - 1))
|
||||||
|
coeff = Constant.new(n)
|
||||||
|
Multiplication.new(coeff, Multiplication.new(new_power, base_diff))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Constant < Expression
|
class Constant < Expression
|
||||||
@@ -173,6 +212,9 @@ module RubyAlgebra
|
|||||||
def op_assoc_type
|
def op_assoc_type
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
Constant.new(0)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Variable < Expression
|
class Variable < Expression
|
||||||
@@ -202,5 +244,8 @@ module RubyAlgebra
|
|||||||
def single_letter?
|
def single_letter?
|
||||||
@is_single_letter
|
@is_single_letter
|
||||||
end
|
end
|
||||||
|
def diff(v)
|
||||||
|
@symbol == v ? Constant.new(1) : Constant.new(0)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user