def test_hfft(self): x = random(14) + 1j*random(14) x_herm = np.concatenate((random(1), x, random(1))) x = np.concatenate((x_herm, x[::-1].conj())) assert_array_almost_equal(np.fft.fft(x), np.fft.hfft(x_herm)) assert_array_almost_equal(np.fft.hfft(x_herm) / np.sqrt(30), np.fft.hfft(x_herm, norm="ortho"))
def test_simple(self): x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], [.5, .5, 1.5], [.5, 1.5, 2.5], [.5, 2.5, 2.5]]) H, edges = histogramdd(x, (2, 3, 3), range=[[-1, 1], [0, 3], [0, 3]]) answer = np.array([[[0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0], [0, 0, 1], [0, 0, 1]]]) assert_array_equal(H, answer) # Check normalization ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]] H, edges = histogramdd(x, bins=ed, density=True) assert_(np.all(H == answer / 12.)) # Check that H has the correct shape. H, edges = histogramdd(x, (2, 3, 4), range=[[-1, 1], [0, 3], [0, 4]], density=True) answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]], [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]]) assert_array_almost_equal(H, answer / 6., 4) # Check that a sequence of arrays is accepted and H has the correct # shape. z = [np.squeeze(y) for y in np.split(x, 3, axis=1)] H, edges = histogramdd(z, bins=(4, 3, 2), range=[[-2, 2], [0, 3], [0, 2]]) answer = np.array([[[0, 0], [0, 0], [0, 0]], [[0, 1], [0, 0], [1, 0]], [[0, 1], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]) assert_array_equal(H, answer) Z = np.zeros((5, 5, 5)) Z[list(range(5)), list(range(5)), list(range(5))] = 1. H, edges = histogramdd([np.arange(5), np.arange(5), np.arange(5)], 5) assert_array_equal(H, Z)
def test_ifftn(self): x = random((30, 20, 10)) + 1j*random((30, 20, 10)) assert_array_almost_equal( np.fft.ifft(np.fft.ifft(np.fft.ifft(x, axis=2), axis=1), axis=0), np.fft.ifftn(x)) assert_array_almost_equal(np.fft.ifftn(x) * np.sqrt(30 * 20 * 10), np.fft.ifftn(x, norm="ortho"))
def test_density(self): x = array([1, 2, 3, 1, 2, 3, 1, 2, 3]) y = array([1, 1, 1, 2, 2, 2, 3, 3, 3]) H, xed, yed = histogram2d(x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], density=True) answer = array([[1, 1, .5], [1, 1, .5], [.5, .5, .25]]) / 9. assert_array_almost_equal(H, answer, 3)
def test_svd_build(self): # Ticket 627. a = array([[0., 1.], [1., 1.], [2., 1.], [3., 1.]]) m, n = a.shape u, s, vh = linalg.svd(a) b = dot(transpose(u[:, n:]), a) assert_array_almost_equal(b, np.zeros((2, 2)))
def test_rfft(self): x = random(30) for n in [x.size, 2*x.size]: for norm in [None, 'ortho']: assert_array_almost_equal( np.fft.fft(x, n=n, norm=norm)[:(n//2 + 1)], np.fft.rfft(x, n=n, norm=norm)) assert_array_almost_equal(np.fft.rfft(x, n=n) / np.sqrt(n), np.fft.rfft(x, n=n, norm="ortho"))
def test_eigh_build(self): # Ticket 662. rvals = [68.60568999, 89.57756725, 106.67185574] cov = array([[77.70273908, 3.51489954, 15.64602427], [3.51489954, 88.97013878, -1.07431931], [15.64602427, -1.07431931, 98.18223512]]) vals, vecs = linalg.eigh(cov) assert_array_almost_equal(vals, rvals)
def test_equal_nan_default(self): # Make sure equal_nan default behavior remains unchanged. (All # of these functions use assert_array_compare under the hood.) # None of these should raise. a = np.array([np.nan]) b = np.array([np.nan]) assert_array_equal(a, b) assert_array_almost_equal(a, b) assert_array_less(a, b) assert_allclose(a, b)
def test_lstsq_complex_larger_rhs(self): # gh-9891 size = 20 n_rhs = 70 G = np.random.randn(size, size) + 1j * np.random.randn(size, size) u = np.random.randn(size, n_rhs) + 1j * np.random.randn(size, n_rhs) b = G.dot(u) # This should work without segmentation fault. u_lstsq, res, rank, sv = linalg.lstsq(G, b, rcond=None) # check results just in case assert_array_almost_equal(u_lstsq, u)
def test_asym(self): x = array([1, 1, 2, 3, 4, 4, 4, 5]) y = array([1, 3, 2, 0, 1, 2, 3, 4]) H, xed, yed = histogram2d(x, y, (6, 5), range=[[0, 6], [0, 5]], density=True) answer = array([[0., 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 1]]) assert_array_almost_equal(H, answer / 8., 3) assert_array_equal(xed, np.linspace(0, 6, 7)) assert_array_equal(yed, np.linspace(0, 5, 6))
def test_weights(self): v = np.random.rand(100) w = np.ones(100) * 5 a, b = histogram(v) na, nb = histogram(v, density=True) wa, wb = histogram(v, weights=w) nwa, nwb = histogram(v, weights=w, density=True) assert_array_almost_equal(a * 5, wa) assert_array_almost_equal(na, nwa) # Check weights are properly applied. v = np.linspace(0, 10, 10) w = np.concatenate((np.zeros(5), np.ones(5))) wa, wb = histogram(v, bins=np.arange(11), weights=w) assert_array_almost_equal(wa, w) # Check with integer weights wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1]) assert_array_equal(wa, [4, 5, 0, 1]) wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], density=True) assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4) # Check weights with non-uniform bin widths a, b = histogram(np.arange(9), [0, 1, 3, 6, 10], weights=[2, 1, 1, 1, 1, 1, 1, 1, 1], density=True) assert_almost_equal(a, [.2, .1, .1, .075])
def test_exotic_weights(self): # Test the use of weights that are not integer or floats, but e.g. # complex numbers or object types. # Complex weights values = np.array([1.3, 2.5, 2.3]) weights = np.array([1, -1, 2]) + 1j * np.array([2, 1, 2]) # Check with custom bins wa, wb = histogram(values, bins=[0, 2, 3], weights=weights) assert_array_almost_equal(wa, np.array([1, 1]) + 1j * np.array([2, 3])) # Check with even bins wa, wb = histogram(values, bins=2, range=[1, 3], weights=weights) assert_array_almost_equal(wa, np.array([1, 1]) + 1j * np.array([2, 3])) # Decimal weights from decimal import Decimal values = np.array([1.3, 2.5, 2.3]) weights = np.array([Decimal(1), Decimal(2), Decimal(3)]) # Check with custom bins wa, wb = histogram(values, bins=[0, 2, 3], weights=weights) assert_array_almost_equal(wa, [Decimal(1), Decimal(5)]) # Check with even bins wa, wb = histogram(values, bins=2, range=[1, 3], weights=weights) assert_array_almost_equal(wa, [Decimal(1), Decimal(5)])
def test_eig_build(self): # Ticket #652 rva = array([ 1.03221168e+02 + 0.j, -1.91843603e+01 + 0.j, -6.04004526e-01 + 15.84422474j, -6.04004526e-01 - 15.84422474j, -1.13692929e+01 + 0.j, -6.57612485e-01 + 10.41755503j, -6.57612485e-01 - 10.41755503j, 1.82126812e+01 + 0.j, 1.06011014e+01 + 0.j, 7.80732773e+00 + 0.j, -7.65390898e-01 + 0.j, 1.51971555e-15 + 0.j, -1.51308713e-15 + 0.j ]) a = arange(13 * 13, dtype=float64) a.shape = (13, 13) a = a % 17 va, ve = linalg.eig(a) va.sort() rva.sort() assert_array_almost_equal(va, rva)
def test_all_1d_norm_preserving(self): # verify that round-trip transforms are norm-preserving x = random(30) x_norm = np.linalg.norm(x) n = x.size * 2 func_pairs = [(np.fft.fft, np.fft.ifft), (np.fft.rfft, np.fft.irfft), # hfft: order so the first function takes x.size samples # (necessary for comparison to x_norm above) (np.fft.ihfft, np.fft.hfft), ] for forw, back in func_pairs: for n in [x.size, 2*x.size]: for norm in [None, 'ortho']: tmp = forw(x, n=n, norm=norm) tmp = back(tmp, n=n, norm=norm) assert_array_almost_equal(x_norm, np.linalg.norm(tmp))
def test_polyfit_build(self): # Ticket #628 ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01, 9.95368241e+00, -3.14526520e+02] x = [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, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176] y = [9.0, 3.0, 7.0, 4.0, 4.0, 8.0, 6.0, 11.0, 9.0, 8.0, 11.0, 5.0, 6.0, 5.0, 9.0, 8.0, 6.0, 10.0, 6.0, 10.0, 7.0, 6.0, 6.0, 6.0, 13.0, 4.0, 9.0, 11.0, 4.0, 5.0, 8.0, 5.0, 7.0, 7.0, 6.0, 12.0, 7.0, 7.0, 9.0, 4.0, 12.0, 6.0, 6.0, 4.0, 3.0, 9.0, 8.0, 8.0, 6.0, 7.0, 9.0, 10.0, 6.0, 8.0, 4.0, 7.0, 7.0, 10.0, 8.0, 8.0, 6.0, 3.0, 8.0, 4.0, 5.0, 7.0, 8.0, 6.0, 6.0, 4.0, 12.0, 9.0, 8.0, 8.0, 8.0, 6.0, 7.0, 4.0, 4.0, 5.0, 7.0] tested = np.polyfit(x, y, 4) assert_array_almost_equal(ref, tested)
def test_nd(self): c = mgrid[-1:1:10j, -2:2:10j] d = mgrid[-1:1:0.1, -2:2:0.2] assert_(c.shape == (2, 10, 10)) assert_(d.shape == (2, 20, 20)) assert_array_equal(c[0][0, :], -np.ones(10, 'd')) assert_array_equal(c[1][:, 0], -2*np.ones(10, 'd')) assert_array_almost_equal(c[0][-1, :], np.ones(10, 'd'), 11) assert_array_almost_equal(c[1][:, -1], 2*np.ones(10, 'd'), 11) assert_array_almost_equal(d[0, 1, :] - d[0, 0, :], 0.1*np.ones(20, 'd'), 11) assert_array_almost_equal(d[1, :, 1] - d[1, :, 0], 0.2*np.ones(20, 'd'), 11)
def test_poly(self): assert_array_almost_equal(np.poly([3, -np.sqrt(2), np.sqrt(2)]), [1, -3, -2, 6]) # From matlab docs A = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] assert_array_almost_equal(np.poly(A), [1, -6, -72, -27]) # Should produce real output for perfect conjugates assert_(np.isrealobj(np.poly([+1.082j, +2.613j, -2.613j, -1.082j]))) assert_(np.isrealobj(np.poly([0+1j, -0+-1j, 1+2j, 1-2j, 1.+3.5j, 1-3.5j]))) assert_(np.isrealobj(np.poly([1j, -1j, 1+2j, 1-2j, 1+3j, 1-3.j]))) assert_(np.isrealobj(np.poly([1j, -1j, 1+2j, 1-2j]))) assert_(np.isrealobj(np.poly([1j, -1j, 2j, -2j]))) assert_(np.isrealobj(np.poly([1j, -1j]))) assert_(np.isrealobj(np.poly([1, -1]))) assert_(np.iscomplexobj(np.poly([1j, -1.0000001j]))) np.random.seed(42) a = np.random.randn(100) + 1j*np.random.randn(100) assert_(np.isrealobj(np.poly(np.concatenate((a, np.conjugate(a))))))
def test_pow(self): """Test raising a matrix to an integer power works as expected.""" m = matrix("1. 2.; 3. 4.") m2 = m.copy() m2 **= 2 mi = m.copy() mi **= -1 m4 = m2.copy() m4 **= 2 assert_array_almost_equal(m2, m**2) assert_array_almost_equal(m4, np.dot(m2, m2)) assert_array_almost_equal(np.dot(mi, m), np.eye(2))
def test_scalar_type_pow(self): m = matrix([[1, 2], [3, 4]]) for scalar_t in [np.int8, np.uint8]: two = scalar_t(2) assert_array_almost_equal(m**2, m**two)
def test_fft(self): x = random(30) + 1j*random(30) assert_array_almost_equal(fft1(x), np.fft.fft(x)) assert_array_almost_equal(fft1(x) / np.sqrt(30), np.fft.fft(x, norm="ortho"))
def test_linspace_equivalence(self): y, st = np.linspace(2, 10, retstep=1) assert_almost_equal(st, 8/49.0) assert_array_almost_equal(y, mgrid[2:10:50j], 13)
def test_irfft2(self): x = random((30, 20)) assert_array_almost_equal(x, np.fft.irfft2(np.fft.rfft2(x))) assert_array_almost_equal( x, np.fft.irfft2(np.fft.rfft2(x, norm="ortho"), norm="ortho"))
def test_ifft(self): x = random(30) + 1j*random(30) assert_array_almost_equal(x, np.fft.ifft(np.fft.fft(x))) assert_array_almost_equal( x, np.fft.ifft(np.fft.fft(x, norm="ortho"), norm="ortho"))
def test_fft2(self): x = random((30, 20)) + 1j*random((30, 20)) assert_array_almost_equal(np.fft.fft(np.fft.fft(x, axis=1), axis=0), np.fft.fft2(x)) assert_array_almost_equal(np.fft.fft2(x) / np.sqrt(30 * 20), np.fft.fft2(x, norm="ortho"))
def test_rfft2(self): x = random((30, 20)) assert_array_almost_equal(np.fft.fft2(x)[:, :11], np.fft.rfft2(x)) assert_array_almost_equal(np.fft.rfft2(x) / np.sqrt(30 * 20), np.fft.rfft2(x, norm="ortho"))
def test_no_side_effects(self): # This is a regression test that ensures that values passed to # ``histogram`` are unchanged. values = np.array([1.3, 2.5, 2.3]) np.histogram(values, range=[-10, 10], bins=100) assert_array_almost_equal(values, [1.3, 2.5, 2.3])