summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/lib/linear.rb
blob: f93404fb6ffe119793852859278065b2ad2ade98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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