Here's that code again in a more readable format:
$A1=Matrix->new_from_array_ref( [[2,-1],[0,3]]);
$solution1=[-2,3]; #What do we want the solution to be?
$x1=Matrix->new_from_array_ref([$solution1]);
$x1 = ~$x1; #transpose
$b1 = $A1*$x1;
$variablenames=Matrix->new_from_array_ref( [['x'],['y']] );
$C1=Mat2System($A1,$b1,$variablenames);
BEGIN_TEXT
Solve the following system by back substitution:
( $C1 )
END_TEXT
sub Mat2System{</p><p>
my $coeffs=shift();</p><p>
my $vec = shift();</p><p>
my $vname=shift();</p><p>
my ($srow,$scol) = $coeffs->dim();</p><p>
my ($vrow,$vcol) = $vec->dim();</p><p>
my ($vnamerow,$vnamecol) = $vname->dim();</p><p>
die "Wrong number of rows or columns" if (($vcol !=1) || ($vrow != $srow) || ($vnamecol !=1) || ($vrow != $vnamerow));</p><p>
my $outstr="\begin{array}";
my $s;
$outstr = $outstr . '{r';</p><p>
for(my $j=0; $j<$scol; $j++){
$outstr = $outstr . 'rr';}
$outstr = $outstr . 'r}';
for(my $j=0;$j<$srow;$j++){
$s=0;
for(my $i=0,my $vn=1;$i<$scol;$i++,$vn++){
my $varname=$vname->element($vn,1);
my $a=$coeffs->element($j+1,$i+1);
if($a==0){
## if coefficient is 0 then goto the next column by putting 2 &&'s
$outstr = $outstr . '&&';
}elsif($a>0){
if($a==1){$a="";}
if($s==0){$outstr = $outstr . "& $a \,$varname";$s=1;
} else{$outstr=$outstr . "&+& $a \, $varname";}
}else{
if($s == 1){
$a=-$a;
if($a==1){$a="";}
$outstr= $outstr . "&- &$a \,$varname";
}else{$outstr = $outstr . "& $a \, $varname";$s=1;}
}}
$outstr = $outstr . "&=&" . $vec->element($j+1,1). "\\";}
$outstr= $outstr . ' end{array}';
return $outstr; }
<| Post or View Comments |> |