rical 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rical.rb +12 -6
- data/lib/rical/version.rb +1 -1
- data/spec/rical_spec.rb +29 -20
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e3b9ddaa4411eba88fbd4ed7da72847ab4ec2941
|
|
4
|
+
data.tar.gz: 366b872dd1f11c1fa557449c5d02785a2ca8f66f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 059ec0e08bd1338050630266c6e8af47fea73ae659f4db102ccb922aa2c23de75420059f85f14985085fc9ff62cfb03fb7f254fea8c0fc55ff0d141419cb0bee
|
|
7
|
+
data.tar.gz: 0c0ebf9d1b94322e108a85a8b042dc084ebf71bc13567c073be589c387500bfee573930c11186ea59ddbf2f5e8867c24341c28c2b885f3f4fbd73920d58fefca
|
data/lib/rical.rb
CHANGED
|
@@ -21,9 +21,9 @@ module Rical
|
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
def inverse_for f:, y: 0.0, **args
|
|
25
|
-
f_1=
|
|
26
|
-
root_for f: f_1, **args
|
|
24
|
+
def inverse_for f:, fargs: nil, y: 0.0, **args
|
|
25
|
+
f_1 = compute_f_1 f, fargs, y
|
|
26
|
+
root_for f: f_1, fargs: fargs, **args
|
|
27
27
|
end
|
|
28
28
|
alias_method :inv_for, :inverse_for
|
|
29
29
|
|
|
@@ -53,8 +53,14 @@ private
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def compute f, x, fargs
|
|
56
|
-
return f.call
|
|
57
|
-
return f.call
|
|
58
|
-
return f.call
|
|
56
|
+
return f.call(x ) unless fargs
|
|
57
|
+
return f.call(x, **fargs) if fargs.is_a? Hash
|
|
58
|
+
return f.call(x, *fargs)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def compute_f_1 f, fargs, y
|
|
62
|
+
return -> (x ) { f.call(x) - Float(y) } unless fargs
|
|
63
|
+
return -> (x, **arg) { f.call(x, **fargs) - Float(y) } if fargs.is_a? Hash
|
|
64
|
+
return -> (x, *arg) { f.call(x, *fargs) - Float(y) }
|
|
59
65
|
end
|
|
60
66
|
end
|
data/lib/rical/version.rb
CHANGED
data/spec/rical_spec.rb
CHANGED
|
@@ -3,11 +3,15 @@ require 'spec_helper'
|
|
|
3
3
|
describe Rical do
|
|
4
4
|
f1 = -> (x) { x**2 - 2 }
|
|
5
5
|
df1 = -> (x) { 2*x }
|
|
6
|
+
f2 = -> (x, a, b) { a*x**2 + b } # f2 = f1 for a=1, b=-2
|
|
7
|
+
df2 = -> (x, a) { a*x } # df2 = df1 for a=2
|
|
8
|
+
f3 = -> (x, a:, b:) { a*x**2 + b } # f3 = f1 for { a: 1, b: -2 }
|
|
9
|
+
df3 = -> (x, a:) { a*x } # df3 = df1 for { a: 2 }
|
|
6
10
|
|
|
7
11
|
context 'Newton-Raphson method' do
|
|
8
12
|
context 'root computation' do
|
|
9
13
|
it 'yields +√2 for f(x) = x²-2' do
|
|
10
|
-
expect(Rical.root_for f: f1, df: df1, x0: 10, method: :
|
|
14
|
+
expect(Rical.root_for f: f1, df: df1, x0: 10, method: :newton_raphson).to be_within(1e-6).of(2**0.5)
|
|
11
15
|
end
|
|
12
16
|
|
|
13
17
|
it 'yields -√2 for f(x) = x²-2' do
|
|
@@ -15,25 +19,17 @@ describe Rical do
|
|
|
15
19
|
end
|
|
16
20
|
|
|
17
21
|
it 'rescues when df(x0) = 0' do
|
|
18
|
-
expect(Rical.root_for f: f1, df: df1, x0: 0.0, method: :
|
|
22
|
+
expect(Rical.root_for f: f1, df: df1, x0: 0.0, method: :n).to be_within(1e-6).of(2**0.5)
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
it 'passes extra arguments (array) to f' do
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
expect(Rical.root_for f: f2, fargs: [1, -2],
|
|
25
|
-
df: df2, dfargs: 2,
|
|
26
|
-
x0: 10,
|
|
27
|
-
method: :newton).to be_within(1e-6).of(2**0.5)
|
|
26
|
+
expect(Rical.root_for f: f2, fargs: [1,-2], df: df2, dfargs: 2, x0: 10, method: :n).
|
|
27
|
+
to be_within(1e-6).of(2**0.5)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it 'passes extra arguments (hash) to f' do
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
expect(Rical.root_for f: f3, fargs: { a: 1, b: -2 },
|
|
34
|
-
df: df3, dfargs: { a: 2 },
|
|
35
|
-
x0: 10,
|
|
36
|
-
method: :newton).to be_within(1e-6).of(2**0.5)
|
|
31
|
+
expect(Rical.root_for f: f3, fargs: {a:1, b:-2}, df: df3, dfargs: {a:2}, x0: 10, method: :n).
|
|
32
|
+
to be_within(1e-6).of(2**0.5)
|
|
37
33
|
end
|
|
38
34
|
end
|
|
39
35
|
|
|
@@ -41,6 +37,16 @@ describe Rical do
|
|
|
41
37
|
it 'yields +2.0 for y=2, f(x)=y=x²-2' do
|
|
42
38
|
expect(Rical.inverse_for f: f1, df: df1, y: 2, x0: 0, method: :newton).to be_within(1e-6).of(2.0)
|
|
43
39
|
end
|
|
40
|
+
|
|
41
|
+
it 'passes extra arguments (array) to f' do
|
|
42
|
+
expect(Rical.inverse_for f: f2, fargs: [1,-2], df: df2, dfargs: 2, y: 2, x0: 10, method: :n).
|
|
43
|
+
to be_within(1e-6).of(2.0)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'passes extra arguments (hash) to f' do
|
|
47
|
+
expect(Rical.inverse_for f: f3, fargs: {a:1,b:-2}, df: df3, dfargs: {a:2}, y: 2, x0: 10, method: :n).
|
|
48
|
+
to be_within(1e-6).of(2.0)
|
|
49
|
+
end
|
|
44
50
|
end
|
|
45
51
|
|
|
46
52
|
end
|
|
@@ -52,23 +58,26 @@ describe Rical do
|
|
|
52
58
|
end
|
|
53
59
|
|
|
54
60
|
it 'yields -√2 for f(x) = x²-2' do
|
|
55
|
-
expect(Rical.root_for f: f1, x0: -10, x1: -9, method: :
|
|
61
|
+
expect(Rical.root_for f: f1, x0: -10, x1: -9, method: :sec).to be_within(1e-6).of(-2**0.5)
|
|
56
62
|
end
|
|
57
63
|
|
|
58
|
-
it 'rescues when f(x1) - f(x0) = 0' do
|
|
59
|
-
|
|
60
|
-
expect(Rical.root_for(f: f1, x0: 9, x1: -9, method: :secant).abs).to be_within(1e-6).of(2**0.5)
|
|
64
|
+
it 'rescues when f(x1) - f(x0) = 0 and converges to one of the roots' do
|
|
65
|
+
expect(Rical.root_for(f: f1, x0: 9, x1: -9, method: :s).abs).to be_within(1e-6).of(2**0.5)
|
|
61
66
|
end
|
|
62
67
|
|
|
63
68
|
it 'passes extra arguments (array) to f' do
|
|
64
69
|
f2 = -> (x, a, b) { a*x**2 + b } # f2 = f1 for a=1, b=-2
|
|
65
|
-
expect(Rical.root_for f: f2, fargs: [1, -2], x0: 10, x1: 9, method: :
|
|
70
|
+
expect(Rical.root_for f: f2, fargs: [1, -2], x0: 10, x1: 9, method: :s).to be_within(1e-6).of(2**0.5)
|
|
66
71
|
end
|
|
67
72
|
end
|
|
68
73
|
|
|
69
74
|
context 'inverse computation' do
|
|
70
75
|
it 'yields +2.0 for y=2, f(x)=y=x²-2' do
|
|
71
|
-
expect(Rical.inverse_for f: f1, y: 2, x0: 0, x1: 1, method: :
|
|
76
|
+
expect(Rical.inverse_for f: f1, y: 2, x0: 0, x1: 1, method: :sec).to be_within(1e-6).of(2.0)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'passes extra arguments (array) to f' do
|
|
80
|
+
expect(Rical.inverse_for f: f2, fargs: [1, -2], y: 2, x0: 10, x1: 9, method: :s).to be_within(1e-6).of(2.0)
|
|
72
81
|
end
|
|
73
82
|
end
|
|
74
83
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rical
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Luis Bacelar
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-01-
|
|
11
|
+
date: 2015-01-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|