def test_poly1D_fit(): polyOrder = np.random.randint(2, 5) numMeasurements = np.random.randint(50, 100) xValues = np.random.rand(numMeasurements) * 100 - 50 coefficients = np.random.rand(polyOrder + 1) * 5 - 10 yValues = [] for x in xValues: y = 0 for order in range(polyOrder + 1): y += coefficients[order] * x**order yValues.append(y + np.random.randn(1).item()) yValues = np.array(yValues) yValues = yValues.reshape(yValues.size, 1) cX = NumCpp.NdArray(1, xValues.size) cY = NumCpp.NdArray(yValues.size, 1) cX.setArray(xValues) cY.setArray(yValues) poly = Polynomial.fit(xValues, yValues.flatten(), polyOrder).convert().coef # noqa polyC = NumCpp.Poly1d.fit( cX, cY, polyOrder).coefficients().getNumpyArray().flatten() assert np.array_equal(np.round(poly, 5), np.round(polyC, 5))
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_ellint_3(): if NumCpp.NUMCPP_NO_USE_BOOST and not NumCpp.STL_SPECIAL_FUNCTIONS: return a = np.random.rand(1).item() b = np.random.rand(1).item() c = np.random.rand(1).item() assert (roundScaler(NumCpp.ellint_3_Scaler(a, b, c), NUM_DECIMALS_ROUND) == roundScaler( float(mpmath.ellippi(b, c, a**2)), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) aArray = NumCpp.NdArray(shape) bArray = NumCpp.NdArray(shape) cArray = NumCpp.NdArray(shape) a = np.random.rand(shape.rows, shape.cols) b = np.random.rand(shape.rows, shape.cols) c = np.random.rand(shape.rows, shape.cols) aArray.setArray(a) bArray.setArray(b) cArray.setArray(c) result = np.zeros_like(a) for row in range(a.shape[0]): for col in range(a.shape[1]): result[row, col] = float( mpmath.ellippi(b[row, col], c[row, col], a[row, col]**2)) assert np.array_equal( roundArray(NumCpp.ellint_3_Array(aArray, bArray, cArray), NUM_DECIMALS_ROUND), roundArray(result, NUM_DECIMALS_ROUND))
def test_det(): order = 2 shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(np.double) 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(np.double) 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(np.double) cArray.setArray(data) assert round(NumCpp.det(cArray)) == round(np.linalg.det(data).item())
def test_poly1D_operators(): numRoots = np.random.randint(3, 10, [1, ]).item() roots = np.random.randint(-20, 20, [numRoots, ]) rootsC = NumCpp.NdArray(1, numRoots) rootsC.setArray(roots) poly = np.poly1d(roots, True) polyC = NumCpp.Poly1d(rootsC, True) numCoefficients = np.random.randint(3, 10, [1, ]).item() coefficients = np.random.randint(-20, 20, [numCoefficients, ]) coefficientsC = NumCpp.NdArray(1, numCoefficients) coefficientsC.setArray(coefficients) polyC2 = NumCpp.Poly1d(coefficientsC, False) poly2 = np.poly1d(np.flip(coefficients)) assert np.array_equal(np.fliplr((polyC + polyC2).coefficients().getNumpyArray()).flatten(), (poly + poly2).coefficients) assert np.array_equal(np.fliplr((polyC - polyC2).coefficients().getNumpyArray()).flatten(), (poly - poly2).coefficients) assert np.array_equal(np.fliplr((polyC * polyC2).coefficients().getNumpyArray()).flatten(), (poly * poly2).coefficients) exponent = np.random.randint(0, 5, [1, ]).item() assert np.array_equal(np.fliplr((polyC2 ** exponent).coefficients().getNumpyArray()).flatten(), (poly2 ** exponent).coefficients) polyC.print()
def test_convolve(): 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(10, 20, shape).astype(float) # noqa 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 weights = np.random.randint(-2, 3, [kernalSize, kernalSize]).astype(float) cWeights = NumCpp.NdArray(kernalSize) cWeights.setArray(weights) dataOutC = NumCpp.convolve(cArray, kernalSize, cWeights, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.convolve(data, weights, mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)
def test_convolve1d(): 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(float) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) weights = np.random.randint(1, 5, [ kernalSize, ]) cWeights = NumCpp.NdArray(1, kernalSize) cWeights.setArray(weights) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.convolve1d(cArray, cWeights, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.convolve(data, weights, mode=mode, cval=constantValue) assert np.array_equal(np.round(dataOutC, 8), np.round(dataOutPy, 8))
def test_functions(): k = np.random.randint(1, 5, [3, 1]).astype(np.double) v = np.random.randint(1, 5, [3, 1]).astype(np.double) theta = np.random.rand(1).item() * np.pi * 2 vec = NumCpp.rodriguesRotation(k, theta, v).flatten() dcm = angleAxisRotation(k, theta) vecPy = dcm.dot(v).flatten() assert np.array_equal(np.round(vec, 10), np.round(vecPy, 10)) radians = np.random.rand(1) * 2 * np.pi axis = np.random.rand(3) cAxis = NumCpp.NdArray(1, 3) cAxis.setArray(axis) rot = NumCpp.DCM.angleAxisRotationNdArray(cAxis, radians.item()) vecBody = list() vecInertial = list() for _ in range(1000): vec = np.random.randint(1, 100, [3, 1]) vec = vec / np.linalg.norm(vec) vecBody.append(vec.flatten()) vecInertial.append(rot.dot(vec).flatten()) vecBody = np.array(vecBody) vecInertial = np.array(vecInertial) rotWahba = NumCpp.wahbasProblem(vecInertial, vecBody) assert np.array_equal(np.round(rotWahba, 10), np.round(rot, 10)) radians = np.random.rand(1) * 2 * np.pi axis = np.random.rand(3) cAxis = NumCpp.NdArray(1, 3) cAxis.setArray(axis) rot = NumCpp.DCM.angleAxisRotationNdArray(cAxis, radians.item()) vecBody = list() vecInertial = list() for _ in range(1000): vec = np.random.randint(1, 100, [3, 1]) vec = vec / np.linalg.norm(vec) vecBody.append(vec.flatten()) vecInertial.append(rot.dot(vec).flatten()) vecBody = np.array(vecBody) vecInertial = np.array(vecInertial) weights = np.random.randint(1, 100) * np.ones( [vecBody.shape[0]]) # all the same weight for simplicity... rotWahba = NumCpp.wahbasProblemWeighted(vecInertial, vecBody, weights) assert np.array_equal(np.round(rotWahba, 10), np.round(rot, 10))
def test_solve(): sizeInput = np.random.randint(5, 50) shape = NumCpp.Shape(sizeInput) aArray = NumCpp.NdArray(shape) a = np.random.randint(1, 100, [shape.rows, shape.cols]) aArray.setArray(a) b = np.random.randint(1, 100, [shape.rows, 1]) bArray = NumCpp.NdArray(*b.shape) bArray.setArray(b) assert np.array_equal(np.round(NumCpp.solve(aArray, bArray), 8), np.round(np.linalg.solve(a, b), 8))
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))
def test_laguerre(): allTrue = True for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_laguerre(order, x) valueCpp = NumCpp.laguerre_Scaler1(order, x) if np.round(valuePy, DECIMALS_ROUND) != np.round(valueCpp, DECIMALS_ROUND): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_laguerre(order, x) valueCpp = NumCpp.laguerre_Array1(order, cArray) if not np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): degree = np.random.randint(0, 10, [1, ]).item() x = np.random.rand(1).item() valuePy = sp.eval_genlaguerre(degree, order, x) valueCpp = NumCpp.laguerre_Scaler2(order, degree, x) if np.round(valuePy, DECIMALS_ROUND) != np.round(valueCpp, DECIMALS_ROUND): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): degree = np.random.randint(0, 10, [1, ]).item() shapeInput = np.random.randint(10, 100, [2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_genlaguerre(degree, order, x) valueCpp = NumCpp.laguerre_Array2(order, degree, cArray) if not np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)): allTrue = False assert allTrue
def test_laguerre(): if NumCpp.NO_USE_BOOST and not NumCpp.STL_SPECIAL_FUNCTIONS: return for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_laguerre(order, x) valueCpp = NumCpp.laguerre_Scaler1(order, x) assert np.round(valuePy, DECIMALS_ROUND) == np.round(valueCpp, DECIMALS_ROUND) for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_laguerre(order, x) valueCpp = NumCpp.laguerre_Array1(order, cArray) assert np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)) for order in range(ORDER_MAX): degree = np.random.randint(0, 10, [ 1, ]).item() x = np.random.rand(1).item() valuePy = sp.eval_genlaguerre(degree, order, x) valueCpp = NumCpp.laguerre_Scaler2(order, degree, x) assert np.round(valuePy, DECIMALS_ROUND) == np.round(valueCpp, DECIMALS_ROUND) for order in range(ORDER_MAX): degree = np.random.randint(0, 10, [ 1, ]).item() shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_genlaguerre(degree, order, x) valueCpp = NumCpp.laguerre_Array2(order, degree, cArray) assert np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND))
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_chebyshev(): allTrue = True for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Scaler(order, x) if np.round(valuePy, DECIMALS_ROUND) != np.round(valueCpp, DECIMALS_ROUND): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Array(order, cArray) if not np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Scaler(order, x) if np.round(valuePy, DECIMALS_ROUND) != np.round(valueCpp, DECIMALS_ROUND): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Array(order, cArray) if not np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)): allTrue = False assert allTrue
def test_inv(): 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.inv(cArray).getNumpyArray(), 9), np.round(np.linalg.inv(data), 9))
def test_poly1D_coefficents_constructor(): numCoefficients = np.random.randint(3, 10, [1, ]).item() coefficients = np.random.randint(-20, 20, [numCoefficients, ]) coefficientsC = NumCpp.NdArray(1, numCoefficients) coefficientsC.setArray(coefficients) polyC = NumCpp.Poly1d(coefficientsC, False) assert np.array_equal(polyC.coefficients().getNumpyArray().flatten(), coefficients)
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(float) 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_poly1D_integ_deriv_area_order(): numRoots = np.random.randint(3, 10, [ 1, ]).item() roots = np.random.randint(-20, 20, [ numRoots, ]) rootsC = NumCpp.NdArray(1, numRoots) rootsC.setArray(roots) poly = np.poly1d(roots, True) polyC = NumCpp.Poly1d(rootsC, True) bounds = np.random.rand(2) * 100 - 50 bounds = np.sort(bounds) polyIntegral = poly.integ() assert np.round(polyC.area(*bounds), 3) == np.round( polyIntegral(bounds[1]) - polyIntegral(bounds[0]), 3) assert np.array_equal( polyC.deriv().coefficients().getNumpyArray().flatten(), np.flipud(poly.deriv().coefficients)) assert np.array_equal( polyC.integ().coefficients().getNumpyArray().flatten(), np.flipud(poly.integ().coefficients)) assert polyC.order() == roots.size value = np.random.randint(-20, 20, [ 1, ]).item() assert polyC[value] == poly(value)
def test_rankFilter(): 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) # noqa cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) rank = np.random.randint(0, kernalSize**2 - 1, [ 1, ]).item() constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.rankFilter(cArray, kernalSize, rank, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.rank_filter(data, rank, size=kernalSize, mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)
def test_uniformFilter1d(): 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(float) 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.uniformFilter1d( cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.mean, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)
def test_Vec2_array_constructor(): components = np.random.rand(2) shape = NumCpp.Shape(1, 2) cArray = NumCpp.NdArray(shape) cArray.setArray(components) vec2 = NumCpp.Vec2(cArray) assert vec2.x == components[0].item() assert vec2.y == components[1].item()
def test_poly1D_roots_constructor(): numRoots = np.random.randint(3, 10, [1, ]).item() roots = np.random.randint(-20, 20, [numRoots, ]) rootsC = NumCpp.NdArray(1, numRoots) rootsC.setArray(roots) poly = np.poly1d(roots, True) polyC = NumCpp.Poly1d(rootsC, True) assert np.array_equal(np.fliplr(polyC.coefficients().getNumpyArray()).flatten().astype(np.int), poly.coefficients)
def test_shuffle(): 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) NumCpp.shuffle(cArray)
def test_lu_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 = NumCpp.lu_decomposition(cArray) p = np.round(np.dot(l.getNumpyArray(), u.getNumpyArray())).astype(np.int) assert np.array_equal(p, data)
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_Vec3_array_constructor(): components = np.random.rand(3) shape = NumCpp.Shape(1, 3) cArray = NumCpp.NdArray(shape) cArray.setArray(components) vec3 = NumCpp.Vec3(cArray) assert vec3.x == components[0].item() assert vec3.y == components[1].item() assert vec3.z == components[2].item()
def test_beta(): if NumCpp.NO_USE_BOOST and not NumCpp.STL_SPECIAL_FUNCTIONS: return a = np.random.rand(1).item() * 10 b = np.random.rand(1).item() * 10 assert (roundScaler(NumCpp.beta_Scaler(a, b), NUM_DECIMALS_ROUND) == roundScaler(sp.beta(a, b).item(), NUM_DECIMALS_ROUND)) shapeInput = np.random.randint(20, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) aArray = NumCpp.NdArray(shape) bArray = NumCpp.NdArray(shape) a = np.random.rand(shape.rows, shape.cols) * 10 b = np.random.rand(shape.rows, shape.cols) * 10 aArray.setArray(a) bArray.setArray(b) assert np.array_equal(roundArray(NumCpp.beta_Array(aArray, bArray), NUM_DECIMALS_ROUND), roundArray(sp.beta(a, b), NUM_DECIMALS_ROUND))
def test_discrete(): shapeInput = np.random.randint(1, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) weights = np.random.randint(1, 10, [shape.rows, shape.cols]) cArray.setArray(weights) assert NumCpp.discrete(shape, cArray) is not None assert NumCpp.discrete(cArray) is not None
def test_chebyshev(): if NumCpp.NO_USE_BOOST: return for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Scaler(order, x) assert np.round(valuePy, DECIMALS_ROUND) == np.round(valueCpp, DECIMALS_ROUND) for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Array(order, cArray) assert np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)) for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Scaler(order, x) assert np.round(valuePy, DECIMALS_ROUND) == np.round(valueCpp, DECIMALS_ROUND) for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Array(order, cArray) assert np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, 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]) 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 dataOutC = NumCpp.laplaceFilter(cArray, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.laplace(data, mode=mode, cval=constantValue) assert np.array_equal(dataOutC, dataOutPy)