| 1 | package Array::Transpose; | ||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use base qw{Exporter}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 3 | our $VERSION = '0.07'; | |||||||||||
| 7 | 3 | our @EXPORT = qw(transpose); | |||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | Array::Transpose - Transposes a 2-Dimensional Array | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | use Array::Transpose; | ||||||||||||
| 16 | @array=transpose(\@array); | ||||||||||||
| 17 | |||||||||||||
| 18 | use Array::Transpose qw{}; | ||||||||||||
| 19 | @array=Array::Transpose::transpose(\@array); | ||||||||||||
| 20 | |||||||||||||
| 21 | Example: | ||||||||||||
| 22 | |||||||||||||
| 23 | use Array::Transpose; | ||||||||||||
| 24 | use Data::Dumper; | ||||||||||||
| 25 | my $array=transpose([ | ||||||||||||
| 26 | [ 0 .. 4 ], | ||||||||||||
| 27 | ["a" .. "e"], | ||||||||||||
| 28 | ]); | ||||||||||||
| 29 | print Data::Dumper->Dump([$array]); | ||||||||||||
| 30 | |||||||||||||
| 31 | Returns | ||||||||||||
| 32 | |||||||||||||
| 33 | $VAR1 = [ | ||||||||||||
| 34 | [ 0, 'a' ], | ||||||||||||
| 35 | [ 1, 'b' ], | ||||||||||||
| 36 | [ 2, 'c' ], | ||||||||||||
| 37 | [ 3, 'd' ], | ||||||||||||
| 38 | [ 4, 'e' ] | ||||||||||||
| 39 | ]; | ||||||||||||
| 40 | |||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||
| 42 | |||||||||||||
| 43 | This package exports one function named transpose. | ||||||||||||
| 44 | |||||||||||||
| 45 | In linear algebra, the transpose of a matrix A is another matrix A' created by any one of the following equivalent actions: | ||||||||||||
| 46 | |||||||||||||
| 47 | =over 2 | ||||||||||||
| 48 | |||||||||||||
| 49 | =item * | ||||||||||||
| 50 | |||||||||||||
| 51 | write the rows of A as the columns of A' | ||||||||||||
| 52 | |||||||||||||
| 53 | =item * | ||||||||||||
| 54 | |||||||||||||
| 55 | write the columns of A as the rows of A' | ||||||||||||
| 56 | |||||||||||||
| 57 | =item * | ||||||||||||
| 58 | |||||||||||||
| 59 | reflect A by its main diagonal (which starts from the top left) to obtain A' | ||||||||||||
| 60 | |||||||||||||
| 61 | =back | ||||||||||||
| 62 | |||||||||||||
| 63 | =head1 USAGE | ||||||||||||
| 64 | |||||||||||||
| 65 | use Array::Transpose; | ||||||||||||
| 66 | my @array = transpose(\@array); | ||||||||||||
| 67 | |||||||||||||
| 68 | =head1 METHODS | ||||||||||||
| 69 | |||||||||||||
| 70 | =head2 transpose | ||||||||||||
| 71 | |||||||||||||
| 72 | Returns a transposed 2-Dimensional Array given a 2-Dimensional Array | ||||||||||||
| 73 | |||||||||||||
| 74 | my $out = transpose($in); #$in=[[],[],[],...]; | ||||||||||||
| 75 | my @out = transpose(\@in); #@in=([],[],[],...); | ||||||||||||
| 76 | |||||||||||||
| 77 | =cut | ||||||||||||
| 78 | |||||||||||||
| 79 | 7 | ▶ | sub transpose { | ||||||||||
|
Subroutine
transpose: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||
| 80 | 7 | ▶ | die("Error: Expecting a single parameter") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | unless @_ == 1; | ||||||||||||
| 82 | 7 | my $in = shift; #[[],[],[]] | |||||||||||
| 83 | 7 | ▶ | die("Error: Expecting parameter to be an array reference") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | unless ref($in) eq "ARRAY"; | ||||||||||||
| 85 | 7 | my @out = (); | |||||||||||
| 86 | 7 | ▶ | if (@$in > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 5 | ▶ | my $cols = scalar(@{$in->[0]}) || 0; | ||||||||||
|
Condition50%1/2
scalar @{$in->[0];} || 0
MC/DC0%0/1
scalar @{$in->[0];} || 0
scalar @{$in->[0];}
Truth table50%1/2
A
A= scalar @{$in->[0];}
| |||||||||||||
| 88 | 5 | foreach my $col (0 .. $cols-1) { | |||||||||||
| 89 | 500016 | push @out, [map {$_->[$col]} @$in]; | |||||||||||
| 90 | } | ||||||||||||
| 91 | } | ||||||||||||
| 92 | 7 | ▶ | return wantarray ? @out : \@out | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | =head1 LIMITATIONS | ||||||||||||
| 96 | |||||||||||||
| 97 | The transpose function assumes all rows have the same number of columns as the first row. | ||||||||||||
| 98 | |||||||||||||
| 99 | =head1 AUTHOR | ||||||||||||
| 100 | |||||||||||||
| 101 | Michael R. Davis | ||||||||||||
| 102 | |||||||||||||
| 103 | =head1 COPYRIGHT | ||||||||||||
| 104 | |||||||||||||
| 105 | This program is free software licensed under the... | ||||||||||||
| 106 | |||||||||||||
| 107 | The BSD License | ||||||||||||
| 108 | |||||||||||||
| 109 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||||
| 110 | |||||||||||||
| 111 | =head1 SEE ALSO | ||||||||||||
| 112 | |||||||||||||
| 113 | =head2 Similar Capabilities | ||||||||||||
| 114 | |||||||||||||
| 115 | L<Math::MatrixReal> method transpose, L<Data::Table> rotate method | ||||||||||||
| 116 | |||||||||||||
| 117 | =head2 Packages built on top of this package | ||||||||||||
| 118 | |||||||||||||
| 119 | L<Array::Transpose::Ragged> | ||||||||||||
| 120 | |||||||||||||
| 121 | =cut | ||||||||||||
| 122 | |||||||||||||
| 123 | 3 | 1; | |||||||||||