I assembled a few tests to exercise some aspects of PDL::Complex. Far
from complete yet. I'll continue later. Many are within TODO blocks
and/or within SKIP blocks, as they currently fail. A few of the
failing tests are not real failures, but failures to meet what I would
have expected. I'm sending the output from git diff. Is this useful? 

Regards,
Luis


On Thu, Dec 06, 2018 at 11:35:38PM -0700, Derek Lamb wrote:
> Hi Luis,
> 
> Thanks for the bug report.
> 
> As you point out, these functions are undocumented.  At first that suggests 
> to me that they are for internal use only.  But then I see that they overload 
> the Perl operator atan2.  So I guess it needs to be corrected!
> 
> Using your implementation with the complex log, I get the expected result:
> 
> perl -Mblib -MPDL -MPDL::Complex -wE 'say atan2(pdl(1)->r2C,pdl(0)->r2C);'
> 1.5708 +0i
> 
> (although atan2 should not return a complex number.)
> 
> Also note that there is already Carg, which does the same thing:
> pdl> p Carg(0+1*i);
> 1.5707963267949
> 
> PDL::Complex is seriously lacking in tests.  Do you have any interest in 
> contributing some tests to t/complex.t?
> 
> To illustrate the problem, 'make test' succeeds with the current git, when I 
> swap the arguments in the subs Catan2 and atan2, and when I replace them with 
> the complex log as you suggested.  That means the Catan2 code is never 
> exercised by the test suite.  Almost certainly there are other functions that 
> don't get exercised either.  Evil edge cases are best, like the one you found 
> here, but even just making sure each subroutine gets exercised once in the 
> test suite helps, too.
> 
> I also noticed that the documentation is lacking: a list of overloaded 
> operators is clearly needed, but that shouldn't be too hard.
> 
> best,
> Derek
> 
> > On Dec 5, 2018, at 11:26 AM, Luis Mochan <[email protected]> wrote:
> > 
> > I believe I found a mistake in the undocumented PDL::Complex functions 
> > Catan2 and atan2. 
> > 
> > pdl> use PDL::Complex
> > pdl> p atan2(1,0)   # perl's function yields PI/2
> > 1.5707963267949
> > pdl> p atan2(pdl(1),pdl(0)) # PDL's function also
> > 1.5707963267949
> > pdl> p atan2(pdl(1)->r2C,pdl(0)->r2C) # but PDL::Complex's function 
> > disagrees
> > 0 +0i
> > pdl> p atan2(pdl(0)->r2C, pdl(1)->r2C) # The first argument can't be zero.
> > NaN +NaNi
> > pdl> p atan2(pdl(0.000001)->r2C, pdl(1)->r2C) # But if ~0 result is not the 
> > expected 0
> > 1.5708 -5.55112e-17i
> > pdl>
> > 
> > I believe the error is in lines 905 and 906 of Basic/Complex/complex.pd
> > 
> > sub Catan2($$) { Catan Cdiv $_[1], $_[0] }
> > sub atan2($$)  { Catan Cdiv $_[1], $_[0] }
> > 
> > where the 1's and 0's should be swapped.
> > 
> > sub Catan2($$) { Catan Cdiv $_[0], $_[1] }
> > sub atan2($$)  { Catan Cdiv $_[0], $_[1] }
> > 
> > I guess it would be even better to use the complex log,
> > 
> > sub Catan2($$) { Clog( $_[1] + i*$_[0])/i }
> > 
> > as it wouldn't fail if the second argument is zero; it's real part
> > would coincide with the usual real atan2 of two real arguments.
> > 
> > 
> > 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          | [email protected]   /\_/\__/
> > GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16  C2DF 5F0A C52B 791E B9EB
> > 
> > 
> > 
> > 
> > _______________________________________________
> > pdl-general mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/pdl-general
> > 
> 
> 

-- 

                                                                  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          | [email protected]   /\_/\__/
GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16  C2DF 5F0A C52B 791E B9EB


diff --git a/t/complex.t b/t/complex.t
index 9156cf7e..4b1c0184 100644
--- a/t/complex.t
+++ b/t/complex.t
@@ -3,16 +3,26 @@ use PDL::Complex;
 use PDL::Config;
 
 BEGIN {
-   use Test::More tests => 22;
+   use Test::More tests => 38;
 }
 
 sub tapprox {
-        my($x,$y) = @_;
-        my $c = abs($x-$y);
-        my $d = max($c);
-        $d < 0.0001;
+	my($x,$y) = @_;
+	my $c = abs($x-$y);
+	my $d = max($c);
+	$d < 0.0001;
 }
 
+$ref = pdl(-1,1);
+$x = i - 1;
+
+ok(ref $x eq PDL::Complex, 'type promotion i - scalar');
+ok(tapprox($x->real,$ref), 'value from i - scalar');
+
+$x = 1-i;
+ok(ref $x eq PDL::Complex, 'type promption scalar - i');
+ok(tapprox($x->real,-$ref), 'value from scalar - i');
+
 $ref = pdl([[-2,1],[-3,1]]);
 $x = i - pdl(2,3);
 
@@ -28,6 +38,20 @@ $ar = $x->real;
 $ar++;
 ok(tapprox($x->real, -$ref+1), 'complex to real dataflow');
 
