Skip to content

Commit b7488b2

Browse files
committed
Added spec, .gitignore and History changes
1 parent 4698073 commit b7488b2

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
doc
2+
pkg
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

spec/spec.opts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
-f s

spec/spec_helper.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$LOAD_PATH.unshift(File.dirname(__FILE__))
2+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3+
require 'minimization.rb'
4+
require 'spec'
5+
require 'spec/autorun'
6+
7+
Spec::Runner.configure do |config|
8+
9+
end
10+
11+
class String
12+
def deindent
13+
gsub /^[ \t]*/, ''
14+
end
15+
end

0 commit comments

Comments
 (0)