|
1 | | -#!/usr/local/bin/ruby |
2 | 1 | # frozen_string_literal: false |
3 | 2 |
|
4 | 3 | # |
|
13 | 12 |
|
14 | 13 | # :stopdoc: |
15 | 14 | require "bigdecimal" |
16 | | -require "bigdecimal/ludcmp" |
17 | 15 |
|
18 | | -# |
19 | | -# NOTE: |
20 | | -# Change following BigDecimal.limit() if needed. |
21 | | -BigDecimal.limit(100) |
22 | | -# |
| 16 | +# Requires gem matrix |
| 17 | +require "matrix" |
| 18 | + |
| 19 | +class PrecisionSpecifiedValue |
| 20 | + # NOTE: |
| 21 | + # Change following PREC if needed. |
| 22 | + |
| 23 | + attr_reader :value |
| 24 | + def initialize(value, prec) |
| 25 | + @value = BigDecimal(value) |
| 26 | + @prec = prec |
| 27 | + end |
| 28 | + |
| 29 | + def unwrap(value) |
| 30 | + PrecisionSpecifiedValue === value ? value.value : value |
| 31 | + end |
| 32 | + |
| 33 | + def coerce(other) |
| 34 | + [self.class.new(unwrap(other), @prec), self] |
| 35 | + end |
| 36 | + |
| 37 | + def abs |
| 38 | + self.class.new(@value.abs, @prec) |
| 39 | + end |
| 40 | + |
| 41 | + def >(other) |
| 42 | + @value > unwrap(other) |
| 43 | + end |
| 44 | + |
| 45 | + def <(other) |
| 46 | + @value < unwrap(other) |
| 47 | + end |
| 48 | + |
| 49 | + def -(other) |
| 50 | + self.class.new(@value.sub(unwrap(other), @prec), @prec) |
| 51 | + end |
| 52 | + |
| 53 | + def +(other) |
| 54 | + self.class.new(@value.add(unwrap(other), @prec), @prec) |
| 55 | + end |
| 56 | + |
| 57 | + def *(other) |
| 58 | + self.class.new(@value.mult(unwrap(other), @prec), @prec) |
| 59 | + end |
| 60 | + |
| 61 | + def quo(other) |
| 62 | + self.class.new(@value.div(unwrap(other), @prec), @prec) |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +return if __FILE__ != $0 |
23 | 67 |
|
24 | | -include LUSolve |
25 | 68 | def rd_order(na) |
26 | | - printf("Number of equations ?") if(na <= 0) |
27 | | - n = ARGF.gets().to_i |
| 69 | + printf("Number of equations ?") if(na <= 0) |
| 70 | + ARGF.gets().to_i |
28 | 71 | end |
29 | 72 |
|
30 | | -na = ARGV.size |
31 | | -zero = BigDecimal("0.0") |
32 | | -one = BigDecimal("1.0") |
| 73 | +na = ARGV.size |
33 | 74 |
|
34 | 75 | while (n=rd_order(na))>0 |
35 | 76 | a = [] |
36 | | - as= [] |
37 | 77 | b = [] |
38 | 78 | if na <= 0 |
39 | 79 | # Read data from console. |
40 | 80 | printf("\nEnter coefficient matrix element A[i,j]\n") |
41 | 81 | for i in 0...n do |
42 | | - for j in 0...n do |
| 82 | + a << n.times.map do |j| |
43 | 83 | printf("A[%d,%d]? ",i,j); s = ARGF.gets |
44 | | - a << BigDecimal(s) |
45 | | - as << BigDecimal(s) |
| 84 | + BigDecimal(s) |
46 | 85 | end |
47 | 86 | printf("Contatant vector element b[%d] ? ",i) |
48 | 87 | b << BigDecimal(ARGF.gets) |
49 | 88 | end |
50 | 89 | else |
51 | | - # Read data from specified file. |
52 | | - printf("Coefficient matrix and constant vector.\n") |
53 | | - for i in 0...n do |
54 | | - s = ARGF.gets |
55 | | - printf("%d) %s",i,s) |
56 | | - s = s.split |
57 | | - for j in 0...n do |
58 | | - a << BigDecimal(s[j]) |
59 | | - as << BigDecimal(s[j]) |
60 | | - end |
61 | | - b << BigDecimal(s[n]) |
62 | | - end |
| 90 | + # Read data from specified file. |
| 91 | + printf("Coefficient matrix and constant vector.\n") |
| 92 | + for i in 0...n do |
| 93 | + s = ARGF.gets |
| 94 | + printf("%d) %s",i,s) |
| 95 | + s = s.split |
| 96 | + a << n.times.map {|j| BigDecimal(s[j]) } |
| 97 | + b << BigDecimal(s[n]) |
| 98 | + end |
63 | 99 | end |
64 | | - x = lusolve(a,b,ludecomp(a,n,zero,one),zero) |
| 100 | + |
| 101 | + prec = 100 |
| 102 | + matrix = Matrix[*a.map {|row| row.map {|v| PrecisionSpecifiedValue.new(v, prec) } }] |
| 103 | + vector = b.map {|v| PrecisionSpecifiedValue.new(v, prec) } |
| 104 | + x = matrix.lup.solve(vector).map(&:value) |
| 105 | + |
65 | 106 | printf("Answer(x[i] & (A*x-b)[i]) follows\n") |
66 | 107 | for i in 0...n do |
67 | 108 | printf("x[%d]=%s ",i,x[i].to_s) |
68 | | - s = zero |
69 | | - for j in 0...n do |
70 | | - s = s + as[i*n+j]*x[j] |
71 | | - end |
72 | | - printf(" & %s\n",(s-b[i]).to_s) |
| 109 | + diff = a[i].zip(x).sum {|aij, xj| aij*xj }.sub(b[i], 10) |
| 110 | + printf(" & %s\n", diff.to_s) |
73 | 111 | end |
74 | 112 | end |
0 commit comments