def runtest_ufuncify(language, backend): a, b, c = symbols('a b c') fabc = ufuncify((a, b, c), a * b + c, backend=backend) facb = ufuncify((a, c, b), a * b + c, backend=backend) grid = numpy.linspace(-2, 2, 50) b = numpy.linspace(-5, 4, 50) c = numpy.linspace(-1, 1, 50) expected = grid * b + c numpy.testing.assert_allclose(fabc(grid, b, c), expected) numpy.testing.assert_allclose(facb(grid, c, b), expected)
def test_wrap_twice_f95_f2py(): runtest_autowrap_twice('f95', 'f2py') f = ufuncify(a, 2 * a, backend='f2py') f2 = ufuncify((a, ), 2 * a, backend='f2py') f3 = ufuncify((a, ), 2 * a, backend='f2py', language='f95') grid = numpy.linspace(-2, 2, 50) numpy.testing.assert_allclose(f(grid), f2(grid)) numpy.testing.assert_allclose(f(grid), f3(grid))
def runtest_ufuncify(language, backend): a, b, c = symbols('a b c') fabc = ufuncify((a, b, c), a*b + c, backend=backend) facb = ufuncify((a, c, b), a*b + c, backend=backend) grid = numpy.linspace(-2, 2, 50) b = numpy.linspace(-5, 4, 50) c = numpy.linspace(-1, 1, 50) expected = grid*b + c numpy.testing.assert_allclose(fabc(grid, b, c), expected) numpy.testing.assert_allclose(facb(grid, c, b), expected)
def runtest_ufuncify(language, backend): a, b, c = symbols('a b c') if backend == 'numpy': pytest.raises(ValueError, lambda: ufuncify((a, b), Eq(b, a + b), backend=backend)) pytest.raises(ValueError, lambda: ufuncify((a, b, c), [Eq(b, a), Eq(c, a**2)], backend=backend)) helpers = [('helper', a*b, (a, b)), ('spam', sin(a), (a,))] fabc = ufuncify((a, b, c), a*b + c, backend=backend) fabc_2 = ufuncify((a, b, c), a*b + c, backend=backend, helpers=helpers) facb = ufuncify((a, c, b), a*b + c, backend=backend) grid = numpy.linspace(-2, 2, 50) b = numpy.linspace(-5, 4, 50) c = numpy.linspace(-1, 1, 50) expected = grid*b + c numpy.testing.assert_allclose(fabc(grid, b, c), expected) numpy.testing.assert_allclose(fabc_2(grid, b, c), expected) numpy.testing.assert_allclose(facb(grid, c, b), expected)
def main(): print(__doc__) x = symbols('x') # a numpy array we can apply the ufuncs to grid = np.linspace(-1, 1, 1000) # set mpmath precision to 20 significant numbers for verification mpmath.mp.dps = 20 print("Compiling legendre ufuncs and checking results:") # Let's also plot the ufunc's we generate for n in range(6): # Setup the Diofant expression to ufuncify expr = legendre(n, x) print("The polynomial of degree %i is" % n) pprint(expr) # This is where the magic happens: binary_poly = ufuncify(x, expr) # It's now ready for use with numpy arrays polyvector = binary_poly(grid) # let's check the values against mpmath's legendre function maxdiff = 0 for j in range(len(grid)): precise_val = mpmath.legendre(n, grid[j]) diff = abs(polyvector[j] - precise_val) if diff > maxdiff: maxdiff = diff print("The largest error in applied ufunc was %e" % maxdiff) assert maxdiff < 1e-14 # We can also attach the autowrapped legendre polynomial to a diofant # function and plot values as they are calculated by the binary function plot1 = plt.pyplot.plot(grid, polyvector, hold=True) print( "Here's a plot with values calculated by the wrapped binary functions") plt.pyplot.show()