+# dataflow from real to complex when using cplx
+
+$refc=$ref->copy;
+$ac = $refc->cplx;
+$ac .= $ac-1-i;
+ok(tapprox($refc, $ref-1), 'real to complex dataflow');
+
+# no dataflow from real to complex when complex
+
+$refc=$ref->copy;
+$ac = $refc->complex;
+$ac .= $ac-1-i;
+ok(tapprox($refc->real, $ref-1), 'real to complex dataflow');
+
 # Check that converting from re/im to mag/ang and
 #  back we get the same thing
 $x = cplx($ref);
@@ -69,8 +93,56 @@ $r **= 2;
 ok($r->at(0) < 100.000000001 && $r->at(0) > 99.999999999 && $r->at(1) == 0,
   'check that imaginary part is exactly zero'); # Wasn't always so
 
-TODO: {
-   local $TODO = "Known_problems sf.net bug #1176614" if ($PDL::Config{SKIP_KNOWN_PROBLEMS} or exists $ENV{SKIP_KNOWN_PROBLEMS} );
+#Check sumover and prodover
+$x=sequence(2,3)+1;
+$y=$x->copy->complex;
+ok(ref $y->sumover eq 'PDL::Complex', 'Type of sumover');
+is($y->sumover->dim(0), 2, 'Dimension 0 of sumover');
+
+ok(ref $y->prodover eq 'PDL::Complex', 'Type of prodover');
+ TODO: {
+     local $TODO="Complex prodover multiplies over dimension 0, not 1";
+     is($y->prodover->dim(0), 2, 'Dimension 0 of prodover');
+   SKIP: {
+       todo_skip "Complex prodover multiplies over dimension 0, not 1",
+	   1 if $y->prodover->dim(0) != 2;
+       ok(tapprox($y->prodover->real, $x->mv(1,0)->prodover),
+	  'Value of prodover');
+     }
+}
+
+#Check sum
+ TODO: {
+     local $TODO="Complex sum works miraculously only for 2,n,m PDL's";
+   SKIP: {
+       todo_skip "Complex sum for 2,n Complex PDL's doesn't work", 1;
+       $x=sequence(2,3)+1;
+       $y=$x->copy->complex;
+       ok(ref $y->sum eq 'PDL::Complex', 'Type of sum');
+       is($y->sum->dims, 1, 'Dimensions of sum');
+       is($y->sum->dim(0), 2, 'Dimension 0 of sum');
+       ok(tapprox($y->sum->real, $x->mv(1,0)->sumover), 'Value of sum');
+     }
+}
+
+#Check prod
+ TODO: {
+     local $TODO="Complex prod doesn't work at all";
+   SKIP: {
+       todo_skip "Complex sum for 2,n Complex PDL's doesn't work", 1;
+       $x=sequence(2,3)+1;
+       $y=$x->copy->complex;
+       ok(ref $y->prod eq 'PDL::Complex', 'Type of prod');
+       is($y->prod->dims, 1, 'Dimensions of prod');
+       is($y->prod->dim(0), 2, 'Dimension 0 of prod');
+       ok(tapprox($y->prod->real, $y->prodover->real),
+	  'Value of prod');
+     }
+}
+
+
+ TODO: {
+     local $TODO = "Known_problems sf.net bug #1176614" if ($PDL::Config{SKIP_KNOWN_PROBLEMS} or exists $ENV{SKIP_KNOWN_PROBLEMS} );
 
 
    # Check stringification of complex piddle
@@ -81,6 +153,22 @@ TODO: {
    ok($c211str=~/(9.123|4.123)/, 'sf.net bug #1176614');
 }
 
+TODO: {
+      local $TODO="Cpow doesn't understand a real argument";
+      ok(tapprox(Cpow(i,2)->real, pdl(1,0)), 'real power of i');
+      ok(tapprox(Cpow(i,pdl(2))->real, pdl(1,0)), 'real pdl power of i');
+}
+
+TODO: {
+      local $TODO="Autoincrement creates new copy, so doesn't flow";
+
+      # autoincrement flow
+      $x=i;
+      $y=$x;
+      $y++;
+      ok(tapprox($x->real, $y->real), 'autoincrement flow');
+      diag("$x should have equaled $y");
+}
 
 $x=3+4*i;$y=4+2*i;$c=1+1*i;
 is(Ccmp(Cmul($x,$y),4+22*i),0,"Cmul");
@@ -88,3 +176,18 @@ is(Ccmp($x*$y,4+22*i),0,"overloaded *");
 is(Ccmp(Cdiv($x,$y),1 + 0.5*i),0,"Cdiv");
 is(Ccmp($x/$y,1+0.5*i),0,"overloaded /");
 ok(tapprox(Cabs(atan2(pdl(1)->r2C,pdl(0)->r2C)),PDL::Math::asin(1)),"atan2");
+
+TODO: {
+      local $TODO="Transpose of complex data should leave 0-th dimension alone";
+
+      $x=sequence(2,3,4)->complex;
+      $y=$x->transpose;
+      is($y->dim(0),2, "Keep dimension 0 when transposing");
+}
+TODO: {
+      local $TODO="complex numbers should not be so after moving dimension 0";
+
+      $x=sequence(2,2)->complex;
+      $y=$x->mv(0,1);
+      ok(ref $y eq 'PDL', 'PDL::Complex becomes real PDL after moving dim 0');
+}
_______________________________________________
pdl-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pdl-general

Reply via email to