solver 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/solver/base.rb CHANGED
@@ -13,18 +13,23 @@ class Base
13
13
  @x = @default_guesses.first
14
14
  @f = eqn || blk
15
15
  @tol = tol # user-requested tolerance
16
+ @max_it = 8192
17
+ reset
18
+ end
19
+
20
+ def reset
16
21
  @l_x = nil
17
22
  @fx = nil
18
23
  @l_fx = nil
19
24
  @ok = true
20
25
  @conv = false
21
- @max_it = 8192
22
26
  end
23
27
 
24
28
  # value of parameters[var] is used as a guess in precedence to the pre-guesses if not nil
25
29
  # use Array for two guesses
26
30
  def root(*guesses)
27
31
  @guess = (guesses + @default_guesses).map{|g| num(g)}
32
+ reset
28
33
  @l_x = @x = @guess.first
29
34
  @l_fx = @fx = eval_f(@x)
30
35
  @ok = true
@@ -44,7 +49,6 @@ class Base
44
49
  @l_fx = @fx
45
50
  @x = next_x
46
51
  @fx = eval_f(@x)
47
- # puts "X=#{@x.inspect}[#{@fx.inspect}]"
48
52
  @conv = test_conv() if @ok
49
53
  break if @conv
50
54
  @iteration += 1
@@ -23,9 +23,14 @@ module Flt::Solver
23
23
 
24
24
  def initialize(context, default_guesses, tol, eqn=nil, &blk)
25
25
  super context, default_guesses, tol, eqn, &blk
26
+ @half = num(Rational(1,2))
27
+ reset
28
+ end
29
+
30
+ def reset
31
+ super
26
32
  @a = @b = @fa = @fb = nil
27
33
  @bracketing = false
28
- @half = num(Rational(1,2))
29
34
  end
30
35
 
31
36
  def step
data/lib/solver/secant.rb CHANGED
@@ -24,9 +24,14 @@ module Flt::Solver
24
24
 
25
25
  def initialize(context, default_guesses, tol, eqn=nil, &blk)
26
26
  super context, default_guesses, tol, eqn, &blk
27
+ @half = num(Rational(1,2))
28
+ reset
29
+ end
30
+
31
+ def reset
32
+ super
27
33
  @a = @b = @fa = @fb = nil
28
34
  @bracketing = false
29
- @half = num(Rational(1,2))
30
35
  end
31
36
 
32
37
  def step
data/solver.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{solver}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
@@ -45,7 +45,8 @@ Gem::Specification.new do |s|
45
45
  s.rubygems_version = %q{1.3.7}
46
46
  s.summary = %q{Numeric solver to exercise the Flt library}
