-
Notifications
You must be signed in to change notification settings - Fork 135
/
roots.t
151 lines (142 loc) · 4.54 KB
/
roots.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use v6;
use Test;
plan 55;
# L<S32::Numeric/Numeric/"=item roots">
sub approx($a, $b){
my ($x,$y);
my $eps = 1e-3;
# coerce both to Complex
$x = $a + 0i;
$y = $b + 0i;
my $re = abs($x.re - $y.re);
my $im = abs($x.im - $y.im);
# both real and imag part must be with $eps of expected
return ( $re < $eps && $im < $eps );
}
sub has_approx($n, @list) {
for @list -> $i {
if approx($i, $n) {
return 1;
}
}
return Mu;
}
{
my @l = roots(0, 1);
ok(@l.elems == 1, 'roots(0, 1) returns 1 element');
ok(has_approx(0, @l), 'roots(0, 1) contains 0');
}
{
my @l = roots(0, 0);
ok(@l.elems == 1, 'roots(0, 0) returns 1 element');
ok(@l[0] ~~ 'NaN', 'roots(0,0) returns NaN');
}
{
my @l = roots(0, -1);
ok(@l.elems == 1, 'roots(0, -1) returns 1 element');
ok(@l[0] ~~ 'NaN', 'roots(0,-1) returns NaN');
}
{
my @l = roots(100, -1);
ok(@l.elems == 1, 'roots(100, -1) returns 1 element');
ok(@l[0] ~~ 'NaN', 'roots(100,-1) returns NaN');
}
{
my @m = roots(1, 0);
ok(@m.elems == 1, 'roots(1, 0) returns 1 element');
ok(@m[0] ~~ 'NaN', 'roots(1,0) returns NaN');
}
{
my @m = roots(Inf, 0);
ok(@m.elems == 1, 'roots(Inf, 0) returns 1 element');
ok(@m[0] ~~ 'NaN', 'roots(Inf,0) returns NaN');
}
{
my @m = roots(-Inf, 0);
ok(@m.elems == 1, 'roots(-Inf, 0) returns 1 element');
ok(@m[0] ~~ 'NaN', 'roots(-Inf,0) returns NaN');
}
{
my @m = roots(NaN, 0);
ok(@m.elems == 1, 'roots(NaN, 0) returns 1 element');
ok(@m[0] ~~ 'NaN', 'roots(NaN,0) returns NaN');
}
{
my @l = roots(4, 2);
ok(@l.elems == 2, 'roots(4, 2) returns 2 elements');
ok(has_approx(2, @l), 'roots(4, 2) contains 2');
ok(has_approx(-2, @l), 'roots(4, 2) contains -2');
}
{
my @l = roots(-1, 2);
ok(@l.elems == 2, 'roots(-1, 2) returns 2 elements');
ok(has_approx(1i, @l), 'roots(-1, 2) contains 1i');
ok(has_approx(-1i, @l), 'roots(-1, 2) contains -1i');
}
#?pugs todo 'feature'
{
my @l = 16.roots(4);
ok(@l.elems == 4, '16.roots(4) returns 4 elements');
ok(has_approx(2, @l), '16.roots(4) contains 2');
ok(has_approx(-2, @l), '16.roots(4) contains -2');
ok(has_approx(2i, @l), '16.roots(4) contains 2i');
ok(has_approx(-2i, @l), '16.roots(4) contains -2i');
}
{
my @l = (-1).roots(2);
ok(@l.elems == 2, '(-1).roots(2) returns 2 elements');
ok(has_approx(1i, @l), '(-1).roots(2) contains i');
ok(has_approx(-1i, @l), '(-1).roots(2) contains -i');
}
{
my @l = 0e0.roots(2);
ok(@l.elems == 2, '0e0.roots(2) returns 2 elements');
ok(has_approx(0, @l), '0e0.roots(2) contains 0');
}
{
my @l = roots(NaN, 1);
ok(@l.elems == 1, 'roots(NaN, 1) returns 1 element');
ok(@l[0] ~~ NaN, 'roots(NaN,1) returns NaN');
}
{
my @l = roots(Inf, 1);
ok(@l.elems == 1, 'roots(Inf, 1) returns 1 element');
ok(@l[0] ~~ Inf, 'roots(Inf,1) returns Inf');
}
my $pi = 312689/99532;
{
my @l = roots(1i,2);
ok(@l.elems == 2, 'roots(1i,2) returns 2 elements');
ok(has_approx(exp(5i*$pi/4), @l), 'exp(5i*$pi/4) is a square root of i');
ok(has_approx(exp(1i*$pi/4), @l), 'exp(1i*$pi/4) is a square root of i');
}
{
my @l = roots(1+1i,2);
ok(@l.elems == 2, 'roots(1+1i,2) returns 2 elements');
ok(has_approx(exp(log(2)/4 + 1i*$pi/8), @l),'exp(log(2)/4 + 1i*$pi/8) is a square root of 1+1i');
ok(has_approx(exp(log(2)/4 + 9i*$pi/8), @l),'exp(log(2)/4 + 9i*$pi/8) is a square root of 1+1i');
}
{
my @l = 8.roots(3);
ok(@l.elems == 3, '8.roots(3) returns 3 elements');
ok(has_approx(2,@l), '2 is a cube root of 8');
ok(has_approx(exp(1/3*(log(8) + 2i*$pi)),@l), 'exp(1/3*(log(8) + 2i*$pi)) is a cube root of 8');
ok(has_approx(exp(1/3*(log(8) + 4i*$pi)),@l), 'exp(1/3*(log(8) + 4i*$pi)) is a cube root of 8');
}
{
my @l = (-8).Num.roots(3);
ok(@l.elems == 3, '(-8).roots(3) returns 3 elements');
ok(has_approx(-2,@l), '2 is a cube root of -8');
ok(has_approx(exp(1/3*(log(8) + 3i*$pi)),@l), 'exp(1/3*(log(8) + 3i*$pi)) is a cube root of -8');
ok(has_approx(exp(1/3*(log(8) + 5i*$pi)),@l), 'exp(1/3*(log(8) + 5i*$pi)) is a cube root of -8');
}
{
my @l = 8.5.roots(4);
ok(@l.elems == 4, '8.5.roots(4) returns 4 elements');
my $quartic = 8.5 ** .25;
ok(has_approx($quartic, @l), '8.5 ** 1/4 is a quartic root of 8.5');
ok(has_approx(-$quartic, @l), '-(8.5 ** 1/4) is a quartic root of 8.5');
ok(has_approx($quartic\i, @l), '(8.5 ** 1/4)i is a quartic root of 8.5');
ok(has_approx(-$quartic\i, @l), '-(8.5 ** 1/4)i is a quartic root of 8.5');
}
# vim: ft=perl6