[system] / trunk / pg / lib / Matrix.pm Repository:
ViewVC logotype

Diff of /trunk/pg/lib/Matrix.pm

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

Revision 1050 Revision 1070
43 43
44sub trace { 44sub trace {
45 my $self = shift; 45 my $self = shift;
46 my $rows = $self->[1]; 46 my $rows = $self->[1];
47 my $cols = $self->[2]; 47 my $cols = $self->[2];
48 warn "Can't take trace of non-square matrix " unless $rows = $cols; 48 warn "Can't take trace of non-square matrix " unless $rows == $cols;
49 my $sum = 0; 49 my $sum = 0;
50 for( my $i = 0; $i<$rows;$i++) { 50 for( my $i = 0; $i<$rows;$i++) {
51 $sum +=$self->[0][$i][$i]; 51 $sum +=$self->[0][$i][$i];
52 } 52 }
53 $sum; 53 $sum;
128 my $rows = @{$vec}; 128 my $rows = @{$vec};
129 my $matrix = new Matrix($rows,1); 129 my $matrix = new Matrix($rows,1);
130 foreach my $i (1..$rows) { 130 foreach my $i (1..$rows) {
131 $matrix->assign($i,1,$vec->[$i-1]); 131 $matrix->assign($i,1,$vec->[$i-1]);
132 } 132 }
133 $matrix; 133 $matrix;
134} 134}
135=head4
136
137 This method takes an array of column vectors, or an array of arrays,
138 and converts them to a matrix where each column is one of the previous
139 vectors.
140
141=cut
142
143sub new_from_col_vecs
144{
145 my $class = shift;
146 my($vecs) = shift;
147 my($rows,$cols);
148
149 if(ref($vecs->[0])eq 'Matrix' ){
150 ($rows,$cols) = (scalar($vecs->[0]->[1]),scalar(@$vecs));
151 }else{
152 ($rows,$cols) = (scalar(@{$vecs->[0]}),scalar(@$vecs));
153 }
154
155 my($i,$j);
156 my $matrix = Matrix->new($rows,$cols);
157
158 if(ref($vecs->[0])eq 'Matrix' ){
159 for ( $i = 0; $i < $cols; $i++ )
160 {
161 for( $j = 0; $j < $rows; $j++ )
162 {
163 $matrix->[0][$j][$i] = $vecs->[$i][0][$j][0];
164 }
165 }
166 }else{
167 for ( $i = 0; $i < $cols; $i++ )
168 {
169 for( $j = 0; $j < $rows; $j++ )
170 {
171 $matrix->[0][$j][$i] = $vecs->[$i]->[$j];
172 }
173 }
174 }
175 return($matrix);
176}
177
1781;

Legend:
Removed from v.1050  
changed lines
  Added in v.1070

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9