| 1 |
from __future__ import with_statement |
| 2 |
|
| 3 |
import math |
| 4 |
import sys |
| 5 |
|
| 6 |
from pypy.conftest import option |
| 7 |
from pypy.interpreter.error import OperationError |
| 8 |
from pypy.interpreter.gateway import interp2app |
| 9 |
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest |
| 10 |
from rpython.rlib.rcomplex import c_pow |
| 11 |
|
| 12 |
|
| 13 |
def rAlmostEqual(a, b, rel_err=2e-15, abs_err=5e-323, msg=''): |
| 14 |
"""Fail if the two floating-point numbers are not almost equal. |
| 15 |
|
| 16 |
Determine whether floating-point values a and b are equal to within |
| 17 |
a (small) rounding error. The default values for rel_err and |
| 18 |
abs_err are chosen to be suitable for platforms where a float is |
| 19 |
represented by an IEEE 754 double. They allow an error of between |
| 20 |
9 and 19 ulps. |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
if math.isnan(a): |
| 25 |
if math.isnan(b): |
| 26 |
return True,'' |
| 27 |
raise AssertionError(msg + '%r should be nan' % (b,)) |
| 28 |
|
| 29 |
if math.isinf(a): |
| 30 |
if a == b: |
| 31 |
return True,'' |
| 32 |
raise AssertionError(msg + 'finite result where infinity expected: '+ \ |
| 33 |
'expected %r, got %r' % (a, b)) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
if not a and not b: |
| 40 |
if math.copysign(1., a) != math.copysign(1., b): |
| 41 |
raise AssertionError( msg + \ |
| 42 |
'zero has wrong sign: expected %r, got %r' % (a, b)) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
try: |
| 49 |
absolute_error = abs(b-a) |
| 50 |
except OverflowError: |
| 51 |
pass |
| 52 |
else: |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if absolute_error <= max(abs_err, rel_err * abs(a)): |
| 58 |
return True,'' |
| 59 |
raise AssertionError(msg + \ |
| 60 |
'%r and %r are not sufficiently close, %g > %g' %\ |
| 61 |
(a, b, absolute_error, max(abs_err, rel_err*abs(a)))) |
| 62 |
|
| 63 |
|
| 64 |
def parse_testfile(fname): |
| 65 |
"""Parse a file with test values |
| 66 |
|
| 67 |
Empty lines or lines starting with -- are ignored |
| 68 |
yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 69 |
""" |
| 70 |
with open(fname) as fp: |
| 71 |
for line in fp: |
| 72 |
|
| 73 |
if line.startswith('--') or not line.strip(): |
| 74 |
continue |
| 75 |
|
| 76 |
lhs, rhs = line.split('->') |
| 77 |
id, fn, arg_real, arg_imag = lhs.split() |
| 78 |
rhs_pieces = rhs.split() |
| 79 |
exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 80 |
flags = rhs_pieces[2:] |
| 81 |
|
| 82 |
yield (id, fn, |
| 83 |
float(arg_real), float(arg_imag), |
| 84 |
float(exp_real), float(exp_imag), |
| 85 |
flags |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
class AppTestUfuncs(BaseNumpyAppTest): |
| 90 |
def setup_class(cls): |
| 91 |
import os |
| 92 |
BaseNumpyAppTest.setup_class.im_func(cls) |
| 93 |
fname128 = os.path.join(os.path.dirname(__file__), 'complex_testcases.txt') |
| 94 |
fname64 = os.path.join(os.path.dirname(__file__), 'complex64_testcases.txt') |
| 95 |
cls.w_testcases128 = cls.space.wrap(list(parse_testfile(fname128))) |
| 96 |
cls.w_testcases64 = cls.space.wrap(list(parse_testfile(fname64))) |
| 97 |
|
| 98 |
cls.w_runAppDirect = cls.space.wrap(option.runappdirect) |
| 99 |
cls.w_isWindows = cls.space.wrap(os.name == 'nt') |
| 100 |
|
| 101 |
if cls.runappdirect: |
| 102 |
def cls_rAlmostEqual(space, *args, **kwargs): |
| 103 |
return rAlmostEqual(*args, **kwargs) |
| 104 |
cls.w_rAlmostEqual = cls.space.wrap(cls_rAlmostEqual) |
| 105 |
def cls_c_pow(space, *args): |
| 106 |
return c_pow(*args) |
| 107 |
cls.w_c_pow = cls.space.wrap(cls_c_pow) |
| 108 |
else: |
| 109 |
def cls_rAlmostEqual(space, __args__): |
| 110 |
args, kwargs = __args__.unpack() |
| 111 |
args = map(space.unwrap, args) |
| 112 |
kwargs = dict([ |
| 113 |
(k, space.unwrap(v)) |
| 114 |
for k, v in kwargs.iteritems() |
| 115 |
]) |
| 116 |
return space.wrap(rAlmostEqual(*args, **kwargs)) |
| 117 |
cls.w_rAlmostEqual = cls.space.wrap(interp2app(cls_rAlmostEqual)) |
| 118 |
def cls_c_pow(space, args_w): |
| 119 |
try: |
| 120 |
retVal = c_pow(*map(space.unwrap, args_w)) |
| 121 |
return space.wrap(retVal) |
| 122 |
except ZeroDivisionError as e: |
| 123 |
raise OperationError(cls.space.w_ZeroDivisionError, |
| 124 |
cls.space.wrap(e.message)) |
| 125 |
except OverflowError as e: |
| 126 |
raise OperationError(cls.space.w_OverflowError, |
| 127 |
cls.space.wrap(e.message)) |
| 128 |
except ValueError as e: |
| 129 |
raise OperationError(cls.space.w_ValueError, |
| 130 |
cls.space.wrap(e.message)) |
| 131 |
cls.w_c_pow = cls.space.wrap(interp2app(cls_c_pow)) |
| 132 |
|
| 133 |
def test_fabs(self): |
| 134 |
from numpy import fabs, dtype |
| 135 |
|
| 136 |
a = dtype('complex128').type(complex(-5., 5.)) |
| 137 |
raises(TypeError, fabs, a) |
| 138 |
|
| 139 |
def test_fmax(self): |
| 140 |
from numpy import fmax, array |
| 141 |
nnan, nan, inf, ninf = float('-nan'), float('nan'), float('inf'), float('-inf') |
| 142 |
a = array((complex(ninf, 10), complex(10, ninf), |
| 143 |
complex( inf, 10), complex(10, inf), |
| 144 |
5+5j, 5-5j, -5+5j, -5-5j, |
| 145 |
0+5j, 0-5j, 5, -5, |
| 146 |
complex(nan, 0), complex(0, nan)), dtype = complex) |
| 147 |
b = [ninf]*a.size |
| 148 |
res = [a[0 ], a[1 ], a[2 ], a[3 ], |
| 149 |
a[4 ], a[5 ], a[6 ], a[7 ], |
| 150 |
a[8 ], a[9 ], a[10], a[11], |
| 151 |
b[12], b[13]] |
| 152 |
assert (fmax(a, b) == res).all() |
| 153 |
b = [inf]*a.size |
| 154 |
res = [b[0 ], b[1 ], a[2 ], b[3 ], |
| 155 |
b[4 ], b[5 ], b[6 ], b[7 ], |
| 156 |
b[8 ], b[9 ], b[10], b[11], |
| 157 |
b[12], b[13]] |
| 158 |
assert (fmax(a, b) == res).all() |
| 159 |
b = [0]*a.size |
| 160 |
res = [b[0 ], a[1 ], a[2 ], a[3 ], |
| 161 |
a[4 ], a[5 ], b[6 ], b[7 ], |
| 162 |
a[8 ], b[9 ], a[10], b[11], |
| 163 |
b[12], b[13]] |
| 164 |
assert (fmax(a, b) == res).all() |
| 165 |
|
| 166 |
def test_fmin(self): |
| 167 |
from numpy import fmin, array |
| 168 |
nnan, nan, inf, ninf = float('-nan'), float('nan'), float('inf'), float('-inf') |
| 169 |
a = array((complex(ninf, 10), complex(10, ninf), |
| 170 |
complex( inf, 10), complex(10, inf), |
| 171 |
5+5j, 5-5j, -5+5j, -5-5j, |
| 172 |
0+5j, 0-5j, 5, -5, |
| 173 |
complex(nan, 0), complex(0, nan)), dtype = complex) |
| 174 |
b = [inf]*a.size |
| 175 |
res = [a[0 ], a[1 ], b[2 ], a[3 ], |
| 176 |
a[4 ], a[5 ], a[6 ], a[7 ], |
| 177 |
a[8 ], a[9 ], a[10], a[11], |
| 178 |
b[12], b[13]] |
| 179 |
assert (fmin(a, b) == res).all() |
| 180 |
b = [ninf]*a.size |
| 181 |
res = [b[0 ], b[1 ], b[2 ], b[3 ], |
| 182 |
b[4 ], b[5 ], b[6 ], b[7 ], |
| 183 |
b[8 ], b[9 ], b[10], b[11], |
| 184 |
b[12], b[13]] |
| 185 |
assert (fmin(a, b) == res).all() |
| 186 |
b = [0]*a.size |
| 187 |
res = [a[0 ], b[1 ], b[2 ], b[3 ], |
| 188 |
b[4 ], b[5 ], a[6 ], a[7 ], |
| 189 |
b[8 ], a[9 ], b[10], a[11], |
| 190 |
b[12], b[13]] |
| 191 |
assert (fmin(a, b) == res).all() |
| 192 |
|
| 193 |
def test_signbit(self): |
| 194 |
from numpy import signbit |
| 195 |
raises(TypeError, signbit, complex(1,1)) |
| 196 |
|
| 197 |
def test_reciprocal(self): |
| 198 |
from numpy import array, reciprocal |
| 199 |
inf = float('inf') |
| 200 |
nan = float('nan') |
| 201 |
|
| 202 |
orig = [2.+4.j, -2.+4.j, 2.-4.j, -2.-4.j, |
| 203 |
complex(inf, 3), complex(inf, -3), complex(inf, -inf), |
| 204 |
complex(nan, 3), 0+0j, 0-0j] |
| 205 |
a2 = 2.**2 + 4.**2 |
| 206 |
r = 2. / a2 |
| 207 |
i = 4. / a2 |
| 208 |
cnan = complex(nan, nan) |
| 209 |
expected = [complex(r, -i), complex(-r, -i), complex(r, i), |
| 210 |
complex(-r, i), |
| 211 |
-0j, 0j, cnan, |
| 212 |
cnan, cnan, cnan] |
| 213 |
for c, rel_err in (('complex64', 2e-7), ('complex128', 2e-15), ('clongdouble', 2e-15)): |
| 214 |
actual = reciprocal(array([orig], dtype=c)) |
| 215 |
for b, a, e in zip(orig, actual, expected): |
| 216 |
assert (a[0].real - e.real) < rel_err |
| 217 |
assert (a[0].imag - e.imag) < rel_err |
| 218 |
|
| 219 |
def test_floorceiltrunc(self): |
| 220 |
from numpy import array, floor, ceil, trunc |
| 221 |
a = array([ complex(-1.4, -1.4), complex(-1.5, -1.5)]) |
| 222 |
raises(TypeError, floor, a) |
| 223 |
raises(TypeError, ceil, a) |
| 224 |
raises(TypeError, trunc, a) |
| 225 |
|
| 226 |
def test_copysign(self): |
| 227 |
from numpy import copysign, dtype |
| 228 |
complex128 = dtype('complex128').type |
| 229 |
a = complex128(complex(-5., 5.)) |
| 230 |
b = complex128(complex(0., 0.)) |
| 231 |
raises(TypeError, copysign, a, b) |
| 232 |
|
| 233 |
def test_exp2(self): |
| 234 |
from numpy import array, exp2 |
| 235 |
inf = float('inf') |
| 236 |
ninf = -float('inf') |
| 237 |
nan = float('nan') |
| 238 |
cmpl = complex |
| 239 |
for c, rel_err in (('complex64', 2e-7), ('complex128', 2e-15), ('clongdouble', 2e-15)): |
| 240 |
a = [cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.), |
| 241 |
cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.), |
| 242 |
cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.), |
| 243 |
cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.), |
| 244 |
cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.), |
| 245 |
cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf), |
| 246 |
cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf), |
| 247 |
cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan), |
| 248 |
] |
| 249 |
b = exp2(array(a,dtype=c)) |
| 250 |
for i in range(len(a)): |
| 251 |
try: |
| 252 |
res = self.c_pow((2,0), (a[i].real, a[i].imag)) |
| 253 |
except OverflowError: |
| 254 |
res = (inf, nan) |
| 255 |
except ValueError: |
| 256 |
res = (nan, nan) |
| 257 |
msg = 'result of 2**%r(%r) got %r expected %r\n ' % \ |
| 258 |
(c,a[i], b[i], res) |
| 259 |
|
| 260 |
|
| 261 |
t1 = float(res[0]) |
| 262 |
t2 = float(b[i].real) |
| 263 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 264 |
t1 = float(res[1]) |
| 265 |
t2 = float(b[i].imag) |
| 266 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 267 |
|
| 268 |
def test_expm1(self): |
| 269 |
import math, cmath |
| 270 |
from numpy import array, expm1 |
| 271 |
inf = float('inf') |
| 272 |
ninf = -float('inf') |
| 273 |
nan = float('nan') |
| 274 |
cmpl = complex |
| 275 |
for c, rel_err in (('complex64', 2e-7), ('complex128', 2e-15), ('clongdouble', 2e-15)): |
| 276 |
a = [cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.), |
| 277 |
cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.), |
| 278 |
cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.), |
| 279 |
cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.), |
| 280 |
cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.), |
| 281 |
cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf), |
| 282 |
cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf), |
| 283 |
cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan), |
| 284 |
] |
| 285 |
b = expm1(array(a,dtype=c)) |
| 286 |
for i in range(len(a)): |
| 287 |
try: |
| 288 |
res = cmath.exp(a[i]) - 1. |
| 289 |
if a[i].imag == 0. and math.copysign(1., a[i].imag)<0: |
| 290 |
res = cmpl(res.real, -0.) |
| 291 |
elif a[i].imag == 0.: |
| 292 |
res = cmpl(res.real, 0.) |
| 293 |
except OverflowError: |
| 294 |
res = cmpl(inf, nan) |
| 295 |
except ValueError: |
| 296 |
res = cmpl(nan, nan) |
| 297 |
msg = 'result of expm1(%r(%r)) got %r expected %r\n ' % \ |
| 298 |
(c,a[i], b[i], res) |
| 299 |
|
| 300 |
|
| 301 |
t1 = float(res.real) |
| 302 |
t2 = float(b[i].real) |
| 303 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 304 |
t1 = float(res.imag) |
| 305 |
t2 = float(b[i].imag) |
| 306 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 307 |
|
| 308 |
def test_not_complex(self): |
| 309 |
from numpy import (radians, deg2rad, degrees, rad2deg, |
| 310 |
logaddexp, logaddexp2, fmod, |
| 311 |
arctan2) |
| 312 |
raises(TypeError, radians, complex(90,90)) |
| 313 |
raises(TypeError, deg2rad, complex(90,90)) |
| 314 |
raises(TypeError, degrees, complex(90,90)) |
| 315 |
raises(TypeError, rad2deg, complex(90,90)) |
| 316 |
raises(TypeError, logaddexp, complex(1, 1), complex(3, 3)) |
| 317 |
raises(TypeError, logaddexp2, complex(1, 1), complex(3, 3)) |
| 318 |
raises(TypeError, arctan2, complex(1, 1), complex(3, 3)) |
| 319 |
raises (TypeError, fmod, complex(90,90), 3) |
| 320 |
|
| 321 |
def test_isnan_isinf(self): |
| 322 |
from numpy import isnan, isinf, array |
| 323 |
assert (isnan(array([0.2+2j, complex(float('inf'),0), |
| 324 |
complex(0,float('inf')), complex(0,float('nan')), |
| 325 |
complex(float('nan'), 0)], dtype=complex)) == \ |
| 326 |
[False, False, False, True, True]).all() |
| 327 |
|
| 328 |
assert (isinf(array([0.2+2j, complex(float('inf'),0), |
| 329 |
complex(0,float('inf')), complex(0,float('nan')), |
| 330 |
complex(float('nan'), 0)], dtype=complex)) == \ |
| 331 |
[False, True, True, False, False]).all() |
| 332 |
|
| 333 |
def test_sign_for_complex_nan(self): |
| 334 |
from numpy import array, nan, sign, isnan |
| 335 |
C = array([nan], dtype=complex) |
| 336 |
res = sign(C) |
| 337 |
assert isnan(res.real) |
| 338 |
assert res.imag == 0+0j |
| 339 |
|
| 340 |
def test_square(self): |
| 341 |
from numpy import square |
| 342 |
assert square(complex(3, 4)) == complex(3,4) * complex(3, 4) |
| 343 |
|
| 344 |
def test_power_simple(self): |
| 345 |
import numpy as np |
| 346 |
a = np.array([1+2j, 2+3j, 3+4j]) |
| 347 |
assert ((a ** 0) == 1).all() |
| 348 |
assert ((a ** 1) == a).all() |
| 349 |
assert ((a ** 2) == (a * a)).all() |
| 350 |
|
| 351 |
def test_power_complex(self): |
| 352 |
import numpy as np |
| 353 |
|
| 354 |
x = np.array([1, 1j, 2, 2.5+.37j, np.inf, np.nan]) |
| 355 |
y = np.array([1, 1j, -0.5+1.5j, -0.5+1.5j, 2, 3]) |
| 356 |
lx = list(range(len(x))) |
| 357 |
|
| 358 |
p_r = [complex(x[i]) ** complex(y[i]) for i in lx] |
| 359 |
|
| 360 |
p_r[4] = complex(np.inf, np.nan) |
| 361 |
|
| 362 |
n_r_s = [x[i] ** y[i] for i in lx] |
| 363 |
n_r_a = x ** y |
| 364 |
for i in lx: |
| 365 |
msg = 'Loop %d' % i |
| 366 |
self.rAlmostEqual(float(n_r_s[i].real), float(p_r[i].real), msg=msg) |
| 367 |
self.rAlmostEqual(float(n_r_s[i].imag), float(p_r[i].imag), msg=msg) |
| 368 |
self.rAlmostEqual(float(n_r_a[i].real), float(p_r[i].real), msg=msg) |
| 369 |
self.rAlmostEqual(float(n_r_a[i].imag), float(p_r[i].imag), msg=msg) |
| 370 |
|
| 371 |
def test_conjugate(self): |
| 372 |
from numpy import conj, conjugate, dtype |
| 373 |
import numpy as np |
| 374 |
|
| 375 |
c0 = dtype('complex128').type(complex(2.5, 0)) |
| 376 |
c1 = dtype('complex64').type(complex(1, 2)) |
| 377 |
|
| 378 |
assert conj is conjugate |
| 379 |
assert conj(c0) == c0 |
| 380 |
assert c0.conjugate() == c0 |
| 381 |
assert conj(c1) == complex(1, -2) |
| 382 |
assert conj(1) == 1 |
| 383 |
assert conj(-3) == -3 |
| 384 |
assert conj(float('-inf')) == float('-inf') |
| 385 |
|
| 386 |
|
| 387 |
assert np.conjugate(1+2j) == 1-2j |
| 388 |
|
| 389 |
eye2 = np.array([[1, 0], [0, 1]]) |
| 390 |
assert (eye2.conjugate() == eye2).all() |
| 391 |
x = eye2 + 1j * eye2 |
| 392 |
for a, b in zip(np.conjugate(x), np.array([[ 1.-1.j, 0.-0.j], [ 0.-0.j, 1.-1.j]])): |
| 393 |
assert a[0] == b[0] |
| 394 |
assert a[1] == b[1] |
| 395 |
|
| 396 |
def test_logn(self): |
| 397 |
import math, cmath |
| 398 |
|
| 399 |
from numpy import log2, array, log1p |
| 400 |
inf = float('inf') |
| 401 |
ninf = -float('inf') |
| 402 |
nan = float('nan') |
| 403 |
cmpl = complex |
| 404 |
log_2 = math.log(2) |
| 405 |
a = [cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.), |
| 406 |
cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.), |
| 407 |
cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.), |
| 408 |
cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.), |
| 409 |
cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.), |
| 410 |
cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf), |
| 411 |
cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf), |
| 412 |
cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan), |
| 413 |
] |
| 414 |
for c,rel_err in (('complex128', 2e-15), ('complex64', 1e-7)): |
| 415 |
b = log2(array(a,dtype=c)) |
| 416 |
for i in range(len(a)): |
| 417 |
try: |
| 418 |
_res = cmath.log(a[i]) |
| 419 |
res = cmpl(_res.real / log_2, _res.imag / log_2) |
| 420 |
except OverflowError: |
| 421 |
res = cmpl(inf, nan) |
| 422 |
except ValueError: |
| 423 |
res = cmpl(ninf, math.atan2(a[i].imag, a[i].real) / log_2) |
| 424 |
msg = 'result of log2(%r(%r)) got %r expected %r\n ' % \ |
| 425 |
(c,a[i], b[i], res) |
| 426 |
|
| 427 |
|
| 428 |
t1 = float(res.real) |
| 429 |
t2 = float(b[i].real) |
| 430 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 431 |
t1 = float(res.imag) |
| 432 |
t2 = float(b[i].imag) |
| 433 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 434 |
for c,rel_err in (('complex128', 2e-15), ('complex64', 1e-7)): |
| 435 |
b = log1p(array(a,dtype=c)) |
| 436 |
for i in range(len(a)): |
| 437 |
try: |
| 438 |
|
| 439 |
res = cmath.log(cmpl(a[i].real+1, a[i].imag)) |
| 440 |
except OverflowError: |
| 441 |
res = cmpl(inf, nan) |
| 442 |
except ValueError: |
| 443 |
res = cmpl(ninf, 0) |
| 444 |
msg = 'result of log1p(%r(%r)) got %r expected %r\n ' % \ |
| 445 |
(c,a[i], b[i], res) |
| 446 |
|
| 447 |
|
| 448 |
t1 = float(res.real) |
| 449 |
t2 = float(b[i].real) |
| 450 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 451 |
t1 = float(res.imag) |
| 452 |
t2 = float(b[i].imag) |
| 453 |
self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg) |
| 454 |
|
| 455 |
def test_logical_ops(self): |
| 456 |
from numpy import logical_and, logical_or, logical_xor, logical_not |
| 457 |
|
| 458 |
c1 = complex(1, 1) |
| 459 |
c3 = complex(3, 0) |
| 460 |
c0 = complex(0, 0) |
| 461 |
assert (logical_and([True, False , True, True], [c1, c1, c3, c0]) |
| 462 |
== [True, False, True, False]).all() |
| 463 |
assert (logical_or([True, False, True, False], [c1, c3, c0, c0]) |
| 464 |
== [True, True, True, False]).all() |
| 465 |
assert (logical_xor([True, False, True, False], [c1, c3, c0, c0]) |
| 466 |
== [False, True, True, False]).all() |
| 467 |
assert (logical_not([c1, c0]) == [False, True]).all() |
| 468 |
|
| 469 |
def test_minimum(self): |
| 470 |
from numpy import array, minimum |
| 471 |
|
| 472 |
a = array([-5.0+5j, -5.0-5j, -0.0-10j, 1.0+10j]) |
| 473 |
b = array([ 3.0+10.0j, 3.0, -2.0+2.0j, -3.0+4.0j]) |
| 474 |
c = minimum(a, b) |
| 475 |
for i in range(4): |
| 476 |
assert c[i] == min(a[i], b[i]) |
| 477 |
|
| 478 |
def test_maximum(self): |
| 479 |
from numpy import array, maximum |
| 480 |
|
| 481 |
a = array([-5.0+5j, -5.0-5j, -0.0-10j, 1.0+10j]) |
| 482 |
b = array([ 3.0+10.0j, 3.0, -2.0+2.0j, -3.0+4.0j]) |
| 483 |
c = maximum(a, b) |
| 484 |
for i in range(4): |
| 485 |
assert c[i] == max(a[i], b[i]) |
| 486 |
|
| 487 |
|
| 488 |
def test_complex_overflow(self): |
| 489 |
from numpy import array, absolute, isinf, complex128, floor_divide |
| 490 |
a = array(complex(1.5e308,1.5e308)) |
| 491 |
|
| 492 |
b = absolute(a) |
| 493 |
assert isinf(b) |
| 494 |
c = array([1.e+110, 1.e-110], dtype=complex128) |
| 495 |
d = floor_divide(c**2, c) |
| 496 |
assert (d == [1.e+110, 0]).all() |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
def test_basic(self): |
| 501 |
import sys |
| 502 |
from numpy import (dtype, add, array, dtype, |
| 503 |
subtract as sub, multiply, divide, negative, absolute as abs, |
| 504 |
floor_divide, real, imag, sign) |
| 505 |
from numpy import (equal, not_equal, greater, greater_equal, less, |
| 506 |
less_equal, isnan) |
| 507 |
assert real(4.0) == 4.0 |
| 508 |
assert imag(0.0) == 0.0 |
| 509 |
a = array([complex(3.0, 4.0)]) |
| 510 |
b = a.real |
| 511 |
b[0] = 1024 |
| 512 |
assert a[0].real == 1024 |
| 513 |
assert b.dtype == dtype(float) |
| 514 |
a = array(complex(3.0, 4.0)) |
| 515 |
b = a.real |
| 516 |
assert b == array(3) |
| 517 |
assert a.imag == array(4) |
| 518 |
a.real = 1024 |
| 519 |
a.imag = 2048 |
| 520 |
assert a.real == 1024 and a.imag == 2048 |
| 521 |
assert b.dtype == dtype(float) |
| 522 |
a = array(4.0) |
| 523 |
b = a.imag |
| 524 |
assert b == 0 |
| 525 |
assert b.dtype == dtype(float) |
| 526 |
exc = raises(TypeError, 'a.imag = 1024') |
| 527 |
assert str(exc.value).startswith("array does not have imaginary") |
| 528 |
exc = raises(ValueError, 'a.real = [1, 3]') |
| 529 |
assert str(exc.value) == \ |
| 530 |
"could not broadcast input array from shape (2) into shape ()" |
| 531 |
a = array('abc') |
| 532 |
assert str(a.real) == 'abc' |
| 533 |
assert str(a.imag) == '' |
| 534 |
for t in 'complex64', 'complex128', 'clongdouble': |
| 535 |
complex_ = dtype(t).type |
| 536 |
O = complex(0, 0) |
| 537 |
c0 = complex_(complex(2.5, 0)) |
| 538 |
c1 = complex_(complex(1, 2)) |
| 539 |
c2 = complex_(complex(3, 4)) |
| 540 |
c3 = complex_(complex(-3, -3)) |
| 541 |
|
| 542 |
assert equal(c0, 2.5) |
| 543 |
assert equal(c1, complex_(complex(1, 2))) |
| 544 |
assert equal(c1, complex(1, 2)) |
| 545 |
assert equal(c1, c1) |
| 546 |
assert not_equal(c1, c2) |
| 547 |
assert not equal(c1, c2) |
| 548 |
|
| 549 |
assert less(c1, c2) |
| 550 |
assert less_equal(c1, c2) |
| 551 |
assert less_equal(c1, c1) |
| 552 |
assert not less(c1, c1) |
| 553 |
|
| 554 |
assert greater(c2, c1) |
| 555 |
assert greater_equal(c2, c1) |
| 556 |
assert not greater(c1, c2) |
| 557 |
|
| 558 |
assert add(c1, c2) == complex_(complex(4, 6)) |
| 559 |
assert add(c1, c2) == complex(4, 6) |
| 560 |
|
| 561 |
assert sub(c0, c0) == sub(c1, c1) == 0 |
| 562 |
assert sub(c1, c2) == complex(-2, -2) |
| 563 |
assert negative(complex(1,1)) == complex(-1, -1) |
| 564 |
assert negative(complex(0, 0)) == 0 |
| 565 |
|
| 566 |
assert multiply(1, c1) == c1 |
| 567 |
assert multiply(2, c2) == complex(6, 8) |
| 568 |
assert multiply(c1, c2) == complex(-5, 10) |
| 569 |
|
| 570 |
assert divide(c0, 1) == c0 |
| 571 |
assert divide(c2, -1) == negative(c2) |
| 572 |
assert divide(c1, complex(0, 1)) == complex(2, -1) |
| 573 |
n = divide(c1, O) |
| 574 |
assert repr(n.real) == 'inf' |
| 575 |
assert repr(n.imag).startswith('inf') |
| 576 |
assert divide(c0, c0) == 1 |
| 577 |
res = divide(c2, c1) |
| 578 |
assert abs(res.real-2.2) < 0.001 |
| 579 |
assert abs(res.imag+0.4) < 0.001 |
| 580 |
|
| 581 |
assert floor_divide(c0, c0) == complex(1, 0) |
| 582 |
assert isnan(floor_divide(c0, complex(0, 0)).real) |
| 583 |
assert floor_divide(c0, complex(0, 0)).imag == 0.0 |
| 584 |
|
| 585 |
assert abs(c0) == 2.5 |
| 586 |
assert abs(c2) == 5 |
| 587 |
assert sign(complex(0, 0)) == 0 |
| 588 |
assert sign(complex(-42, 0)) == -1 |
| 589 |
assert sign(complex(42, 0)) == 1 |
| 590 |
assert sign(complex(-42, 2)) == -1 |
| 591 |
assert sign(complex(42, 2)) == 1 |
| 592 |
assert sign(complex(-42, -3)) == -1 |
| 593 |
assert sign(complex(42, -3)) == 1 |
| 594 |
assert sign(complex(0, -42)) == -1 |
| 595 |
assert sign(complex(0, 42)) == 1 |
| 596 |
|
| 597 |
inf_c = complex_(complex(float('inf'), 0.)) |
| 598 |
assert repr(abs(inf_c)) == 'inf' |
| 599 |
assert repr(abs(complex(float('nan'), float('nan')))) == 'nan' |
| 600 |
|
| 601 |
|
| 602 |
if '__pypy__' in sys.builtin_module_names: |
| 603 |
exct, excm = TypeError, 'readonly attribute' |
| 604 |
else : |
| 605 |
exct, excm = AttributeError, 'is not writable' |
| 606 |
exc = raises(exct, 'c2.real = 10.') |
| 607 |
assert excm in exc.value[0] |
| 608 |
exc = raises(exct, 'c2.imag = 10.') |
| 609 |
assert excm in exc.value[0] |
| 610 |
assert(real(c2) == 3.0) |
| 611 |
assert(imag(c2) == 4.0) |
| 612 |
|
| 613 |
def test_conj(self): |
| 614 |
from numpy import array |
| 615 |
|
| 616 |
a = array([1 + 2j, 1 - 2j]) |
| 617 |
assert (a.conj() == [1 - 2j, 1 + 2j]).all() |
| 618 |
a = array([1,2,3.4J],dtype=complex) |
| 619 |
assert a[2].conjugate() == 0-3.4j |
| 620 |
|
| 621 |
def test_math(self): |
| 622 |
if self.isWindows: |
| 623 |
skip('windows does not support c99 complex') |
| 624 |
import sys |
| 625 |
import numpy as np |
| 626 |
rAlmostEqual = self.rAlmostEqual |
| 627 |
|
| 628 |
for t, testcases in ( |
| 629 |
('complex128', self.testcases128), |
| 630 |
|
| 631 |
): |
| 632 |
complex_ = np.dtype(t).type |
| 633 |
for id, fn, ar, ai, er, ei, flags in testcases: |
| 634 |
arg = complex_(complex(ar, ai)) |
| 635 |
expected = (er, ei) |
| 636 |
if fn.startswith('acos'): |
| 637 |
fn = 'arc' + fn[1:] |
| 638 |
elif fn.startswith('asin'): |
| 639 |
fn = 'arc' + fn[1:] |
| 640 |
elif fn.startswith('atan'): |
| 641 |
fn = 'arc' + fn[1:] |
| 642 |
elif fn in ('rect', 'polar'): |
| 643 |
continue |
| 644 |
function = getattr(np, fn) |
| 645 |
_actual = function(arg) |
| 646 |
actual = (_actual.real, _actual.imag) |
| 647 |
|
| 648 |
if 'ignore-real-sign' in flags: |
| 649 |
actual = (abs(actual[0]), actual[1]) |
| 650 |
expected = (abs(expected[0]), expected[1]) |
| 651 |
if 'ignore-imag-sign' in flags: |
| 652 |
actual = (actual[0], abs(actual[1])) |
| 653 |
expected = (expected[0], abs(expected[1])) |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
if fn in ('log', 'log10'): |
| 658 |
real_abs_err = 2e-15 |
| 659 |
else: |
| 660 |
real_abs_err = 5e-323 |
| 661 |
|
| 662 |
error_message = ( |
| 663 |
'%s: %s(%r(%r, %r))\n' |
| 664 |
'Expected: complex(%r, %r)\n' |
| 665 |
'Received: complex(%r, %r)\n' |
| 666 |
) % (id, fn, complex_, ar, ai, |
| 667 |
expected[0], expected[1], |
| 668 |
actual[0], actual[1]) |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
rAlmostEqual(float(expected[0]), float(actual[0]), |
| 673 |
abs_err=real_abs_err, msg=error_message) |
| 674 |
rAlmostEqual(float(expected[1]), float(actual[1]), |
| 675 |
msg=error_message) |
| 676 |
sys.stderr.write('.') |
| 677 |
sys.stderr.write('\n') |
| 678 |
|
| 679 |
def test_complexbox_to_pycomplex(self): |
| 680 |
from numpy import dtype |
| 681 |
x = dtype('complex128').type(3.4j) |
| 682 |
assert complex(x) == 3.4j |