mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
558c428b72
commit
cff57b4a26
4 changed files with 10 additions and 9 deletions
46
ext/bigdecimal/sample/linear.rb
Normal file
46
ext/bigdecimal/sample/linear.rb
Normal file
|
|
@ -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
|
||||
38
ext/bigdecimal/sample/nlsolve.rb
Normal file
38
ext/bigdecimal/sample/nlsolve.rb
Normal file
|
|
@ -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
|
||||
50
ext/bigdecimal/sample/pi.rb
Normal file
50
ext/bigdecimal/sample/pi.rb
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue