1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Copyright ©2017 The Gonum Authors. All rights reserved.
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file.
8
9package cmplx64
10
11import (
12	"testing"
13
14	math "gonum.org/v1/gonum/internal/math32"
15)
16
17// The higher-precision values in vc26 were used to derive the
18// input arguments vc (see also comment below). For reference
19// only (do not delete).
20var vc26 = []complex64{
21	(4.97901192488367350108546816 + 7.73887247457810456552351752i),
22	(7.73887247457810456552351752 - 0.27688005719200159404635997i),
23	(-0.27688005719200159404635997 - 5.01060361827107492160848778i),
24	(-5.01060361827107492160848778 + 9.63629370719841737980004837i),
25	(9.63629370719841737980004837 + 2.92637723924396464525443662i),
26	(2.92637723924396464525443662 + 5.22908343145930665230025625i),
27	(5.22908343145930665230025625 + 2.72793991043601025126008608i),
28	(2.72793991043601025126008608 + 1.82530809168085506044576505i),
29	(1.82530809168085506044576505 - 8.68592476857560136238589621i),
30	(-8.68592476857560136238589621 + 4.97901192488367350108546816i),
31}
32
33var vc = []complex64{
34	(4.9790119248836735e+00 + 7.7388724745781045e+00i),
35	(7.7388724745781045e+00 - 2.7688005719200159e-01i),
36	(-2.7688005719200159e-01 - 5.0106036182710749e+00i),
37	(-5.0106036182710749e+00 + 9.6362937071984173e+00i),
38	(9.6362937071984173e+00 + 2.9263772392439646e+00i),
39	(2.9263772392439646e+00 + 5.2290834314593066e+00i),
40	(5.2290834314593066e+00 + 2.7279399104360102e+00i),
41	(2.7279399104360102e+00 + 1.8253080916808550e+00i),
42	(1.8253080916808550e+00 - 8.6859247685756013e+00i),
43	(-8.6859247685756013e+00 + 4.9790119248836735e+00i),
44}
45
46// The expected results below were computed by the high precision calculators
47// at http://keisan.casio.com/.  More exact input values (array vc[], above)
48// were obtained by printing them with "%.26f".  The answers were calculated
49// to 26 digits (by using the "Digit number" drop-down control of each
50// calculator).
51
52var abs = []float32{
53	9.2022120669932650313380972e+00,
54	7.7438239742296106616261394e+00,
55	5.0182478202557746902556648e+00,
56	1.0861137372799545160704002e+01,
57	1.0070841084922199607011905e+01,
58	5.9922447613166942183705192e+00,
59	5.8978784056736762299945176e+00,
60	3.2822866700678709020367184e+00,
61	8.8756430028990417290744307e+00,
62	1.0011785496777731986390856e+01,
63}
64
65var conj = []complex64{
66	(4.9790119248836735e+00 - 7.7388724745781045e+00i),
67	(7.7388724745781045e+00 + 2.7688005719200159e-01i),
68	(-2.7688005719200159e-01 + 5.0106036182710749e+00i),
69	(-5.0106036182710749e+00 - 9.6362937071984173e+00i),
70	(9.6362937071984173e+00 - 2.9263772392439646e+00i),
71	(2.9263772392439646e+00 - 5.2290834314593066e+00i),
72	(5.2290834314593066e+00 - 2.7279399104360102e+00i),
73	(2.7279399104360102e+00 - 1.8253080916808550e+00i),
74	(1.8253080916808550e+00 + 8.6859247685756013e+00i),
75	(-8.6859247685756013e+00 - 4.9790119248836735e+00i),
76}
77
78var sqrt = []complex64{
79	(2.6628203086086130543813948e+00 + 1.4531345674282185229796902e+00i),
80	(2.7823278427251986247149295e+00 - 4.9756907317005224529115567e-02i),
81	(1.5397025302089642757361015e+00 - 1.6271336573016637535695727e+00i),
82	(1.7103411581506875260277898e+00 + 2.8170677122737589676157029e+00i),
83	(3.1390392472953103383607947e+00 + 4.6612625849858653248980849e-01i),
84	(2.1117080764822417640789287e+00 + 1.2381170223514273234967850e+00i),
85	(2.3587032281672256703926939e+00 + 5.7827111903257349935720172e-01i),
86	(1.7335262588873410476661577e+00 + 5.2647258220721269141550382e-01i),
87	(2.3131094974708716531499282e+00 - 1.8775429304303785570775490e+00i),
88	(8.1420535745048086240947359e-01 + 3.0575897587277248522656113e+00i),
89}
90
91// special cases
92var vcAbsSC = []complex64{
93	NaN(),
94}
95var absSC = []float32{
96	math.NaN(),
97}
98var vcConjSC = []complex64{
99	NaN(),
100}
101var conjSC = []complex64{
102	NaN(),
103}
104var vcIsNaNSC = []complex64{
105	complex(math.Inf(-1), math.Inf(-1)),
106	complex(math.Inf(-1), math.NaN()),
107	complex(math.NaN(), math.Inf(-1)),
108	complex(0, math.NaN()),
109	complex(math.NaN(), 0),
110	complex(math.Inf(1), math.Inf(1)),
111	complex(math.Inf(1), math.NaN()),
112	complex(math.NaN(), math.Inf(1)),
113	complex(math.NaN(), math.NaN()),
114}
115var isNaNSC = []bool{
116	false,
117	false,
118	false,
119	true,
120	true,
121	false,
122	false,
123	false,
124	true,
125}
126var vcSqrtSC = []complex64{
127	NaN(),
128}
129var sqrtSC = []complex64{
130	NaN(),
131}
132
133// functions borrowed from pkg/math/all_test.go
134func tolerance(a, b, e float32) bool {
135	d := a - b
136	if d < 0 {
137		d = -d
138	}
139
140	// note: b is correct (expected) value, a is actual value.
141	// make error tolerance a fraction of b, not a.
142	if b != 0 {
143		e = e * b
144		if e < 0 {
145			e = -e
146		}
147	}
148	return d < e
149}
150func veryclose(a, b float32) bool { return tolerance(a, b, 1e-7) }
151func alike(a, b float32) bool {
152	switch {
153	case a != a && b != b: // math.IsNaN(a) && math.IsNaN(b):
154		return true
155	case a == b:
156		return math.Signbit(a) == math.Signbit(b)
157	}
158	return false
159}
160
161func cTolerance(a, b complex64, e float32) bool {
162	d := Abs(a - b)
163	if b != 0 {
164		e = e * Abs(b)
165		if e < 0 {
166			e = -e
167		}
168	}
169	return d < e
170}
171func cVeryclose(a, b complex64) bool { return cTolerance(a, b, 1e-7) }
172func cAlike(a, b complex64) bool {
173	switch {
174	case IsNaN(a) && IsNaN(b):
175		return true
176	case a == b:
177		return math.Signbit(real(a)) == math.Signbit(real(b)) && math.Signbit(imag(a)) == math.Signbit(imag(b))
178	}
179	return false
180}
181
182func TestAbs(t *testing.T) {
183	for i := 0; i < len(vc); i++ {
184		if f := Abs(vc[i]); !veryclose(abs[i], f) {
185			t.Errorf("Abs(%g) = %g, want %g", vc[i], f, abs[i])
186		}
187	}
188	for i := 0; i < len(vcAbsSC); i++ {
189		if f := Abs(vcAbsSC[i]); !alike(absSC[i], f) {
190			t.Errorf("Abs(%g) = %g, want %g", vcAbsSC[i], f, absSC[i])
191		}
192	}
193}
194func TestConj(t *testing.T) {
195	for i := 0; i < len(vc); i++ {
196		if f := Conj(vc[i]); !cVeryclose(conj[i], f) {
197			t.Errorf("Conj(%g) = %g, want %g", vc[i], f, conj[i])
198		}
199	}
200	for i := 0; i < len(vcConjSC); i++ {
201		if f := Conj(vcConjSC[i]); !cAlike(conjSC[i], f) {
202			t.Errorf("Conj(%g) = %g, want %g", vcConjSC[i], f, conjSC[i])
203		}
204	}
205}
206func TestIsNaN(t *testing.T) {
207	for i := 0; i < len(vcIsNaNSC); i++ {
208		if f := IsNaN(vcIsNaNSC[i]); isNaNSC[i] != f {
209			t.Errorf("IsNaN(%v) = %v, want %v", vcIsNaNSC[i], f, isNaNSC[i])
210		}
211	}
212}
213func TestSqrt(t *testing.T) {
214	for i := 0; i < len(vc); i++ {
215		if f := Sqrt(vc[i]); !cVeryclose(sqrt[i], f) {
216			t.Errorf("Sqrt(%g) = %g, want %g", vc[i], f, sqrt[i])
217		}
218	}
219	for i := 0; i < len(vcSqrtSC); i++ {
220		if f := Sqrt(vcSqrtSC[i]); !cAlike(sqrtSC[i], f) {
221			t.Errorf("Sqrt(%g) = %g, want %g", vcSqrtSC[i], f, sqrtSC[i])
222		}
223	}
224}
225
226func BenchmarkAbs(b *testing.B) {
227	for i := 0; i < b.N; i++ {
228		Abs(complex(2.5, 3.5))
229	}
230}
231func BenchmarkConj(b *testing.B) {
232	for i := 0; i < b.N; i++ {
233		Conj(complex(2.5, 3.5))
234	}
235}
236func BenchmarkSqrt(b *testing.B) {
237	for i := 0; i < b.N; i++ {
238		Sqrt(complex(2.5, 3.5))
239	}
240}
241