diff options
author | shigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-04-24 13:37:32 +0000 |
---|---|---|
committer | shigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-04-24 13:37:32 +0000 |
commit | cff57b4a267b61561b04c527b5ce04d64cbcf78b (patch) | |
tree | 1196b15571b715ea69fee1ea90ce8bb2e958e787 /ext/bigdecimal/sample | |
parent | 558c428b72e666a58d6819cb45b63e75f15ae537 (diff) | |
download | ruby-cff57b4a267b61561b04c527b5ce04d64cbcf78b.tar.gz |
Dir. rearrangement according to the suggestions from Minero Aoki.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3722 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/sample')
-rw-r--r-- | ext/bigdecimal/sample/linear.rb | 46 | ||||
-rw-r--r-- | ext/bigdecimal/sample/nlsolve.rb | 38 | ||||
-rw-r--r-- | ext/bigdecimal/sample/pi.rb | 50 |
3 files changed, 134 insertions, 0 deletions
diff --git a/ext/bigdecimal/sample/linear.rb b/ext/bigdecimal/sample/linear.rb new file mode 100644 index 0000000000..f93404fb6f --- /dev/null +++ b/ext/bigdecimal/sample/linear.rb @@ -0,0 +1,46 @@ +#!/usr/local/bin/ruby + +# +# linear.rb +# +# Solves linear equation system(A*x = b) by LU decomposition method. +# where A is a coefficient matrix,x is an answer vector,b is a constant vector. +# +require "bigdecimal" +require "ludcmp" + +include LUSolve + +def rd_order + printf("Number of equations ?") + n = gets().chomp.to_i +end + +zero = BigDecimal::new("0.0") +one = BigDecimal::new("1.0") + +while (n=rd_order())>0 + a = [] + as= [] + b = [] + printf("\nEnter coefficient matrix element A[i,j]\n"); + for i in 0...n do + for j in 0...n do + printf("A[%d,%d]? ",i,j); s = gets + a <<=BigDecimal::new(s); + as<<=BigDecimal::new(s); + end + printf("Contatant vector element b[%d] ? ",i);b<<=BigDecimal::new(gets); + end + printf "ANS=" + x = lusolve(a,b,ludecomp(a,n,zero,one),zero) + p x + printf "A*x-b\n" + for i in 0...n do + s = zero + for j in 0...n do + s = s + as[i*n+j]*x[j] + end + p s-b[i] + end +end diff --git a/ext/bigdecimal/sample/nlsolve.rb b/ext/bigdecimal/sample/nlsolve.rb new file mode 100644 index 0000000000..08f17f9ecd --- /dev/null +++ b/ext/bigdecimal/sample/nlsolve.rb @@ -0,0 +1,38 @@ +#!/usr/local/bin/ruby + +# +# nlsolve.rb +# An example for solving nonlinear algebraic equation system. +# + +require "bigdecimal" +require "newton" +include Newton + +class Function + def initialize() + @zero = BigDecimal::new("0.0") + @one = BigDecimal::new("1.0") + @two = BigDecimal::new("2.0") + @ten = BigDecimal::new("10.0") + @eps = BigDecimal::new("1.0e-16") + end + def zero;@zero;end + def one ;@one ;end + def two ;@two ;end + def ten ;@ten ;end + def eps ;@eps ;end + def values(x) # <= defines functions solved + f = [] + f1 = x[0]*x[0] + x[1]*x[1] - @two # f1 = x**2 + y**2 - 2 => 0 + f2 = x[0] - x[1] # f2 = x - y => 0 + f <<= f1 + f <<= f2 + f + end +end + f = BigDecimal::limit(100) + f = Function.new + x = [f.zero,f.zero] # Initial values + n = nlsolve(f,x) + p x diff --git a/ext/bigdecimal/sample/pi.rb b/ext/bigdecimal/sample/pi.rb new file mode 100644 index 0000000000..cbb8448a38 --- /dev/null +++ b/ext/bigdecimal/sample/pi.rb @@ -0,0 +1,50 @@ +#!/usr/local/bin/ruby + +# +# pi.rb +# + +require "bigdecimal" +# +# Calculates 3.1415.... (the number of times that a circle's diameter +# will fit around the circle) using J. Machin's formula. +# +def big_pi(sig) # sig: Number of significant figures + exp = -sig + pi = BigDecimal::new("0") + two = BigDecimal::new("2") + m25 = BigDecimal::new("-0.04") + m57121 = BigDecimal::new("-57121") + + u = BigDecimal::new("1") + k = BigDecimal::new("1") + w = BigDecimal::new("1") + t = BigDecimal::new("-80") + while (u.exponent >= exp) + t = t*m25 + u,r = t.div(k,sig) + pi = pi + u + k = k+two + end + + u = BigDecimal::new("1") + k = BigDecimal::new("1") + w = BigDecimal::new("1") + t = BigDecimal::new("956") + while (u.exponent >= exp ) + t,r = t.div(m57121,sig) + u,r = t.div(k,sig) + pi = pi + u + k = k+two + end + pi +end + +if $0 == __FILE__ + if ARGV.size == 1 + print "PI("+ARGV[0]+"):\n" + p big_pi(ARGV[0].to_i) + else + print "TRY: ruby pi.rb 1000 \n" + end +end |