blib/lib/Math/Orthonormalize.pm

filter: Statementstmt 100% 80 / 80 Branchbran 64.7% 22 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 88.0% 118 / 134 CC 24 SCAR 16.7
CC
24
Cov
88%
orthogonalize
8.1
scalar_product
6.0
scale
5.5
CRAP 5.3
↑ Summary
1
2 =head1 NAME
3
4 Math::Orthonormalize - Gram-Schmidt Orthonormalization of vectors
5
6 =head1 SYNOPSIS
7
8 use Math::Orthonormalize qw(:all);
9
10 my @base_of_r_2 = (
11 [2, 1],
12 [1, 3]
13 );
14 my $vector = [1, 2, 3];
15
16 my @orthonormalized = orthonormalize(@base_of_r_2);
17 my @orthogonalized = orthogonalize(@base_of_r_2);
18
19 my $normalized = normalize($vector);
20 my $scaled = scale(2, $vector);
21 my $scalar = scalar_product($vector1, $vector2);
22
23 =head1 DESCRIPTION
24
25 Math::Orthonormalize offers subroutines to compute normalized or
26 non-normalized orthogonal bases of Euclidean vector spaces. That means:
27 Given a vector base of R^n, it computes a new base of R^n whose individual
28 vectors are all orthogonal. If those new base vectors all have a length of
29 1, the base is orthonormalized.
30
31 The module uses the Gram-Schmidt Algorithm.
32
33 =head2 EXPORT
34
35 No subroutines are exported by default, but the standart Exporter semantics are
36 in place, including the ':all' tag that imports all of the exportable
37 subroutines which are listed below.
38
39 =cut
40
41 package Math::Orthonormalize;
42
43 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46
47 6 ▶use Math::Symbolic qw/parse_from_string/;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
49
50 6 our $VERSION = '1.00';
51
52 6 require Exporter;
53 6 our @ISA = qw(Exporter);
54 6 our %EXPORT_TAGS = ( 'all' => [ qw(
55 orthonormalize
56 orthogonalize
57 scalar_product
58 normalize
59 scale
60 ) ] );
61 6 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
62 6 our @EXPORT = ();
63
64 =head1 SUBROUTINES
65
66 =head2 orthonormalize
67
68 Takes any number (>1) of vectors (array refs of vector components) as argument
69 which form a base (that is, they are linearly independent) and returns
70 an orthogonalized and normalized base of the same vector space
71 (that is, n new array references).
72
73 =cut
74
75 3 ▶sub orthonormalize {
Subroutine orthonormalize: called CC=4 SCAR=14.0
Pod documented
76 3 my @vectors = @_;
77 3 ▶ croak "No arguments to orthogonalize"
Branch: 1 branch
BranchTrueFalseTotal
unless @vectors 0 3 3
78 if not @vectors;
79 croak "Arguments to orthogonalize must be array refs (vectors)"
80 3 ▶ if grep {ref($_) ne 'ARRAY'} @vectors;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @vectors 0 3 3
81 3 my $dim = @{$vectors[0]};
82 croak "Vectors must have same dimension for orthogonalization"
83 3 ▶ if grep {@$_ != $dim} @vectors;
Branch: 1 branch
BranchTrueFalseTotal
if grep {@$_ != $dim;} @vectors 0 3 3
84
85 3 my @base = orthogonalize(@vectors);
86 3 @base = map {normalize($_)} @base;
87 3 return @base;
88 }
89
90
91 =head2 orthogonalize
92
93 Takes any number (>1) of vectors (array refs of vector components) as argument
94 which form a base (that is, they are linearly independent) and returns
95 an orthogonalized base of the same vector space (that is, n new array
96 references).
97
98 =cut
99
100 6 ▶sub orthogonalize {
Subroutine orthogonalize: called CC=8 SCAR=21.0
Pod documented
101 6 my @vectors = @_;
102 6 ▶ croak "No arguments to orthogonalize"
Branch: 1 branch
BranchTrueFalseTotal
unless @vectors 0 6 6
103 if not @vectors;
104 croak "Arguments to orthogonalize must be array refs (vectors)"
105 6 ▶ if grep {ref($_) ne 'ARRAY'} @vectors;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @vectors 0 6 6
106 6 my $dim = @{$vectors[0]};
107 croak "Vectors must have same dimension for orthogonalization"
108 6 ▶ if grep {@$_ != $dim} @vectors;
Branch: 1 branch
BranchTrueFalseTotal
if grep {@$_ != $dim;} @vectors 0 6 6
109
110 6 my @newbase;
111 6 push @newbase, $vectors[0];
112 6 my @squares;
113 6 ▶ push @squares, scalar_product($vectors[0], $vectors[0]) if @vectors > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @vectors > 1 4 2 6
114 6 foreach my $i (1..$#vectors) {
115 4 my $new = $vectors[$i];
116 4 foreach my $j (0..$#newbase) {
117 4 my $vec = scale(
118 scalar_product($vectors[$i], $newbase[$j])
119 / $squares[$j],
120 $newbase[$j]
121 );
122 4 @$new = map {$_ -= shift @$vec} @$new;
123 }
124 4 push @newbase, $new;
125 4 ▶ push @squares, scalar_product($new, $new) if $i < $#vectors;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $#vectors 0 4 4
126 }
127 6 return @newbase;
128 }
129
130 =head2 normalize
131
132 Normalizes a vector. That is, it changes the vector length to 1 without
133 changing the vector's direction.
134
135 Takes an array reference with the vector components as argument and returns
136 a new array reference containing the normalized vector components.
137
138 =cut
139
140 9 ▶sub normalize {
Subroutine normalize: called CC=5 SCAR=16.2
Pod documented
141 9 my $v = shift;
142 9 ▶ croak "Argument to normalize() must be an array ref (vector)"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 9 9
143 if not ref($v) eq 'ARRAY';
144 9 ▶ croak "Cannot normalize 0-dimensional vectors"
Branch: 1 branch
BranchTrueFalseTotal
if @$v == 0 0 9 9
145 if @$v == 0;
146 9 my $sum = $v->[0]**2;
147 9 $sum += $v->[$_]**2 for 1..$#$v;
148 9 $sum = sqrt($sum);
149 9 ▶ croak "Cannot normalize 0-vector"
Branch: 1 branch
BranchTrueFalseTotal
if $sum == 0 1 8 9
150 if $sum == 0;
151 8 return [map {$_ / $sum} @$v];
152 }
153
154 =head2 scale
155
156 Takes a scalar and a vector (array reference of vector components) as
157 arguments. Multiplies every component of the vector by the specified
158 scalar and returns a new array reference containing the scaled vector
159 components.
160
161 =cut
162
163 8 ▶sub scale {
Subroutine scale: called CC=5 SCAR=17.0
Pod documented
164 8 ▶ croak "scale() takes a scalar and a vector (ary ref) as arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 and ref $_[1] eq "ARRAY" 0 8 8
Condition33%1/3
@_ == 2 and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "ARRAY"
@_ == 2 ref $_[1] eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 2B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
165 if not @_ == 2 or not ref($_[1]) eq 'ARRAY';
166 croak "Cannot handle 0-dimensional vectors"
167 8 ▶ if @{$_[1]} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[1];} == 0 0 8 8
168 8 my $new = [];
169 8 foreach (@{$_[1]}) {
170 19 push @$new, $_[0] * $_;
171 }
172 8 return $new;
173 }
174
175 =head2 scalar_product
176
177 Computes the scalar product of two vectors. Expects two array references
178 with vector components (same number of components) as argument and
179 returns their scalar product.
180
181 =cut
182
183 17 ▶sub scalar_product {
Subroutine scalar_product: called CC=6 SCAR=17.9
Pod documented
184 17 ▶ croak "Invalid number of arguments to scalar_product()"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 17 17
185 if not @_ == 2;
186 17 my ($v1, $v2) = @_;
187 17 ▶ croak "Cannot compute the scalar product of vectors of different ",
Branch: 1 branch
BranchTrueFalseTotal
if @$v1 != @$v2 3 14 17
188 "dimensions"
189 if @$v1 != @$v2;
190 14 ▶ croak "Cannot deal with 0-dimensional vectors"
Branch: 1 branch
BranchTrueFalseTotal
if @$v1 == 0 1 13 14
191 if @$v1 == 0;
192 13 my $sum = $v1->[0] * $v2->[0];
193 13 ▶ return $sum if @$v1 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$v1 == 1 1 12 13
194
195 12 $sum += $v1->[$_] * $v2->[$_] for 1..$#$v1;
196 12 return $sum;
197 }
198
199
200 6 1;
201 __END__
↑ Summary