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_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_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_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_newton(): root = np.random.randint(-50, 50, [ 1, ]).item() roots = np.array([root, root + np.random.randint(5, 50, [ 1, ]).item()]) largestRoot = np.max(roots).item() rootsC = NumCpp.NdArray(1, roots.size) rootsC.setArray(roots) polyC = NumCpp.Poly1d(rootsC, True) rootC = int(np.round(NumCpp.newton_roots(polyC, largestRoot))) assert rootC == largestRoot
def test_simpson(): numCoefficients = np.random.randint(2, 5, [ 1, ]).item() coefficients = np.random.randint(-20, 20, [ numCoefficients, ]) coefficientsC = NumCpp.NdArray(1, numCoefficients) coefficientsC.setArray(coefficients) poly = np.poly1d(np.flipud(coefficients), False) polyIntegral = poly.integ() polyC = NumCpp.Poly1d(coefficientsC, False) a, b = np.sort(np.random.rand(2) * 100 - 50) area = np.round(polyIntegral(b) - polyIntegral(a), NUM_DECIMALS_ROUND) areaC = np.round(NumCpp.integrate_simpson(polyC, a, b), NUM_DECIMALS_ROUND) assert area == areaC
def test_romberg(): PERCENT_LEEWAY = 0.1 numCoefficients = np.random.randint(2, 5, [ 1, ]).item() coefficients = np.random.randint(-20, 20, [ numCoefficients, ]) coefficientsC = NumCpp.NdArray(1, numCoefficients) coefficientsC.setArray(coefficients) poly = np.poly1d(np.flipud(coefficients), False) polyIntegral = poly.integ() polyC = NumCpp.Poly1d(coefficientsC, False) a, b = np.sort(np.random.rand(2) * 100 - 50) area = np.round(polyIntegral(b) - polyIntegral(a), NUM_DECIMALS_ROUND) areaC = np.round(NumCpp.integrate_romberg(polyC, a, b), NUM_DECIMALS_ROUND) # romberg is much less acurate so let's give it some leeway areaLow, areaHigh = np.sort( [area * (1 - PERCENT_LEEWAY), area * (1 + PERCENT_LEEWAY)]) assert areaLow < areaC < areaHigh