blib/lib/PDL/Complex.pm

filter: Statementstmt 68.0% 224 / 329 Branchbran 33.9% 70 / 206 Conditioncond 38.8% 21 / 54 MC/DCmcdc 20.0% 6 / 30 Subroutinesub 84.1% 69 / 82 Podpod 48.1% 13 / 27 totaltotal 55.3% 403 / 728 CC 103 SCAR 44.1
CC
103
Cov
53%
str1D
240.0
str2D
238.8
complex
20.0
CRAP 82.6
« blib/lib/PDL/IO/IDL.pm ↑ Summary blib/lib/PDL/Doc.pm »
1  
2 #
3 # GENERATED WITH PDL::PP! Don't modify!
4 #
5 package PDL::Complex;
6  
7 3 @EXPORT_OK = qw( Ctan Catan re im i cplx real PDL::PP r2C PDL::PP i2C PDL::PP Cr2p PDL::PP Cp2r PDL::PP Cadd PDL::PP Csub PDL::PP Cmul PDL::PP Cprodover PDL::PP Cscale PDL::PP Cdiv PDL::PP Ccmp PDL::PP Cconj PDL::PP Cabs PDL::PP Cabs2 PDL::PP Carg PDL::PP Csin PDL::PP Ccos PDL::PP Cexp PDL::PP Clog PDL::PP Cpow PDL::PP Csqrt PDL::PP Casin PDL::PP Cacos PDL::PP Csinh PDL::PP Ccosh PDL::PP Ctanh PDL::PP Casinh PDL::PP Cacosh PDL::PP Catanh PDL::PP Cproj PDL::PP Croots PDL::PP rCpolynomial );
8 3 %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
9  
10 3 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 BEGIN {
16
17 3 ▶ @ISA = ( 'PDL::Exporter','DynaLoader','PDL' );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 push @PDL::Core::PP, __PACKAGE__;
19 3 bootstrap PDL::Complex ;
20 }
21  
22  
23  
24 3 our $VERSION = '2.009';
25 3 ▶ use PDL::Slices;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶ use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶ use PDL::Bad;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 ▶ use vars qw($sep $sep2);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32  
33 =encoding iso-8859-1
34  
35 =head1 NAME
36  
37 PDL::Complex - handle complex numbers
38  
39 =head1 SYNOPSIS
40  
41 use PDL;
42 use PDL::Complex;
43  
44 =head1 DESCRIPTION
45  
46 This module features a growing number of functions manipulating complex
47 numbers. These are usually represented as a pair C<[ real imag ]> or
48 C<[ angle phase ]>. If not explicitly mentioned, the functions can work
49 inplace (not yet implemented!!!) and require rectangular form.
50  
51 While there is a procedural interface available (C<< $a/$b*$c <=> Cmul
52 (Cdiv $a, $b), $c) >>), you can also opt to cast your pdl's into the
53 C<PDL::Complex> datatype, which works just like your normal piddles, but
54 with all the normal perl operators overloaded.
55  
56 The latter means that C<sin($a) + $b/$c> will be evaluated using the
57 normal rules of complex numbers, while other pdl functions (like C<max>)
58 just treat the piddle as a real-valued piddle with a lowest dimension of
59 size 2, so C<max> will return the maximum of all real and imaginary parts,
60 not the "highest" (for some definition)
61  
62 =head1 TIPS, TRICKS & CAVEATS
63  
64 =over 4
65  
66 =item *
67  
68 C<i> is a constant exported by this module, which represents
69 C<-1**0.5>, i.e. the imaginary unit. it can be used to quickly and
70 conveniently write complex constants like this: C<4+3*i>.
71  
72 =item *
73  
74 Use C<r2C(real-values)> to convert from real to complex, as in C<$r
75 = Cpow $cplx, r2C 2>. The overloaded operators automatically do that for
76 you, all the other functions, do not. So C<Croots 1, 5> will return all
77 the fifths roots of 1+1*i (due to threading).
78  
79 =item *
80  
81 use C<cplx(real-valued-piddle)> to cast from normal piddles into the
82 complex datatype. Use C<real(complex-valued-piddle)> to cast back. This
83 requires a copy, though.
84  
85 =item *
86  
87 This module has received some testing by Vanuxem Grégory
88 (g.vanuxem at wanadoo dot fr). Please report any other errors you
89 come across!
90  
91 =back
92  
93 =head1 EXAMPLE WALK-THROUGH
94  
95 The complex constant five is equal to C<pdl(1,0)>:
96  
97 pdl> p $x = r2C 5
98 5 +0i
99  
100 Now calculate the three cubic roots of of five:
101  
102 pdl> p $r = Croots $x, 3
103 [1.70998 +0i -0.854988 +1.48088i -0.854988 -1.48088i]
104  
105 Check that these really are the roots:
106  
107 pdl> p $r ** 3
108 [5 +0i 5 -1.22465e-15i 5 -7.65714e-15i]
109  
110 Duh! Could be better. Now try by multiplying C<$r> three times with itself:
111  
112 pdl> p $r*$r*$r
113 [5 +0i 5 -4.72647e-15i 5 -7.53694e-15i]
114  
115 Well... maybe C<Cpow> (which is used by the C<**> operator) isn't as
116 bad as I thought. Now multiply by C<i> and negate, which is just a very
117 expensive way of swapping real and imaginary parts.
118  
119 pdl> p -($r*i)
120 [0 -1.70998i 1.48088 +0.854988i -1.48088 +0.854988i]
121  
122 Now plot the magnitude of (part of) the complex sine. First generate the
123 coefficients:
124  
125 pdl> $sin = i * zeroes(50)->xlinvals(2,4) + zeroes(50)->xlinvals(0,7)
126  
127 Now plot the imaginary part, the real part and the magnitude of the sine
128 into the same diagram:
129  
130 pdl> use PDL::Graphics::Gnuplot
131 pdl> gplot( with => 'lines',
132 PDL::cat(im ( sin $sin ),
133 re ( sin $sin ),
134 abs( sin $sin ) ))
135  
136 An ASCII version of this plot looks like this:
137  
138 30 ++-----+------+------+------+------+------+------+------+------+-----++
139 + + + + + + + + + + +
140 | $$|
141 | $ |
142 25 ++ $$ ++
143 | *** |
144 | ** *** |
145 | $$* *|
146 20 ++ $** ++
147 | $$$* #|
148 | $$$ * # |
149 | $$ * # |
150 15 ++ $$$ * # ++
151 | $$$ ** # |
152 | $$$$ * # |
153 | $$$$ * # |
154 10 ++ $$$$$ * # ++
155 | $$$$$ * # |
156 | $$$$$$$ * # |
157 5 ++ $$$############ * # ++
158 |*****$$$### ### * # |
159 * #***** # * # |
160 | ### *** ### ** # |
161 0 ## *** # * # ++
162 | * # * # |
163 | *** # ** # |
164 | * # * # |
165 -5 ++ ** # * # ++
166 | *** ## ** # |
167 | * #* # |
168 | **** ***## # |
169 -10 ++ **** # # ++
170 | # # |
171 | ## ## |
172 + + + + + + + ### + ### + + +
173 -15 ++-----+------+------+------+------+------+-----###-----+------+-----++
174 0 5 10 15 20 25 30 35 40 45 50
175  
176 =cut
177  
178 3 my $i;
179 3 ▶BEGIN { $i = bless pdl 0,1 }
Subroutine BEGIN: called CC=1 SCAR=0.0
180 10 ▶sub i () { $i->copy };
Subroutine i: called CC=1 SCAR=0.0
Pod undocumented
181  
182  
183  
184  
185  
186  
187 =head1 FUNCTIONS
188  
189  
190  
191 =cut
192  
193  
194  
195  
196  
197 =head2 cplx real-valued-pdl
198  
199 Cast a real-valued piddle to the complex datatype. The first dimension of
200 the piddle must be of size 2. After this the usual (complex) arithmetic
201 operators are applied to this pdl, rather than the normal elementwise pdl
202 operators. Dataflow to the complex parent works. Use C<sever> on the result
203 if you don't want this.
204  
205  
206 =head2 complex real-valued-pdl
207  
208 Cast a real-valued piddle to the complex datatype I<without> dataflow
209 and I<inplace>. Achieved by merely reblessing a piddle. The first dimension of
210 the piddle must be of size 2.
211  
212 =head2 real cplx-valued-pdl
213  
214 Cast a complex valued pdl back to the "normal" pdl datatype. Afterwards
215 the normal elementwise pdl operators are used in operations. Dataflow
216 to the real parent works. Use C<sever> on the result if you don't want this.
217  
218 =cut
219  
220 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 sub cplx($) {
222 1 ▶ return $_[0] if UNIVERSAL::isa($_[0],'PDL::Complex'); # NOOP if just piddle
Subroutine cplx: called CC=4 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "PDL::Complex") 0 1 1
223 1 ▶ croak "first dimsize must be 2" unless $_[0]->dims > 0 && $_[0]->dim(0) == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->dims > 0 and $_[0]->dim(0) == 2 0 1 1
Condition33%1/3
$_[0]->dims > 0 and $_[0]->dim(0) == 2
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0]->dims > 0 and $_[0]->dim(0) == 2
$_[0]->dims > 0 $_[0]->dim(0) == 2
Truth table33%1/3
A and B
A= $_[0]->dims > 0B= $_[0]->dim(0) == 2
ABresult
0X0
100
111
224 1 bless $_[0]->slice('');
225 }
226  
227 sub complex($) {
228 0 ▶ return $_[0] if UNIVERSAL::isa($_[0],'PDL::Complex'); # NOOP if just piddle
Subroutine complex: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "PDL::Complex") 0 0 0
229 0 ▶ croak "first dimsize must be 2" unless $_[0]->dims > 0 && $_[0]->dim(0) == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->dims > 0 and $_[0]->dim(0) == 2 0 0 0
Condition0%0/3
$_[0]->dims > 0 and $_[0]->dim(0) == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->dims > 0 and $_[0]->dim(0) == 2
$_[0]->dims > 0 $_[0]->dim(0) == 2
Truth table0%0/3
A and B
A= $_[0]->dims > 0B= $_[0]->dim(0) == 2
ABresult
0X0
100
111
230 0 bless $_[0];
231 }
232  
233 3 *PDL::cplx = \&cplx;
234 3 *PDL::complex = \&complex;
235  
236 sub real($) {
237 4 ▶ return $_[0] unless UNIVERSAL::isa($_[0],'PDL::Complex'); # NOOP unless complex
Subroutine real: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], "PDL::Complex") 0 4 4
238 4 bless $_[0]->slice(''), 'PDL';
239 }
240  
241  
242  
243  
244  
245 =head2 r2C
246  
247 =for sig
248  
249 Signature: (r(); [o]c(m=2))
250  
251 =for ref
252  
253 convert real to complex, assuming an imaginary part of zero
254  
255 =for bad
256  
257 r2C does not process bad values.
258 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
259  
260  
261 =cut
262  
263  
264  
265  
266  
267 3 *PDL::r2C = \&PDL::Complex::r2C;
268 sub PDL::Complex::r2C($) {
269 15 ▶ return $_[0] if UNIVERSAL::isa($_[0],'PDL::Complex');
Subroutine r2C: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "PDL::Complex") 0 15 15
270 15 my $r = __PACKAGE__->initialize;
271 15 &PDL::Complex::_r2C_int($_[0], $r);
272 15 $r }
273  
274  
275  
276 3 ▶BEGIN {*r2C = \&PDL::Complex::r2C;
Subroutine BEGIN: called CC=1 SCAR=0.0
277 }
278  
279  
280  
281  
282 =head2 i2C
283  
284 =for sig
285  
286 Signature: (r(); [o]c(m=2))
287  
288 =for ref
289  
290 convert imaginary to complex, assuming a real part of zero
291  
292 =for bad
293  
294 i2C does not process bad values.
295 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
296  
297  
298 =cut
299  
300  
301  
302  
303 0 ▶*PDL::i2C = \&PDL::Complex::i2C; sub PDL::Complex::i2C($) { my $r = __PACKAGE__->initialize; &PDL::Complex::_i2C_int($_[0], $r); $r }
Subroutine i2C: not called CC=1 SCAR=3.5
Pod documented
304  
305 3 ▶BEGIN {*i2C = \&PDL::Complex::i2C;
Subroutine BEGIN: called CC=1 SCAR=0.0
306 }
307  
308  
309  
310  
311 =head2 Cr2p
312  
313 =for sig
314  
315 Signature: (r(m=2); float+ [o]p(m=2))
316  
317 =for ref
318  
319 convert complex numbers in rectangular form to polar (mod,arg) form. Works inplace
320  
321 =for bad
322  
323 Cr2p does not process bad values.
324 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
325  
326  
327 =cut
328  
329  
330  
331  
332  
333  
334 3 ▶BEGIN {*Cr2p = \&PDL::Complex::Cr2p;
Subroutine BEGIN: called CC=1 SCAR=0.0
335 }
336  
337  
338  
339  
340 =head2 Cp2r
341  
342 =for sig
343  
344 Signature: (r(m=2); [o]p(m=2))
345  
346 =for ref
347  
348 convert complex numbers in polar (mod,arg) form to rectangular form. Works inplace
349  
350 =for bad
351  
352 Cp2r does not process bad values.
353 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
354  
355  
356 =cut
357  
358  
359  
360  
361  
362  
363 3 ▶BEGIN {*Cp2r = \&PDL::Complex::Cp2r;
Subroutine BEGIN: called CC=1 SCAR=0.0
364 }
365  
366  
367  
368  
369 3 ▶BEGIN {*Cadd = \&PDL::Complex::Cadd;
Subroutine BEGIN: called CC=1 SCAR=0.0
370 }
371  
372  
373  
374  
375 3 ▶BEGIN {*Csub = \&PDL::Complex::Csub;
Subroutine BEGIN: called CC=1 SCAR=0.0
376 }
377  
378  
379  
380  
381 =head2 Cmul
382  
383 =for sig
384  
385 Signature: (a(m=2); b(m=2); [o]c(m=2))
386  
387 =for ref
388  
389 complex multiplication
390  
391 =for bad
392  
393 Cmul does not process bad values.
394 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
395  
396  
397 =cut
398  
399  
400  
401  
402  
403  
404 3 ▶BEGIN {*Cmul = \&PDL::Complex::Cmul;
Subroutine BEGIN: called CC=1 SCAR=0.0
405 }
406  
407  
408  
409  
410 =head2 Cprodover
411  
412 =for sig
413  
414 Signature: (a(m=2,n); [o]c(m=2))
415  
416 =for ref
417  
418 Project via product to N-1 dimension
419  
420 =for bad
421  
422 Cprodover does not process bad values.
423 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
424  
425  
426 =cut
427  
428  
429  
430  
431  
432  
433 3 ▶BEGIN {*Cprodover = \&PDL::Complex::Cprodover;
Subroutine BEGIN: called CC=1 SCAR=0.0
434 }
435  
436  
437  
438  
439 =head2 Cscale
440  
441 =for sig
442  
443 Signature: (a(m=2); b(); [o]c(m=2))
444  
445 =for ref
446  
447 mixed complex/real multiplication
448  
449 =for bad
450  
451 Cscale does not process bad values.
452 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
453  
454  
455 =cut
456  
457  
458  
459  
460  
461  
462 3 ▶BEGIN {*Cscale = \&PDL::Complex::Cscale;
Subroutine BEGIN: called CC=1 SCAR=0.0
463 }
464  
465  
466  
467  
468 =head2 Cdiv
469  
470 =for sig
471  
472 Signature: (a(m=2); b(m=2); [o]c(m=2))
473  
474 =for ref
475  
476 complex division
477  
478 =for bad
479  
480 Cdiv does not process bad values.
481 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
482  
483  
484 =cut
485  
486  
487  
488  
489  
490  
491 3 ▶BEGIN {*Cdiv = \&PDL::Complex::Cdiv;
Subroutine BEGIN: called CC=1 SCAR=0.0
492 }
493  
494  
495  
496  
497 =head2 Ccmp
498  
499 =for sig
500  
501 Signature: (a(m=2); b(m=2); [o]c())
502  
503 =for ref
504  
505 Complex comparison oeprator (spaceship). It orders by real first, then by imaginary. Hm, but it is mathematical nonsense! Complex numbers cannot be ordered.
506  
507 =for bad
508  
509 Ccmp does not process bad values.
510 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
511  
512  
513 =cut
514  
515  
516  
517  
518  
519  
520 3 ▶BEGIN {*Ccmp = \&PDL::Complex::Ccmp;
Subroutine BEGIN: called CC=1 SCAR=0.0
521 }
522  
523  
524  
525  
526 =head2 Cconj
527  
528 =for sig
529  
530 Signature: (a(m=2); [o]c(m=2))
531  
532 =for ref
533  
534 complex conjugation. Works inplace
535  
536 =for bad
537  
538 Cconj does not process bad values.
539 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
540  
541  
542 =cut
543  
544  
545  
546  
547  
548  
549 3 ▶BEGIN {*Cconj = \&PDL::Complex::Cconj;
Subroutine BEGIN: called CC=1 SCAR=0.0
550 }
551  
552  
553  
554  
555 =head2 Cabs
556  
557 =for sig
558  
559 Signature: (a(m=2); [o]c())
560  
561 =for ref
562  
563 complex C<abs()> (also known as I<modulus>)
564  
565 =for bad
566  
567 Cabs does not process bad values.
568 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
569  
570  
571 =cut
572  
573  
574  
575  
576 sub PDL::Complex::Cabs($) {
577 4 ▶ my $pdl= shift;
Subroutine Cabs: called CC=1 SCAR=0.0
Pod documented
578 4 my $abs = PDL->null;
579 4 &PDL::Complex::_Cabs_int($pdl, $abs);
580 4 $abs;
581 }
582  
583 3 ▶BEGIN {*Cabs = \&PDL::Complex::Cabs;
Subroutine BEGIN: called CC=1 SCAR=0.0
584 }
585  
586  
587  
588  
589 =head2 Cabs2
590  
591 =for sig
592  
593 Signature: (a(m=2); [o]c())
594  
595 =for ref
596  
597 complex squared C<abs()> (also known I<squared modulus>)
598  
599 =for bad
600  
601 Cabs2 does not process bad values.
602 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
603  
604  
605 =cut
606  
607  
608  
609  
610 sub PDL::Complex::Cabs2($) {
611 2 ▶ my $pdl= shift;
Subroutine Cabs2: called CC=1 SCAR=0.0
Pod documented
612 2 my $abs2 = PDL->null;
613 2 &PDL::Complex::_Cabs2_int($pdl, $abs2);
614 2 $abs2;
615 }
616  
617 3 ▶BEGIN {*Cabs2 = \&PDL::Complex::Cabs2;
Subroutine BEGIN: called CC=1 SCAR=0.0
618 }
619  
620  
621  
622  
623 =head2 Carg
624  
625 =for sig
626  
627 Signature: (a(m=2); [o]c())
628  
629 =for ref
630  
631 complex argument function ("angle")
632  
633 =for bad
634  
635 Carg does not process bad values.
636 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
637  
638  
639 =cut
640  
641  
642  
643  
644 sub PDL::Complex::Carg($) {
645 1 ▶ my $pdl= shift;
Subroutine Carg: called CC=1 SCAR=0.0
Pod documented
646 1 my $arg = PDL->null;
647 1 &PDL::Complex::_Carg_int($pdl, $arg);
648 1 $arg;
649 }
650  
651 3 ▶BEGIN {*Carg = \&PDL::Complex::Carg;
Subroutine BEGIN: called CC=1 SCAR=0.0
652 }
653  
654  
655  
656  
657 =head2 Csin
658  
659 =for sig
660  
661 Signature: (a(m=2); [o]c(m=2))
662  
663 =for ref
664  
665 sin (a) = 1/(2*i) * (exp (a*i) - exp (-a*i)). Works inplace
666  
667 =for bad
668  
669 Csin does not process bad values.
670 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
671  
672  
673 =cut
674  
675  
676  
677  
678  
679  
680 3 ▶BEGIN {*Csin = \&PDL::Complex::Csin;
Subroutine BEGIN: called CC=1 SCAR=0.0
681 }
682  
683  
684  
685  
686 =head2 Ccos
687  
688 =for sig
689  
690 Signature: (a(m=2); [o]c(m=2))
691  
692 =for ref
693  
694 cos (a) = 1/2 * (exp (a*i) + exp (-a*i)). Works inplace
695  
696 =for bad
697  
698 Ccos does not process bad values.
699 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
700  
701  
702 =cut
703  
704  
705  
706  
707  
708  
709 3 ▶BEGIN {*Ccos = \&PDL::Complex::Ccos;
Subroutine BEGIN: called CC=1 SCAR=0.0
710 }
711  
712  
713  
714 =head2 Ctan a [not inplace]
715  
716 tan (a) = -i * (exp (a*i) - exp (-a*i)) / (exp (a*i) + exp (-a*i))
717  
718 =cut
719  
720 0 ▶sub Ctan($) { Csin($_[0]) / Ccos($_[0]) }
Subroutine Ctan: not called CC=1 SCAR=6.9
Pod documented
721  
722  
723  
724  
725  
726  
727 =head2 Cexp
728  
729 =for sig
730  
731 Signature: (a(m=2); [o]c(m=2))
732  
733 =for ref
734  
735 exp (a) = exp (real (a)) * (cos (imag (a)) + i * sin (imag (a))). Works inplace
736  
737 =for bad
738  
739 Cexp does not process bad values.
740 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
741  
742  
743 =cut
744  
745  
746  
747  
748  
749  
750 3 ▶BEGIN {*Cexp = \&PDL::Complex::Cexp;
Subroutine BEGIN: called CC=1 SCAR=0.0
751 }
752  
753  
754  
755  
756 =head2 Clog
757  
758 =for sig
759  
760 Signature: (a(m=2); [o]c(m=2))
761  
762 =for ref
763  
764 log (a) = log (cabs (a)) + i * carg (a). Works inplace
765  
766 =for bad
767  
768 Clog does not process bad values.
769 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
770  
771  
772 =cut
773  
774  
775  
776  
777  
778  
779 3 ▶BEGIN {*Clog = \&PDL::Complex::Clog;
Subroutine BEGIN: called CC=1 SCAR=0.0
780 }
781  
782  
783  
784  
785 =head2 Cpow
786  
787 =for sig
788  
789 Signature: (a(m=2); b(m=2); [o]c(m=2))
790  
791 =for ref
792  
793 complex C<pow()> (C<**>-operator)
794  
795 =for bad
796  
797 Cpow does not process bad values.
798 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
799  
800  
801 =cut
802  
803  
804  
805  
806  
807  
808 3 ▶BEGIN {*Cpow = \&PDL::Complex::Cpow;
Subroutine BEGIN: called CC=1 SCAR=0.0
809 }
810  
811  
812  
813  
814 =head2 Csqrt
815  
816 =for sig
817  
818 Signature: (a(m=2); [o]c(m=2))
819  
820 =for ref
821  
822 Works inplace
823  
824 =for bad
825  
826 Csqrt does not process bad values.
827 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
828  
829  
830 =cut
831  
832  
833  
834  
835  
836  
837 3 ▶BEGIN {*Csqrt = \&PDL::Complex::Csqrt;
Subroutine BEGIN: called CC=1 SCAR=0.0
838 }
839  
840  
841  
842  
843 =head2 Casin
844  
845 =for sig
846  
847 Signature: (a(m=2); [o]c(m=2))
848  
849 =for ref
850  
851 Works inplace
852  
853 =for bad
854  
855 Casin does not process bad values.
856 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
857  
858  
859 =cut
860  
861  
862  
863  
864  
865  
866 3 ▶BEGIN {*Casin = \&PDL::Complex::Casin;
Subroutine BEGIN: called CC=1 SCAR=0.0
867 }
868  
869  
870  
871  
872 =head2 Cacos
873  
874 =for sig
875  
876 Signature: (a(m=2); [o]c(m=2))
877  
878 =for ref
879  
880 Works inplace
881  
882 =for bad
883  
884 Cacos does not process bad values.
885 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
886  
887  
888 =cut
889  
890  
891  
892  
893  
894  
895 3 ▶BEGIN {*Cacos = \&PDL::Complex::Cacos;
Subroutine BEGIN: called CC=1 SCAR=0.0
896 }
897  
898  
899  
900 =head2 Catan cplx [not inplace]
901  
902 Return the complex C<atan()>.
903  
904 =cut
905  
906 sub Catan($) {
907 0 ▶ my $z = shift;
Subroutine Catan: not called CC=1 SCAR=6.9
Pod documented
908 0 Cmul Clog(Cdiv (PDL::Complex::i+$z, PDL::Complex::i-$z)), pdl(0, 0.5);
909 }
910  
911  
912  
913  
914  
915 =head2 Csinh
916  
917 =for sig
918  
919 Signature: (a(m=2); [o]c(m=2))
920  
921 =for ref
922  
923 sinh (a) = (exp (a) - exp (-a)) / 2. Works inplace
924  
925 =for bad
926  
927 Csinh does not process bad values.
928 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
929  
930  
931 =cut
932  
933  
934  
935  
936  
937  
938 3 ▶BEGIN {*Csinh = \&PDL::Complex::Csinh;
Subroutine BEGIN: called CC=1 SCAR=0.0
939 }
940  
941  
942  
943  
944 =head2 Ccosh
945  
946 =for sig
947  
948 Signature: (a(m=2); [o]c(m=2))
949  
950 =for ref
951  
952 cosh (a) = (exp (a) + exp (-a)) / 2. Works inplace
953  
954 =for bad
955  
956 Ccosh does not process bad values.
957 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
958  
959  
960 =cut
961  
962  
963  
964  
965  
966  
967 3 ▶BEGIN {*Ccosh = \&PDL::Complex::Ccosh;
Subroutine BEGIN: called CC=1 SCAR=0.0
968 }
969  
970  
971  
972  
973 =head2 Ctanh
974  
975 =for sig
976  
977 Signature: (a(m=2); [o]c(m=2))
978  
979 =for ref
980  
981 Works inplace
982  
983 =for bad
984  
985 Ctanh does not process bad values.
986 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
987  
988  
989 =cut
990  
991  
992  
993  
994  
995  
996 3 ▶BEGIN {*Ctanh = \&PDL::Complex::Ctanh;
Subroutine BEGIN: called CC=1 SCAR=0.0
997 }
998  
999  
1000  
1001  
1002 =head2 Casinh
1003  
1004 =for sig
1005  
1006 Signature: (a(m=2); [o]c(m=2))
1007  
1008 =for ref
1009  
1010 Works inplace
1011  
1012 =for bad
1013  
1014 Casinh does not process bad values.
1015 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1016  
1017  
1018 =cut
1019  
1020  
1021  
1022  
1023  
1024  
1025 3 ▶BEGIN {*Casinh = \&PDL::Complex::Casinh;
Subroutine BEGIN: called CC=1 SCAR=0.0
1026 }
1027  
1028  
1029  
1030  
1031 =head2 Cacosh
1032  
1033 =for sig
1034  
1035 Signature: (a(m=2); [o]c(m=2))
1036  
1037 =for ref
1038  
1039 Works inplace
1040  
1041 =for bad
1042  
1043 Cacosh does not process bad values.
1044 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1045  
1046  
1047 =cut
1048  
1049  
1050  
1051  
1052  
1053  
1054 3 ▶BEGIN {*Cacosh = \&PDL::Complex::Cacosh;
Subroutine BEGIN: called CC=1 SCAR=0.0
1055 }
1056  
1057  
1058  
1059  
1060 =head2 Catanh
1061  
1062 =for sig
1063  
1064 Signature: (a(m=2); [o]c(m=2))
1065  
1066 =for ref
1067  
1068 Works inplace
1069  
1070 =for bad
1071  
1072 Catanh does not process bad values.
1073 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1074  
1075  
1076 =cut
1077  
1078  
1079  
1080  
1081  
1082  
1083 3 ▶BEGIN {*Catanh = \&PDL::Complex::Catanh;
Subroutine BEGIN: called CC=1 SCAR=0.0
1084 }
1085  
1086  
1087  
1088  
1089 =head2 Cproj
1090  
1091 =for sig
1092  
1093 Signature: (a(m=2); [o]c(m=2))
1094  
1095 =for ref
1096  
1097 compute the projection of a complex number to the riemann sphere. Works inplace
1098  
1099 =for bad
1100  
1101 Cproj does not process bad values.
1102 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1103  
1104  
1105 =cut
1106  
1107  
1108  
1109  
1110  
1111  
1112 3 ▶BEGIN {*Cproj = \&PDL::Complex::Cproj;
Subroutine BEGIN: called CC=1 SCAR=0.0
1113 }
1114  
1115  
1116  
1117  
1118 =head2 Croots
1119  
1120 =for sig
1121  
1122 Signature: (a(m=2); [o]c(m=2,n); int n => n)
1123  
1124 =for ref
1125  
1126 Compute the C<n> roots of C<a>. C<n> must be a positive integer. The result will always be a complex type!
1127  
1128 =for bad
1129  
1130 Croots does not process bad values.
1131 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1132  
1133  
1134 =cut
1135  
1136  
1137  
1138  
1139 sub PDL::Complex::Croots($$) {
1140 0 ▶ my ($pdl, $n) = @_;
Subroutine Croots: not called CC=1 SCAR=6.9
Pod documented
1141 0 my $r = PDL->null;
1142 0 &PDL::Complex::_Croots_int($pdl, $r, $n);
1143 0 bless $r;
1144 }
1145  
1146 3 ▶BEGIN {*Croots = \&PDL::Complex::Croots;
Subroutine BEGIN: called CC=1 SCAR=0.0
1147 }
1148  
1149  
1150  
1151 =head2 re cplx, im cplx
1152  
1153 Return the real or imaginary part of the complex number(s) given. These
1154 are slicing operators, so data flow works. The real and imaginary parts
1155 are returned as piddles (ref eq PDL).
1156  
1157 =cut
1158  
1159 1 ▶sub re($) { bless $_[0]->slice("(0)"), 'PDL'; }
Subroutine re: called CC=1 SCAR=0.0
Pod documented
1160 1 ▶sub im($) { bless $_[0]->slice("(1)"), 'PDL'; }
Subroutine im: called CC=1 SCAR=0.0
Pod documented
1161  
1162 3 *PDL::Complex::re = \&re;
1163 3 *PDL::Complex::im = \&im;
1164  
1165  
1166  
1167  
1168  
1169 =head2 rCpolynomial
1170  
1171 =for sig
1172  
1173 Signature: (coeffs(n); x(c=2,m); [o]out(c=2,m))
1174  
1175 =for ref
1176  
1177 evaluate the polynomial with (real) coefficients C<coeffs> at the (complex) position(s) C<x>. C<coeffs[0]> is the constant term.
1178  
1179 =for bad
1180  
1181 rCpolynomial does not process bad values.
1182 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
1183  
1184  
1185 =cut
1186  
1187  
1188  
1189  
1190  
1191  
1192 3 ▶BEGIN {*rCpolynomial = \&PDL::Complex::rCpolynomial;
Subroutine BEGIN: called CC=1 SCAR=0.0
1193 }
1194  
1195  
1196 ;
1197  
1198  
1199 # overload must be here, so that all the functions can be seen
1200  
1201 # undocumented compatibility functions
1202 0 ▶sub Catan2($$) { Catan Cdiv $_[1], $_[0] }
Subroutine Catan2: not called CC=1 SCAR=6.9
Pod undocumented
1203 0 ▶sub atan2($$) { Catan Cdiv $_[1], $_[0] }
Subroutine atan2: not called CC=1 SCAR=6.9
Pod undocumented
1204  
1205 sub _gen_biop {
1206 21 ▶ local $_ = shift;
Subroutine _gen_biop: called CC=5 SCAR=19.4
1207 21 my $sub;
1208 21 ▶ if (/(\S+)\+(\w+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/(\S+)\+(\w+)/) { } 6 15 21
elsif (/(\S+)\-(\w+)/) { } 15 0 15
1209 6 ▶ $sub = eval 'sub { '.$2.' $_[0], ref $_[1] eq __PACKAGE__ ? $_[1] : r2C $_[1] }';
Subroutine __ANON__: called CC=2 SCAR=7.4
Branch: 2 branches
BranchTrueFalseTotal
ref $_[1] eq __PACKAGE__ ? : 0 4 4
ref $_[1] eq __PACKAGE__ ? : 0 5 5
1210 } elsif (/(\S+)\-(\w+)/) {
1211 15 ▶ $sub = eval 'sub { my $b = ref $_[1] eq __PACKAGE__ ? $_[1] : r2C $_[1];
Subroutine __ANON__: called CC=3 SCAR=16.2
Branch: 10 branches
BranchTrueFalseTotal
ref $_[1] eq __PACKAGE__ ? : 1 3 4
$_[2] ? : 0 4 4
ref $_[1] eq __PACKAGE__ ? : 2 3 5
$_[2] ? : 1 4 5
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
1212 $_[2] ? '.$2.' $b, $_[0] : '.$2.' $_[0], $b }';
1213 } else {
1214 0 die;
1215 }
1216 21 ▶ if($1 eq "atan2" || $1 eq "<=>") { return ($1, $sub) }
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "atan2" or $1 eq "<=>") 6 15 21
Condition100%3/3
$1 eq "atan2" or $1 eq "<=>"
l!l&&r!l&&!r
3315
MC/DC100%2/2
$1 eq "atan2" or $1 eq "<=>"
$1 eq "atan2" $1 eq "<=>"
Truth table100%3/3
A or B
A= $1 eq "atan2"B= $1 eq "<=>"
ABresult
1X1
011
000
1217 15 ($1, $sub, "$1=", $sub);
1218 }
1219  
1220 sub _gen_unop {
1221 21 ▶ my ($op, $func) = ($_[0] =~ /(.+)@(\w+)/);
Subroutine _gen_unop: called CC=2 SCAR=9.7
1222 21 ▶ *$op = \&$func if $op =~ /\w+/; # create an alias
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ /\w+/ 21 0 21
1223 21 ▶ ($op, eval 'sub { '.$func.' $_[0] }');
Subroutine __ANON__: not called CC=1 SCAR=3.1
1224 }
1225  
1226 sub _gen_cpop {
1227 0 ▶ ($_[0], eval 'sub { my $b = ref $_[1] eq __PACKAGE__ ? $_[1] : r2C $_[1];
Subroutine __ANON__: not called CC=3 SCAR=24.2
Subroutine _gen_cpop: called CC=1 SCAR=6.5
Branch: 12 branches
BranchTrueFalseTotal
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
ref $_[1] eq __PACKAGE__ ? : 0 0 0
$_[2] ? : 0 0 0
1228 ($_[2] ? $b <=> $_[0] : $_[0] <=> $b) '.$_[0].' 0 }');
1229 }
1230  
1231 sub initialize {
1232 # Bless a null PDL into the supplied 1st arg package
1233 # If 1st arg is a ref, get the package from it
1234 64 ▶ bless PDL->null, ref($_[0]) ? ref($_[0]) : $_[0];
Subroutine initialize: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 63 64
1235 }
1236  
1237 use overload
1238 (map _gen_biop($_), qw(++Cadd --Csub *+Cmul /-Cdiv **-Cpow atan2-Catan2 <=>-Ccmp)),
1239 (map _gen_unop($_), qw(sin@Csin cos@Ccos exp@Cexp abs@Cabs log@Clog sqrt@Csqrt abs@Cabs)),
1240 (map _gen_cpop($_), qw(< <= == != >= >)),
1241 0 ▶ '++' => sub { $_[0] += 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1242 0 ▶ '--' => sub { $_[0] -= 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1243 3 '""' => \&PDL::Complex::string
1244 3 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
1245  
1246 # overwrite PDL's overloading to honour subclass methods in + - * /
1247 { package PDL;
1248 3 my $warningFlag;
1249 # This strange usage of BEGINs is to ensure the
1250 # warning messages get disabled and enabled in the
1251 # proper order. Without the BEGIN's the 'use overload'
1252 # would be called first.
1253 3 ▶ BEGIN {$warningFlag = $^W; # Temporarily disable warnings caused by
Subroutine BEGIN: called CC=1 SCAR=0.0
1254 3 $^W = 0; # redefining PDL's subs
1255 }
1256  
1257  
1258 sub cp(;@) {
1259 4 ▶ my $foo;
Subroutine cp: called CC=4 SCAR=13.9
Pod undocumented
1260 4 ▶ if (ref $_[1]
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and ref $_[1] ne 'PDL' and defined($foo = overload::Method($_[1], '+'))) { } 2 2 4
Condition83%5/6
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "+"))
!ll&&!rl&&r
202
ref $_[1] and ref $_[1] ne "PDL"
!ll&&!rl&&r
112
MC/DC66%2/3
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "+"))
ref $_[1] ref $_[1] ne "PDL" defined($foo = overload::Method($_[1], "+"))
Truth table75%3/4
A and B and C
A= ref $_[1]B= ref $_[1] ne "PDL"C= defined($foo = overload::Method($_[1], "+"))
ABCresult
0XX0
10X0
1100
1111
1261 && (ref $_[1] ne 'PDL')
1262 && defined ($foo = overload::Method($_[1],'+')))
1263 2 { &$foo($_[1], $_[0], !$_[2])}
1264 2 else { PDL::plus (@_)}
1265 }
1266  
1267 sub cm(;@) {
1268 4 ▶ my $foo;
Subroutine cm: called CC=4 SCAR=17.9
Pod undocumented
1269 4 ▶ if (ref $_[1]
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and ref $_[1] ne 'PDL' and defined($foo = overload::Method($_[1], '*'))) { } 0 4 4
Condition33%2/6
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "*"))
!ll&&!rl&&r
400
ref $_[1] and ref $_[1] ne "PDL"
!ll&&!rl&&r
400
MC/DC0%0/3
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "*"))
ref $_[1] ref $_[1] ne "PDL" defined($foo = overload::Method($_[1], "*"))
Truth table25%1/4
A and B and C
A= ref $_[1]B= ref $_[1] ne "PDL"C= defined($foo = overload::Method($_[1], "*"))
ABCresult
0XX0
10X0
1100
1111
1270 && (ref $_[1] ne 'PDL')
1271 && defined ($foo = overload::Method($_[1],'*')))
1272 0 { &$foo($_[1], $_[0], !$_[2])}
1273 4 else { PDL::mult (@_)}
1274 }
1275  
1276 sub cmi(;@) {
1277 8 ▶ my $foo;
Subroutine cmi: called CC=4 SCAR=13.9
Pod undocumented
1278 8 ▶ if (ref $_[1]
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and ref $_[1] ne 'PDL' and defined($foo = overload::Method($_[1], '-'))) { } 1 7 8
Condition83%5/6
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "-"))
!ll&&!rl&&r
701
ref $_[1] and ref $_[1] ne "PDL"
!ll&&!rl&&r
251
MC/DC66%2/3
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "-"))
ref $_[1] ref $_[1] ne "PDL" defined($foo = overload::Method($_[1], "-"))
Truth table75%3/4
A and B and C
A= ref $_[1]B= ref $_[1] ne "PDL"C= defined($foo = overload::Method($_[1], "-"))
ABCresult
0XX0
10X0
1100
1111
1279 && (ref $_[1] ne 'PDL')
1280 && defined ($foo = overload::Method($_[1],'-')))
1281 1 { &$foo($_[1], $_[0], !$_[2])}
1282 7 else { PDL::minus (@_)}
1283 }
1284  
1285 sub cd(;@) {
1286 0 ▶ my $foo;
Subroutine cd: not called CC=4 SCAR=30.0
Pod undocumented
1287 0 ▶ if (ref $_[1]
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and ref $_[1] ne 'PDL' and defined($foo = overload::Method($_[1], '/'))) { } 0 0 0
Condition0%0/6
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "/"))
!ll&&!rl&&r
000
ref $_[1] and ref $_[1] ne "PDL"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_[1] and ref $_[1] ne "PDL" and defined($foo = overload::Method($_[1], "/"))
ref $_[1] ref $_[1] ne "PDL" defined($foo = overload::Method($_[1], "/"))
Truth table0%0/4
A and B and C
A= ref $_[1]B= ref $_[1] ne "PDL"C= defined($foo = overload::Method($_[1], "/"))
ABCresult
0XX0
10X0
1100
1111
1288 && (ref $_[1] ne 'PDL')
1289 && defined ($foo = overload::Method($_[1],'/')))
1290 0 { &$foo($_[1], $_[0], !$_[2])}
1291 0 else { PDL::divide (@_)}
1292 }
1293  
1294  
1295 # Used in overriding standard PDL +, -, *, / ops in the complex subclass.
1296 use overload (
1297 3 '+' => \&cp,
1298 '*' => \&cm,
1299 '-' => \&cmi,
1300 '/' => \&cd,
1301 3 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
1302  
1303  
1304  
1305 3 ▶ BEGIN{ $^W = $warningFlag;} # Put Back Warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
1306 3 };
1307  
1308  
1309 {
1310  
1311 3 our $floatformat = "%4.4g"; # Default print format for long numbers
1312 3 our $doubleformat = "%6.6g";
1313  
1314 3 $PDL::Complex::_STRINGIZING = 0;
1315  
1316 sub PDL::Complex::string {
1317 1 ▶ my($self,$format1,$format2)=@_;
Subroutine string: called CC=10 SCAR=26.9
Pod undocumented
1318 1 my @dims = $self->dims;
1319 1 ▶ return PDL::string($self) if ($dims[0] != 2);
Branch: 1 branch
BranchTrueFalseTotal
if $dims[0] != 2 0 1 1
1320  
1321 1 ▶ if($PDL::Complex::_STRINGIZING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_STRINGIZING) 0 1 1
1322 0 return "ALREADY_STRINGIZING_NO_LOOPS";
1323 }
1324 1 local $PDL::Complex::_STRINGIZING = 1;
1325 1 my $ndims = $self->getndims;
1326 1 ▶ if($self->nelem > $PDL::toolongtoprint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nelem > $PDL::toolongtoprint) 0 1 1
1327 0 return "TOO LONG TO PRINT";
1328 }
1329 1 ▶ if ($ndims==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($ndims == 0) 0 1 1
1330 0 PDL::Core::string($self,$format1);
1331 }
1332 1 ▶ return "Null" if $self->isnull;
Branch: 1 branch
BranchTrueFalseTotal
if $self->isnull 0 1 1
1333 1 ▶ return "Empty" if $self->isempty; # Empty piddle
Branch: 1 branch
BranchTrueFalseTotal
if $self->isempty 0 1 1
1334 1 ▶ local $sep = $PDL::use_commas ? ", " : " ";
Branch: 1 branch
BranchTrueFalseTotal
$PDL::use_commas ? : 0 1 1
1335 1 ▶ local $sep2 = $PDL::use_commas ? ", " : "";
Branch: 1 branch
BranchTrueFalseTotal
$PDL::use_commas ? : 0 1 1
1336 1 ▶ if ($ndims < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndims < 3) { } 0 1 1
1337 0 return str1D($self,$format1,$format2);
1338 }
1339 else{
1340 1 return strND($self,$format1,$format2,0);
1341 }
1342 }
1343  
1344  
1345 sub sum {
1346 1 ▶ my($x) = @_;
Subroutine sum: called CC=1 SCAR=0.0
Pod undocumented
1347 1 my $tmp = $x->mv(0,1)->clump(0,2)->mv(1,0)->sumover;
1348 1 return $tmp->squeeze;
1349 }
1350  
1351 sub sumover{
1352 1 ▶ my $m = shift;
Subroutine sumover: called CC=1 SCAR=0.0
Pod undocumented
1353 1 PDL::Ufunc::sumover($m->xchg(0,1));
1354 }
1355  
1356  
1357 sub strND {
1358 1 ▶ my($self,$format1,$format2,$level)=@_;
Subroutine strND: called CC=4 SCAR=24.3
Pod undocumented
1359 1 my @dims = $self->dims;
1360  
1361 1 ▶ if ($#dims==2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#dims == 2) { } 1 0 1
1362 1 return str2D($self,$format1,$format2,$level);
1363 }
1364 else {
1365 0 my $secbas = join '',map {":,"} @dims[0..$#dims-1];
1366 0 my $ret="\n"." "x$level ."["; my $j;
1367 0 ▶ for ($j=0; $j<$dims[$#dims]; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $dims[$#dims]) 0 0 0
1368 0 my $sec = $secbas . "($j)";
1369  
1370 0 $ret .= strND($self->slice($sec),$format1,$format2, $level+1);
1371 0 chop $ret; $ret .= $sep2;
1372 }
1373 0 ▶ chop $ret if $PDL::use_commas;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::use_commas 0 0 0
1374 0 $ret .= "\n" ." "x$level ."]\n";
1375 0 return $ret;
1376 }
1377 }
1378  
1379  
1380 # String 1D array in nice format
1381 #
1382 sub str1D {
1383 0 ▶ my($self,$format1,$format2)=@_;
Subroutine str1D: not called CC=15 SCAR=54.8
Pod undocumented
1384 0 ▶ barf "Not 1D" if $self->getndims() > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->getndims > 2 0 0 0
1385 0 my $x = PDL::Core::listref_c($self);
1386 0 my ($ret,$dformat,$t, $i);
1387  
1388 0 my $dtype = $self->get_datatype();
1389 0 ▶ $dformat = $PDL::Complex::floatformat if $dtype == $PDL_F;
Branch: 1 branch
BranchTrueFalseTotal
if $dtype == $PDL_F 0 0 0
1390 0 ▶ $dformat = $PDL::Complex::doubleformat if $dtype == $PDL_D;
Branch: 1 branch
BranchTrueFalseTotal
if $dtype == $PDL_D 0 0 0
1391  
1392 0 ▶ $ret = "[" if $self->getndims() > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->getndims > 1 0 0 0
1393 0 my $badflag = $self->badflag();
1394 0 ▶ for($i=0; $i<=$#$x; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $x->$#*) 0 0 0
1395 0 $t = $$x[$i];
1396 0 ▶ if ( $badflag and $t eq "BAD" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($badflag and $t eq 'BAD') { } 0 0 0
elsif ($format1) { } 0 0 0
Condition0%0/3
$badflag and $t eq "BAD"
!ll&&!rl&&r
000
MC/DC0%0/2
$badflag and $t eq "BAD"
$badflag $t eq "BAD"
Truth table0%0/3
A and B
A= $badflagB= $t eq "BAD"
ABresult
0X0
100
111
1397 # do nothing
1398 } elsif ($format1) {
1399 0 $t = sprintf $format1,$t;
1400 } else{ # Default
1401 0 ▶ if ($dformat && length($t)>7) { # Try smaller
Branch: 1 branch
BranchTrueFalseTotal
if ($dformat and length $t > 7) 0 0 0
Condition0%0/3
$dformat and length $t > 7
!ll&&!rl&&r
000
MC/DC0%0/2
$dformat and length $t > 7
$dformat length $t > 7
Truth table0%0/3
A and B
A= $dformatB= length $t > 7
ABresult
0X0
100
111
1402 0 $t = sprintf $dformat,$t;
1403 }
1404 }
1405 0 ▶ $ret .= $i % 2 ?
Branch: 3 branches
BranchTrueFalseTotal
$i % 2 ? : 0 0 0
$i < $x->$#* ? : 0 0 0
substr($$x[$i + 1], 0, 1) eq '-' ? : 0 0 0
1406 $i<$#$x ? $t."i$sep" : $t."i"
1407 : substr($$x[$i+1],0,1) eq "-" ? "$t " : $t." +";
1408 }
1409 0 ▶ $ret.="]" if $self->getndims() > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->getndims > 1 0 0 0
1410 0 return $ret;
1411 }
1412  
1413  
1414 sub str2D {
1415 1 ▶ my($self,$format1,$format2,$level)=@_;
Subroutine str2D: called CC=46 SCAR=54.8
Pod undocumented
1416 1 my @dims = $self->dims();
1417 1 ▶ barf "Not 2D" if scalar(@dims)!=3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @dims != 3 0 1 1
1418 1 my $x = PDL::Core::listref_c($self);
1419 1 my ($i, $f, $t, $len1, $len2, $ret);
1420  
1421 1 my $dtype = $self->get_datatype();
1422 1 my $badflag = $self->badflag();
1423  
1424 1 my $findmax = 0;
1425  
1426 1 ▶ if (!defined $format1 || !defined $format2 ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $format1 && defined $format2 or $format1 eq "" or $format2 eq "") 0 0 0
Condition33%3/9
not defined $format1 && defined $format2 or $format1 eq "" or $format2 eq ""
l!l&&r!l&&!r
100
not defined $format1 && defined $format2 or $format1 eq ""
l!l&&r!l&&!r
100
defined $format1 && defined $format2
!ll&&!rl&&r
100
MC/DC0%0/4
not defined $format1 && defined $format2 or $format1 eq "" or $format2 eq ""
defined $format1 defined $format2 $format1 eq "" $format2 eq ""
Truth table20%1/5
not(A && B) or C or D
A= defined $format1B= defined $format2C= $format1 eq ""D= $format2 eq ""
ABCDresult
0XXX1
10XX1
111X1
11011
11000
1427 $format1 eq '' || $format2 eq '') {
1428 1 $len1= $len2 = 0;
1429  
1430 1 ▶ if ( $badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($badflag) { } 0 1 1
1431 0 ▶ for ($i=0; $i<=$#$x; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $x->$#*) 0 0 0
1432 0 ▶ if ( $$x[$i] eq "BAD" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->[$i] eq 'BAD') { } 0 0 0
1433 0 $f = 3;
1434 }
1435 else {
1436 0 $f = length($$x[$i]);
1437 }
1438 0 ▶ if ($i % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) { } 0 0 0
1439 0 ▶ $len2 = $f if $f > $len2;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len2 0 0 0
1440 }
1441 else {
1442 0 ▶ $len1 = $f if $f > $len1;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len1 0 0 0
1443 }
1444 }
1445 } else {
1446 1 ▶ for ($i=0; $i<=$#$x; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $x->$#*) 2 1 3
1447 2 $f = length($$x[$i]);
1448 2 ▶ if ($i % 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) { } 1 1 2
1449 1 ▶ $len2 = $f if $f > $len2;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len2 1 0 1
1450 }
1451 else{
1452 1 ▶ $len1 = $f if $f > $len1;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len1 1 0 1
1453 }
1454 }
1455 }
1456  
1457 1 $format1 = '%'.$len1.'s';
1458 1 $format2 = '%'.$len2.'s';
1459  
1460 1 ▶ if ($len1 > 5){
Branch: 1 branch
BranchTrueFalseTotal
if ($len1 > 5) 1 0 1
1461 1 ▶ if ($dtype == $PDL_F) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dtype == $PDL_F) { } 0 1 1
elsif ($dtype == $PDL_D) { } 1 0 1
1462 0 $format1 = $PDL::Complex::floatformat;
1463 0 $findmax = 1;
1464 } elsif ($dtype == $PDL_D) {
1465 1 $format1 = $PDL::Complex::doubleformat;
1466 1 $findmax = 1;
1467 } else {
1468 0 $findmax = 0;
1469 }
1470 }
1471 1 ▶ if($len2 > 5){
Branch: 1 branch
BranchTrueFalseTotal
if ($len2 > 5) 1 0 1
1472 1 ▶ if ($dtype == $PDL_F) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dtype == $PDL_F) { } 0 1 1
elsif ($dtype == $PDL_D) { } 1 0 1
1473 0 $format2 = $PDL::Complex::floatformat;
1474 0 $findmax = 1;
1475 } elsif ($dtype == $PDL_D) {
1476 1 $format2 = $PDL::Complex::doubleformat;
1477 1 $findmax = 1;
1478 } else {
1479 0 ▶ $findmax = 0 unless $findmax;
Branch: 1 branch
BranchTrueFalseTotal
unless $findmax 0 0 0
1480 }
1481 }
1482 }
1483  
1484 1 ▶ if($findmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($findmax) 1 0 1
1485 1 $len1 = $len2=0;
1486  
1487 1 ▶ if ( $badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($badflag) { } 0 1 1
1488 0 ▶ for($i=0; $i<=$#$x; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $x->$#*) 0 0 0
1489 0 $findmax = $i % 2;
1490 0 ▶ if ( $$x[$i] eq 'BAD' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($x->[$i] eq 'BAD') { } 0 0 0
1491 0 $f = 3;
1492 }
1493 else{
1494 0 ▶ $f = $findmax ? length(sprintf $format2,$$x[$i]) :
Branch: 1 branch
BranchTrueFalseTotal
$findmax ? : 0 0 0
1495 length(sprintf $format1,$$x[$i]);
1496 }
1497 0 ▶ if ($findmax){
Branch: 1 branch
BranchTrueFalseTotal
if ($findmax) { } 0 0 0
1498 0 ▶ $len2 = $f if $f > $len2;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len2 0 0 0
1499 }
1500 else{
1501 0 ▶ $len1 = $f if $f > $len1;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len1 0 0 0
1502 }
1503 }
1504 } else {
1505 1 ▶ for ($i=0; $i<=$#$x; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $x->$#*) 2 1 3
1506 2 ▶ if ($i % 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) { } 1 1 2
1507 1 $f = length(sprintf $format2,$$x[$i]);
1508 1 ▶ $len2 = $f if $f > $len2;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len2 1 0 1
1509 }
1510 else{
1511 1 $f = length(sprintf $format1,$$x[$i]);
1512 1 ▶ $len1 = $f if $f > $len1;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $len1 1 0 1
1513 }
1514 }
1515 }
1516  
1517  
1518 } # if: $findmax
1519  
1520 1 $ret = "\n" . ' 'x$level . "[\n";
1521 {
1522 1 my $level = $level+1;
1523 1 $ret .= ' 'x$level .'[';
1524 1 $len2 += 2;
1525  
1526 1 ▶ for ($i=0; $i<=$#$x; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $x->$#*) 2 1 3
1527 2 $findmax = $i % 2;
1528 2 ▶ if ($findmax){
Branch: 1 branch
BranchTrueFalseTotal
if ($findmax) { } 1 1 2
1529 1 ▶ if ( $badflag and $$x[$i] eq 'BAD' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($badflag and $x->[$i] eq 'BAD') { } 0 1 1
Condition33%1/3
$badflag and $x->[$i] eq "BAD"
!ll&&!rl&&r
100
MC/DC0%0/2
$badflag and $x->[$i] eq "BAD"
$badflag $x->[$i] eq "BAD"
Truth table33%1/3
A and B
A= $badflagB= $x->[$i] eq "BAD"
ABresult
0X0
100
111
1530 #||
1531 #($findmax && $$x[$i - 1 ] eq 'BAD') ||
1532 #(!$findmax && $$x[$i +1 ] eq 'BAD')){
1533 0 $f = "BAD";
1534 }
1535 else{
1536 1 $f = sprintf $format2, $$x[$i];
1537 1 ▶ if (substr($$x[$i],0,1) eq '-'){
Branch: 1 branch
BranchTrueFalseTotal
if (substr($x->[$i], 0, 1) eq '-') { } 0 1 1
1538 0 $f.='i';
1539 }
1540 else{
1541 1 $f =~ s/(\s*)(.*)/+$2i/;
1542 }
1543 }
1544 1 $t = $len2-length($f);
1545 }
1546 else{
1547 1 ▶ if ( $badflag and $$x[$i] eq 'BAD' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($badflag and $x->[$i] eq 'BAD') { } 0 1 1
Condition33%1/3
$badflag and $x->[$i] eq "BAD"
!ll&&!rl&&r
100
MC/DC0%0/2
$badflag and $x->[$i] eq "BAD"
$badflag $x->[$i] eq "BAD"
Truth table33%1/3
A and B
A= $badflagB= $x->[$i] eq "BAD"
ABresult
0X0
100
111
1548 0 $f = "BAD";
1549 }
1550 else{
1551 1 $f = sprintf $format1, $$x[$i];
1552 1 $t = $len1-length($f);
1553 }
1554 }
1555  
1556 2 ▶ $f = ' 'x$t.$f if $t>0;
Branch: 1 branch
BranchTrueFalseTotal
if $t > 0 0 2 2
1557  
1558 2 $ret .= $f;
1559 2 ▶ if (($i+1)%($dims[1]*2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($i + 1) % ($dims[1] * 2)) { } 1 1 2
1560 1 ▶ $ret.=$sep if $findmax;
Branch: 1 branch
BranchTrueFalseTotal
if $findmax 0 1 1
1561 }
1562 else{ # End of output line
1563 1 $ret.=']';
1564 1 ▶ if ($i==$#$x) { # very last number
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $x->$#*) { } 1 0 1
1565 1 $ret.="\n";
1566 }
1567 else{
1568 0 $ret.= $sep2."\n" . ' 'x$level .'[';
1569 }
1570 }
1571 }
1572 }
1573 1 $ret .= ' 'x$level."]\n";
1574 1 return $ret;
1575 }
1576  
1577 }
1578  
1579 =head1 AUTHOR
1580  
1581 Copyright (C) 2000 Marc Lehmann <pcg@goof.com>.
1582 All rights reserved. There is no warranty. You are allowed
1583 to redistribute this software / documentation as described
1584 in the file COPYING in the PDL distribution.
1585  
1586 =head1 SEE ALSO
1587  
1588 perl(1), L<PDL>.
1589  
1590 =cut
1591  
1592  
1593  
1594  
1595  
1596  
1597 # Exit with OK status
1598  
1599 3 1;
1600  
1601
« blib/lib/PDL/IO/IDL.pm ↑ Summary blib/lib/PDL/Doc.pm »