6
# Solves linear equation system(A*x = b) by LU decomposition method.
7
# where A is a coefficient matrix,x is an answer vector,b is a constant vector.
10
# ruby linear.rb [input file solved]
14
require "bigdecimal/ludcmp"
18
# Change following BigDecimal::limit() if needed.
19
BigDecimal::limit(100)
24
printf("Number of equations ?") if(na <= 0)
29
zero = BigDecimal::new("0.0")
30
one = BigDecimal::new("1.0")
32
while (n=rd_order(na))>0
37
# Read data from console.
38
printf("\nEnter coefficient matrix element A[i,j]\n");
41
printf("A[%d,%d]? ",i,j); s = ARGF.gets
42
a << BigDecimal::new(s);
43
as << BigDecimal::new(s);
45
printf("Contatant vector element b[%d] ? ",i); b << BigDecimal::new(ARGF.gets);
48
# Read data from specified file.
49
printf("Coefficient matrix and constant vector.\n");
55
a << BigDecimal::new(s[j]);
56
as << BigDecimal::new(s[j]);
58
b << BigDecimal::new(s[n]);
61
x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
62
printf("Answer(x[i] & (A*x-b)[i]) follows\n")
64
printf("x[%d]=%s ",i,x[i].to_s)
67
s = s + as[i*n+j]*x[j]
69
printf(" & %s\n",(s-b[i]).to_s)