2003-03-28 00:00:21 -05:00
|
|
|
#!/usr/local/bin/ruby
|
2015-12-16 00:31:54 -05:00
|
|
|
# frozen_string_literal: false
|
2003-03-28 00:00:21 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2003-10-20 23:18:44 -04:00
|
|
|
# USAGE:
|
|
|
|
# ruby linear.rb [input file solved]
|
|
|
|
#
|
|
|
|
|
2013-06-13 10:44:25 -04:00
|
|
|
# :stopdoc:
|
2003-03-28 00:00:21 -05:00
|
|
|
require "bigdecimal"
|
2005-02-18 12:31:14 -05:00
|
|
|
require "bigdecimal/ludcmp"
|
2003-03-28 00:00:21 -05:00
|
|
|
|
2003-10-20 23:18:44 -04:00
|
|
|
#
|
|
|
|
# NOTE:
|
2014-01-02 10:10:13 -05:00
|
|
|
# Change following BigDecimal.limit() if needed.
|
|
|
|
BigDecimal.limit(100)
|
2003-10-20 23:18:44 -04:00
|
|
|
#
|
2003-03-28 00:00:21 -05:00
|
|
|
|
2003-10-20 23:18:44 -04:00
|
|
|
include LUSolve
|
|
|
|
def rd_order(na)
|
|
|
|
printf("Number of equations ?") if(na <= 0)
|
|
|
|
n = ARGF.gets().to_i
|
2003-03-28 00:00:21 -05:00
|
|
|
end
|
|
|
|
|
2003-10-20 23:18:44 -04:00
|
|
|
na = ARGV.size
|
2018-12-05 06:30:24 -05:00
|
|
|
zero = BigDecimal("0.0")
|
|
|
|
one = BigDecimal("1.0")
|
2003-03-28 00:00:21 -05:00
|
|
|
|
2003-10-20 23:18:44 -04:00
|
|
|
while (n=rd_order(na))>0
|
2003-03-28 00:00:21 -05:00
|
|
|
a = []
|
|
|
|
as= []
|
|
|
|
b = []
|
2003-10-20 23:18:44 -04:00
|
|
|
if na <= 0
|
|
|
|
# Read data from console.
|
2018-12-05 06:30:24 -05:00
|
|
|
printf("\nEnter coefficient matrix element A[i,j]\n")
|
2003-10-20 23:18:44 -04:00
|
|
|
for i in 0...n do
|
|
|
|
for j in 0...n do
|
|
|
|
printf("A[%d,%d]? ",i,j); s = ARGF.gets
|
2018-12-05 06:30:24 -05:00
|
|
|
a << BigDecimal(s)
|
|
|
|
as << BigDecimal(s)
|
2003-10-20 23:18:44 -04:00
|
|
|
end
|
2018-12-05 06:30:24 -05:00
|
|
|
printf("Contatant vector element b[%d] ? ",i)
|
|
|
|
b << BigDecimal(ARGF.gets)
|
2003-10-20 23:18:44 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
# Read data from specified file.
|
2018-12-05 06:30:24 -05:00
|
|
|
printf("Coefficient matrix and constant vector.\n")
|
2003-10-20 23:18:44 -04:00
|
|
|
for i in 0...n do
|
|
|
|
s = ARGF.gets
|
|
|
|
printf("%d) %s",i,s)
|
|
|
|
s = s.split
|
|
|
|
for j in 0...n do
|
2018-12-05 06:30:24 -05:00
|
|
|
a << BigDecimal(s[j])
|
|
|
|
as << BigDecimal(s[j])
|
2003-10-20 23:18:44 -04:00
|
|
|
end
|
2018-12-05 06:30:24 -05:00
|
|
|
b << BigDecimal(s[n])
|
2003-10-20 23:18:44 -04:00
|
|
|
end
|
2003-03-28 00:00:21 -05:00
|
|
|
end
|
|
|
|
x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
|
2003-10-20 23:18:44 -04:00
|
|
|
printf("Answer(x[i] & (A*x-b)[i]) follows\n")
|
2003-03-28 00:00:21 -05:00
|
|
|
for i in 0...n do
|
2003-10-20 23:18:44 -04:00
|
|
|
printf("x[%d]=%s ",i,x[i].to_s)
|
|
|
|
s = zero
|
|
|
|
for j in 0...n do
|
2003-03-28 00:00:21 -05:00
|
|
|
s = s + as[i*n+j]*x[j]
|
2003-10-20 23:18:44 -04:00
|
|
|
end
|
|
|
|
printf(" & %s\n",(s-b[i]).to_s)
|
2003-03-28 00:00:21 -05:00
|
|
|
end
|
|
|
|
end
|