OSDN Git Service

quadratic_equation generator has p_minus option now
authorTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 18 Mar 2010 02:52:46 +0000 (11:52 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 18 Mar 2010 02:52:46 +0000 (11:52 +0900)
lib/mint/generator/quadratic_equation.rb
spec/generator/quadratic_equation_spec.rb
spec/solver/maxima/quadratic_equation_spec.rb

index 7c93264..462b93c 100644 (file)
@@ -25,6 +25,8 @@ module Mint::Generator
   # [_p_max_]
   #   p の最大値を4以上の整数で指定します。
   #   素数を指定することは出来ません。
+  # [_p_minus_]
+  #   真を指定すると、p の値として負の値も生成します。
   # [_q_min_]
   #   q の最小値を0以上の整数で指定します。
   # [_q_max_]
@@ -51,6 +53,7 @@ module Mint::Generator
     option :r_max,       20
     option :p_min,       0
     option :p_max,       12
+    option :p_minus,     true
     option :q_min,       0
     option :q_max,       40
 
@@ -120,9 +123,7 @@ module Mint::Generator
     end
 
     def generate_p
-      min = [options[:p_min], -options[:p_max]].max
-      minus = min < 0 ? true : false
-      create_integer(min, options[:p_max], minus) {|pa|
+      create_integer(options[:p_min], options[:p_max], options[:p_minus]) {|pa|
         !Prime.prime?(pa)
       }
     end
index 7124a89..04b8687 100644 (file)
@@ -67,7 +67,7 @@ module Mint::Generator
           before do
             @options = {
               :r_min => r_min, :r_max => r_max,
-              :p_min => p_min, :p_max => p_max,
+              :p_min => p_min, :p_max => p_max, :p_minus => false,
               :q_min => q_min, :q_max => q_max,
             }
             subject.__send__(:options=, @options)
@@ -77,6 +77,11 @@ module Mint::Generator
 
           it { (@options[:r_min]..@options[:r_max]).should include(subject.__send__(:generate_r)) }
           it { (@options[:p_min]..@options[:p_max]).should include(subject.__send__(:generate_p)) }
+          it do
+            subject.__send__(:options=, subject.__send__(:options).merge(:p_minus => true))
+            (-@options[:p_min]..@options[:p_max]).should include(subject.__send__(:generate_p))
+            subject.__send__(:options=, subject.__send__(:options).merge(:p_minus => false))
+          end
 
           it { @squares.should include(subject.__send__(:rational_q)) }
           it { @squares.should_not include(subject.__send__(:irrational_q)) }
index 05bac9d..fcc2395 100644 (file)
@@ -18,6 +18,7 @@ module Mint::Solver::Maxima
       ['225 x^2 - 120 x + 31', ['4/15+root(15)i/15', '4/15-root(15)i/15']],
       ['x^2  + 7 x + 16',      ['-7/2+root(15)i/2', '-7/2-root(15)i/2']],
       ['49 x^2 - 84 x + 64',   ['6/7+2root(7)i/7', '6/7-2root(7)i/7']],
+      ['(x + 2)^2', ['-2']],
 
     ].each do |exp, ans|
       it exp do