Perl - Determinant of Matrix Containing Variables -


i have perl program containing following methods:

  • det find determinant of matrix.
  • identitymatrix return n n identity matrix (1s on main diagnal, rest 0s).
  • matrixadd add 2 matrices together.
  • matrixscalarmultiply multiply integer matrix.

i can find determinant of, example, matrix a - i where

enter image description here

(it 0)

but if want find determinant of a - ri?

enter image description here

in case, want program solve characteristic polynomial solution (a.k.a. determinant containing variables) along these lines instead of integer value:

enter image description here

any suggestions on how handle this? code below:

#!/usr/bin/perl #perl solver.pl  use strict; use warnings;  ### solve characteristic polynomial det(a - ri)   @a = ( # 3x3, det = -3    [1, 3, -3],    [1, 0, 0],    [0, 1, 0], );  # test_matrix = - $test_matrix = matrixadd( \@a,      matrixscalarmultiply( identitymatrix(3), -1 ) );  print "\ntest:\n"; for( $i = 0; $i <= $#$test_matrix; $i++ ){      print "[";     for( $j = 0; $j <= $#$test_matrix; $j++ ){         $j == $#$test_matrix ? print $test_matrix->[$i][$j], "]\n" :              print $test_matrix->[$i][$j], ", ";     } }  $dd = det ($test_matrix); print "det = $dd \n";    # recursively find determinant of real square matrix # call on n n matrices n >= 2 # # arg0 = matrix reference sub det{     ($a) = @_;      #base: 2x2 matrix     if( $#$a + 1 == 2 ){ #recall $#$a == last index of         return $a->[0][0]*$a->[1][1] - $a->[1][0]*$a->[0][1];     }      #cofactor expansion matrices > 2x2     $answer = 0;     for( $col = 0; $col <= $#$a; $col++ ){         $m = (); #sub matrix         $multiplier = $a->[0][$col];         if( $col % 2 == 1 ){    #+, -, +, -, ...             $multiplier *= -1;         }          for( $i = 1; $i <= $#$a; $i++ ){              #j indexer a, k m             for( ($j, $k) = (0, 0); $j <= $#$a; $j++ ){                 $m->[$i-1][$k++] = $a->[$i][$j] unless $j == $col;             }         }          $answer += $multiplier*det( $m );     }#end cofactor expansion      return $answer; }#end det()   # return reference n n identity matrix # can in perl! # # arg0 = dimension 'n' sub identitymatrix{     $n = shift;     @ret;      (my $i = 0; $i < $n; $i++ ){         (my $j = 0; $j < $n; $j++ ){             $ret[$i][$j] = $i == $j ? 1 : 0;         }     }      return \@ret;    }   # return reference n n matrix sum # of 2 different n n matrices, "a" , "b" # # arg0, 1 = references pair of matrices add sub matrixadd{     @ret;     ($a, $b) = ($_[0], $_[1]);      (my $i = 0; $i <= $#$a; $i++ ){         (my $j = 0; $j <= $#$a; $j++ ){             $ret[$i][$j] = $a->[$i][$j] + $b->[$i][$j];         }     }      return \@ret; }   # return reference matrix multiplied given scalar # # arg0 = reference matrix # arg1 = scalar multiply sub matrixscalarmultiply{     @ret;     ($a, $multiplier) = ($_[0], $_[1]);      (my $i = 0; $i <= $#$a; $i++ ){         (my $j = 0; $j <= $#$a; $j++ ){             $ret[$i][$j] = $a->[$i][$j] * $multiplier;         }     }      return \@ret; } 

this called symbolic math , in wheelhouse of tools mathematica. perl, there packages math::synbolic couldn't tell how easy use.

on other hand, if interested in values of r have determinant of 0 , not interested in characteristic polynomial looks like, looking eigenvalues of a. there perl libraries that, too.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -