File: minimization_unidimensional_spec.rb

package info (click to toggle)
ruby-minimization 0.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 1,243; makefile: 3
file content (62 lines) | stat: -rw-r--r-- 1,801 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe Minimization::Unidimensional, "subclass" do 
  before(:all) do
    @p1=rand(100)
    @p2=rand(100)
    @func=lambda {|x| (x-@p1)**2+@p2}
    @funcd=lambda {|x| 2*(x-@p1)}
    @funcdd=lambda  {|x| 2}
  end
  
  describe Minimization::NewtonRaphson  do
    before do
      @min = Minimization::NewtonRaphson.new(-1000,1000, @func,@funcd, @funcdd)
      @min.iterate
    end
    it "#x_minimum be close to expected" do 
      @min.x_minimum.should be_within(@min.epsilon).of(@p1)
    end
    it "#f_minimum ( f(x)) be close to expected" do 
      @min.f_minimum.should be_within(@min.epsilon).of(@p2)
    end
    context "#log" do
      subject {@min.log}
      it {should be_instance_of Array}
      it {should respond_to :to_table}
    end
  end
  
  
  describe Minimization::GoldenSection  do
    before do
      @min = Minimization::GoldenSection.minimize(-1000,1000, &@func)
    end
    it "#x_minimum be close to expected" do 
      @min.x_minimum.should be_within(@min.epsilon).of(@p1)
    end
    it "#f_minimum ( f(x)) be close to expected" do 
      @min.f_minimum.should be_within(@min.epsilon).of(@p2)
    end
    context "#log" do
      subject {@min.log}
      it {should be_instance_of Array}
      it {should respond_to :to_table}
    end
  end
  describe Minimization::Brent  do
    before do
      @min = Minimization::Brent.minimize(-1000,1000, &@func)
    end
    it "should x be correct" do 
      @min.x_minimum.should be_within(@min.epsilon).of(@p1)
    end
    it "should f(x) be correct" do 
      @min.f_minimum.should be_within(@min.epsilon).of(@p2)
    end
    context "#log" do
      subject {@min.log}
      it {should be_instance_of Array}
      it {should respond_to :to_table}
    end
  end
end