add substitution and differentiation
This commit is contained in:
@@ -50,7 +50,7 @@ module RubyAlgebra
|
||||
@locals[command.lhs] = result
|
||||
return "#{command.lhs} = #{result}"
|
||||
when :subs
|
||||
result = operand1.evaluate(operand2)
|
||||
result = operand1.substitute(operand2)
|
||||
@locals[command.lhs] = result
|
||||
return "#{command.lhs} = #{result}"
|
||||
end
|
||||
|
||||
@@ -261,10 +261,17 @@ module RubyAlgebra
|
||||
raise ParserError, "unexpected token #{n.type}, expected ="
|
||||
end
|
||||
n = tokenizer.next_token
|
||||
unless n.type == :num || n.type == :plus || n.type == :minus
|
||||
raise ParserError, "unexpected token #{n.type}, expected -, + or number"
|
||||
end
|
||||
if n.type == :plus || n.type == :minus
|
||||
negative = true if n.type == :minus
|
||||
n = tokenizer.next_token
|
||||
end
|
||||
unless n.type == :num
|
||||
raise ParserError, "unexpected token #{n.type}, expected number"
|
||||
end
|
||||
substitutions[variable] = n.value
|
||||
substitutions[variable] = if negative then -n.value else n.value end
|
||||
end
|
||||
n = tokenizer.next_token
|
||||
unless n.type == :paren && n.closing == true
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user