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