[Perldl] Complex oddity

Luis Mochan mochan at fis.unam.mx
Sat Aug 31 07:49:49 HST 2013


I found an strange behavior in PDL::Complex.
(I'm running  PDL v2.006 under perl5 (revision 5 version 16 subversion 1)) 

To illustrate this behavior, I declare a 1D complex array from a 2D real pdl:

pdl> $a=sequence(2,3)->cplx
pdl> p $a
[0 +1i  2 +3i  4 +5i]

# Pdl allows me to transpose the array, as if it were an ordinary 2x3 pdl:

pdl> p $a->transpose

[
 [0 2 4]
 [1 3 5]
]

# but it doesn't realize that after transposition it is no longer a 1D
# complex array  

pdl> p $a->transpose->info
PDL::Complex: Double D [3,2]

# Thus, further operations sometimes fail, such as

pdl> p $a->transpose+$a->transpose

Error in Cadd:Wrong dims
 at (eval 58) line 1.
        PDL::Complex::__ANON__('PDL::Complex=SCALAR(0x89f5248)', 'PDL::Complex=SCALAR(0x89f5088)', '') called at (eval 85) line 4
        main::__ANON__() called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 724
        eval {...} called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 724
        main::eval_and_report('p $a->transpose+$a->transpose\x{a}') called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 660
        main::process_input() called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 680
        eval {...} called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 680

# or

pdl> p $a->transpose*$a->transpose
Error in Cmul:Wrong dims
 at (eval 60) line 1.
        PDL::Complex::__ANON__('PDL::Complex=SCALAR(0x865a370)', 'PDL::Complex=SCALAR(0x89f50e8)', '') called at (eval 86) line 4
        main::__ANON__() called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 724
        eval {...} called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 724
        main::eval_and_report('p $a->transpose*$a->transpose\x{a}') called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 660
        main::process_input() called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 680
        eval {...} called at /home/mochan/perl5/perlbrew/perls/perl-5.16.1/bin/perldl line 680

# Had my array been 2x2 instead of 2x3, the last operation would have
# succeded, but yielding the wrong result:

pdl> p $a->transpose*$a->transpose

[
 [0 4]
 [1 9]
]

pdl> $b=$a->cplx

pdl> p $b->transpose*$b->transpose
[-4 +0i  -8 +6i]


# I guess that operations that destroy the representation of complex
# numbers should be disallowed (such as moving the 0-th dimension
# elsewhere), or produce a warning, or remove the 'Complex' character
# of the result

# I found another strange result when, erroneously using a complex
# function on a non-complex argument, without issuing an
# error/warning. For example, 
pdl> $a=pdl(4)
pdl> p sqrt($a)
2
pdl> p Csqrt($a)
[ 2.1973682 0.91017972]

When I changed Csqrt to (the correct) sqrt in the particular program
where I found this odd behavior, the results of an unrelated
calculation changed. I still haven't found if that is due to some
un-initialized memory or the likes, but I wonder if Csqrt may be
inadvertently changing values in illegal memory locations when it is
passed an erroneous argument.  

Best regards,
Luis

-- 

                                                                  o
W. Luis Mochán,                      | tel:(52)(777)329-1734     /<(*)
Instituto de Ciencias Físicas, UNAM  | fax:(52)(777)317-5388     `>/   /\
Apdo. Postal 48-3, 62251             |                           (*)/\/  \
Cuernavaca, Morelos, México          | mochan at fis.unam.mx   /\_/\__/
GPG: DD344B85,  2ADC B65A 5499 C2D3 4A3B  93F3 AE20 0F5E DD34 4B85





More information about the Perldl mailing list