[system] / trunk / pg / macros / PGmorematrixmacros.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/PGmorematrixmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1294 Revision 1327
22 } 22 }
23 $A_lr = $A->decompose_LR(); 23 $A_lr = $A->decompose_LR();
24 $det = $A_lr->det_LR(); 24 $det = $A_lr->det_LR();
25 } 25 }
26 return $A; 26 return $A;
27}
28
29=head4 random_diag_matrix
30
31This method returns a random nxn diagonal matrix.
32
33=cut
34
35sub random_diag_matrix{ ## Builds and returns a random diagonal \$n by \$n matrix
36
37 warn "Usage: \$new_matrix = random_diag_matrix(\$n)" if (@_ != 1);
38
39 my $D = new Matrix($_[0],$_[0]);
40 my $norm = 0;
41 while( $norm == 0 ){
42 foreach my $i (1..$_[0]){
43 foreach my $j (1..$_[0]){
44 if( $i != $j ){
45 $D->assign($i,$j,0);
46 }else{
47 $D->assign($i,$j,random(-9,9,1));
48 }
49 }
50 }
51 $norm = abs($D);
52 }
53 return $D;
27} 54}
28 55
29sub swap_rows{ 56sub swap_rows{
30 57
31 warn "Usage: \$new_matrix = swap_rows(\$matrix,\$row1,\$row2);" 58 warn "Usage: \$new_matrix = swap_rows(\$matrix,\$row1,\$row2);"
610 }else{ 637 }else{
611 1; 638 1;
612 } 639 }
613} 640}
614 641
642sub is_diagonal{
643 my $matrix = shift;
644 my %options = @_;
645 my $process_ans_hash = ( ref( $matrix ) eq 'AnswerHash' ) ? 1 : 0 ;
646 my ($rh_ans);
647 if ($process_ans_hash) {
648 $rh_ans = $matrix;
649 $matrix = $rh_ans->{ra_student_ans};
650 }
651
652 return 0 unless defined($matrix);
653
654 if( ref($matrix) eq 'ARRAY' ){
655 my @matrix = @{$matrix};
656 @matrix = @{$matrix[0]} if ref($matrix[0][0]) eq 'ARRAY';
657 if( ref($matrix[0]) ne 'ARRAY' or scalar( @matrix ) != scalar( @{$matrix[0]} ) ){
658 warn "It is impossible for a non-square matrix to be diagonal, if you are a student, please tell your professor that there is a problem.";
659 }
660
661 for( my $i = 0; $i < scalar( @matrix ) ; $i++ ){
662 for( my $j = 0; $j < scalar( @{$matrix[0]} ); $j++ ){
663 if( $matrix[$i][$j] != 0 and $i != $j )
664 {
665 if ($process_ans_hash){
666 $rh_ans->throw_error('EVAL');
667 return $rh_ans;
668 } else {
669 return 0;
670 }
671 }
672 }
673 }
674 if ($process_ans_hash){
675 return $rh_ans;
676 } else {
677 return 1;
678 }
679 }elsif( ref($matrix) eq 'Matrix' ){
680 if( $matrix->[1] != $matrix->[2] ){
681 warn "It is impossible for a non-square matrix to be diagonal, if you are a student, please tell your professor that there is a problem.";
682 if ($process_ans_hash){
683 $rh_ans->throw_error('EVAL');
684 return $rh_ans;
685 } else {
686 return 0;
687 }
688 }
689 for( my $i = 0; $i < $matrix->[1] ; $i++ ){
690 for( my $j = 0; $j < $matrix->[2] ; $j++ ){
691 if( $matrix->[0][$i][$j] != 0 and $i != $j ){
692 if ($process_ans_hash){
693 $rh_ans->throw_error('EVAL');
694 return $rh_ans;
695 } else {
696 return 0;
697 }
698 }
699 }
700 }
701 if ($process_ans_hash){
702 return $rh_ans;
703 } else {
704 return 1;
705 }
706 }else{
707 warn "There is a problem with the problem, please alert your professor.";
708 if ($process_ans_hash){
709 $rh_ans->throw_error('EVAL');
710 return $rh_ans;
711 } else {
712 return 0;
713 }
714 }
715
716}
717
718
615sub are_unit_vecs{ 719sub are_unit_vecs{
616 my ( $vec_ref,%opts ) = @_; 720 my ( $vec_ref,%opts ) = @_;
617 my @vecs = (); 721 my @vecs = ();
618 if( ref($vec_ref) eq 'AnswerHash' ) 722 if( ref($vec_ref) eq 'AnswerHash' )
619 { 723 {
815 $rh_ans->{rm_correct_ans} = Matrix->new_from_col_vecs(\@correct_space); 919 $rh_ans->{rm_correct_ans} = Matrix->new_from_col_vecs(\@correct_space);
816 $rh_ans->{ra_student_ans} = \@space; 920 $rh_ans->{ra_student_ans} = \@space;
817 return compare_basis( $rh_ans, %options ); 921 return compare_basis( $rh_ans, %options );
818 } 922 }
819} 923}
924
925sub pretty_matrix{
926 my $matrix = shift;
820 927
928 if( ref($matrix) ne 'Matrix' ){
929 warn "Usage: \$pretty_matrix = pretty_matrix(\$matrix)";
930 }
931
932 for( my $i = 0; $i < $matrix->[1]; $i++ ){
933 for( my $j = 0; $j < $matrix->[2]; $j++ ){
934 if( $matrix->[0][$i][$j] - sprintf("%.0f", $matrix->[0][$i][$j] ) < $main::functZeroLevelTolDefault ){
935 $matrix->[0][$i][$j] = sprintf("%.0f", $matrix->[0][$i][$j] );
936 $matrix->[0][$i][$j] = 0 if( $matrix->[0][$i][$j] == 0);
937 }
938 }
939 }
940 $matrix;
941}
821 942
8221; 9431;

Legend:
Removed from v.1294  
changed lines
  Added in v.1327

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9