matrix - illegal division by zero:Perl -


i have written code find determinant of 10x10 matrix. code gives proper result till 9x9 matrix. 10x10 matrix gives following error

"use of uninitialized value in multiplication <*> @ line 23

illegal division 0 @ line 21"

i tried 11x11 matrix also, giving wrong answer.

why code giving such error...

following code:

#!/usr/bin/perl use strict;  use warnings;  @x1=(   [5, 6, 3, 2, 4, 9, 3, 5, 4, 2],    [12, 9, 8, 3, 3, 0, 6, 9, 3, 4],   [8, 6, 5, 8, 9, 3, 9, 3, 9, 5],   [6, 4, 3, 0, 6, 4, 8, 2, 22, 8],   [8, 3, 2, 5, 2, 12, 7, 1, 6, 9],   [5, 9, 3, 9, 5, 1, 3, 8, 4, 2],   [3, 10, 4, 16, 4, 7, 2, 12, 9, 6],   [2, 12, 9, 13, 8, 3, 1, 16, 0, 6],   [3, 6, 8, 5, 12, 8, 4, 19, 8, 5],   [2, 5, 6, 4, 9, 10, 3, 11, 7, 3] ); # matrix of nxn (my $i=0;$i le 9;$i++) {   (my $j=0;$j le 9;$j++) {        if($j>$i) {          $ratio = $x1[$j][$i]/$x1[$i][$i];           for(my $k = 0; $k le 9; $k++){                $x1[$j][$k] -= $ratio * $x1[$i][$k];           }       }     } }  $det1 = 1;  for(my $i = 0; $i le 9; $i++){   $det1 *= $x1[$i][$i]; } printf $det1," "; 

le doesn't think does. http://perldoc.perl.org/perlop.html

binary "le" returns true if left argument stringwise less or equal right argument.

print 10 le 9,"\n"; print 10 <= 9,"\n"; 

it's stringwise comparison not numeric one.

so "10" le "9" true, because alphabetically 10 before 9.

but work fine smaller matrix, because 9 le 8 valid comparison , works 'right way'.

you should use <= instead:

binary "<=" returns true if left argument numerically less or equal right argument.

you can auto-scale using $#x1 comparison, value of last array index. in example above, $#x1 9, because array 0-9


Comments