def test_det(): order = 2 shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(float) cArray.setArray(data) assert round(NumCpp.det(cArray)) == round(np.linalg.det(data).item()) order = 3 shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(float) cArray.setArray(data) assert round(NumCpp.det(cArray)) == round(np.linalg.det(data).item()) order = np.random.randint(4, 8, [1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(float) cArray.setArray(data) assert round(NumCpp.det(cArray)) == round(np.linalg.det(data).item())
def test_randFloat(): shapeInput = np.random.randint(1, 100, [ 2, ]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) values = np.random.randint(1, 100, [ 2, ]) values.sort() assert NumCpp.randFloat(inShape, values[0].item(), values[1].item() + 1) is not None assert NumCpp.randFloat(values[0].item(), values[1].item() + 1) is not None
def test_cholesky(): shapeInput = np.random.randint(5, 50, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) a = np.random.randint(1, 100, [shape.rows, shape.cols]).flatten() aL = np.tril(a) b = aL.dot(aL.transpose()) cArray.setArray(b) assert np.array_equal(np.round(NumCpp.cholesky(cArray).getNumpyArray()), np.round(aL))
def test_bernoulli(): shapeInput = np.random.randint(1, 100, [ 2, ]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) p = np.random.rand() randArray = NumCpp.bernoulli(inShape, p).getNumpyArray() assert np.array_equiv(randArray.shape, shapeInput) randValue = NumCpp.bernoulli(p) assert isinstance(randValue, bool)
def test_factorial(): n = np.random.randint(0, 170) assert (roundScaler(NumCpp.factorial_Scaler(n), NUM_DECIMALS_ROUND) == roundScaler(sp.factorial(n).item(), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArrayUInt32(shape) data = np.random.randint(0, 170, [shape.rows, shape.cols], dtype=np.uint32) cArray.setArray(data) assert np.array_equal(roundArray(NumCpp.factorial_Array(cArray), NUM_DECIMALS_ROUND), roundArray(sp.factorial(data), NUM_DECIMALS_ROUND))
def test_laplace(): if NumCpp.NO_USE_BOOST: return shapeInput = np.random.randint(1, 100, [ 2, ]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) loc = np.random.rand() * 10 scale = np.random.rand() * 100 assert NumCpp.laplace(inShape, loc, scale) is not None assert NumCpp.laplace(loc, scale) is not None
def test_gaussianFilter(): for mode in modes.keys(): shape = np.random.randint(100, 200, [2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape).astype(np.double) cArray.setArray(data) constantValue = np.random.randint(0, 5, [1, ]).item() # only actaully needed for constant boundary condition sigma = np.random.rand(1).item() * 2 dataOutC = NumCpp.gaussianFilter(cArray, sigma, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.gaussian_filter(data, sigma, mode=mode, cval=constantValue) assert np.array_equal(np.round(dataOutC, 2), np.round(dataOutPy, 2))
def test_nonCentralChiSquared(): if NumCpp.NO_USE_BOOST: return shapeInput = np.random.randint(1, 100, [ 2, ]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) k = np.random.rand() * 10 ll = np.random.rand() * 100 assert NumCpp.nonCentralChiSquared(inShape, k, ll) is not None assert NumCpp.nonCentralChiSquared(k, ll) is not None
def test_choice(): shapeInput = np.random.randint(1, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert NumCpp.choiceSingle(cArray) is not None shapeInput = np.random.randint(1, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) num = np.random.randint(1, data.size, [ 1, ]).item() assert NumCpp.choiceMultiple(cArray, num) is not None
def test_pivotLU_decomposition(): sizeInput = np.random.randint(5, 50) shape = NumCpp.Shape(sizeInput) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) l, u, p = NumCpp.pivotLU_decomposition(cArray) lhs = p.dot(data) rhs = l.dot(u) assert np.array_equal(np.round(lhs, 10), np.round(rhs, 10)) shapeInput = np.random.randint(5, 50, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) uArray = NumCpp.NdArray() sArray = NumCpp.NdArray() vArray = NumCpp.NdArray() NumCpp.svd(cArray, uArray, sArray, vArray) data2 = np.dot(uArray.getNumpyArray(), np.dot(sArray.getNumpyArray(), vArray.getNumpyArray())) assert np.array_equal(np.round(data, 9), np.round(data2, 9))
def test_matrix_power(): order = np.random.randint(5, 50, [1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) assert np.array_equal(NumCpp.matrix_power(cArray, 0).getNumpyArray(), np.linalg.matrix_power(data, 0)) order = np.random.randint(5, 50, [1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) assert np.array_equal(NumCpp.matrix_power(cArray, 1).getNumpyArray(), np.linalg.matrix_power(data, 1)) order = np.random.randint(5, 50, [1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) assert np.array_equal(np.round(NumCpp.matrix_power(cArray, -1).getNumpyArray(), 8), np.round(np.linalg.matrix_power(data, -1), 8)) order = np.random.randint(5, 50, [1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 5, [shape.rows, shape.cols]).astype(float) cArray.setArray(data) power = np.random.randint(2, 9, [1, ]).item() assert np.array_equal(NumCpp.matrix_power(cArray, power).getNumpyArray(), np.linalg.matrix_power(data, power)) order = np.random.randint(5, 50, [1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) power = np.random.randint(2, 9, [1, ]).item() * -1 assert np.array_equal(np.round(NumCpp.matrix_power(cArray, power).getNumpyArray(), 9), np.round(np.linalg.matrix_power(data, power), 9))
def test_gamma1pm1(): # There is no scipy equivalent to this function value = np.random.rand(1).item() assert NumCpp.gamma1pm1_Scaler(value) is not None shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert NumCpp.gamma1pm1_Array(cArray) is not None
def test_prime(): # There is no scipy equivalent to this function value = np.random.randint(10000) assert NumCpp.prime_Scaler(value) is not None shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArrayUInt32(shape) data = np.random.randint(0, 10000, [shape.rows, shape.cols]) cArray.setArray(data) assert NumCpp.prime_Array(cArray) is not None
def test_permutation(): assert NumCpp.permutationScaler(np.random.randint(1, 100, [ 1, ]).item()) is not None shapeInput = np.random.randint(1, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 10, [shape.rows, shape.cols]) cArray.setArray(data) assert NumCpp.permutationArray(cArray) is not None
def test_multi_dot(): shapeInput = np.random.randint(5, 50, [2, ]) shape1 = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) shape2 = NumCpp.Shape(shape1.cols, np.random.randint(5, 50, [1, ]).item()) shape3 = NumCpp.Shape(shape2.cols, np.random.randint(5, 50, [1, ]).item()) shape4 = NumCpp.Shape(shape3.cols, np.random.randint(5, 50, [1, ]).item()) cArray1 = NumCpp.NdArray(shape1) cArray2 = NumCpp.NdArray(shape2) cArray3 = NumCpp.NdArray(shape3) cArray4 = NumCpp.NdArray(shape4) data1 = np.random.randint(1, 10, [shape1.rows, shape1.cols]) data2 = np.random.randint(1, 10, [shape2.rows, shape2.cols]) data3 = np.random.randint(1, 10, [shape3.rows, shape3.cols]) data4 = np.random.randint(1, 10, [shape4.rows, shape4.cols]) cArray1.setArray(data1) cArray2.setArray(data2) cArray3.setArray(data3) cArray4.setArray(data4) assert np.array_equal(np.round(NumCpp.multi_dot(cArray1, cArray2, cArray3, cArray4), 9), np.round(np.linalg.multi_dot([data1, data2, data3, data4]), 9)) shapeInput = np.random.randint(5, 50, [2, ]) shape1 = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) shape2 = NumCpp.Shape(shape1.cols, np.random.randint(5, 50, [1, ]).item()) shape3 = NumCpp.Shape(shape2.cols, np.random.randint(5, 50, [1, ]).item()) shape4 = NumCpp.Shape(shape3.cols, np.random.randint(5, 50, [1, ]).item()) cArray1 = NumCpp.NdArrayComplexDouble(shape1) cArray2 = NumCpp.NdArrayComplexDouble(shape2) cArray3 = NumCpp.NdArrayComplexDouble(shape3) cArray4 = NumCpp.NdArrayComplexDouble(shape4) real1 = np.random.randint(1, 100, [shape1.rows, shape1.cols]) imag1 = np.random.randint(1, 100, [shape1.rows, shape1.cols]) data1 = real1 + 1j * imag1 real2 = np.random.randint(1, 100, [shape2.rows, shape2.cols]) imag2 = np.random.randint(1, 100, [shape2.rows, shape2.cols]) data2 = real2 + 1j * imag2 real3 = np.random.randint(1, 100, [shape3.rows, shape3.cols]) imag3 = np.random.randint(1, 100, [shape3.rows, shape3.cols]) data3 = real3 + 1j * imag3 real4 = np.random.randint(1, 100, [shape4.rows, shape4.cols]) imag4 = np.random.randint(1, 100, [shape4.rows, shape4.cols]) data4 = real4 + 1j * imag4 cArray1.setArray(data1) cArray2.setArray(data2) cArray3.setArray(data3) cArray4.setArray(data4) assert np.array_equal(np.round(NumCpp.multi_dot(cArray1, cArray2, cArray3, cArray4), 9), np.round(np.linalg.multi_dot([data1, data2, data3, data4]), 9))
def test_complementaryMedianFilter(): for mode in modes.keys(): shape = np.random.randint(100, 200, [2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.complementaryMedianFilter(cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = data - filters.median_filter(data, size=kernalSize, mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)
def test_uniform_filter(): for mode in modes.keys(): shape = np.random.randint(100, 200, [2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.uniformFilter(cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.uniform_filter(data, size=kernalSize, mode=mode, cval=constantValue) assert np.array_equal(np.round(dataOutC, 8), np.round(dataOutPy, 8))
def test_triangle(): if NumCpp.NO_USE_BOOST: return shapeInput = np.random.randint(1, 100, [ 2, ]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) values = np.random.rand(3) values.sort() assert NumCpp.triangle(inShape, values[0].item(), values[1].item(), values[2].item()) is not None assert NumCpp.triangle(values[0].item(), values[1].item(), values[2].item()) is not None
def test_zeta(): if NumCpp.NO_USE_BOOST and not NumCpp.STL_SPECIAL_FUNCTIONS: return value = np.random.rand(1).item() * 5 + 1 assert (roundScaler(NumCpp.riemann_zeta_Scaler(value), NUM_DECIMALS_ROUND) == roundScaler(sp.zeta(value, 1).item(), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) * 5 + 1 cArray.setArray(data) assert np.array_equal(roundArray(NumCpp.riemann_zeta_Array(cArray), NUM_DECIMALS_ROUND), roundArray(sp.zeta(data, 1), NUM_DECIMALS_ROUND))
def test_gaussianFilter1d(): for mode in modes.keys(): size = np.random.randint(1000, 2000, [1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [size, ]).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) sigma = np.random.rand(1).item() * 2 constantValue = np.random.randint(0, 5, [1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.gaussianFilter1d(cArray, sigma, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.gaussian_filter(data, sigma, mode=mode, cval=constantValue) assert np.array_equal(np.round(dataOutC, 7), np.round(dataOutPy, 7))
def test_medianFilter1d(): for mode in modes.keys(): size = np.random.randint(1000, 2000, [1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [size, ]) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.medianFilter1d(cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.median, footprint=np.ones([kernalSize, ]), mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)
def test_trigamma(): if NumCpp.NO_USE_BOOST: return value = np.random.rand(1).item() assert (roundScaler(NumCpp.trigamma_Scaler(value), NUM_DECIMALS_ROUND) == roundScaler(sp.polygamma(1, value).item(), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert np.array_equal(roundArray(NumCpp.trigamma_Array(cArray), NUM_DECIMALS_ROUND), roundArray(sp.polygamma(1, data), NUM_DECIMALS_ROUND))
def test_expint(): if NumCpp.NO_USE_BOOST and not NumCpp.STL_SPECIAL_FUNCTIONS: return a = np.random.rand(1).item() assert (roundScaler(NumCpp.expint_Scaler(a), NUM_DECIMALS_ROUND) == roundScaler(sp.expi(a).item(), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) aArray = NumCpp.NdArray(shape) a = np.random.rand(shape.rows, shape.cols) aArray.setArray(a) assert np.array_equal(roundArray(NumCpp.expint_Array(aArray), NUM_DECIMALS_ROUND), roundArray(sp.expi(a), NUM_DECIMALS_ROUND))
def test_spherical_hankel_1(): # There is no equivalent scipy functions order = np.random.randint(0, 10) value = np.random.rand(1).item() assert NumCpp.spherical_hankel_2_Scaler(order, value) is not None shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) order = np.random.randint(0, 6) value = NumCpp.NdArray(shape) valuePy = np.random.rand(shape.rows, shape.cols) * 10 value.setArray(valuePy) assert NumCpp.spherical_hankel_2_Array(order, value) is not None
def test_softmax(): shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert np.array_equal( roundArray(NumCpp.softmax(cArray, NumCpp.Axis.NONE), NUM_DECIMALS_ROUND), roundArray(sp.softmax(data), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert np.array_equal( roundArray(NumCpp.softmax(cArray, NumCpp.Axis.ROW), NUM_DECIMALS_ROUND), roundArray(sp.softmax(data, axis=0), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert np.array_equal( roundArray(NumCpp.softmax(cArray, NumCpp.Axis.COL), NUM_DECIMALS_ROUND), roundArray(sp.softmax(data, axis=1), NUM_DECIMALS_ROUND))
def test_laplaceFilter(): for mode in modes.keys(): shape = np.random.randint(100, 200, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) # noqa cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape).astype(float) # noqa cArray.setArray(data) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.laplaceFilter(cArray, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.laplace(data, mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)
def test_airy_bi_prime(): value = np.random.rand(1).item() assert (roundScaler(NumCpp.airy_bi_prime_Scaler(value), NUM_DECIMALS_ROUND) == roundScaler( sp.airy(value)[3].item(), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) assert np.array_equal( roundArray(NumCpp.airy_bi_prime_Array(cArray), NUM_DECIMALS_ROUND), roundArray(sp.airy(data)[3], NUM_DECIMALS_ROUND))
def test_beta(): if NumCpp.NO_USE_BOOST: return shapeInput = np.random.randint(1, 100, [ 2, ]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) alpha = np.random.rand() beta = np.random.rand() randArray = NumCpp.beta(inShape, alpha, beta).getNumpyArray() assert np.array_equiv(randArray.shape, shapeInput) randValue = NumCpp.beta(alpha, beta) assert isinstance(randValue, float)
def test_digamma(): value = np.random.rand(1).item() * 10 assert (roundScaler(NumCpp.digamma_Scaler(value), NUM_DECIMALS_ROUND) == roundScaler( sp.digamma(value), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) * 10 cArray.setArray(data) assert np.array_equal( roundArray(NumCpp.digamma_Array(cArray), NUM_DECIMALS_ROUND), roundArray(sp.digamma(data), NUM_DECIMALS_ROUND))
def test_lstsq(): shapeInput = np.random.randint(5, 50, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) aArray = NumCpp.NdArray(shape) bArray = NumCpp.NdArray(1, shape.rows) aData = np.random.randint(1, 100, [shape.rows, shape.cols]) bData = np.random.randint(1, 100, [ shape.rows, ]) aArray.setArray(aData) bArray.setArray(bData) x = NumCpp.lstsq(aArray, bArray, 1e-12).getNumpyArray().flatten() assert np.array_equal( np.round(x, 9), np.round(np.linalg.lstsq(aData, bData, rcond=None)[0], 9))