|
| 1 | +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') |
| 2 | +describe Minimization::Unidimensional, "subclass" do |
| 3 | + before(:all) do |
| 4 | + @p1=rand(100) |
| 5 | + @p2=rand(100) |
| 6 | + @func=lambda {|x| (x-@p1)**2+@p2} |
| 7 | + @funcd=lambda {|x| 2*(x-@p1)} |
| 8 | + @funcdd=lambda {|x| 2} |
| 9 | + end |
| 10 | + |
| 11 | + describe Minimization::NewtonRaphson do |
| 12 | + before do |
| 13 | + @min = Minimization::NewtonRaphson.new(-1000,1000, @func,@funcd, @funcdd) |
| 14 | + @min.iterate |
| 15 | + end |
| 16 | + it "#x_minimum be close to expected" do |
| 17 | + @min.x_minimum.should be_close(@p1,@min.epsilon) |
| 18 | + end |
| 19 | + it "#f_minimum ( f(x)) be close to expected" do |
| 20 | + @min.f_minimum.should be_close(@p2,@min.epsilon) |
| 21 | + end |
| 22 | + context "#log" do |
| 23 | + subject {@min.log} |
| 24 | + it {should be_instance_of Array} |
| 25 | + it {should respond_to :to_table} |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + |
| 30 | + describe Minimization::GoldenSection do |
| 31 | + before do |
| 32 | + @min = Minimization::GoldenSection.minimize(-1000,1000, &@func) |
| 33 | + end |
| 34 | + it "#x_minimum be close to expected" do |
| 35 | + @min.x_minimum.should be_close(@p1,@min.epsilon) |
| 36 | + end |
| 37 | + it "#f_minimum ( f(x)) be close to expected" do |
| 38 | + @min.f_minimum.should be_close(@p2,@min.epsilon) |
| 39 | + end |
| 40 | + context "#log" do |
| 41 | + subject {@min.log} |
| 42 | + it {should be_instance_of Array} |
| 43 | + it {should respond_to :to_table} |
| 44 | + end |
| 45 | + end |
| 46 | + describe Minimization::Brent do |
| 47 | + before do |
| 48 | + @min = Minimization::Brent.minimize(-1000,1000, &@func) |
| 49 | + end |
| 50 | + it "should x be correct" do |
| 51 | + @min.x_minimum.should be_close(@p1,@min.epsilon) |
| 52 | + end |
| 53 | + it "should f(x) be correct" do |
| 54 | + @min.f_minimum.should be_close(@p2,@min.epsilon) |
| 55 | + end |
| 56 | + context "#log" do |
| 57 | + subject {@min.log} |
| 58 | + it {should be_instance_of Array} |
| 59 | + it {should respond_to :to_table} |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments