0: #!/usr/bin/perl5.6.1 -w
1: # Another Algorithm we learned in school as Perl code (-;
2: # It is used for solving equations, and I put it here hoping
3: # for some hints what could be improved, because concerning
4: # elegance I do not like it and would be glad to have the
5: # possibilitie to learn from more experienced folks (-;
6: # (especially readability is a problem for me)
7:
8: use strict;
9: my (@MatrixA,@MatrixB,@MatrixX);
10: # MatrixA will include the left-side of the equation,MatrixB the right, MatrixX the solutions
11:
12: my $line_counter=0;
13: # Input are equations of the Form: a*x1+b*x2+c*x3+..=A
14: # Written as:a b c=A
15: # Either in a file or just entered or line by line.
16: # Input of the left side, has to be quadratic!
17: while(<>)
18: {
19: my @line_array;
20: chomp;
21: die "Wrong Character in Input, $_" unless /^[0123456789 -=]+$/;
22: die "No B-Val found" unless s/=(-?\d+)$//;
23: @line_array=(split / /);
24: $MatrixB[$line_counter]=$1;
25: $MatrixA[$line_counter]=\@line_array;
26: $line_counter++;
27: }
28: #Check if all input lines have the same number of elements
29: foreach (0..($line_counter-2))
30: {
31: my $line_length_1=@{$MatrixA[$_]};
32: my $line_length_2=@{$MatrixA[$_+1]};
33: die "ERROR:(Columns 1/Columns 2: $line_length_1/$line_length_2)\n" unless ($line_length_1==$line_length_2);
34: }
35:
36: my $input_lines=@MatrixB;
37: my $input_columns=@{$MatrixA[0]};
38:
39: die "Matrix not quadratic"
40: unless ($input_lines==$input_columns);
41:
42: my $format=$input_columns-1;
43:
44: sub Print_Matrix
45: {
46: print join(" ",@{$MatrixA[$_]})."=".$MatrixB[$_]."\n" foreach (0..$input_columns-1);
47: print "\n";
48: }
49:
50:
51: &Print_Matrix;
52: {
53: # Creation of a compareable Matrix
54: # To check if two lines are equivalent
55: my (@Norm_MatrixA,@Norm_MatrixB);
56: foreach my $current_line (0..$format)
57: {
58: my $factor=$MatrixA[$current_line][0];
59: foreach (0..($format))
60: {
61: $Norm_MatrixA[$current_line][$_]=$MatrixA[$current_line][$_]/$factor;
62: $Norm_MatrixB[$current_line]=$MatrixB[$current_line]/$factor;
63: }
64: }
65: # Compare the Lines of the Matrix:
66: foreach (0..($format))
67: {
68: my $upper_line=$_;
69: my $eq_counter=0;
70: foreach(($upper_line+1)..($format))
71: {
72: my $lower_line=$_;
73: foreach(0..($format))
74: {
75: $eq_counter++ if ($Norm_MatrixA[$upper_line][$_]==$Norm_MatrixA[$lower_line][$_]);
76: }
77: $eq_counter++ if ($Norm_MatrixB[$upper_line]==$Norm_MatrixB[$lower_line]);
78: die "Two Lines of Matrix ident: $upper_line/$lower_line" if($eq_counter>=$input_columns)
79: }
80: }
81: }
82:
83: # Now here comes the Gauss Algorithm
84: foreach my $SubMatrix (0..($input_lines-2))
85: {
86: foreach my $line (($SubMatrix+1)..($format))
87: {
88: my $pivot=$MatrixA[$SubMatrix][$SubMatrix];
89: my $first=$MatrixA[$line][$SubMatrix];
90: foreach my $Spalte (0..($format))
91: {
92: $MatrixA[$line][$Spalte]=$MatrixA[$SubMatrix][$Spalte]+($MatrixA[$line][$Spalte]*$pivot/$first*-1);
93: }
94: $MatrixB[$line]=$MatrixB[$SubMatrix]+($MatrixB[$line]*$pivot/$first*-1);
95: }
96: &Print_Matrix;
97: }
98:
99: # Get the solutions
100: foreach my $solve_line (reverse (0..($format)))
101: {
102: my $leftsum=0;
103: ($leftsum+=($MatrixA[$solve_line][$_]*$MatrixX[$_]))foreach ($solve_line+1..$format);
104: $MatrixX[$solve_line]=($MatrixB[$solve_line]-$leftsum)/($MatrixA[$solve_line][$solve_line]);
105: }
106: print "x_$_=$MatrixX[$_-1]\n"foreach(1..$format+1);
107:
In reply to Gaussian Elimination Algorithm by grexman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |