How not to display Zero or One as Coefficient in a Linear Equation  topic started 5/17/2005; 10:48:33 AM last post 5/18/2005; 12:10:35 PM 









Thomas Hagedorn  Re: How not to display Zero or One as Coefficient in a Linear Equation 5/18/2005; 12:10:35 PM (reads: 1856, responses: 0) 
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{ my $coeffs=shift(); my $vec = shift(); my $vname=shift(); my ($srow,$scol) = $coeffs>dim(); my ($vrow,$vcol) = $vec>dim(); my ($vnamerow,$vnamecol) = $vname>dim(); die "Wrong number of rows or columns" if (($vcol !=1)  ($vrow != $srow)  ($vnamecol !=1)  ($vrow != $vnamerow)); my $outstr="\begin{array}"; my $s; $outstr = $outstr . '{r'; 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; } 