| 1 | package Math::MatrixReal::Ext1; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Math::MatrixReal; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use base qw/Math::MatrixReal/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | sub new_from_cols { | ||||||||||||||||||||||||||
|
Subroutine
new_from_cols: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 10 | my $this = shift; | |||||||||||||||||||||||||||
| 13 | 10 | ▶ | my $extra_args = ( @_ > 1 && ref($_[-1]) eq 'HASH' ) ? pop : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[-1] eq "HASH"
@_ > 1
ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[-1] eq "HASH"
| |||||||||||||||||||||||||||||
| 14 | 10 | $extra_args->{_type} = 'column'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 10 | return $this->_new_from_rows_or_cols(@_, $extra_args ); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub new_from_columns { | ||||||||||||||||||||||||||
|
Subroutine
new_from_columns: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 4 | my $this = shift; | |||||||||||||||||||||||||||
| 20 | 4 | $this->new_from_cols(@_); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 8 | ▶ | sub new_from_rows { | ||||||||||||||||||||||||||
|
Subroutine
new_from_rows: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 8 | my $this = shift; | |||||||||||||||||||||||||||
| 24 | 8 | ▶ | my $extra_args = ( @_ > 1 && ref($_[-1]) eq 'HASH' ) ? pop : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ > 1 and ref $_[-1] eq "HASH"
MC/DC0%0/2
@_ > 1 and ref $_[-1] eq "HASH"
@_ > 1
ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[-1] eq "HASH"
| |||||||||||||||||||||||||||||
| 25 | 8 | $extra_args->{_type} = 'row'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | return $this->_new_from_rows_or_cols(@_, $extra_args ); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 18 | ▶ | sub _new_from_rows_or_cols { | ||||||||||||||||||||||||||
|
Subroutine
_new_from_rows_or_cols: called CC=19 SCAR=29.5
| |||||||||||||||||||||||||||||
| 31 | 18 | my $proto = shift; | |||||||||||||||||||||||||||
| 32 | 18 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 33 | 18 | my $ref_to_vectors = shift; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # these additional args are internal at the moment, | ||||||||||||||||||||||||||||
| 36 | # but in the future the user could pass e.g. {pad=>1} to | ||||||||||||||||||||||||||||
| 37 | # request padding | ||||||||||||||||||||||||||||
| 38 | 18 | my $args = pop; | |||||||||||||||||||||||||||
| 39 | 18 | my $vector_type = $args->{_type}; | |||||||||||||||||||||||||||
| 40 | 18 | ▶ | die "Internal ".__PACKAGE__." error" unless $vector_type =~ /^(row|column)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # step back one frame because this private method is | ||||||||||||||||||||||||||||
| 43 | # not how the user called it | ||||||||||||||||||||||||||||
| 44 | 18 | my $caller_subname = (caller(1))[3]; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # note--this die() could be inconvenient if someone had something | ||||||||||||||||||||||||||||
| 47 | # really fancy that knew how to be dereffed as an array | ||||||||||||||||||||||||||||
| 48 | # (can you do that with a tied scalar?), but I'm not putting | ||||||||||||||||||||||||||||
| 49 | # the rest of the world through an eval--they can just | ||||||||||||||||||||||||||||
| 50 | # deref and pass a reference themselves. If that ever happens | ||||||||||||||||||||||||||||
| 51 | # we can add an arg to skip this check | ||||||||||||||||||||||||||||
| 52 | 18 | ▶ | croak "$caller_subname: need a reference to an array of ${vector_type}s" unless ref($ref_to_vectors) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 16 | my @vectors = @{$ref_to_vectors}; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 16 | my $matrix; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 16 | my $other_type = {row=>'column', column=>'row'}->{$vector_type}; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 16 | my %matrix_dim = ( | |||||||||||||||||||||||||||
| 60 | $vector_type => scalar( @vectors ), | ||||||||||||||||||||||||||||
| 61 | $other_type => 0, # we will correct this in a bit | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # row and column indices are one based | ||||||||||||||||||||||||||||
| 65 | 16 | my $current_vector_count = 1; | |||||||||||||||||||||||||||
| 66 | 16 | foreach my $current_vector (@vectors) { | |||||||||||||||||||||||||||
| 67 | # dimension is one-based, so we're | ||||||||||||||||||||||||||||
| 68 | # starting with one here and incrementing | ||||||||||||||||||||||||||||
| 69 | # as we go. The other dimension is fixed (for now, until | ||||||||||||||||||||||||||||
| 70 | # we add the 'pad' option), and gets set later | ||||||||||||||||||||||||||||
| 71 | 39 | my $ref = ref( $current_vector ) ; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 39 | ▶ | if ( $ref eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 74 | # we hope this is a properly formatted Math::MatrixReal string, | ||||||||||||||||||||||||||||
| 75 | # but if not we just let the Math::MatrixReal die() do it's | ||||||||||||||||||||||||||||
| 76 | # thing | ||||||||||||||||||||||||||||
| 77 | 9 | $current_vector = $class->new_from_string( $current_vector ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | elsif ( $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 80 | 12 | my @array = @$current_vector; | |||||||||||||||||||||||||||
| 81 | 12 | ▶ | croak "$caller_subname: one $vector_type you gave me was a ref to an array with no elements" unless @array ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # we need to create the right kind of string based on whether | ||||||||||||||||||||||||||||
| 83 | # they said they were sending us rows or columns: | ||||||||||||||||||||||||||||
| 84 | 11 | ▶ | if ($vector_type eq 'row') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 4 | $current_vector = $class->new_from_string( '[ '. join( " ", @array) ." ]\n" ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 7 | $current_vector = $class->new_from_string( '[ '. join( " ]\n[ ", @array) ." ]\n" ); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | ▶ | elsif ( $ref ne 'HASH' and $current_vector->isa('Math::MatrixReal') ) { | |||||||||||||||||||||||||||
|
Condition66%2/3
$ref ne "HASH" and $current_vector->isa("Math::MatrixReal")
MC/DC50%1/2
$ref ne "HASH" and $current_vector->isa("Math::MatrixReal")
$ref ne "HASH"
$current_vector->isa("Math::MatrixReal")
Truth table66%2/3
A and B
A= $ref ne "HASH"B= $current_vector->isa("Math::MatrixReal")
| |||||||||||||||||||||||||||||
| 92 | # it's already a Math::MatrixReal something. | ||||||||||||||||||||||||||||
| 93 | # we don't need to do anything, it will all | ||||||||||||||||||||||||||||
| 94 | # work out | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | # we have no idea, error time! | ||||||||||||||||||||||||||||
| 98 | 1 | croak "$caller_subname: I only know how to deal with array refs, strings, and things that inherit from Math::MatrixReal\n"; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # starting now we know $current_vector isa Math::MatrixReal thingy | ||||||||||||||||||||||||||||
| 102 | 36 | my @vector_dims = $current_vector->dim; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #die unless the appropriate dimension is 1 | ||||||||||||||||||||||||||||
| 105 | 36 | ▶ | croak "$caller_subname: I don't accept $other_type vectors" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 106 | unless ($vector_dims[ $vector_type eq 'row' ? 0 : 1 ] == 1) ; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # the other dimension is the length of our vector | ||||||||||||||||||||||||||||
| 109 | 34 | ▶ | my $length = $vector_dims[ $vector_type eq 'row' ? 1 : 0 ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # set the "other" dimension to the length of this | ||||||||||||||||||||||||||||
| 112 | # vector the first time through | ||||||||||||||||||||||||||||
| 113 | 34 | ▶ | $matrix_dim{$other_type} ||= $length; | ||||||||||||||||||||||||||
|
Condition66%2/3
$matrix_dim{$other_type} ||= $length
MC/DC0%0/2
$matrix_dim{$other_type} ||= $length
$matrix_dim{$other_type}
$length
Truth table66%2/3
A ||= B
A= $matrix_dim{$other_type}B= $length
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # die unless length of this vector matches the first length | ||||||||||||||||||||||||||||
| 116 | croak "$caller_subname: one $vector_type has [$length] elements and another one had [$matrix_dim{$other_type}]--all of the ${vector_type}s passed in must have the same dimension" | ||||||||||||||||||||||||||||
| 117 | 34 | ▶ | unless ($length == $matrix_dim{$other_type}) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # create the matrix the first time through | ||||||||||||||||||||||||||||
| 120 | 32 | ▶ | $matrix ||= $class->new($matrix_dim{row}, $matrix_dim{column}); | ||||||||||||||||||||||||||
|
Condition66%2/3
$matrix ||= $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
MC/DC0%0/2
$matrix ||= $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
$matrix
$class->new($matrix_dim{"row"}, $matrix_dim{"column"})
Truth table66%2/3
A ||= B
A= $matrixB= $class->new($matrix_dim{"row"}, $matrix_dim{"column"})
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # step along the vector assigning the value of each element | ||||||||||||||||||||||||||||
| 123 | # to the correct place in the matrix we're building | ||||||||||||||||||||||||||||
| 124 | 32 | foreach my $element_index ( 1..$length ){ | |||||||||||||||||||||||||||
| 125 | # args for vector assignment: | ||||||||||||||||||||||||||||
| 126 | # initialize both to one and reset the correct | ||||||||||||||||||||||||||||
| 127 | # one below | ||||||||||||||||||||||||||||
| 128 | 92 | my ($v_r, $v_c) = (1,1); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # args for matrix assignment | ||||||||||||||||||||||||||||
| 131 | 92 | my ($row_index, $column_index, $value); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 92 | ▶ | if ($vector_type eq 'row') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 57 | $row_index = $current_vector_count; | |||||||||||||||||||||||||||
| 135 | 57 | $v_c = $column_index = $element_index; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 35 | $v_r = $row_index = $element_index; | |||||||||||||||||||||||||||
| 139 | 35 | $column_index = $current_vector_count; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 92 | $value = $current_vector->element($v_r, $v_c); | |||||||||||||||||||||||||||
| 142 | 92 | $matrix->assign($row_index, $column_index, $value); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 32 | $current_vector_count ++ ; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 9 | return $matrix; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||