Permalink
Browse files

Added spec, .gitignore and History changes

  • Loading branch information...
1 parent 4698073 commit b7488b29a5e49eb82f1d2db8ab72b5b0675a10f9 @clbustos committed Apr 15, 2010
Showing with 81 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +62 −0 spec/minimization_unidimensional_spec.rb
  3. +2 −0 spec/spec.opts
  4. +15 −0 spec/spec_helper.rb
View
2 .gitignore
@@ -0,0 +1,2 @@
+doc
+pkg
View
62 spec/minimization_unidimensional_spec.rb
@@ -0,0 +1,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_close(@p1,@min.epsilon)
+ end
+ it "#f_minimum ( f(x)) be close to expected" do
+ @min.f_minimum.should be_close(@p2,@min.epsilon)
+ 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_close(@p1,@min.epsilon)
+ end
+ it "#f_minimum ( f(x)) be close to expected" do
+ @min.f_minimum.should be_close(@p2,@min.epsilon)
+ 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_close(@p1,@min.epsilon)
+ end
+ it "should f(x) be correct" do
+ @min.f_minimum.should be_close(@p2,@min.epsilon)
+ end
+ context "#log" do
+ subject {@min.log}
+ it {should be_instance_of Array}
+ it {should respond_to :to_table}
+ end
+ end
+end
View
2 spec/spec.opts
@@ -0,0 +1,2 @@
+--color
+-f s
View
15 spec/spec_helper.rb
@@ -0,0 +1,15 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'minimization.rb'
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end
+
+class String
+ def deindent
+ gsub /^[ \t]*/, ''
+ end
+end

0 comments on commit b7488b2

Please sign in to comment.