Skip to content

Commit 19cca0f

Browse files
committed
Basic/t/nat_complex.t replace tapprox with is_pdl - #34
1 parent a17e954 commit 19cca0f

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

Basic/t/nat_complex.t

+16-41
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ use PDL::Core::Dev;
55
use PDL::Types qw(ppdefs ppdefs_complex ppdefs_all);
66

77
use Test::More;
8-
9-
sub tapprox {
10-
my($x,$y) = @_;
11-
my $c = abs($x-$y);
12-
my $d = max($c);
13-
$d < 0.0001;
14-
}
8+
use Test::PDL;
159

1610
is_deeply [ ppdefs() ], [qw(A B S U L K N P Q F D E)];
1711
is_deeply [ ppdefs_complex() ], [qw(G C H)];
@@ -22,7 +16,7 @@ my $ref2 = squeeze(czip($ref->slice("0,"), $ref->slice("1,")));
2216
my $x = i() -pdl (-2, -3);
2317

2418
is($x->type, 'cdouble', 'type promotion i - ndarray');
25-
ok(tapprox($x->im,$ref->slice("1,:")), 'value from i - ndarray');
19+
is_pdl $x->im, $ref->slice("(1),:"), 'value from i - ndarray';
2620
ok !$x->type->real, 'complex type not real';
2721
ok double->real, 'real type is real';
2822
ok !$x->sumover->type->real, 'sumover type=complex';
@@ -34,7 +28,7 @@ is $x->re->type, 'double', 'real real part';
3428
my $y=cfloat($x);
3529
is type($y), 'cfloat', 'type conversion to cfloat';
3630
is $y->re->type, 'float', 'real real part';
37-
ok(tapprox($x->im,$ref->slice("0,1")), 'value from ndarray - i') or diag 'got: ', $x->im;
31+
is_pdl $x->im, $ref->slice("(0),(1),*2"), 'value from ndarray - i';
3832
is zeroes($_->[0], 2)->r2C->type, $_->[1], "r2C $_->[0] -> $_->[1]"
3933
for [byte, 'cdouble'], [long, 'cdouble'],
4034
[float, 'cfloat'], [cfloat, 'cfloat'],
@@ -69,64 +63,45 @@ for (float, double, ldouble, cfloat, cdouble, cldouble) {
6963
# dataflow from complex to real
7064
my $ar = $x->re;
7165
$ar++;
72-
ok(tapprox($x->re, -$ref->slice("0,")->squeeze + 1), 'complex to real dataflow') or diag "got=".$x->re, "expected=".(-$ref->slice("0,")->squeeze + 1);
66+
is_pdl $x->re, -$ref->slice("0,")->squeeze + 1, 'complex to real dataflow';
7367
my $ai = $x->im;
7468
$x+=i;
7569
my $expected = pdl(-2, -2);
76-
ok(tapprox($x->im, $expected), 'dataflow after conversion')
77-
or diag "got=".$x->im, "\nexpected=$expected";
70+
is_pdl $x->im, $expected, 'dataflow after conversion';
7871
$ai++;
79-
$expected++;
80-
ok(tapprox($x->im, $expected), 'dataflow after change ->im')
81-
or diag "got=".$x->im, "\nexpected=$expected";
72+
is_pdl $x->im, $expected+1, 'dataflow after change ->im';
8273
}
8374

8475
# Check that converting from re/im to mag/ang and
8576
# back we get the same thing
8677
$x = $ref2->copy;
8778
my $a=abs($x);
8879
my $p=carg($x)->double; # force to double to avoid glibc bug 18594
89-
90-
$y = czip($a*cos($p), $a*sin($p));
91-
ok(tapprox($x-$y, 0.), 'check re/im and mag/ang equivalence')
92-
or diag "For ($x), got: ($y) from a=($a) p=($p) cos(p)=(", cos($p), ") sin(p)=(", sin($p), ")";
80+
is_pdl czip($a*cos($p), $a*sin($p)), $x, 'check re/im and mag/ang equivalence';
9381

9482
# Catan, Csinh, Ccosh, Catanh, Croots
9583

9684
my $cabs = sqrt($x->re**2+$x->im**2);
9785

9886
ok(abs($x)->type->real, 'Cabs type real');
99-
ok(tapprox(abs $x, $cabs), 'Cabs value') or diag "got: (@{[abs $x]}), expected ($cabs)";
100-
ok(tapprox(abs2($x), $cabs**2), 'Cabs2 value') or diag "got: (@{[abs2 $x]}), expected (", $cabs**2, ")";
87+
is_pdl abs $x, $cabs, 'Cabs value';
88+
is_pdl abs2($x), $cabs**2, 'Cabs2 value';
10189
ok abs2(cdouble(5))->type->real, 'abs2 always real';
10290
ok(carg($x)->type->real, 'Carg type real');
103-
ok(tapprox(carg($x), atan2($x->im, $x->re)), 'Carg value');
91+
is_pdl carg($x), atan2($x->im, $x->re), 'Carg value';
10492

105-
{
106-
# Check cat'ing
107-
$y = $x->re->copy + 1;
108-
my $bigArray = $x->cat($y);
109-
my $sum = $bigArray->sum;
110-
my $cz = czip(8, -2);
111-
my $abs = abs($sum + $cz);
112-
ok(all($abs < .0001), 'check cat for complex') or diag "got:$abs from cat(x=$x y=$y): $bigArray";
113-
}
93+
is_pdl $x->cat($x->re->copy + 1), pdl('-2+i -3+i; -1 -2'), 'cat for complex';
11494

11595
if (PDL::Core::Dev::got_complex_version('pow', 2)) {
116-
ok(tapprox($x**2, $x * $x), '** op complex')
117-
or diag "For ($x), got: ", $x**2, ", expected: ", $x * $x;
118-
ok(tapprox($x->pow(2), $x * $x), 'complex pow')
119-
or diag "Got: ", $x->pow(2), ", expected: ", $x * $x;
120-
ok(tapprox($x->power(2, 0), $x * $x), 'complex power')
121-
or diag "Got: ", $x->power(2, 0), ", expected: ", $x * $x;
96+
is_pdl $x**2, $x * $x, '** op complex';
97+
is_pdl $x->pow(2), $x * $x, 'complex pow';
98+
is_pdl $x->power(2, 0), $x * $x, 'complex power';
12299
my $z = pdl(0) + i()*pdl(0);
123100
$z **= 2;
124-
ok(tapprox($z, i2C(0)), 'check that 0 +0i exponentiates correctly'); # Wasn't always so.
101+
is_pdl $z, i2C(0), 'check that 0 +0i exponentiates correctly'; # Wasn't always so.
125102
my $r = r2C(-10);
126103
$r **= 2;
127-
ok(tapprox($r, r2C(100)),
128-
'check that imaginary part is exactly zero') # Wasn't always so
129-
or diag "got: ", $r;
104+
is_pdl $r, r2C(100), 'check imaginary part exactly zero'; # Wasn't always so
130105
}
131106

132107
my $asin_2 = PDL::asin(2)."";

0 commit comments

Comments
 (0)