47
47
  s.test_files = [
48
- "test/helper.rb",
48
+ "test/chk.rb",
49
+ "test/helper.rb",
49
50
  "test/test_function.rb",
50
51
  "test/test_psolver.rb",
51
52
  "test/test_rfsecant.rb",
data/test/chk.rb ADDED
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper'))
2
+ require 'flt/float'
3
+ require 'flt/tolerance'
4
+
5
+ include Flt
6
+
7
+ @context = Float.context
8
+ @tolerance = Flt.Tolerance(3,:decimals)
9
+ @delta = 5E-4
10
+ @tvm = Flt::Solver::TVM.new(@tolerance, @context)
11
+
12
+ puts "-----------------"
13
+
14
+ sol = @tvm.solve(:t=>63, :m0=>0, :pmt=>-1000000, :i=>@context.Num('0.00000161')*12, :p=>12)
15
+ puts sol.inspect
16
+
17
+ puts "-----------------"
18
+ n = 31536000
19
+ sol = @tvm.solve(:t=>n, :m0=>0, :pmt=>-@context.Num('0.01'), :i=>@context.Num(10)/n, :p=>1)
20
+ puts sol.inspect
21
+
22
+ puts "-----------------"
23
+ @tvm = Flt::Solver::TVM.new(@tolerance, @context)
24
+ n = 31536000
25
+ sol = @tvm.solve(:t=>n, :m0=>0, :pmt=>-@context.Num('0.01'), :i=>@context.Num(10)/n, :p=>1)
26
+ puts sol.inspect
data/test/test_tvm.rb CHANGED
@@ -12,8 +12,8 @@ class TestTVM < Test::Unit::TestCase
12
12
 
13
13
  setup do
14
14
  @context = Float.context
15
- @tolerance = Flt.Tolerance(3,:decimals)
16
- @delta = 5E-4
15
+ @tolerance = Flt.Tolerance(2,:decimals)
16
+ @delta = 5E-3
17
17
  @tvm = Flt::Solver::TVM.new(@tolerance, @context)
18
18
  end
19
19
 
@@ -29,6 +29,17 @@ class TestTVM < Test::Unit::TestCase
29
29
  assert_in_delta 240, sol[:t], @delta
30
30
  end
31
31
 
32
+ should "solve correclty for :m" do
33
+ sol = @tvm.solve(:t=>63, :m0=>0, :pmt=>-1000000, :i=>@context.Num('0.00000161')*12, :p=>12)
34
+ assert_equal [:m], sol.keys
35
+ assert_in_delta @context.Num('63000031.4433'), sol[:m], @delta
36
+
37
+ n = 31536000
38
+ sol = @tvm.solve(:t=>n, :m0=>0, :pmt=>-@context.Num('0.01'), :i=>@context.Num(10)/n, :p=>1)
39
+ assert_equal [:m], sol.keys
40
+ assert_in_delta @context.Num('331667.006691'), sol[:m], @delta
41
+ end
42
+
32
43
  end
33
44
 
34
45
  end
@@ -36,9 +47,9 @@ class TestTVM < Test::Unit::TestCase
36
47
  context "using DecNum arithmetic" do
37
48
 
38
49
  setup do
39
- @context = Float.context
50
+ @context = Flt::DecNum.context
40
51
  @tolerance = Flt.Tolerance(3,:decimals)
41
- @delta = 5E-4
52
+ @delta = @context.Num('5E-4')
42
53
  @tvm = Flt::Solver::TVM.new(@tolerance, @context)
43
54
  end
44
55
 
@@ -49,10 +60,56 @@ class TestTVM < Test::Unit::TestCase
49
60
  end
50
61
 
51
62
  should "solve correclty for :t" do
52
- sol = @tvm.solve(:pmt=>-55.45975978539105, :m0=>10000, :m=>0, :i=>3, :p=>12)
63
+ sol = @tvm.solve(:pmt=>@context.Num('-55.45975978539105'), :m0=>10000, :m=>0, :i=>3, :p=>12)
53
64
  assert_equal [:t], sol.keys
54
65
  assert_in_delta 240, sol[:t], @delta
55
66
  end
56
-
67
+
68
+ should "solve correclty for :m" do
69
+ sol = @tvm.solve(:t=>63, :m0=>0, :pmt=>-1000000, :i=>@context.Num('0.00000161')*12, :p=>12)
70
+ assert_equal [:m], sol.keys
71
+ assert_in_delta @context.Num('63000031.4433'), sol[:m], @delta
72
+
73
+ n = 31536000
74
+ sol = @tvm.solve(:t=>n, :m0=>0, :pmt=>-@context.Num('0.01'), :i=>@context.Num(10)/n, :p=>1)
75
+ assert_equal [:m], sol.keys
76
+ assert_in_delta @context.Num('331667.006691'), sol[:m], @delta
77
+ end
78
+
79
+ context "and high precision" do
80
+ setup do
81
+ @context.precision = 28
82
+ @tolerance = Tolerance(12, :sig_decimals)
83
+ @tvm = Flt::Solver::TVM.new(@tolerance, @context)
84
+ end
85
+
86
+ should "solve correclty for :pmt" do
87
+ sol = @tvm.solve(:t=>240, :m0=>10000, :m=>0, :i=>3, :p=>12)
88
+ assert_equal [:pmt], sol.keys
89
+ expected = @context.Num('-55.45975978539105')
90
+ assert_in_delta expected, sol[:pmt], @tolerance.value(expected)
91
+ end
92
+
93
+ should "solve correclty for :t" do
94
+ sol = @tvm.solve(:pmt=>@context.Num('-55.45975978539105'), :m0=>10000, :m=>0, :i=>3, :p=>12)
95
+ assert_equal [:t], sol.keys
96
+ expected = @context.Num('240')
97
+ assert_in_delta expected, sol[:t], @tolerance.value(expected)
98
+ end
99
+
100
+ should "solve correclty for :m" do
101
+ sol = @tvm.solve(:t=>63, :m0=>0, :pmt=>-1000000, :i=>@context.Num('0.00000161')*12, :p=>12)
102
+ assert_equal [:m], sol.keys
103
+ expected = @context.Num('63000031.4433')
104
+ assert_in_delta expected, sol[:m], @tolerance.value(expected)
105
+
106
+ n = 31536000
107
+ sol = @tvm.solve(:t=>n, :m0=>0, :pmt=>-@context.Num('0.01'), :i=>@context.Num(10)/n, :p=>1)
108
+ assert_equal [:m], sol.keys
109
+ expected = @context.Num('331667.006691')
110
+ assert_in_delta expected, sol[:m], @tolerance.value(expected)
111
+ end
112
+
113
+ end
57
114
  end
58
115
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Javier Goizueta
@@ -75,6 +75,7 @@ files:
75
75
  - test/test_rfsecant.rb
76
76
  - test/test_secant.rb
77
77
  - test/test_tvm.rb
78
+ - test/chk.rb
78
79
  has_rdoc: true
79
80
  homepage: http://github.com/jgoizueta/solver
80
81
  licenses: []
@@ -110,6 +111,7 @@ signing_key:
110
111
  specification_version: 3
111
112
  summary: Numeric solver to exercise the Flt library
112
113
  test_files:
114
+ - test/chk.rb
113
115
  - test/helper.rb
114
116
  - test/test_function.rb
115
117
  - test/test_psolver.rb