OSDN Git Service

divided high order expression
authorTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Feb 2010 08:53:20 +0000 (17:53 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Feb 2010 08:53:20 +0000 (17:53 +0900)
  * factorization and expansion

12 files changed:
lib/mint/generator.rb
lib/mint/generator/arithmetic_base.rb
lib/mint/generator/decimal_arithmetic.rb
lib/mint/generator/expansion.rb [new file with mode: 0644]
lib/mint/generator/factorization.rb [new file with mode: 0644]
lib/mint/generator/fractional_arithmetic.rb
lib/mint/generator/high_order_expression_base.rb [moved from lib/mint/generator/high_order_expression.rb with 66% similarity]
lib/mint/generator/ordinary_arithmetic.rb
spec/generator/expansion_spec.rb [new file with mode: 0644]
spec/generator/factorization_spec.rb [new file with mode: 0644]
spec/generator/high_order_expression_base_spec.rb [new file with mode: 0644]
spec/generator/high_order_expression_spec.rb [deleted file]

index e0d9feb..6c5afc8 100644 (file)
@@ -2,13 +2,15 @@
 
 
 module Mint::Generator
-  autoload :Utilities,  'generator/utilities/comman_utils.rb'
+  autoload :Utilities,           'generator/utilities/comman_utils.rb'
+  autoload :Base,                'generator/base'
+  autoload :Arithmetic,          'generator/arithmetic_base'
+  autoload :HighOrderExpression, 'generator/high_order_expression_base'
 end
 
-require 'generator/base'
-
 generators = File.dirname(__FILE__) + '/generator/*.rb'
 Dir[generators].each do |path|
+  next if /base/ =~ path
   require path
 end
 
index 7eeb9e0..45bbc01 100644 (file)
@@ -6,19 +6,19 @@ module Mint::Generator
 
     DEFAULT_OPERATORS = %w[+ - * div]
 
-               private
+    private
 
-               include Utilities
+    include Utilities
 
     def do_generate(setting)
-                       defaults = {:term_number => 2, :operators => DEFAULT_OPERATORS}.merge(setting)
-                       @options = options(defaults)
+      defaults = {:term_number => 2, :operators => DEFAULT_OPERATORS}.merge(setting)
+      @options = options(defaults)
       expression.gsub(%r! (-[0-9./]+)!, ' (\\1)')
-               end
+    end
 
-               def operand
-                       raise 'need to overwrite'
-               end
+    def operand
+      raise 'need to overwrite'
+    end
 
     def expression
       result = []
@@ -51,5 +51,6 @@ module Mint::Generator
       end while numerator_part == denominator_part
       "#{numerator_part}/#{denominator_part}"
     end
-       end
+  end
 end
+
index da05111..8aaddf6 100644 (file)
@@ -4,16 +4,16 @@ module Mint::Generator
 
   class DecimalArithmetic < Arithmetic
 
-               private
+    private
 
-               def generate_problem
+    def generate_problem
       defaults = { :minus => true, :min => 0, :max => 100 }
-                       do_generate(defaults)
-               end
+      do_generate(defaults)
+    end
 
-               def operand
-                       decimal
-               end
-       end
+    def operand
+      decimal
+    end
+  end
 end
 
diff --git a/lib/mint/generator/expansion.rb b/lib/mint/generator/expansion.rb
new file mode 100644 (file)
index 0000000..e7ace34
--- /dev/null
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+module Mint::Generator
+
+  class Expansion < HighOrderExpression
+
+    private
+    def generate_problem
+      defaults =  {
+        :order_min => 2, :order_max => 3,
+        :factor_minus => true,
+        :factor_min   => 1,
+        :factor_max   => 100
+      }
+      do_generate(defaults)
+    end
+
+    def expression
+      expansion
+    end
+  end
+end
+
diff --git a/lib/mint/generator/factorization.rb b/lib/mint/generator/factorization.rb
new file mode 100644 (file)
index 0000000..78eb25f
--- /dev/null
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+module Mint::Generator
+
+  class Factorization < HighOrderExpression
+
+    private
+    def generate_problem
+      defaults = {
+        :order_min => 2, :order_max => 3,
+        :factor_minus => true,
+        :factor_min   => 1,
+        :factor_max   => 100
+      }
+      do_generate(defaults)
+    end
+
+    def expression
+      factorization
+    end
+  end
+end
+
index 3dae561..16bb14f 100644 (file)
@@ -4,16 +4,16 @@ module Mint::Generator
 
   class FractionalArithmetic < Arithmetic
 
-               private
+    private
 
-               def generate_problem
+    def generate_problem
       defaults = { :minus => true, :min => 0, :max => 100 }
-                       do_generate(defaults)
-               end
+      do_generate(defaults)
+    end
 
-               def operand
-                       fraction
-               end
-       end
+    def operand
+      fraction
+    end
+  end
 end
 
@@ -8,33 +8,10 @@ module Mint::Generator
 
     include Utilities
 
-    # TODO 係数ごとに設定できるようにする
-    def generate_problem
-      easy_factorization = {
-        :order_min => 2, :order_max => 3, :x => %w[x],
-        :factor_minus => true, :factor_min =>1, :factor_max => 100
-      }
-      factorization = {
-        :order_min => 2, :order_max => 3, :x => ['x'],
-        :factor_minus => true, :factor_min =>1, :factor_max => 100
-      }
-      expansion = {
-        :order_min => 2, :order_max => 3, :x => ['x'],
-        :factor_minus => true, :factor_min =>1, :factor_max => 100
-      }
-      options = options(:factorization => factorization, :expansion => expansion)
-      expression(options)
-    end
-
-    def expression(options)
-      result = []
-      if options[:factorization]
-        result << factorization(options[:factorization])
-      end
-      if options[:expansion]
-        result << expansion(options[:expansion])
-      end
-      result.sample
+    def do_generate(options)
+      defaults = { :x => ['x'] }.merge(options)
+      @options = options(defaults)
+      expression
     end
 
     # 判別式
@@ -45,17 +22,17 @@ module Mint::Generator
       b**2 - 4 * a * c
     end
 
-    def factorization(options)
+    def factorization
       order = create_integer(options[:order_min], options[:order_max], false)
       case order
-      when 2 then easy_factorization2(options)
-      when 3 then easy_factorization3(options)
+      when 2 then easy_factorization2
+      when 3 then easy_factorization3
       else
         raise NotImplementedError
       end
     end
 
-    def easy_factorization2(options)
+    def easy_factorization2
       alpha = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
       beta  = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
       a = 1
@@ -65,7 +42,7 @@ module Mint::Generator
       factorization2(a, b, c, x)
     end
 
-    def easy_factorization3(options)
+    def easy_factorization3
       alpha = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
       beta  = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
       gamma = create_integer(options[:factor_min], options[:factor_max], options[:factor_minus])
@@ -77,7 +54,7 @@ module Mint::Generator
       factorization3(a, b, c, d, x)
     end
 
-    def old_factorization2(options)
+    def old_factorization2
       begin
         a, b, c = Array.new(3){ factor(options) }
       end while a == 0 || (b == 0 && c == 0) || discriminant(a, b, c) < 0
@@ -85,7 +62,7 @@ module Mint::Generator
       factorization2(a, b, c, x)
     end
 
-    def old_factorization3(options)
+    def old_factorization3
       begin
         a, b, c, d = Array.new(4){ factor(options) }
         # 一階微分した式 = 0 とした方程式の判別式 < 0 ならば元の三次方程式は実数解なし
@@ -113,15 +90,15 @@ module Mint::Generator
         gsub(%r!\+ -!, '- ').gsub(/\A1#{x}|([ -])1#{x}/, '\1'+x)
     end
 
-    def expansion(options)
+    def expansion
       result = []
       order = create_integer(options[:order_min], options[:order_max], false)
       x = options[:x].sample
-      order.times{ result << single(_factor(options), _factor(options), x) }
+      order.times{ result << single(_factor, _factor, x) }
       result.join("")
     end
 
-    def _factor(options)
+    def _factor
       factor(options, 'factor')
     end
   end
index 44d83a7..b98bd54 100644 (file)
@@ -4,16 +4,16 @@ module Mint::Generator
 
   class OrdinaryArithmetic < Arithmetic
 
-               private
+    private
 
-               def generate_problem
+    def generate_problem
       defaults = { :minus => true, :min => 0, :max => 100 }
-                       do_generate(defaults)
-               end
+      do_generate(defaults)
+    end
 
-               def operand
-                       integer
-               end
-       end
+    def operand
+      integer
+    end
+  end
 end
 
diff --git a/spec/generator/expansion_spec.rb b/spec/generator/expansion_spec.rb
new file mode 100644 (file)
index 0000000..b3817ed
--- /dev/null
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+module Mint::Generator
+  describe Expansion do
+    subject{ Expansion.new }
+    context "order 2" do
+      before do
+        options = {
+          :x => ['x', 'y', 'z'],
+          :order_min  => 2, :order_max  => 2,
+          :factor_min => 1, :factor_max => 10
+        }
+        @problems = subject.generate(options)
+      end
+      it{ @problems.should have(1).problem }
+      it{ @problems.first.scan(%r!\(.+?\)!).should have(2).terms }
+      it{ @problems.first.should_not match(/(?:\d)1[xyz]/) }
+    end
+  end
+end
+
diff --git a/spec/generator/factorization_spec.rb b/spec/generator/factorization_spec.rb
new file mode 100644 (file)
index 0000000..09f6bfb
--- /dev/null
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+module Mint::Generator
+
+  describe Factorization do
+
+    context "order 2" do
+
+      subject{ Factorization.new }
+
+      before do
+        options = {
+          :order_min => 2, :order_max => 2, :x => ['x', 'y', 'z'],
+          :factor_min => 1, :factor_max => 10
+        }
+        @problems = subject.generate(options)
+      end
+      it{ @problems.should have(1).problem }
+      it{ @problems.first.split(%r! [+-] !).should have_at_most(3).terms }
+      it{ @problems.first.should match(%r!(?:\d+)?[xyz]\^2 [+-] (?:\d+)?[xyz] [+-] \d+!) }
+    end
+    context "order 2 when factor may be 0" do
+      subject{ Factorization.new(10) }
+      before do
+        options = {
+          :order_min => 2, :order_max => 2,
+          :factor_min => 0, :factor_max => 10
+        }
+        @problems = subject.generate(options)
+      end
+      it{ @problems.should have(10).problem }
+      it "have 3 terms at most" do
+        @problems.each do |problem|
+          problem.split(%r! [+-] !).should have_at_most(3).terms
+        end
+      end
+      it "have 2 terms at least" do
+        @problems.each do |problem|
+          problem.split(%r! [+-] !).should have_at_least(2).terms
+        end
+      end
+      it "match format" do
+        @problems.each do |problem|
+          problem.should match(%r!(?:\d+)?x\^2(?: [+-] (?:\d+)?x)?(?: [+-] \d+)?!)
+        end
+      end
+      it "does not match '1x'" do
+        @problems.each do |problem|
+          problem.should_not match(%r!\A1x|[ -]1x!)
+        end
+      end
+    end
+    context "order 3" do
+
+      subject { Factorization.new }
+
+      before do
+        options = {
+          :order_min => 3, :order_max => 3,
+          :factor_min => 1, :factor_max => 10
+        }
+        @problems = subject.generate(options)
+      end
+      it{ @problems.should have(1).problem }
+      it{ @problems.first.split(%r! [+-] !).should have_at_most(4).terms }
+      it{ @problems.first.should match(%r!(?:\d+)?x\^3 [+-] (?:\d+)?x\^2 [+-] (?:\d+)?x [+-] \d+!) }
+      it{ @problems.first.should_not match(%r!\A1x|[ -]1x!) }
+    end
+  end
+end
+
diff --git a/spec/generator/high_order_expression_base_spec.rb b/spec/generator/high_order_expression_base_spec.rb
new file mode 100644 (file)
index 0000000..42800d1
--- /dev/null
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../spec_helper.rb'
+
+module Mint::Generator
+  describe HighOrderExpression do
+    it 'need to write spec'
+  end
+end
+
diff --git a/spec/generator/high_order_expression_spec.rb b/spec/generator/high_order_expression_spec.rb
deleted file mode 100644 (file)
index 124e029..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-# -*- coding: utf-8 -*-
-
-require File.dirname(__FILE__) + '/../spec_helper.rb'
-
-module Mint
-  describe Generator::HighOrderExpression do
-    context "generate factorization" do
-      context "order 2" do
-        subject{ Generator::HighOrderExpression.new }
-        before do
-          factorization = {
-            :order_min => 2, :order_max => 2, :x => ['x', 'y', 'z'],
-            :factor_minus => true, :factor_min => 1, :factor_max => 10
-          }
-          @problems = subject.generate(:factorization => factorization, :expansion => nil)
-        end
-        it{ @problems.should have(1).problem }
-        it{ @problems.first.split(%r! [+-] !).should have_at_most(3).terms }
-        it{ @problems.first.should match(%r!(?:\d+)?[xyz]\^2 [+-] (?:\d+)?[xyz] [+-] \d+!) }
-      end
-      context "order 2 when factor may be 0" do
-        subject{ Generator::HighOrderExpression.new(10) }
-        before do
-          factorization = {
-            :order_min => 2, :order_max => 2, :x => ['x'],
-            :factor_minus => true, :factor_min => 0, :factor_max => 10
-          }
-          @problems = subject.generate(:factorization => factorization, :expansion => nil)
-        end
-        it{ @problems.should have(10).problem }
-        it "have 3 terms at most" do
-          @problems.each do |problem|
-            problem.split(%r! [+-] !).should have_at_most(3).terms
-          end
-        end
-        it "have 2 terms at least" do
-          @problems.each do |problem|
-            problem.split(%r! [+-] !).should have_at_least(2).terms
-          end
-        end
-        it "match format" do
-          @problems.each do |problem|
-            problem.should match(%r!(?:\d+)?x\^2(?: [+-] (?:\d+)?x)?(?: [+-] \d+)?!)
-          end
-        end
-        it "does not match '1x'" do
-          @problems.each do |problem|
-            problem.should_not match(%r!\A1x|[ -]1x!)
-          end
-        end
-      end
-      context "order 3" do
-        subject{ Generator::HighOrderExpression.new }
-        before do
-          factorization = {
-            :order_min => 3, :order_max => 3, :x => ['x'],
-            :factor_minus => true, :factor_min => 1, :factor_max => 10
-          }
-          @problems = subject.generate(:factorization => factorization, :expansion => nil)
-        end
-        it{ @problems.should have(1).problem }
-        it{ @problems.first.split(%r! [+-] !).should have_at_most(4).terms }
-        it{ @problems.first.should match(%r!(?:\d+)?x\^3 [+-] (?:\d+)?x\^2 [+-] (?:\d+)?x [+-] \d+!) }
-        it{ @problems.first.should_not match(%r!\A1x|[ -]1x!) }
-      end
-    end
-
-    context "generate expansion" do
-      subject{ Generator::HighOrderExpression.new }
-      context "order 2" do
-        before do
-          expansion = {
-            :order_min => 2, :order_max => 2, :x => ['x', 'y', 'z'],
-            :factor_minus => true, :factor_min => 1, :factor_max => 10
-          }
-          @problems = subject.generate(:factorization => nil, :expansion => expansion)
-        end
-        it{ @problems.should have(1).problem }
-        it{ @problems.first.scan(%r!\(.+?\)!).should have(2).terms }
-        it{ @problems.first.should_not match(/(?:\d)1[xyz]/) }
-      end
-    end
-  end
-end