| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | 3 | my $dim = @{$vectors[0]}; | |||||||||||||||||||||||||||
| 82 | croak "Vectors must have same dimension for orthogonalization" | ||||||||||||||||||||||||||||
| 83 | 3 | ▶ | if grep {@$_ != $dim} @vectors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | 6 | my $dim = @{$vectors[0]}; | |||||||||||||||||||||||||||
| 107 | croak "Vectors must have same dimension for orthogonalization" | ||||||||||||||||||||||||||||
| 108 | 6 | ▶ | if grep {@$_ != $dim} @vectors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 143 | if not ref($v) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 144 | 9 | ▶ | croak "Cannot normalize 0-dimensional vectors" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
@_ == 2 and ref $_[1] eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 185 | if not @_ == 2; | ||||||||||||||||||||||||||||
| 186 | 17 | my ($v1, $v2) = @_; | |||||||||||||||||||||||||||
| 187 | 17 | ▶ | croak "Cannot compute the scalar product of vectors of different ", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | "dimensions" | ||||||||||||||||||||||||||||
| 189 | if @$v1 != @$v2; | ||||||||||||||||||||||||||||
| 190 | 14 | ▶ | croak "Cannot deal with 0-dimensional vectors" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | if @$v1 == 0; | ||||||||||||||||||||||||||||
| 192 | 13 | my $sum = $v1->[0] * $v2->[0]; | |||||||||||||||||||||||||||
| 193 | 13 | ▶ | return $sum if @$v1 == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 12 | $sum += $v1->[$_] * $v2->[$_] for 1..$#$v1; | |||||||||||||||||||||||||||
| 196 | 12 | return $sum; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 6 | 1; | |||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||