# from Math::MatrixReal::Ext1 sub new_from_cols { my $proto = shift; my $class = ref($proto) || $proto; my $ref_to_cols = shift; my @cols = @{$ref_to_cols}; my $cols = scalar( @cols ); my $matrix = 0; my $rows = 0; # each arg is a column, but we don't know what form they're # in yet my $col_index = 0; foreach my $col (@cols) { # it's one-based $col_index ++; my $ref = ref( $col ) ; if ( $ref && $ref->can('dim') && $ref->can('element') ) { # it's already an object that can do what we need it to } elsif ( $ref eq 'ARRAY' ) { my @array = @$col; my $length = scalar( @array ); $col = $class->new_from_string( '[ '. join( " ]\n[ ", @array) ." ]\n" ); } elsif ( $ref eq '' ) { # we hope this is a string $col = $class->new_from_string( $col ); } else { # we have no idea, error time! croak __PACKAGE__."::new_from_cols(): can only accept array refs, strings, and objects with Math::MatrixReal-like functionality \n"; } my ($length, $one) = $col->dim; croak __PACKAGE__."::new_from_cols(): This isn't a column vector" unless ($one == 1) ; # if we already have a height, check that this is the same if ($rows) { croak __PACKAGE__."::new_from_cols(): This column vector has $length elements and an earlier one had $rows--all vectors must be the same size as the first one" unless ($length == $rows) ; } # else, we have a new height # TODO: maybe this should check for zero else { $rows = $length; } # create the matrix the first time through unless ($matrix) { $matrix = $class->new($rows, $cols); } foreach my $row_index (1..$rows){ my $value = $col->element($row_index, 1); $matrix->assign($row_index, $col_index, $value); } } return $matrix; } #from Math::MatrixReal::Ext1 sub new_from_rows { my $proto = shift; my $class = ref($proto) || $proto; my $ref_to_rows = shift; my @rows = @{$ref_to_rows}; my $rows = scalar( @rows ); my $matrix = 0; my $cols = 0; # each arg is a column, but we don't know what form they're # in yet my $row_index = 0; foreach my $row (@rows) { # it's one-based $row_index ++; my $ref = ref( $row ) ; if ( $ref && $ref->can('dim') && $ref->can('element')) { # it's already capable of doing what we need } elsif ( $ref eq 'ARRAY' ) { my @array = @$row; my $length = scalar( @array ); $row = $class->new_from_string( '[ '. join( " ", @array) ." ]\n" ); } elsif ( $ref eq '' ) { # we hope this is a string $row = $class->new_from_string( $row ); } else { # we have no idea, error time! croak __PACKAGE__."::new_from_rows(): can only accept array refs, strings, and objects with Math::MatrixReal-like functionality \n"; } my ($one, $length) = $row->dim; croak __PACKAGE__."::new_from_rows(): This isn't a row vector" unless ($one == 1) ; # if we already have a height, check that this is the same if ($cols) { croak __PACKAGE__."::new_from_rows(): This row vector has $length elements and an earlier one had $cols--all vectors must be the same size as the first one" unless ($length == $cols) ; } # else, we have a new width FIXME maybe this should check for zero else { $cols = $length; } # create the matrix the first time through unless ($matrix) { $matrix = $class->new($rows, $cols); } foreach my $col_index (1..$cols){ my $value = $row->element(1, $col_index); $matrix->assign($row_index, $col_index, $value); } } return $matrix; }