Compare commits
3
Commits
polynomials
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9fc325d61c | ||
|
|
2890087e36 | ||
|
|
b7c03ecdcf |
@@ -1,130 +0,0 @@
|
||||
require "ruby_algebra"
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.expect_with :rspec do |expectations|
|
||||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.describe RubyAlgebra do
|
||||
describe "diff and evaluate" do
|
||||
let(:x) { RubyAlgebra::Variable.new("x") }
|
||||
let(:c) { RubyAlgebra::Constant.new(5) }
|
||||
|
||||
it "differentiates constant to 0" do
|
||||
expr = RubyAlgebra::Constant.new(10)
|
||||
derivative = expr.diff("x")
|
||||
expect(derivative.evaluate).to eq 0
|
||||
end
|
||||
|
||||
it "differentiates variable to 1" do
|
||||
expr = RubyAlgebra::Variable.new("x")
|
||||
derivative = expr.diff("x")
|
||||
expect(derivative.evaluate).to eq 1
|
||||
end
|
||||
|
||||
it "differentiates variable to 0 for different variable" do
|
||||
expr = RubyAlgebra::Variable.new("x")
|
||||
derivative = expr.diff("y")
|
||||
expect(derivative.evaluate).to eq 0
|
||||
end
|
||||
|
||||
it "differentiates sum" do
|
||||
expr = RubyAlgebra::Addition.new(x, c)
|
||||
derivative = expr.diff("x")
|
||||
expect(derivative.evaluate).to eq 1
|
||||
end
|
||||
|
||||
it "differentiates difference" do
|
||||
expr = RubyAlgebra::Subtraction.new(x, c)
|
||||
derivative = expr.diff("x")
|
||||
expect(derivative.evaluate).to eq 1
|
||||
end
|
||||
|
||||
it "differentiates product" do
|
||||
expr = RubyAlgebra::Multiplication.new(x, x)
|
||||
derivative = expr.diff("x")
|
||||
expr_at_3 = RubyAlgebra::Multiplication.new(
|
||||
RubyAlgebra::Constant.new(2),
|
||||
RubyAlgebra::Constant.new(3)
|
||||
)
|
||||
two_x = RubyAlgebra::Multiplication.new(
|
||||
RubyAlgebra::Constant.new(2),
|
||||
RubyAlgebra::Constant.new(3)
|
||||
)
|
||||
expect(two_x.evaluate).to eq 6
|
||||
end
|
||||
|
||||
it "differentiates power with constant exponent" do
|
||||
expr = RubyAlgebra::Power.new(x, RubyAlgebra::Constant.new(3))
|
||||
derivative = expr.diff("x")
|
||||
x_squared = RubyAlgebra::Power.new(
|
||||
RubyAlgebra::Constant.new(2),
|
||||
RubyAlgebra::Constant.new(2)
|
||||
)
|
||||
result = RubyAlgebra::Multiplication.new(
|
||||
RubyAlgebra::Constant.new(3),
|
||||
x_squared
|
||||
)
|
||||
expect(result.evaluate).to eq 12
|
||||
end
|
||||
|
||||
it "evaluates constant expression" do
|
||||
expr = RubyAlgebra::Addition.new(
|
||||
RubyAlgebra::Constant.new(3),
|
||||
RubyAlgebra::Constant.new(4)
|
||||
)
|
||||
expect(expr.evaluate).to eq 7
|
||||
end
|
||||
|
||||
it "evaluates complex constant expression" do
|
||||
expr = RubyAlgebra::Division.new(
|
||||
RubyAlgebra::Addition.new(
|
||||
RubyAlgebra::Multiplication.new(
|
||||
RubyAlgebra::Constant.new(2),
|
||||
RubyAlgebra::Constant.new(3)
|
||||
),
|
||||
RubyAlgebra::Constant.new(4)
|
||||
),
|
||||
RubyAlgebra::Constant.new(2)
|
||||
)
|
||||
expect(expr.evaluate).to eq 5
|
||||
end
|
||||
|
||||
it "returns nil for expression with variable" do
|
||||
expr = RubyAlgebra::Addition.new(
|
||||
RubyAlgebra::Constant.new(5),
|
||||
RubyAlgebra::Variable.new("x")
|
||||
)
|
||||
expect(expr.evaluate).to be_nil
|
||||
end
|
||||
|
||||
it "checks constant? predicate" do
|
||||
constant_expr = RubyAlgebra::Addition.new(
|
||||
RubyAlgebra::Constant.new(2),
|
||||
RubyAlgebra::Constant.new(3)
|
||||
)
|
||||
variable_expr = RubyAlgebra::Addition.new(
|
||||
RubyAlgebra::Constant.new(2),
|
||||
RubyAlgebra::Variable.new("x")
|
||||
)
|
||||
|
||||
expect(constant_expr).to be_constant
|
||||
expect(variable_expr).not_to be_constant
|
||||
end
|
||||
|
||||
it "differentiates complex expression" do
|
||||
x_squared = RubyAlgebra::Power.new(x, RubyAlgebra::Constant.new(2))
|
||||
three_x = RubyAlgebra::Multiplication.new(
|
||||
RubyAlgebra::Constant.new(3),
|
||||
x
|
||||
)
|
||||
inner = RubyAlgebra::Addition.new(x_squared, three_x)
|
||||
expr = RubyAlgebra::Multiplication.new(inner, x)
|
||||
|
||||
derivative = expr.diff("x")
|
||||
expect(derivative).to be_a(RubyAlgebra::Expression)
|
||||
expect(derivative.constant?).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,83 @@
|
||||
require 'rspec'
|
||||
require_relative '../lib/ruby_algebra/polynomial'
|
||||
require_relative '../lib/ruby_algebra/parser'
|
||||
|
||||
RSpec.describe "RubyAlgebra Extended Suite" do
|
||||
let(:x) { RubyAlgebra::Polynomial.new([RubyAlgebra::Term.new(1, { "x" => 1 })]) }
|
||||
let(:y) { RubyAlgebra::Polynomial.new([RubyAlgebra::Term.new(1, { "y" => 1 })]) }
|
||||
|
||||
describe "Математические крайности (Edge Cases)" do
|
||||
it "умножение на ноль дает пустой многочлен (0)" do
|
||||
zero = RubyAlgebra::Polynomial.new([RubyAlgebra::Term.new(0)])
|
||||
result = x * zero
|
||||
expect(result.terms.reject { |t| t.coeff == 0 }).to be_empty
|
||||
end
|
||||
|
||||
it "возведение в степень через умножение (x * x * x = x^3)" do
|
||||
result = x * x * x
|
||||
expect(result.terms.first.variables["x"]).to eq 3
|
||||
expect(result.terms.first.coeff).to eq 1
|
||||
end
|
||||
|
||||
it "сложение противоположных значений (x + (-x) = 0)" do
|
||||
minus_x = x * -1
|
||||
result = x + minus_x
|
||||
expect(result.terms.reject { |t| t.coeff == 0 }).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe "Дифференцирование (Продвинутое)" do
|
||||
it "производная от константы всегда 0" do
|
||||
expr = RubyAlgebra::Polynomial.new([RubyAlgebra::Term.new(999)])
|
||||
expect(expr.diff("x").terms.all? { |t| t.coeff == 0 }).to be true
|
||||
end
|
||||
|
||||
it "вторая производная: diff(x^2) -> 2x, diff(2x) -> 2" do
|
||||
first_der = (x * x).diff("x")
|
||||
second_der = first_der.diff("x")
|
||||
expect(second_der.terms.first.coeff).to eq 2
|
||||
expect(second_der.terms.first.variables).to be_empty
|
||||
end
|
||||
|
||||
it "частная производная: diff(x*y, 'x') -> y" do
|
||||
expr = x * y
|
||||
derivative = expr.diff("x")
|
||||
expect(derivative.terms.first.variables).to eq({ "y" => 1 })
|
||||
expect(derivative.terms.first.variables).not_to have_key("x")
|
||||
end
|
||||
end
|
||||
|
||||
describe "Подстановка (Substitution)" do
|
||||
it "подстановка в многочлен с двумя переменными (x + y where x=1, y=2)" do
|
||||
expr = x + y
|
||||
# substitute принимает хеш значений
|
||||
result = expr.substitute({ "x" => 1, "y" => 2 })
|
||||
# Должно получиться 3 (константа)
|
||||
expect(result.terms.first.coeff).to eq 3
|
||||
expect(result.terms.first.variables).to be_empty
|
||||
end
|
||||
|
||||
it "частичная подстановка (x*y where x=2) -> 2y" do
|
||||
expr = x * y
|
||||
result = expr.substitute({ "x" => 2 })
|
||||
expect(result.terms.first.coeff).to eq 2
|
||||
expect(result.terms.first.variables).to eq({ "y" => 1 })
|
||||
end
|
||||
end
|
||||
|
||||
describe "Строковое представление (to_s)" do
|
||||
it "корректно отображает отрицательные коэффициенты" do
|
||||
expr = x - 5
|
||||
expect(expr.to_s).to eq "x - 5"
|
||||
end
|
||||
|
||||
it "не пишет '1' перед переменной (x вместо 1x)" do
|
||||
expect(x.to_s).to eq "x"
|
||||
end
|
||||
|
||||
it "выводит '0', если многочлен пуст" do
|
||||
zero_poly = RubyAlgebra::Polynomial.new([])
|
||||
expect(zero_poly.to_s).to eq "0"
|
||||
end
|
||||
end
|
||||
end
|
||||
+123
-46
@@ -1,67 +1,144 @@
|
||||
require "ruby_algebra"
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.expect_with :rspec do |expectations|
|
||||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.describe RubyAlgebra::Parser do
|
||||
describe "#parse" do
|
||||
it "returns nil for an empty string" do
|
||||
expect(RubyAlgebra::Parser.parse("")).to eq nil
|
||||
# Хелпер для быстрой сборки ожидаемых многочленов в тестах
|
||||
def build_poly(terms_array)
|
||||
terms = terms_array.map do |t|
|
||||
RubyAlgebra::Term.new(t[:coeff], t[:vars] || {})
|
||||
end
|
||||
RubyAlgebra::Polynomial.new(terms)
|
||||
end
|
||||
|
||||
it "returns Constant(a) for a" do
|
||||
[10, 100, 343843, 180.1, 777, 0.52242].each do |a|
|
||||
expect(RubyAlgebra::Parser.parse(a.to_s)).to eq RubyAlgebra::Constant.new(a)
|
||||
describe '.parse_polynomial' do
|
||||
let(:tokenizer) { ->(str) { RubyAlgebra::Parser::Tokenizer.new(str) } }
|
||||
|
||||
it 'парсит простое число как константный многочлен' do
|
||||
result = RubyAlgebra::Parser.parse_polynomial(tokenizer.call("42"))
|
||||
expect(result).to eq(build_poly([{ coeff: 42 }]))
|
||||
end
|
||||
|
||||
it 'парсит одиночную переменную' do
|
||||
result = RubyAlgebra::Parser.parse_polynomial(tokenizer.call("x"))
|
||||
expect(result).to eq(build_poly([{ coeff: 1, vars: { "x" => 1 } }]))
|
||||
end
|
||||
|
||||
it 'парсит многочлен с несколькими термами и степенями' do
|
||||
# 2x^2 + 3x - 5
|
||||
result = RubyAlgebra::Parser.parse_polynomial(tokenizer.call("2x^2 + 3x - 5"))
|
||||
expected = build_poly([
|
||||
{ coeff: 2, vars: { "x" => 2 } },
|
||||
{ coeff: 3, vars: { "x" => 1 } },
|
||||
{ coeff: -5, vars: {} }
|
||||
])
|
||||
expect(result).to eq(expected)
|
||||
end
|
||||
|
||||
it 'обрабатывает отрицательный первый терм' do
|
||||
result = RubyAlgebra::Parser.parse_polynomial(tokenizer.call("-x + 1"))
|
||||
expect(result).to eq(build_poly([
|
||||
{ coeff: -1, vars: { "x" => 1 } },
|
||||
{ coeff: 1 }
|
||||
]))
|
||||
end
|
||||
end
|
||||
|
||||
it "returns Variable(x) for x" do
|
||||
["x", "y", "z", "coolVariable"].each do |x|
|
||||
expect(RubyAlgebra::Parser.parse(x)).to eq RubyAlgebra::Variable.new(x)
|
||||
describe '.parse_command' do
|
||||
context 'Арифметические операции (взаимодействие многочленов)' do
|
||||
it 'парсит команду сложения: C := A + B' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("C := A + B")
|
||||
expect(cmd).to be_a(RubyAlgebra::AssignmentCommand)
|
||||
expect(cmd.operation).to eq(:add)
|
||||
expect(cmd.lhs).to eq("C")
|
||||
expect(cmd.operand1).to eq("A")
|
||||
expect(cmd.operand2).to eq("B")
|
||||
end
|
||||
|
||||
it 'парсит команду вычитания: Result := P1 - P2' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("Result := P1 - P2")
|
||||
expect(cmd.operation).to eq(:sub)
|
||||
expect(cmd.operand1).to eq("P1")
|
||||
expect(cmd.operand2).to eq("P2")
|
||||
end
|
||||
|
||||
it 'парсит команду умножения: M := Poly1 * Poly2' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("M := Poly1 * Poly2")
|
||||
expect(cmd.operation).to eq(:mult)
|
||||
expect(cmd.operand1).to eq("Poly1")
|
||||
expect(cmd.operand2).to eq("Poly2")
|
||||
end
|
||||
|
||||
it 'парсит деление с остатком: Q, R := A / B' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("Q, R := A / B")
|
||||
expect(cmd.operation).to eq(:div)
|
||||
expect(cmd.lhs).to eq(["Q", "R"])
|
||||
expect(cmd.operand1).to eq("A")
|
||||
expect(cmd.operand2).to eq("B")
|
||||
end
|
||||
end
|
||||
|
||||
it "returns Addition(Constant(5), Variable(x)) for 5 + x" do
|
||||
expect(RubyAlgebra::Parser.parse("5 + x")).to eq RubyAlgebra::Addition.new(RubyAlgebra::Constant.new(5), RubyAlgebra::Variable.new("x"))
|
||||
context 'Операции с числами (Scales)' do
|
||||
it 'парсит умножение на число как :scale' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("A := B * 10")
|
||||
expect(cmd.operation).to eq(:scale)
|
||||
expect(cmd.operand2).to eq(10)
|
||||
end
|
||||
|
||||
it "returns Subtraction(Variable(z), Variable(h)) for z - h" do
|
||||
expect(RubyAlgebra::Parser.parse("z - h")).to eq RubyAlgebra::Subtraction.new(RubyAlgebra::Variable.new("z"), RubyAlgebra::Variable.new("h"))
|
||||
it 'парсит деление на число как :scale с инверсией' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("A := B / 2")
|
||||
expect(cmd.operation).to eq(:scale)
|
||||
expect(cmd.operand2).to eq(0.5)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns Multiplication(Constant(5), Constant(8)) for 60 * 8" do
|
||||
expect(RubyAlgebra::Parser.parse("60 * 8")).to eq RubyAlgebra::Multiplication.new(RubyAlgebra::Constant.new(60), RubyAlgebra::Constant.new(8))
|
||||
context 'Специальные методы' do
|
||||
it 'парсит дифференцирование: D := Diff(P, x)' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("D := Diff(P, x)")
|
||||
expect(cmd.operation).to eq(:diff)
|
||||
expect(cmd.operand1).to eq("P")
|
||||
expect(cmd.operand2).to eq(["x"])
|
||||
end
|
||||
|
||||
it "returns Division(Constant(3), Variable(j)) for 3/j" do
|
||||
expect(RubyAlgebra::Parser.parse("3/j")).to eq RubyAlgebra::Division.new(RubyAlgebra::Constant.new(3), RubyAlgebra::Variable.new("j"))
|
||||
it 'парсит подстановку: S := Subs(P, x=2, y=3)' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("S := Subs(P, x=2, y=3)")
|
||||
expect(cmd.operation).to eq(:subs)
|
||||
expect(cmd.operand1).to eq("P")
|
||||
expect(cmd.operand2).to eq({ "x" => 2, "y" => 3 })
|
||||
end
|
||||
end
|
||||
|
||||
it "returns Division(Constant(3), Variable(j)) for 3/j" do
|
||||
expect(RubyAlgebra::Parser.parse("3/j")).to eq RubyAlgebra::Division.new(RubyAlgebra::Constant.new(3), RubyAlgebra::Variable.new("j"))
|
||||
end
|
||||
|
||||
it "returns Power(Constant(2), Constant(24)) for 2^24" do
|
||||
expect(RubyAlgebra::Parser.parse("2^24")).to eq RubyAlgebra::Power.new(RubyAlgebra::Constant.new(2), RubyAlgebra::Constant.new(24))
|
||||
end
|
||||
|
||||
it "returns Multiplication(Constant(-1), Variable(x)) for -x" do
|
||||
expect(RubyAlgebra::Parser.parse("-x")).to eq RubyAlgebra::Multiplication.new(RubyAlgebra::Constant.new(-1), RubyAlgebra::Variable.new("x"))
|
||||
end
|
||||
|
||||
it "returns Variable(x) for +x" do
|
||||
expect(RubyAlgebra::Parser.parse("x")).to eq RubyAlgebra::Variable.new("x")
|
||||
end
|
||||
|
||||
it "returns Addition(Addition(Constant(3), Constant(30)), Constant(300)) for 3 + 30 + 300" do
|
||||
expect(RubyAlgebra::Parser.parse("3 + 30 + 300")).to eq RubyAlgebra::Addition.new(RubyAlgebra::Addition.new(RubyAlgebra::Constant.new(3), RubyAlgebra::Constant.new(30)), RubyAlgebra::Constant.new(300))
|
||||
end
|
||||
|
||||
it "returns Power(Constant(10), Power(Constant(10), Constant(10))) for 10 ^ 10 ^ 10" do
|
||||
expect(RubyAlgebra::Parser.parse("10 ^ 10 ^ 10")).to eq RubyAlgebra::Power.new(RubyAlgebra::Constant.new(10), RubyAlgebra::Power.new(RubyAlgebra::Constant.new(10), RubyAlgebra::Constant.new(10)))
|
||||
it 'парсит команду вывода (Display)' do
|
||||
cmd = RubyAlgebra::Parser.parse_command("A")
|
||||
expect(cmd).to be_a(RubyAlgebra::DisplayCommand)
|
||||
expect(cmd.item).to eq("A")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.describe RubyAlgebra::Interpreter do
|
||||
let(:interpreter) { RubyAlgebra::Interpreter.new }
|
||||
|
||||
it 'выполняет цепочку: присваивание и сложение' do
|
||||
# Создаем полиномы через парсер и выполняем команды через интерпретатор
|
||||
p1_cmd = RubyAlgebra::Parser.parse_command("A := 2x")
|
||||
p2_cmd = RubyAlgebra::Parser.parse_command("B := 3x + 1")
|
||||
add_cmd = RubyAlgebra::Parser.parse_command("C := A + B")
|
||||
|
||||
interpreter.execute(p1_cmd)
|
||||
interpreter.execute(p2_cmd)
|
||||
result_str = interpreter.execute(add_cmd)
|
||||
|
||||
expect(result_str).to include("5x + 1")
|
||||
end
|
||||
|
||||
it 'выполняет деление с остатком' do
|
||||
# (x^2 - 1) / (x - 1) = x + 1, остаток 0
|
||||
interpreter.execute(RubyAlgebra::Parser.parse_command("A := 1x^2 - 1"))
|
||||
interpreter.execute(RubyAlgebra::Parser.parse_command("B := 1x - 1"))
|
||||
|
||||
# В вашей грамматике деление: Q, R := A / B
|
||||
cmd = RubyAlgebra::Parser.parse_command("Q, R := A / B")
|
||||
interpreter.execute(cmd)
|
||||
|
||||
expect(interpreter.execute(RubyAlgebra::Parser.parse_command("Q"))).to eq("x + 1.0")
|
||||
expect(interpreter.execute(RubyAlgebra::Parser.parse_command("R"))).to eq("0")
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user