For all mathematicians... This code solves $var_no
number
of equations for $var_no number of variables. It uses
Crout's Reduction Algorithm. The 2D array called
@matrix
is the
coefficient matrix.
# Crout's Reduction Algorithm
if($messages eq "on"){
print "$n<code>$n";
}
$beginning_time = localtime(time);
if($matrix[1][1]!=0){
for($k=2;$k<=$var_no;$k+=1){
for($i=$k;$i<=$var_no;$i+=1){
for($j=($var_no+1);$j>=($k-1);$j-=1){
if($messages eq "on"){
print $matrix[$i][$j];
print " ---> ";
}
# Crout's Reduction Formula
$matrix[$i][$j] -= $matrix[$k-1][$j] * ( $matrix[$
+i][$k-1] / $matrix[$k-1][$k-1] );
if($messages eq "on"){
print $matrix[$i][$j];
print $n;
}
}
}
}
}
if($messages eq "on"){
for($i=1;$i<=($var_no+1);$i+=1){
for($j=1;$j<=($var_no+1);$j+=1){
print $matrix[$i][$j];
print " ";
}
print $n
}
print "</code>$n";
}
# Solving the equations once the matrix is reduced.
for($i=$var_no;$i>=1;$i-=1){
$sum = 0;
for($k=($i+1);$k<=$var_no;$k+=1){
$sum += $x[$k] * $matrix[$i][$k];
}
$x[$i] = ( $matrix[$i][$var_no+1] - $sum) / $matrix[$i][$i]
}
$ending_time = localtime(time);
if($messages eq "on"){
print "$n<code>\n";
for($i=1;$i<=$var_no;$i+=1){
print $x[$i];
print $n
}
print "$nThe whole thing done in only ".($ending_time-$beginni
+ng_time)." seconds.";
print "</code>$n";
}