summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/lib/newton.rb
diff options
context:
space:
mode:
authorshigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-18 15:24:25 +0000
committershigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-18 15:24:25 +0000
commite242cf9defb5442ef223535abe93399352cf139e (patch)
treeb8eb591e57c326fd4e7cb49365f7e51783179237 /ext/bigdecimal/lib/newton.rb
parentb10272dc371a03844f685dc6db765c2bea11d29b (diff)
More pathes from Tadasi Saito.
As discussed in ruby-dev ML: E,PI, etc are disabled. BigDecimal op String disabled. to_f changed. lib directory moved. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4092 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/lib/newton.rb')
-rw-r--r--ext/bigdecimal/lib/newton.rb75
1 files changed, 0 insertions, 75 deletions
diff --git a/ext/bigdecimal/lib/newton.rb b/ext/bigdecimal/lib/newton.rb
deleted file mode 100644
index 67a92474ac..0000000000
--- a/ext/bigdecimal/lib/newton.rb
+++ /dev/null
@@ -1,75 +0,0 @@
-#
-# newton.rb
-#
-# Solves nonlinear algebraic equation system f = 0 by Newton's method.
-# (This program is not dependent on BigDecimal)
-#
-# To call:
-# n = nlsolve(f,x)
-# where n is the number of iterations required.
-# x is the solution vector.
-# f is the object to be solved which must have following methods.
-#
-# f ... Object to compute Jacobian matrix of the equation systems.
-# [Methods required for f]
-# f.values(x) returns values of all functions at x.
-# f.zero returns 0.0
-# f.one returns 1.0
-# f.two returns 1.0
-# f.ten returns 10.0
-# f.eps convergence criterion
-# x ... initial values
-#
-require "ludcmp"
-require "jacobian"
-
-module Newton
- include LUSolve
- include Jacobian
-
- def norm(fv,zero=0.0)
- s = zero
- n = fv.size
- for i in 0...n do
- s += fv[i]*fv[i]
- end
- s
- end
-
- def nlsolve(f,x)
- nRetry = 0
- n = x.size
-
- f0 = f.values(x)
- zero = f.zero
- one = f.one
- two = f.two
- p5 = one/two
- d = norm(f0,zero)
- minfact = f.ten*f.ten*f.ten
- minfact = one/minfact
- e = f.eps
- while d >= e do
- nRetry += 1
- # Not yet converged. => Compute Jacobian matrix
- dfdx = jacobian(f,f0,x)
- # Solve dfdx*dx = -f0 to estimate dx
- dx = lusolve(dfdx,f0,ludecomp(dfdx,n,zero,one),zero)
- fact = two
- xs = x.dup
- begin
- fact *= p5
- if fact < minfact then
- raize "Failed to reduce function values."
- end
- for i in 0...n do
- x[i] = xs[i] - dx[i]*fact
- end
- f0 = f.values(x)
- dn = norm(f0,zero)
- end while(dn>=d)
- d = dn
- end
- nRetry
- end
-end