implement strict equality operator for subclasses of Expression

This commit is contained in:
2026-03-23 10:42:31 +03:00
parent 5cb52f7bb7
commit b4319a41f4

View File

@@ -50,6 +50,12 @@ module RubyAlgebra
def op_assoc_type def op_assoc_type
:left :left
end end
def ==(other)
return false if other.type != type
lhs == other.lhs && rhs == other.rhs
end
def diff(v) def diff(v)
Addition.new(@lhs.diff(v), @rhs.diff(v)) Addition.new(@lhs.diff(v), @rhs.diff(v))
end end
@@ -116,6 +122,11 @@ module RubyAlgebra
:left :left
end end
def ==(other)
return false if other.type != type
lhs == other.lhs && rhs == other.rhs
end
def diff(v) def diff(v)
u_prime = @lhs.diff(v) u_prime = @lhs.diff(v)
v_prime = @rhs.diff(v) v_prime = @rhs.diff(v)
@@ -179,6 +190,11 @@ module RubyAlgebra
:right :right
end end
def ==(other)
return false if other.type != type
base == other.base && exponent == other.exponent
end
def diff(v) def diff(v)
unless @exponent.is_a?(Constant) unless @exponent.is_a?(Constant)
raise NotImplementedError, "Дифференцирование степени с неконстантным показателем не реализовано" raise NotImplementedError, "Дифференцирование степени с неконстантным показателем не реализовано"
@@ -215,6 +231,12 @@ module RubyAlgebra
def op_assoc_type def op_assoc_type
nil nil
end end
def ==(other)
return false if other.type != type
value == other.value
end
def diff(v) def diff(v)
Constant.new(0) Constant.new(0)
end end
@@ -248,6 +270,11 @@ module RubyAlgebra
@is_single_letter @is_single_letter
end end
def ==(other)
return false if other.type != type
symbol == other.symbol
end
def diff(v) def diff(v)
@symbol == v ? Constant.new(1) : Constant.new(0) @symbol == v ? Constant.new(1) : Constant.new(0)
end end