class Factory < Mint::FactoryBase
def self.create(type, context = Maxima)
- context.const_get(constantize(type)).new(amount)
+ context.const_get(constantize(type)).new
rescue NameError
nil
end
end
module Maxima
- class Solver
+ class Factory
def self.create(type)
- Solver::Factory.create(type)
+ Solver::Factory.create(type, Maxima)
end
end
end
module Mint
module Solver
-
- @@_current_engine = nil
-
- class << self
- def current_engine
- @@_current_engine
- end
-
- def set_engine(engine)
- @@_current_engine = engine
- end
-
- def clear_engine
- @@_current_engine = nil
- end
-
- def solve(problem)
- current_engine.solve(problem)
- end
- end
end
end
-require 'solver/engines'
+dir = File.dirname(__FILE__) + '/solver/*'
+Dir[dir].each do |solver|
+ require solver
+end
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine
+module Mint::Solver
+
class Base
- def self.solve(problem)
+ def solve(problem)
unless problem.instance_of?(Mint::Expression)
raise 'need to Mint::Expression'
end
end
private
- def self.solve_problem(problem)
+ def solve_problem(problem)
raise 'you must overwrite this method.'
end
end
+++ /dev/null
-# -*- coding: utf-8 -*-
-
-module Mint::Solver
- module Engine
- end
-end
-
-# mint/solver/engines/base must be required first
-require File.dirname(__FILE__) + '/engines/base'
-solvers = File.dirname(__FILE__) + '/engines/*'
-Dir[solvers].each do |engine|
- require engine
-end
-
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine
+module Mint::Solver
module Maxima
end
end
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine::Maxima
+module Mint::Solver::Maxima
class Arithmetic < Base
private
- def self.solve_problem(problem)
+ def solve_problem(problem)
maxima(problem)
end
end
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine::Maxima
+module Mint::Solver::Maxima
- class Base < Mint::Solver::Engine::Base
+ class Base < Mint::Solver::Base
private
- def self.maxima(expression)
+ def maxima(expression)
result = exec_maxima(expression)
result.match(/^\(%o2\)\s+(.*)$/)[1]
end
- def self.expand(expression)
+ def expand(expression)
maxima("expand(#{expression})")
end
- def self.factor(expression)
+ def factor(expression)
maxima("factor(#{expression})")
end
- def self.partfrac(expression)
+ def partfrac(expression)
unless x = expression.match(/([a-zA-Z])/)
raise 'invalid fractional expression'
end
maxima("partfrac(#{expression}, #{x[0]})")
end
- def self.exec_maxima(expression)
+ def exec_maxima(expression)
require 'open3'
command = "maxima -r 'display2d:false; #{expression};'"
Open3.popen3(command) do |sin, sout, serr|
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine::Maxima
+module Mint::Solver::Maxima
class Expantion < Base
private
- def self.solve_problem(problem)
+ def solve_problem(problem)
expand(problem)
end
end
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine::Maxima
+module Mint::Solver::Maxima
class Factorization < Base
private
- def self.solve_problem(problem)
+ def solve_problem(problem)
factor(problem)
end
end
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine::Maxima
+module Mint::Solver::Maxima
class Fraction < Base
private
- def self.solve_problem(problem)
+ def solve_problem(problem)
maxima(problem)
end
end
# -*- coding: utf-8 -*-
-module Mint::Solver::Engine::Maxima
+module Mint::Solver::Maxima
class PartilanFractionExpansion < Base
private
- def self.solve_problem(problem)
+ def solve_problem(problem)
partfrac(problem)
end
end
context Maxima do
it 'can generate solver' do
- pending 'not implemented yet'
- solver = Factory.create(:ordinary_arithmetic)
- solver.should be_an_instance_of(OrdinaryArithmetic)
+ solver = Factory.create(:fraction)
+ solver.should be_an_instance_of(Fraction)
end
end
end
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../spec_helper.rb'
-module Mint
+module Mint::Solver
- describe Solver::Engine::Base do
+ describe Base do
- subject { Solver::Engine::Base }
+ subject { Base.new }
describe 'core functions' do
context 'solve problem' do
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../../spec_helper.rb'
-module Mint::Solver::Engine
- describe Maxima::Arithmetic do
- subject { Maxima::Arithmetic }
+module Mint::Solver::Maxima
+
+ describe Arithmetic do
+
+ subject { Arithmetic.new }
[
['-92.92 - (-0.45)', '-92.47'],
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../../spec_helper.rb'
-module Mint::Solver::Engine
+module Mint::Solver::Maxima
- describe Maxima::Base do
+ describe Base do
- subject { Maxima::Base }
+ subject { Base.new }
it 'uses maxima' do
answer = subject.__send__(:maxima, '2 + 3')
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../../spec_helper.rb'
+
+module Mint::Solver::Maxima
+
+ describe Expantion do
+
+ subject { Expantion.new }
-module Mint::Solver::Engine
- describe Maxima::Expantion do
- subject { Maxima::Expantion }
[
['(2a + 1)(3a - 2)', '6a^2 - a - 2'],
].each do |exp, ans|
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../../spec_helper.rb'
-module Mint::Solver::Engine
- describe Maxima::Factorization do
- subject { Maxima::Factorization }
+module Mint::Solver::Maxima
+
+ describe Factorization do
+
+ subject { Factorization.new }
[
['6a^2 - a - 2', '(2a + 1)(3a - 2)'],
].each do |exp, ans|
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../../spec_helper.rb'
+
+module Mint::Solver::Maxima
+
+ describe Fraction do
+
+ subject { Fraction.new }
-module Mint::Solver::Engine
- describe Maxima::Fraction do
- subject { Maxima::Fraction }
[
['-70/71 div 84/50', '-125/213'],
['25/66 * 60/93', '250/1023'],
# -*- coding: utf-8 -*-
-require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+require File.dirname(__FILE__) + '/../../spec_helper.rb'
+
+module Mint::Solver::Maxima
+
+ describe PartilanFractionExpansion do
+
+ subject { PartilanFractionExpansion.new }
-module Mint::Solver::Engine
- describe Maxima::PartilanFractionExpansion do
- subject { Maxima::PartilanFractionExpansion }
[
['1/(x + 1)(x + 2)', '1 / (x + 1) - 1 / (x + 2)'],
].each do |exp, ans|