add substitution and differentiation

This commit is contained in:
2026-04-13 19:58:40 +03:00
parent 1fff529e83
commit 037c34b38e
3 changed files with 31 additions and 12 deletions

View File

@@ -33,16 +33,12 @@ module RubyAlgebra
result
end
def diff
raise NotImplementedError
def diff(variable)
Polynomial.new(@terms.map { |term| term.diff(variable) })
end
def substitute(values)
result = 0
@terms.each do |term|
# ...
end
result
Polynomial.new(@terms.map { |term| term.substitute(values) })
end
def +(other)
@@ -190,12 +186,28 @@ module RubyAlgebra
end
end
def diff(var)
raise NotImplementedError
def diff(variable)
return Term.new if @variables[variable].nil?
new_coeff = @coeff
new_variables = @variables.filter { |var, power| var != variable }
unless @variables[variable] == 1
new_coeff *= @variables[variable]
new_variables[variable] = @variables[variable] - 1
end
Term.new(new_coeff, new_variables)
end
def substitute(values)
raise NotImplementedError
new_coeff = @coeff
new_variables = {}
@variables.each do |var, pow|
if values[var].nil?
new_variables[var] = pow
else
new_coeff *= values[var] ** pow
end
end
Term.new(new_coeff, new_variables)
end
def +(other)