blib/lib/Math/VectorReal.pm

filter: Statementstmt 60.4% 113 / 187 Branchbran 28.5% 20 / 70 Conditioncond 26.0% 12 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 63.8% 23 / 36 Podpod 77.7% 14 / 18 totaltotal 47.6% 182 / 382 CC 58 SCAR 24.8
CC
58
Cov
48%
_trace
45.5
matrix_row2vector
30.0
matrix_col2vector
30.0
CRAP 11.9
« blib/lib/Chemistry/File/SDF.pm ↑ Summary blib/lib/Chemistry/File.pm »
1 #
2 # Math::VectorReal Vector Mathematics
3 #
4 #
5 # Copyright (c) 2001 Anthony Thyssen. All rights reserved. This program
6 # is free software; you can redistribute it and/or modify it under the
7 # same terms as Perl itself.
8 #
9 package Math::VectorReal;
10  
11 =head1 NAME
12  
13 Math::VectorReal - Module to handle 3D Vector Mathematics
14  
15 =head1 SYNOPSIS
16  
17 #!/usr/bin/perl
18 use Math::VectorReal;
19  
20 $a = vector( 1, 2, .5 );
21 print "Vector as string (MatrixReal default format)\n\$a => ", $a;
22  
23 print $a->stringify("Formated Output \$a => { %g, %g, %g }\n");
24  
25 # I hate newline in the default output format (defined as MatrixReal)
26 $Math::VectorReal::FORMAT = "[ %.5f %.5f %.5f ]";
27 print "Modified default output format \$a => $a\n";
28  
29 print 'length => ', $a->length, "\n";
30 print 'normalised => ', $a->norm, "\n";
31  
32 use Math::VectorReal qw(:all); # Include O X Y Z axis constant vectors
33 print 'string concat $a."**" = ', $a."**", "\n";
34 print 'vector constant X = ', X, "\n";
35 print 'subtraction $a - Z = ', $a - Z, "\n";
36 print 'scalar divide $a / 3 = ', $a / 3, "\n";
37 print 'dot product $a . Y = ', $a . Y, "\n";
38 print 'cross product $a x Y = ', $a x Y, "\n";
39  
40 print "Plane containing points X, \$a, Z (in anti-clockwise order)\n";
41 ($n,$d) = plane( X, $a, Z ); # return normal and disance from O
42 print ' normal = $n = ', $n, "\n";
43 print ' disance from O = $d = ', $d, "\n";
44 print ' Y axis intersect = $d/($n.Y) = ', $d/($n.Y), "\n";
45  
46 print "VectorReal and MatrixReal interaction\n\n";
47 use Math::MatrixReal; # Not required for pure vector math as above
48  
49 $r = $a->vector2matrix_row; # convert to MatrixReal Row Vector
50 $c = $a->vector2matrix_col; # convert to MatrixReal Column Vector
51 print 'Vector as a MatrixReal Row $r (vector -> matrix) => ', "\n", $r;
52 print 'Vector as a MatrixReal Col $c (vector -> matrix) => ', "\n", $c;
53  
54 $nx = $a->norm; $ny = $nx x Z; $nz = $nx x $ny; # orthogonal vectors
55 $R = vector_matrix( $nx, $ny, $nz ); # make the rotation matrix
56 print 'Rotation Matrix from 3 Vectors $R => ',"\n", $R, "\n";
57  
58 print "Extract the Y row from the matrix as a VectorReal..\n";
59 print '$R->matrix_row2vector(1) => ', $R->matrix_row2vector(1), "\n";
60  
61 print "Rotate a vector with above rotation matrix\n";
62 print '$a * $R (vector -> vector)',"\n", $a * $R, "\n";
63  
64 print "Rotate a MatrixReal column (post multiply)...\n";
65 print "(NB: matrix must be transposed (~) to match column format)\n";
66 print '~$R * $c (col_matrix -> col_matrix) =>',"\n", ~$R * $c, "\n";
67  
68 =head1 DESCRIPTION
69  
70 The C<Math::VectorReal> package defines a 3D mathematical "vector", in a way
71 that is compatible with the previous CPAN module C<Math::MatrixReal>. However
72 it provides a more vector oriented set of mathematical functions and overload
73 operators, to the C<MatrixReal> package. For example the normal perl string
74 functions "x" and "." have been overloaded to allow vector cross and dot
75 product operations. Vector math formula thus looks like vector math formula in
76 perl programs using this package.
77  
78 While this package is compatible with Math::MatrixReal, you DO NOT need to
79 have that package to perform purely vector orientated calculations. You will
80 need it however if you wish to do matrix operations with these vectors. The
81 interface has been designed with this package flexibility in mind.
82  
83 The vectors are defined in the same way as a "row" C<Math::MatrixReal> matrix,
84 instead of that packages choice of "column" definition for vector operations.
85 Such vectors are multiplied to matices with the vector on the left and the
86 matrix on the right. EG: v * M -> 'v
87  
88 Not only is this the way I prefer to handle vectors, but it is the way most
89 graphics books use vectors. As a bonus it results in no overload conflicts
90 between this package and that of Math::MatrixReal, (the left objects overload
91 operator is called to do the mathematics). It also is a lot simpler than
92 C<MatrixReal> column vector methods, which were designed for equation solving
93 rather than 3D geometry operations.
94  
95 The vector_matrix() function provided, simplifies the creation a
96 C<MatrixReal> object from 3 (usually orthogonal) vectors. This with its vector
97 orientated math operators makes it very easy to define orthogonal rotation
98 matrices from C<Math::VectorReal> objects. See a rough example in the
99 synopsis above, or in the file "matrix_test" in the packages source.
100  
101 NOTE: the 6th element the C<Math::MatrixReal> array object is used to hold the
102 length of the vector so that it can be re-used without needing to be
103 re-calculated all the time. This means the expensive sqrt() function, need not
104 be called unless nessary. This usage should not effect the direct use of
105 these objects in the C<Math::MatrixReal> functions.
106  
107 =cut
108  
109 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 #require Math::MatrixReal; # not required!
111  
112 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 27 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 27 require Exporter;
115  
116 27 @ISA = qw(Exporter);
117  
118 27 @EXPORT = qw( vector plane vector_matrix );
119  
120 27 @EXPORT_OK = qw( O X Y Z );
121  
122 27 %EXPORT_TAGS = (
123 axis => [ qw( O X Y Z ) ], # Unix Axis Vector Constants
124 all => [@EXPORT, @EXPORT_OK]
125 );
126  
127 27 $VERSION = '1.0';
128  
129 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 27 ▶use vars qw( $FORMAT $TRACE );
Subroutine BEGIN: called CC=1 SCAR=0.0
131 27 $TRACE = 0;
132 27 $FORMAT = "[ %#19.12E %#19.12E %#19.12E ]\n"; # output format (as MatrixReal)
133  
134 =head1 CONSTANTS
135  
136 Four constant vectors are available for export (using an ":all" tag).
137 these are
138  
139 0 = [ 0 0 0 ] the zero vector or origin
140 X = [ 1 0 0 ] |
141 Y = [ 0 1 0 ] > Unit axis vectors
142 Z = [ 0 0 1 ] |
143  
144 =cut
145  
146 # Constant Vector Functions
147 # The format is as per a Math::MatrixReal object, with extra length item
148 288 ▶sub O() { bless [ [[0,0,0]], 1,3, undef,undef,undef, 0 ], __PACKAGE__; }
Subroutine O: called CC=1 SCAR=0.0
Pod undocumented
149 0 ▶sub X() { bless [ [[1,0,0]], 1,3, undef,undef,undef, 1 ], __PACKAGE__; }
Subroutine X: not called CC=1 SCAR=6.9
Pod undocumented
150 16 ▶sub Y() { bless [ [[0,1,0]], 1,3, undef,undef,undef, 1 ], __PACKAGE__; }
Subroutine Y: called CC=1 SCAR=0.0
Pod undocumented
151 0 ▶sub Z() { bless [ [[0,0,1]], 1,3, undef,undef,undef, 1 ], __PACKAGE__; }
Subroutine Z: not called CC=1 SCAR=6.9
Pod undocumented
152  
153 =head1 CONSTRUCTORS
154  
155 =over 4
156  
157 =item new(x,y,z)
158  
159 Create a new vector with the values of C<x>, C<y>, C<z> returning the
160 appropriate object.
161  
162 =item vector(x,y,z)
163  
164 As C<new> but is a exported function which does not require a package
165 reference to create a C<Math::VectorReal> object.
166  
167 =item clone()
168  
169 Return a completely new copy of the referring C<Math::VectorReal> object.
170  
171 =cut
172  
173 4841 ▶sub new { # typical object creation (not many checks)
Subroutine new: called CC=3 SCAR=11.4
Pod documented
174 4841 ▶ croak "Usage: \$vector = ".__PACKAGE__."->new(x,y,z);\n" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4841 4841
175 4841 my $ref = shift;
176 4841 ▶ return bless [ [[ @_ ]], 1,3 ], ref $ref || $ref;
Condition66%2/3
ref $ref || $ref
l!l&&r!l&&!r
41466950
MC/DC0%0/2
ref $ref || $ref
ref $ref $ref
Truth table66%2/3
A || B
A= ref $refB= $ref
ABresult
1X1
011
000
177 }
178  
179  
180 695 ▶sub vector { # normal way to create a vector - Exported function
Subroutine vector: called CC=5 SCAR=20.9
Pod documented
181 # This works as both a Object Method or Exported Function
182 695 ▶ croak "Usage: \$vector = ".__PACKAGE__."->vector(x,y,z);\n".
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 or @_ == 4 and ref $_[0] 0 695 695
Condition16%1/6
@_ == 3 or @_ == 4 and ref $_[0]
l!l&&r!l&&!r
69500
@_ == 4 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $_[0]
@_ == 3 @_ == 4 ref $_[0]
Truth table25%1/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $_[0]
ABCresult
1XX1
0111
00X0
0100
183 " or \$vector = vector(x,y,z);\n"
184 unless @_ == 3 || @_ == 4 && ref $_[0];
185 695 my $class = __PACKAGE__;
186 695 ▶ $class = ref shift if @_ == 4;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 4 0 695 695
187 695 return $class->new(@_);
188 }
189  
190 3319 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
191 3319 ▶ croak "Usage: \$vector_copy = \$vector->clone;\n" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 3319 3319
192 3319 my $v = shift;
193 3319 my $c = $v->new( $v->array ); # create a new vector using values
194 3319 ▶ $c->[6] = $v->[6] if defined $v->[6]; # also note its length (if known)
Branch: 1 branch
BranchTrueFalseTotal
if defined $v->[6] 1009 2310 3319
195 3319 return $c;
196 }
197  
198 =head1 METHODS
199  
200 =item array()
201  
202 Return the x,y,z elements of the referring vector are an array of values.
203  
204 =item x()
205  
206 Return the x element of the referring vector.
207  
208 =item y()
209  
210 Return the y element of the referring vector.
211  
212 =item z()
213  
214 Return the z element of the referring vector.
215  
216 =item stringify( [ FORMAT ] )
217  
218 Return the referring verctor as a string. The C<FORMAT> if given is used
219 to sprintf format the vector. This is used for all VectorReal to String
220 conversions.
221  
222 By default this format is the same as it would be for a C<Math::MatrixReal>
223 object, "[ %#19.12E %#19.12E %#19.12E ]\n". Note that this includes a newline
224 character!.
225  
226 However unlike C<Math::MatrixReal> you can assign a new default sprintf
227 format by assigning it to the packages C<$FORMAT> variable. For Example
228  
229 $Math::VectorReal::FORMAT = "{ %g, %g, %g }"
230  
231 Which is a good format to output vectors for use by the POVray (Persistance of
232 Vision Raytracer) program.
233  
234 =item length()
235  
236 Return the length of the given vector. As a side effect the length is saved
237 into that vectors object to avoid the use of the expensive sqrt() function.
238  
239 =item norm()
240  
241 Normalise the Vector. That is scalar divide the vector by its length, so that
242 it becomes of length one. Normal vectors are commonly use to define
243 directions, without scale, or orientation of a 3 dimensional plane.
244  
245 =cut
246  
247 4988 ▶sub array { # return vector as an array of values
Subroutine array: called CC=1 SCAR=0.0
Pod documented
248 4988 my $v = shift;
249 4988 return @{$v->[0][0]};
250 }
251  
252 0 ▶sub x {
Subroutine x: not called CC=1 SCAR=6.9
Pod documented
253 0 my $v = shift;
254 0 return ($v->array)[0];
255 }
256  
257 0 ▶sub y {
Subroutine y: not called CC=1 SCAR=6.9
Pod documented
258 0 my $v = shift;
259 0 return ($v->array)[1];
260 }
261  
262 0 ▶sub z {
Subroutine z: not called CC=1 SCAR=6.9
Pod documented
263 0 my $v = shift;
264 0 return ($v->array)[2];
265 }
266  
267 0 ▶sub stringify { # convert a vector to a string (with optional format)
Subroutine stringify: not called CC=2 SCAR=17.9
Pod documented
268 0 my( $v, $fmt ) = @_;
269 0 ▶ $fmt = $FORMAT unless defined $fmt; # if not given use current default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fmt 0 0 0
270 0 return sprintf $fmt, $v->array;
271 }
272  
273 1511 ▶sub length { # convert a vector to a string
Subroutine length: called CC=2 SCAR=6.9
Pod documented
274 1511 my $v = shift;
275 1511 ▶ return $v->[6] if defined $v->[6];
Branch: 1 branch
BranchTrueFalseTotal
if defined $v->[6] 83 1428 1511
276 1428 return $v->[6] = sqrt( $v.$v );
277 }
278  
279 348 ▶sub norm { # scale vector to a length of one
Subroutine norm: called CC=1 SCAR=0.0
Pod documented
280 348 my $v = shift;
281 348 return $v / $v->length;
282 }
283  
284 =item plane( v1, v2, v3 )
285  
286 Given three points defined counter clockwise on a plane, return an array in
287 which the first element is the planes normal unit vector, and the second its
288 distance from the origin, along that vector. NOTE: the distance may be
289 negitive, in which case the origon is above the defined plane in 3d space.
290  
291 =cut
292  
293 0 ▶sub plane { # Given three points on the plane (right-hand rule)
Subroutine plane: not called CC=2 SCAR=17.9
Pod documented
294 # return a normal vector and distance from origin for a plane
295 0 ▶ croak "Usage: (\$normal, \$distance) = plane(\$p1,\$p2,\$p3);\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 0 0
296 unless @_ == 3;
297 0 my ($a, $b, $c) = @_;
298 0 my $normal = (($b - $a) x ($c - $b))->norm;
299 0 return ( $normal, $a . $normal );
300 }
301  
302 =item vector_matrix( nx, ny, nz )
303  
304 Given the new location for the X, Y and Z vectors, concatanate them together
305 (row wise) to create a C<Math::MatrixReal> translation matrix. For example
306 if the 3 vectors are othogonal to each other, the matrix created will be
307 a rotation matrix to rotate the X, Y and Z axis to the given vectors. See
308 above for an example.
309  
310 =cut
311  
312 0 ▶sub vector_matrix {
Subroutine vector_matrix: not called CC=1 SCAR=6.9
Pod documented
313 0 my( $nx, $ny, $nz ) = @_;
314 0 bless [ [[$nx->array],
315 [$ny->array],
316 [$nz->array]], 3, 3 ], "Math::MatrixReal";
317 }
318  
319 # ------------------------------------------------------------------
320 # Convertsions between Math::MatrixReal and Math::VectorReal packages
321  
322 =back
323  
324 =head1 VECTOR/MATRIX CONVERSION
325  
326 The following functions provide links between the C<Math::VectorReal> and
327 C<Math::MatrixReal> packages.
328  
329 NOTE: While this package is closely related to C<Math::MatrixReal>, it does
330 NOT require that that package to be installed unless you actually want to
331 perform matrix operations.
332  
333 Also the overload operations will automatically handle vector/matrix
334 mathematics (See below).
335  
336 =head2 Vector to Matrix Conversion
337  
338 =over 4
339  
340 =item vector2matrix_row( [CLASS] )
341  
342 =item vector2matrix_col( [CLASS] )
343  
344 Convert C<Math::VectorReal> objects to a C<Math::MatrixReal> objects.
345 Optional argument defines the object class to be returned (defaults to
346 C<Math::MatrixReal>).
347  
348 Note that as a C<Math::VectorReal> is internally equivelent to a
349 C<Math::MatrixReal> row matrix, C<vector2matrix_row> is essentually just a
350 bless operation, which is NOT required to use with C<Math::MatrixReal>
351 functions.
352  
353 The C<vector2matrix_col> performs the required transpose to convert the
354 C<Math::VectorReal> object into a C<Math::MatrixReal> version of a vector (a
355 column matrix).
356  
357 =cut
358  
359 0 ▶sub vector2matrix_row {
Subroutine vector2matrix_row: not called CC=3 SCAR=24.8
Pod documented
360 0 my( $v, $ref ) = @_;
361 0 ▶ $ref ||= "Math::MatrixReal";
Condition0%0/2
$ref ||= "Math::MatrixReal"
l!l
00
MC/DC0%0/1
$ref ||= "Math::MatrixReal"
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
362 0 ▶ bless $v->clone, ref $ref || $ref; # clone and bless (object unchanged)
Condition0%0/3
ref $ref || $ref
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $ref || $ref
ref $ref $ref
Truth table0%0/3
A || B
A= ref $refB= $ref
ABresult
1X1
011
000
363 }
364  
365 0 ▶sub vector2matrix_col {
Subroutine vector2matrix_col: not called CC=3 SCAR=24.8
Pod documented
366 0 my( $v, $ref ) = @_;
367 0 ▶ $ref ||= "Math::MatrixReal";
Condition0%0/2
$ref ||= "Math::MatrixReal"
l!l
00
MC/DC0%0/1
$ref ||= "Math::MatrixReal"
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
368 0 my @v = $v->array;
369 0 ▶ bless [ [[$v[0]],[$v[1]],[$v[2]]], 3, 1 ], ref $ref || $ref;
Condition0%0/3
ref $ref || $ref
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $ref || $ref
ref $ref $ref
Truth table0%0/3
A || B
A= ref $refB= $ref
ABresult
1X1
011
000
370 }
371  
372 =head2 Matrix to Vector Conversion
373  
374 =item matrix_row2vector( [ROW] )
375  
376 =item matrix_col2vector( [COLUMN] )
377  
378 When referred to by a C<Math::MatrixReal> object, extracts the vector
379 from the matrix. the optional argument defines which row or column of the
380 matrix is to be extracted as a C<Math::VectorReal> vector.
381  
382 =cut
383  
384 { # Enclose MartixReal package in a block
385 package Math::MatrixReal; # Fake a change into the Math::MatrixReal package
386 27 ▶use Carp; # import carp into this package
Subroutine BEGIN: called CC=1 SCAR=0.0
387  
388 0 ▶sub matrix_row2vector {
Subroutine matrix_row2vector: not called CC=5 SCAR=34.0
389 0 my $m = shift; my($rows,$cols) = ($m->[1],$m->[2]);
390 0 my $r = shift; # optional, which column from matrix
391 0 ▶ croak "Error: matrix does not have 3D rows" unless ($cols == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless $cols == 3 0 0 0
392 0 ▶ if ( defined $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
393 0 ▶ croak "Error: matrix does not have that row" unless ( $r < $rows);
Branch: 1 branch
BranchTrueFalseTotal
unless $r < $rows 0 0 0
394 }
395 else { # if no option, it must be a Math::MatrixReal Row Vector
396 0 ▶ croak "Error: matrix given to matrix_row2vector is not a 3D row matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == 1 0 0 0
397 unless ($rows == 1);
398 0 $r = 0;
399 }
400 0 return Math::VectorReal->new(@{$m->[0][$r]}); # same result, only cleaned up
401 }
402  
403 0 ▶sub matrix_col2vector {
Subroutine matrix_col2vector: not called CC=5 SCAR=34.0
404 0 my $m = shift; my($rows,$cols) = ($m->[1],$m->[2]);
405 0 my $c = shift; # optional, which column from matrix
406 0 ▶ croak "Error: matrix does not have 3D rows" unless ($rows == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless $rows == 3 0 0 0
407 0 ▶ if ( defined $c ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c) { } 0 0 0
408 0 ▶ croak "Error: matrix does not have that column" unless ( $c < $cols);
Branch: 1 branch
BranchTrueFalseTotal
unless $c < $cols 0 0 0
409 }
410 else { # if no option, it must be a Math::MatrixReal Column Vector
411 0 ▶ croak "Error: matrix given to matrix_col2vector is not a 3D column matrix"
Branch: 1 branch
BranchTrueFalseTotal
unless $cols == 1 0 0 0
412 unless ($cols == 1);
413 0 $c = 0;
414 }
415 0 return Math::VectorReal->new($m->[0][0][$c], $m->[0][1][$c], $m->[0][2][$c]);
416 }
417  
418 } # Return to the Math::VectorReal package we are really defining
419  
420 # ------------------------------------------------------------------
421 # Overloaded Math functions
422  
423 =back
424  
425 =head1 OPERATOR OVERLOADING
426  
427 Overload operations are provided to perform the usual string conversion,
428 addition, subtraction, unary minus, scalar multiplation & division. On top of
429 this however the multiply have been expanded to look for and execute
430 C<MatrixReal> multiplation.
431  
432 The Main purpose of this package however was to provide the special vector
433 product operations: dot product "." and cross product "x". In perl these
434 operations are normally used for string operations, but if either argument
435 is a C<VectorReal> object, the operation will attempt the approprate
436 vector math operation instead.
437  
438 Note however that if one side of the dot "." operator is already a string,
439 then the vector will be converted to a sting and a string concatantion will be
440 performed. The cross operator "x" will just croak() as it is non-sensical to
441 either repeat the string conversion of a vector, OR to repeat a string,
442 vector, times!
443  
444 Overloaded operator summery...
445 neg unary minus - multiply vector by -1
446 "" automatic string conversion using stringify() function
447 + vector addition
448 - vector subtraction
449 / scalar division (left argument must be the vector)
450 * scalar multiplication OR MatrixReal multiplication
451 x vector/cross product of two vectors
452 . dot product of two vectors OR vector/string concatanation
453  
454 Posible future addition '~' to transpose a C<VectorReal> into a
455 C<MatrixReal> column vector (as per that operator on C<MatrixReal> objects).
456 It was not added as it just did not seem to be needed.
457  
458 =cut
459  
460 use overload
461 27 ▶ 'neg' => \&_negate,
Subroutine BEGIN: called CC=1 SCAR=0.0
462 '""' => \&_stringify,
463 '+' => \&_addition,
464 '-' => \&_subtract,
465 '*' => \&_multiply,
466 '/' => \&_scalar_divide,
467 'x' => \&_cross_product, # Redefination of the string function
468 '.' => \&_dot_product, # These includes stingify/concatanation
469 27 'fallback' => undef;
470  
471  
472 6338 ▶sub _trace {
Subroutine _trace: called CC=7 SCAR=38.2
473 6338 ▶ return unless $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Math::VectorReal::TRACE 6338 0 6338
474 0 my($text,$object,$argument,$flip) = @_;
475 0 ▶ unless (defined $object) { $object = 'undef'; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 0 0 0
476 0 ▶ unless (defined $argument) { $argument = 'undef'; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $argument) 0 0 0
477 0 ▶ unless (defined $flip) { $flip = 'undef'; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flip) 0 0 0
478 0 ▶ if (ref($object)) { $object = ref($object); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object) 0 0 0
479 0 ▶ if (ref($argument)) { $argument = ref($argument); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $argument) 0 0 0
480 0 $argument =~ s/\n/\\n/g;
481 0 print "$text: \$obj='$object' \$arg='$argument' \$flip='$flip'\n";
482 }
483  
484  
485 83 ▶sub _negate {
Subroutine _negate: called CC=2 SCAR=6.9
486 83 my($object,$argument,$flip) = @_;
487 83 _trace("'neg'",$object,$argument,$flip);
488 83 my $v = $object->clone;
489 83 for ( 0 .. 2 ) { $v->[0][0][$_] = -$v->[0][0][$_]; }
490 # $v->[6]; does not change.
491 83 return $v
492 }
493  
494 0 ▶sub _stringify {
Subroutine _stringify: not called CC=1 SCAR=6.9
495 0 my($object,$argument,$flip) = @_;
496 0 _trace("'\"\"'",$object,$argument,$flip);
497 0 return $object->stringify;
498 }
499  
500  
501 281 ▶sub _addition {
Subroutine _addition: called CC=5 SCAR=17.8
502 # Operation on two vectors, as such $flip will be undefined or false
503 # The operation is also communitive - order does not matter.
504 281 my($object,$argument,$flip) = @_;
505 281 _trace("'+'",$object,$argument,$flip);
506 281 ▶ if ( (defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) 281 0 281
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
00281
defined $argument and ref $argument
!ll&&!rl&&r
00281
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
507 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) {
508 281 my $v = $object->clone;
509 281 for ( 0 .. 2 ) { $v->[0][0][$_] += $argument->[0][0][$_]; }
510 281 $#{$v} = 2; # any cached vector length is now invalid
511 281 return $v;
512 }
513 0 croak("non-vector argument for '+'");
514 }
515  
516  
517 2128 ▶sub _subtract {
Subroutine _subtract: called CC=5 SCAR=17.8
518 2128 my($object,$argument,$flip) = @_;
519 2128 _trace("'-'",$object,$argument,$flip);
520 # Operation on two vectors, as such $flip will be undefined or false
521 # Note; however this is not communitive - order matters
522 2128 ▶ if ( (defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) 2128 0 2128
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
002128
defined $argument and ref $argument
!ll&&!rl&&r
002128
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
523 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) {
524 2128 my $v = $object->clone;
525 2128 for ( 0 .. 2 ) { $v->[0][0][$_] -= $argument->[0][0][$_]; }
526 2128 $#{$v} = 2; # any cached vector length is now invalid
527 2128 return $v;
528 }
529 0 croak("non-vector argument for '-'");
530 }
531  
532  
533 479 ▶sub _multiply {
Subroutine _multiply: called CC=6 SCAR=21.2
534 479 my($object,$argument,$flip) = @_;
535 479 _trace("'*'",$object,$argument,$flip);
536 479 ▶ if ( ref($argument) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $argument) { } 0 479 479
elsif (defined $argument) { } 479 0 479
537 # Assume multiply by Math::MatrixReal object EG: $v * $M --> $new_v
538 # Order is communicative, but $flip should NOT be true
539 0 ▶ if ( ! $flip ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $flip) { } 0 0 0
540 0 return ( $object->vector2matrix_row($argument)
541 * $argument )->matrix_row2vector;
542 } else { # just in case flip is true..
543 0 return ( $argument *
544 $object->vector2matrix_row($argument) )->matrix_row2vector;
545 }
546 }
547 elsif ( defined $argument ) {
548 # defined $argument must be a scalar, so Scalar Multiply
549 # Communitive - order does not matter, $flip can be ignored
550 479 my $v = $object->clone;
551 479 for ( 0 .. 2 ) { $v->[0][0][$_] *= $argument; }
552 479 ▶ $v->[6] *= abs($argument) if defined $v->[6]; # multiply vector length
Branch: 1 branch
BranchTrueFalseTotal
if defined $v->[6] 396 83 479
553 479 return $v;
554 }
555 0 croak("undefined argument given for vector multiply");
556 }
557  
558  
559 348 ▶sub _scalar_divide {
Subroutine _scalar_divide: called CC=6 SCAR=19.6
560 348 my($object,$argument,$flip) = @_;
561 348 _trace("'/'",$object,$argument,$flip);
562 # The order is very important, you can NOT divide a scalar by a vector
563 348 ▶ croak("You can not divide a scalar by a vector") if $flip;
Branch: 1 branch
BranchTrueFalseTotal
if $flip 0 348 348
564 # The provided $argument must be a defined scalar
565 348 ▶ if ( (defined $argument) && ! ref($argument) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and not ref $argument) 348 0 348
Condition33%1/3
defined $argument and not ref $argument
!ll&&!rl&&r
00348
MC/DC0%0/2
defined $argument and not ref $argument
defined $argument not ref $argument
Truth table33%1/3
A and not(B)
A= defined $argumentB= not ref $argument
ABresult
0X0
100
111
566 348 my $v = $object->clone;
567 348 for ( 0 .. 2 ) { $v->[0][0][$_] /= $argument; }
568 348 ▶ $v->[6] /= abs($argument) if defined $v->[6]; # do vector length
Branch: 1 branch
BranchTrueFalseTotal
if defined $v->[6] 348 0 348
569 348 return $v;
570 }
571 0 croak("non-scalar given for vector scalar divide");
572 }
573  
574  
575 827 ▶sub _cross_product {
Subroutine _cross_product: called CC=4 SCAR=14.9
576 827 my($object,$argument,$flip) = @_;
577 # Operation on two vectors, as such $flip will be undefined or false
578 # Note: however this is not communitive - order does matters
579 827 _trace("'x'",$object,$argument,$flip);
580 827 ▶ if ( (defined $argument) && ref($argument) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) 827 0 827
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
00827
defined $argument and ref $argument
!ll&&!rl&&r
00827
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
581 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) {
582 827 my $v = $object->new;
583 827 my @o = $object->array;
584 827 my @a = $argument->array;
585 827 @{$v->[0][0]} = ( $o[1]*$a[2] - $o[2]*$a[1],
586 $o[2]*$a[0] - $o[0]*$a[2],
587 $o[0]*$a[1] - $o[1]*$a[0] );
588 827 $#{$v} = 2; # any cached vector length is now invalid
589 827 return $v;
590 }
591 0 croak("string 'x' with a vector does not make sense!");
592 }
593  
594  
595 2192 ▶sub _dot_product {
Subroutine _dot_product: called CC=7 SCAR=29.1
596 2192 my($object,$argument,$flip) = @_;
597 2192 ▶ if ( (defined $argument) && ref($argument) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) { } 2192 0 2192
elsif (defined $flip) { } 0 0 0
Condition33%2/6
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
!ll&&!rl&&r
002192
defined $argument and ref $argument
!ll&&!rl&&r
002192
MC/DC0%0/3
defined $argument and ref $argument and not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
defined $argument ref $argument not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
Truth table25%1/4
A and B and not(C)
A= defined $argumentB= ref $argumentC= not ref($argument) =~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/
ABCresult
0XX0
10X0
1100
1111
598 (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) {
599 # Operation on two vectors, and communitive - order does not matter
600 2192 _trace("'.'",$object,$argument,$flip);
601 2192 my $v = 0; # result is NOT an object, but a scalar
602 2192 for ( 0 .. 2 ) { $v += $object->[0][0][$_] * $argument->[0][0][$_]; }
603 2192 return $v;
604 }
605 # Argument is NOT a vector! Assume String concatenation wanted
606 elsif ( defined $flip ) {
607 0 ▶ if ( $flip ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flip) { } 0 0 0
608 0 _trace("'.\"\"'",$object,$argument,$flip);
609 0 return $argument . $object->stringify;
610 } else {
611 0 _trace("'\"\".'",$object,$argument,$flip);
612 0 return $object->stringify . $argument;
613 }
614 }
615 # concatenate a string to a vector
616 0 _trace("'.='",$object,$argument,$flip);
617 0 return $object->stringify . $argument;
618 # Concatenate a vector to string is handled automatically with '""' operator
619 }
620  
621 27 1;
622 # ------------------------------------------------------------------
623  
624 =head1 SEE ALSO
625  
626 The C<Math::MatrixReal> CPAN Module by Steffen Beyer
627 and the C<Math::MatrixReal-Ext1> CPAN extension by Mike South
628  
629 =head1 AUTHOR
630  
631 Anthony Thyssen E<lt>F<anthony@cit.gu.edu.au>E<gt>
632  
633 =head1 COPYRIGHT
634  
635 Copyright (c) 2001 Anthony Thyssen. All rights reserved. This program is free
636 software; you can redistribute it and/or modify it under the same terms as
637 Perl itself. I would appreciate any suggestions however.
638  
639 =cut
640  
« blib/lib/Chemistry/File/SDF.pm ↑ Summary blib/lib/Chemistry/File.pm »