commit
This commit is contained in:
@@ -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
|
||||
83
spec/expression_spec.rb
Normal file
83
spec/expression_spec.rb
Normal file
@@ -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
|
||||
@@ -138,7 +138,7 @@ RSpec.describe RubyAlgebra::Interpreter do
|
||||
cmd = RubyAlgebra::Parser.parse_command("Q, R := A / B")
|
||||
interpreter.execute(cmd)
|
||||
|
||||
expect(interpreter.execute(RubyAlgebra::Parser.parse_command("Q"))).to eq("1.0x + 1.0")
|
||||
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