rubysl-bigdecimal 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/ext/rubysl/bigdecimal/bigdecimal.c +4760 -0
- data/ext/rubysl/bigdecimal/bigdecimal.h +220 -0
- data/ext/rubysl/bigdecimal/extconf.rb +6 -0
- data/lib/bigdecimal.rb +1 -0
- data/lib/bigdecimal/README +60 -0
- data/lib/bigdecimal/bigdecimal_en.html +796 -0
- data/lib/bigdecimal/bigdecimal_ja.html +799 -0
- data/lib/bigdecimal/jacobian.rb +85 -0
- data/lib/bigdecimal/ludcmp.rb +84 -0
- data/lib/bigdecimal/math.rb +235 -0
- data/lib/bigdecimal/newton.rb +77 -0
- data/lib/bigdecimal/sample/linear.rb +71 -0
- data/lib/bigdecimal/sample/nlsolve.rb +38 -0
- data/lib/bigdecimal/sample/pi.rb +20 -0
- data/lib/bigdecimal/util.rb +65 -0
- data/lib/rubysl/bigdecimal.rb +2 -0
- data/lib/rubysl/bigdecimal/version.rb +5 -0
- data/rubysl-bigdecimal.gemspec +24 -0
- data/spec/abs_spec.rb +49 -0
- data/spec/add_spec.rb +178 -0
- data/spec/case_compare_spec.rb +6 -0
- data/spec/ceil_spec.rb +122 -0
- data/spec/coerce_spec.rb +25 -0
- data/spec/comparison_spec.rb +80 -0
- data/spec/div_spec.rb +143 -0
- data/spec/divide_spec.rb +6 -0
- data/spec/divmod_spec.rb +233 -0
- data/spec/double_fig_spec.rb +8 -0
- data/spec/eql_spec.rb +5 -0
- data/spec/equal_value_spec.rb +6 -0
- data/spec/exponent_spec.rb +37 -0
- data/spec/finite_spec.rb +34 -0
- data/spec/fix_spec.rb +56 -0
- data/spec/fixtures/classes.rb +17 -0
- data/spec/floor_spec.rb +109 -0
- data/spec/frac_spec.rb +47 -0
- data/spec/gt_spec.rb +86 -0
- data/spec/gte_spec.rb +90 -0
- data/spec/induced_from_spec.rb +36 -0
- data/spec/infinite_spec.rb +31 -0
- data/spec/inspect_spec.rb +40 -0
- data/spec/limit_spec.rb +29 -0
- data/spec/lt_spec.rb +84 -0
- data/spec/lte_spec.rb +90 -0
- data/spec/minus_spec.rb +57 -0
- data/spec/mode_spec.rb +64 -0
- data/spec/modulo_spec.rb +11 -0
- data/spec/mult_spec.rb +23 -0
- data/spec/multiply_spec.rb +25 -0
- data/spec/nan_spec.rb +22 -0
- data/spec/new_spec.rb +120 -0
- data/spec/nonzero_spec.rb +28 -0
- data/spec/plus_spec.rb +49 -0
- data/spec/power_spec.rb +5 -0
- data/spec/precs_spec.rb +48 -0
- data/spec/quo_spec.rb +12 -0
- data/spec/remainder_spec.rb +83 -0
- data/spec/round_spec.rb +193 -0
- data/spec/shared/eql.rb +65 -0
- data/spec/shared/modulo.rb +146 -0
- data/spec/shared/mult.rb +97 -0
- data/spec/shared/power.rb +83 -0
- data/spec/shared/quo.rb +59 -0
- data/spec/shared/to_int.rb +27 -0
- data/spec/sign_spec.rb +46 -0
- data/spec/split_spec.rb +87 -0
- data/spec/sqrt_spec.rb +111 -0
- data/spec/sub_spec.rb +52 -0
- data/spec/to_f_spec.rb +54 -0
- data/spec/to_i_spec.rb +6 -0
- data/spec/to_int_spec.rb +7 -0
- data/spec/to_s_spec.rb +71 -0
- data/spec/truncate_spec.rb +100 -0
- data/spec/uminus_spec.rb +57 -0
- data/spec/uplus_spec.rb +19 -0
- data/spec/ver_spec.rb +10 -0
- data/spec/zero_spec.rb +27 -0
- metadata +243 -0
@@ -0,0 +1,85 @@
|
|
1
|
+
#
|
2
|
+
# require 'bigdecimal/jacobian'
|
3
|
+
#
|
4
|
+
# Provides methods to compute the Jacobian matrix of a set of equations at a
|
5
|
+
# point x. In the methods below:
|
6
|
+
#
|
7
|
+
# f is an Object which is used to compute the Jacobian matrix of the equations.
|
8
|
+
# It must provide the following methods:
|
9
|
+
#
|
10
|
+
# f.values(x):: returns the values of all functions at x
|
11
|
+
#
|
12
|
+
# f.zero:: returns 0.0
|
13
|
+
# f.one:: returns 1.0
|
14
|
+
# f.two:: returns 1.0
|
15
|
+
# f.ten:: returns 10.0
|
16
|
+
#
|
17
|
+
# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
|
18
|
+
#
|
19
|
+
# x is the point at which to compute the Jacobian.
|
20
|
+
#
|
21
|
+
# fx is f.values(x).
|
22
|
+
#
|
23
|
+
module Jacobian
|
24
|
+
#--
|
25
|
+
def isEqual(a,b,zero=0.0,e=1.0e-8)
|
26
|
+
aa = a.abs
|
27
|
+
bb = b.abs
|
28
|
+
if aa == zero && bb == zero then
|
29
|
+
true
|
30
|
+
else
|
31
|
+
if ((a-b)/(aa+bb)).abs < e then
|
32
|
+
true
|
33
|
+
else
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
#++
|
39
|
+
|
40
|
+
# Computes the derivative of f[i] at x[i].
|
41
|
+
# fx is the value of f at x.
|
42
|
+
def dfdxi(f,fx,x,i)
|
43
|
+
nRetry = 0
|
44
|
+
n = x.size
|
45
|
+
xSave = x[i]
|
46
|
+
ok = 0
|
47
|
+
ratio = f.ten*f.ten*f.ten
|
48
|
+
dx = x[i].abs/ratio
|
49
|
+
dx = fx[i].abs/ratio if isEqual(dx,f.zero,f.zero,f.eps)
|
50
|
+
dx = f.one/f.ten if isEqual(dx,f.zero,f.zero,f.eps)
|
51
|
+
until ok>0 do
|
52
|
+
s = f.zero
|
53
|
+
deriv = []
|
54
|
+
if(nRetry>100) then
|
55
|
+
raize "Singular Jacobian matrix. No change at x[" + i.to_s + "]"
|
56
|
+
end
|
57
|
+
dx = dx*f.two
|
58
|
+
x[i] += dx
|
59
|
+
fxNew = f.values(x)
|
60
|
+
for j in 0...n do
|
61
|
+
if !isEqual(fxNew[j],fx[j],f.zero,f.eps) then
|
62
|
+
ok += 1
|
63
|
+
deriv <<= (fxNew[j]-fx[j])/dx
|
64
|
+
else
|
65
|
+
deriv <<= f.zero
|
66
|
+
end
|
67
|
+
end
|
68
|
+
x[i] = xSave
|
69
|
+
end
|
70
|
+
deriv
|
71
|
+
end
|
72
|
+
|
73
|
+
# Computes the Jacobian of f at x. fx is the value of f at x.
|
74
|
+
def jacobian(f,fx,x)
|
75
|
+
n = x.size
|
76
|
+
dfdx = Array::new(n*n)
|
77
|
+
for i in 0...n do
|
78
|
+
df = dfdxi(f,fx,x,i)
|
79
|
+
for j in 0...n do
|
80
|
+
dfdx[j*n+i] = df[j]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
dfdx
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#
|
2
|
+
# Solves a*x = b for x, using LU decomposition.
|
3
|
+
#
|
4
|
+
module LUSolve
|
5
|
+
# Performs LU decomposition of the n by n matrix a.
|
6
|
+
def ludecomp(a,n,zero=0,one=1)
|
7
|
+
prec = BigDecimal.limit(nil)
|
8
|
+
ps = []
|
9
|
+
scales = []
|
10
|
+
for i in 0...n do # pick up largest(abs. val.) element in each row.
|
11
|
+
ps <<= i
|
12
|
+
nrmrow = zero
|
13
|
+
ixn = i*n
|
14
|
+
for j in 0...n do
|
15
|
+
biggst = a[ixn+j].abs
|
16
|
+
nrmrow = biggst if biggst>nrmrow
|
17
|
+
end
|
18
|
+
if nrmrow>zero then
|
19
|
+
scales <<= one.div(nrmrow,prec)
|
20
|
+
else
|
21
|
+
raise "Singular matrix"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
n1 = n - 1
|
25
|
+
for k in 0...n1 do # Gaussian elimination with partial pivoting.
|
26
|
+
biggst = zero;
|
27
|
+
for i in k...n do
|
28
|
+
size = a[ps[i]*n+k].abs*scales[ps[i]]
|
29
|
+
if size>biggst then
|
30
|
+
biggst = size
|
31
|
+
pividx = i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
raise "Singular matrix" if biggst<=zero
|
35
|
+
if pividx!=k then
|
36
|
+
j = ps[k]
|
37
|
+
ps[k] = ps[pividx]
|
38
|
+
ps[pividx] = j
|
39
|
+
end
|
40
|
+
pivot = a[ps[k]*n+k]
|
41
|
+
for i in (k+1)...n do
|
42
|
+
psin = ps[i]*n
|
43
|
+
a[psin+k] = mult = a[psin+k].div(pivot,prec)
|
44
|
+
if mult!=zero then
|
45
|
+
pskn = ps[k]*n
|
46
|
+
for j in (k+1)...n do
|
47
|
+
a[psin+j] -= mult.mult(a[pskn+j],prec)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
raise "Singular matrix" if a[ps[n1]*n+n1] == zero
|
53
|
+
ps
|
54
|
+
end
|
55
|
+
|
56
|
+
# Solves a*x = b for x, using LU decomposition.
|
57
|
+
#
|
58
|
+
# a is a matrix, b is a constant vector, x is the solution vector.
|
59
|
+
#
|
60
|
+
# ps is the pivot, a vector which indicates the permutation of rows performed
|
61
|
+
# during LU decomposition.
|
62
|
+
def lusolve(a,b,ps,zero=0.0)
|
63
|
+
prec = BigDecimal.limit(nil)
|
64
|
+
n = ps.size
|
65
|
+
x = []
|
66
|
+
for i in 0...n do
|
67
|
+
dot = zero
|
68
|
+
psin = ps[i]*n
|
69
|
+
for j in 0...i do
|
70
|
+
dot = a[psin+j].mult(x[j],prec) + dot
|
71
|
+
end
|
72
|
+
x <<= b[ps[i]] - dot
|
73
|
+
end
|
74
|
+
(n-1).downto(0) do |i|
|
75
|
+
dot = zero
|
76
|
+
psin = ps[i]*n
|
77
|
+
for j in (i+1)...n do
|
78
|
+
dot = a[psin+j].mult(x[j],prec) + dot
|
79
|
+
end
|
80
|
+
x[i] = (x[i]-dot).div(a[psin+i],prec)
|
81
|
+
end
|
82
|
+
x
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,235 @@
|
|
1
|
+
#
|
2
|
+
#--
|
3
|
+
# Contents:
|
4
|
+
# sqrt(x, prec)
|
5
|
+
# sin (x, prec)
|
6
|
+
# cos (x, prec)
|
7
|
+
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
|
8
|
+
# exp (x, prec)
|
9
|
+
# log (x, prec)
|
10
|
+
# PI (prec)
|
11
|
+
# E (prec) == exp(1.0,prec)
|
12
|
+
#
|
13
|
+
# where:
|
14
|
+
# x ... BigDecimal number to be computed.
|
15
|
+
# |x| must be small enough to get convergence.
|
16
|
+
# prec ... Number of digits to be obtained.
|
17
|
+
#++
|
18
|
+
#
|
19
|
+
# Provides mathematical functions.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# require "bigdecimal"
|
24
|
+
# require "bigdecimal/math"
|
25
|
+
#
|
26
|
+
# include BigMath
|
27
|
+
#
|
28
|
+
# a = BigDecimal((PI(100)/2).to_s)
|
29
|
+
# puts sin(a,100) # -> 0.10000000000000000000......E1
|
30
|
+
#
|
31
|
+
module BigMath
|
32
|
+
|
33
|
+
# Computes the square root of x to the specified number of digits of
|
34
|
+
# precision.
|
35
|
+
#
|
36
|
+
# BigDecimal.new('2').sqrt(16).to_s
|
37
|
+
# -> "0.14142135623730950488016887242096975E1"
|
38
|
+
#
|
39
|
+
def sqrt(x,prec)
|
40
|
+
x.sqrt(prec)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Computes the sine of x to the specified number of digits of precision.
|
44
|
+
#
|
45
|
+
# If x is infinite or NaN, returns NaN.
|
46
|
+
def sin(x, prec)
|
47
|
+
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
48
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
49
|
+
n = prec + BigDecimal.double_fig
|
50
|
+
one = BigDecimal("1")
|
51
|
+
two = BigDecimal("2")
|
52
|
+
x1 = x
|
53
|
+
x2 = x.mult(x,n)
|
54
|
+
sign = 1
|
55
|
+
y = x
|
56
|
+
d = y
|
57
|
+
i = one
|
58
|
+
z = one
|
59
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
60
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
61
|
+
sign = -sign
|
62
|
+
x1 = x2.mult(x1,n)
|
63
|
+
i += two
|
64
|
+
z *= (i-one) * i
|
65
|
+
d = sign * x1.div(z,m)
|
66
|
+
y += d
|
67
|
+
end
|
68
|
+
y
|
69
|
+
end
|
70
|
+
|
71
|
+
# Computes the cosine of x to the specified number of digits of precision.
|
72
|
+
#
|
73
|
+
# If x is infinite or NaN, returns NaN.
|
74
|
+
def cos(x, prec)
|
75
|
+
raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
|
76
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
77
|
+
n = prec + BigDecimal.double_fig
|
78
|
+
one = BigDecimal("1")
|
79
|
+
two = BigDecimal("2")
|
80
|
+
x1 = one
|
81
|
+
x2 = x.mult(x,n)
|
82
|
+
sign = 1
|
83
|
+
y = one
|
84
|
+
d = y
|
85
|
+
i = BigDecimal("0")
|
86
|
+
z = one
|
87
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
88
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
89
|
+
sign = -sign
|
90
|
+
x1 = x2.mult(x1,n)
|
91
|
+
i += two
|
92
|
+
z *= (i-one) * i
|
93
|
+
d = sign * x1.div(z,m)
|
94
|
+
y += d
|
95
|
+
end
|
96
|
+
y
|
97
|
+
end
|
98
|
+
|
99
|
+
# Computes the arctangent of x to the specified number of digits of precision.
|
100
|
+
#
|
101
|
+
# If x is infinite or NaN, returns NaN.
|
102
|
+
# Raises an argument error if x > 1.
|
103
|
+
def atan(x, prec)
|
104
|
+
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
|
105
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
106
|
+
raise ArgumentError, "x.abs must be less than 1.0" if x.abs>=1
|
107
|
+
n = prec + BigDecimal.double_fig
|
108
|
+
y = x
|
109
|
+
d = y
|
110
|
+
t = x
|
111
|
+
r = BigDecimal("3")
|
112
|
+
x2 = x.mult(x,n)
|
113
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
114
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
115
|
+
t = -t.mult(x2,n)
|
116
|
+
d = t.div(r,m)
|
117
|
+
y += d
|
118
|
+
r += 2
|
119
|
+
end
|
120
|
+
y
|
121
|
+
end
|
122
|
+
|
123
|
+
# Computes the value of e (the base of natural logarithms) raised to the
|
124
|
+
# power of x, to the specified number of digits of precision.
|
125
|
+
#
|
126
|
+
# If x is infinite or NaN, returns NaN.
|
127
|
+
#
|
128
|
+
# BigMath::exp(BigDecimal.new('1'), 10).to_s
|
129
|
+
# -> "0.271828182845904523536028752390026306410273E1"
|
130
|
+
def exp(x, prec)
|
131
|
+
raise ArgumentError, "Zero or negative precision for exp" if prec <= 0
|
132
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
133
|
+
n = prec + BigDecimal.double_fig
|
134
|
+
one = BigDecimal("1")
|
135
|
+
x1 = one
|
136
|
+
y = one
|
137
|
+
d = y
|
138
|
+
z = one
|
139
|
+
i = 0
|
140
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
141
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
142
|
+
x1 = x1.mult(x,n)
|
143
|
+
i += 1
|
144
|
+
z *= i
|
145
|
+
d = x1.div(z,m)
|
146
|
+
y += d
|
147
|
+
end
|
148
|
+
y
|
149
|
+
end
|
150
|
+
|
151
|
+
# Computes the natural logarithm of x to the specified number of digits
|
152
|
+
# of precision.
|
153
|
+
#
|
154
|
+
# Returns x if x is infinite or NaN.
|
155
|
+
#
|
156
|
+
def log(x, prec)
|
157
|
+
raise ArgumentError, "Zero or negative argument for log" if x <= 0 || prec <= 0
|
158
|
+
return x if x.infinite? || x.nan?
|
159
|
+
one = BigDecimal("1")
|
160
|
+
two = BigDecimal("2")
|
161
|
+
n = prec + BigDecimal.double_fig
|
162
|
+
x = (x - one).div(x + one,n)
|
163
|
+
x2 = x.mult(x,n)
|
164
|
+
y = x
|
165
|
+
d = y
|
166
|
+
i = one
|
167
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
168
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
169
|
+
x = x2.mult(x,n)
|
170
|
+
i += two
|
171
|
+
d = x.div(i,m)
|
172
|
+
y += d
|
173
|
+
end
|
174
|
+
y*two
|
175
|
+
end
|
176
|
+
|
177
|
+
# Computes the value of pi to the specified number of digits of precision.
|
178
|
+
def PI(prec)
|
179
|
+
raise ArgumentError, "Zero or negative argument for PI" if prec <= 0
|
180
|
+
n = prec + BigDecimal.double_fig
|
181
|
+
zero = BigDecimal("0")
|
182
|
+
one = BigDecimal("1")
|
183
|
+
two = BigDecimal("2")
|
184
|
+
|
185
|
+
m25 = BigDecimal("-0.04")
|
186
|
+
m57121 = BigDecimal("-57121")
|
187
|
+
|
188
|
+
pi = zero
|
189
|
+
|
190
|
+
d = one
|
191
|
+
k = one
|
192
|
+
w = one
|
193
|
+
t = BigDecimal("-80")
|
194
|
+
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
|
195
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
196
|
+
t = t*m25
|
197
|
+
d = t.div(k,m)
|
198
|
+
k = k+two
|
199
|
+
pi = pi + d
|
200
|
+
end
|
201
|
+
|
202
|
+
d = one
|
203
|
+
k = one
|
204
|
+
w = one
|
205
|
+
t = BigDecimal("956")
|
206
|
+
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
|
207
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
208
|
+
t = t.div(m57121,n)
|
209
|
+
d = t.div(k,m)
|
210
|
+
pi = pi + d
|
211
|
+
k = k+two
|
212
|
+
end
|
213
|
+
pi
|
214
|
+
end
|
215
|
+
|
216
|
+
# Computes e (the base of natural logarithms) to the specified number of
|
217
|
+
# digits of precision.
|
218
|
+
def E(prec)
|
219
|
+
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
|
220
|
+
n = prec + BigDecimal.double_fig
|
221
|
+
one = BigDecimal("1")
|
222
|
+
y = one
|
223
|
+
d = y
|
224
|
+
z = one
|
225
|
+
i = 0
|
226
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
227
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
228
|
+
i += 1
|
229
|
+
z *= i
|
230
|
+
d = one.div(z,m)
|
231
|
+
y += d
|
232
|
+
end
|
233
|
+
y
|
234
|
+
end
|
235
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#
|
2
|
+
# newton.rb
|
3
|
+
#
|
4
|
+
# Solves the nonlinear algebraic equation system f = 0 by Newton's method.
|
5
|
+
# This program is not dependent on BigDecimal.
|
6
|
+
#
|
7
|
+
# To call:
|
8
|
+
# n = nlsolve(f,x)
|
9
|
+
# where n is the number of iterations required,
|
10
|
+
# x is the initial value vector
|
11
|
+
# f is an Object which is used to compute the values of the equations to be solved.
|
12
|
+
# It must provide the following methods:
|
13
|
+
#
|
14
|
+
# f.values(x):: returns the values of all functions at x
|
15
|
+
#
|
16
|
+
# f.zero:: returns 0.0
|
17
|
+
# f.one:: returns 1.0
|
18
|
+
# f.two:: returns 1.0
|
19
|
+
# f.ten:: returns 10.0
|
20
|
+
#
|
21
|
+
# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
|
22
|
+
#
|
23
|
+
# On exit, x is the solution vector.
|
24
|
+
#
|
25
|
+
require "bigdecimal/ludcmp"
|
26
|
+
require "bigdecimal/jacobian"
|
27
|
+
|
28
|
+
module Newton
|
29
|
+
include LUSolve
|
30
|
+
include Jacobian
|
31
|
+
|
32
|
+
def norm(fv,zero=0.0)
|
33
|
+
s = zero
|
34
|
+
n = fv.size
|
35
|
+
for i in 0...n do
|
36
|
+
s += fv[i]*fv[i]
|
37
|
+
end
|
38
|
+
s
|
39
|
+
end
|
40
|
+
|
41
|
+
def nlsolve(f,x)
|
42
|
+
nRetry = 0
|
43
|
+
n = x.size
|
44
|
+
|
45
|
+
f0 = f.values(x)
|
46
|
+
zero = f.zero
|
47
|
+
one = f.one
|
48
|
+
two = f.two
|
49
|
+
p5 = one/two
|
50
|
+
d = norm(f0,zero)
|
51
|
+
minfact = f.ten*f.ten*f.ten
|
52
|
+
minfact = one/minfact
|
53
|
+
e = f.eps
|
54
|
+
while d >= e do
|
55
|
+
nRetry += 1
|
56
|
+
# Not yet converged. => Compute Jacobian matrix
|
57
|
+
dfdx = jacobian(f,f0,x)
|
58
|
+
# Solve dfdx*dx = -f0 to estimate dx
|
59
|
+
dx = lusolve(dfdx,f0,ludecomp(dfdx,n,zero,one),zero)
|
60
|
+
fact = two
|
61
|
+
xs = x.dup
|
62
|
+
begin
|
63
|
+
fact *= p5
|
64
|
+
if fact < minfact then
|
65
|
+
raise "Failed to reduce function values."
|
66
|
+
end
|
67
|
+
for i in 0...n do
|
68
|
+
x[i] = xs[i] - dx[i]*fact
|
69
|
+
end
|
70
|
+
f0 = f.values(x)
|
71
|
+
dn = norm(f0,zero)
|
72
|
+
end while(dn>=d)
|
73
|
+
d = dn
|
74
|
+
end
|
75
|
+
nRetry
|
76
|
+
end
|
77
|
+
end
|