本文整理汇总了Python中sympy.utilities.autowrap.autowrap函数的典型用法代码示例。如果您正苦于以下问题:Python autowrap函数的具体用法?Python autowrap怎么用?Python autowrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了autowrap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: runtest_autowrap_twice
def runtest_autowrap_twice(language, backend):
f = autowrap((((a + b)/c)**5).expand(), language, backend)
g = autowrap((((a + b)/c)**4).expand(), language, backend)
# check that autowrap updates the module name. Else, g gives the same as f
assert f(1, -2, 1) == -1.0
assert g(1, -2, 1) == 1.0
示例2: test_issue_15230
def test_issue_15230():
has_module('f2py')
x, y = symbols('x, y')
expr = Mod(x, 3.0) - Mod(y, -2.0)
f = autowrap(expr, args=[x, y], language='F95')
exp_res = float(expr.xreplace({x: 3.5, y: 2.7}).evalf())
assert abs(f(3.5, 2.7) - exp_res) < 1e-14
x, y = symbols('x, y', integer=True)
expr = Mod(x, 3) - Mod(y, -2)
f = autowrap(expr, args=[x, y], language='F95')
assert f(3, 2) == expr.xreplace({x: 3, y: 2})
示例3: test_autowrap_args
def test_autowrap_args():
x, y, z = symbols("x y z")
raises(CodeGenArgumentListError, lambda: autowrap(Eq(z, x + y), backend="dummy", args=[x]))
f = autowrap(Eq(z, x + y), backend="dummy", args=[y, x])
assert f() == str(x + y)
assert f.args == "y, x"
assert f.returns == "z"
raises(CodeGenArgumentListError, lambda: autowrap(Eq(z, x + y + z), backend="dummy", args=[x, y]))
f = autowrap(Eq(z, x + y + z), backend="dummy", args=[y, x, z])
assert f() == str(x + y + z)
assert f.args == "y, x, z"
assert f.returns == "z"
示例4: test_autowrap_args
def test_autowrap_args():
x, y, z = symbols('x y z')
raises(CodeGenArgumentListError, "autowrap(Eq(z, x + y), backend='dummy', args=[x])")
f = autowrap(Eq(z, x + y), backend='dummy', args=[y, x])
assert f() == str(x + y)
assert f.args == "y, x"
assert f.returns == "z"
raises(CodeGenArgumentListError, "autowrap(Eq(z, x + y + z), backend='dummy', args=[x, y])")
f = autowrap(Eq(z, x + y + z), backend='dummy', args=[y, x, z])
assert f() == str(x + y + z)
assert f.args == "y, x, z"
assert f.returns == "z"
示例5: runtest_issue_10274
def runtest_issue_10274(language, backend):
expr = (a - b + c)**(13)
tmp = tempfile.mkdtemp()
f = autowrap(expr, language, backend, tempdir=tmp, helpers=('helper', a - b + c, (a, b, c)))
assert f(1, 1, 1) == 1
for file in os.listdir(tmp):
if file.startswith("wrapped_code_") and file.endswith(".c"):
fil = open(tmp + '/' + file)
assert fil.read() == ("/******************************************************************************\n"
" * Code generated with sympy "+ sympy.__version__+" *\n"
" * *\n"
" * See http://www.sympy.org/ for more information. *\n"
" * *\n"
" * This file is part of 'autowrap' *\n"
" ******************************************************************************/\n"
"#include " + '"' + file[:-1]+ 'h"' + "\n"
"#include <math.h>\n"
"\n"
"double helper(double a, double b, double c) {\n"
"\n"
" double helper_result;\n"
" helper_result = a - b + c;\n"
" return helper_result;\n"
"\n"
"}\n"
"\n"
"double autofunc(double a, double b, double c) {\n"
"\n"
" double autofunc_result;\n"
" autofunc_result = pow(helper(a, b, c), 13);\n"
" return autofunc_result;\n"
"\n"
"}\n")
示例6: runtest_autowrap_trace
def runtest_autowrap_trace(language, backend):
has_module('numpy')
A = IndexedBase('A')
n = symbols('n', integer=True)
i = Idx('i', n)
trace = autowrap(A[i, i], language, backend)
assert trace(numpy.eye(100)) == 100
示例7: sympy_function
def sympy_function(sympy_expression, sympy_symbols, mode=None, lambdify_modules=None, apply_factory=generic_applier):
"""
Convert a sympy expression into a function whose arguments reflect a particular vector structure.
:param sympy_expression: A sympy expression in a flattened set of variables.
:param sympy_symbols: The symbols from the expression, assembled into arrays reflecting their vector structure.
Examples:
[[x_1, x_2], [y_1, y_2]] : expects two vectors (iterables) of length 2
[[x_1, x_2], y] : expects a vector of length 2 and a scalar.
:param mode: Either 'lambda' or 'compile'. 'lambda' will use sympy.lambdify while 'compile' will use sympy.autowrap
default: Lambda
:param apply_factory: An expression which will apply the flattening operator to the arguments, and then pass
to the sympy expression.
:return: The callable expression
"""
from sympy import lambdify
from sympy.utilities import autowrap
flattened = []
for s in sympy_symbols:
if type(s) is list:
flattened += s
else:
flattened.append(s)
if mode is None or mode.lower() == "lambda":
sympyd = lambdify(flattened, sympy_expression, modules=lambdify_modules)
elif mode.lower() in ["cython", "f2py"]:
sympyd = autowrap.autowrap(sympy_expression, backend=mode.lower(), args=flattened)
else:
raise Exception("Mode for generation of sympy function {} not understood.".format(mode))
return apply_factory(sympyd, sympy_symbols)
示例8: test_autowrap_dummy
def test_autowrap_dummy():
x, y, z = symbols("x y z")
# Uses DummyWrapper to test that codegen works as expected
f = autowrap(x + y, backend="dummy")
assert f() == str(x + y)
assert f.args == "x, y"
assert f.returns == "nameless"
f = autowrap(Eq(z, x + y), backend="dummy")
assert f() == str(x + y)
assert f.args == "x, y"
assert f.returns == "z"
f = autowrap(Eq(z, x + y + z), backend="dummy")
assert f() == str(x + y + z)
assert f.args == "x, y, z"
assert f.returns == "z"
示例9: __init__
def __init__(self, backend=None):
'''
Initializing the class. Automatically checks which backend is
available. Currently only those linked to numpy are used where
those linked with Theano are not.
'''
if backend is None:
self._backend = None
x = sympy.Symbol('x')
expr = sympy.sin(x) / x
# lets assume that we can't do theano.... (for now)
# now lets explore the other options
try:
# first, f2py. This is the best because Cython below may
# throw out errors with older versions of sympy due to a
# bug (calling numpy.h, a c header file which has problem
# dealing with vector output).
a = autowrap(expr, args=[x])
a(1)
# congrats!
self._backend = 'f2py'
except:
try:
import cython
a = autowrap(expr, args=[x], backend='Cython')
a(1)
# also need to test the matrix version because
# previous version of sympy does not work when compiling
# a matrix
exprMatrix = sympy.zeros(2,1)
exprMatrix[0] = expr
exprMatrix[1] = expr
a = autowrap(exprMatrix, args=[x], backend='Cython')
a(1)
self._backend = 'Cython'
except:
# we have truely failed in life. A standard lambda function!
# unfortunately, this may be the case when we are running
# stuff in a parallel setting where we create objects in
# pure computation nodes with no compile mechanism
self._backend = 'lambda'
else:
self._backend = backend
示例10: test_autowrap_store_files
def test_autowrap_store_files():
x, y = symbols("x y")
tmp = tempfile.mkdtemp()
try:
f = autowrap(x + y, backend="dummy", tempdir=tmp)
assert f() == str(x + y)
assert os.access(tmp, os.F_OK)
finally:
shutil.rmtree(tmp)
示例11: __init__
def __init__(self, points, index, dimension=1):
x = sy.symbols('x')
x_i = points[index][0]
def f(j):
return (x - points[j][0]) / (x_i - points[j][0])
if dimension == 1:
self._L = product(f, 1, index) * product(f, index+2, len(points))
self._value = autowrap(self._L)
self._grad = autowrap(sy.diff(self._L, x), args=(x,))
else:
self._L = sy.Matrix([product(f, 1, index) * product(f, index+2, len(points)) for index in range(dimension)])
self._value = autowrap(self._L, args=[x])
self._grad = autowrap(
sy.Matrix([sy.diff(self._L[i], x) for i in range(dimension)]),
args=[x]
)
示例12: runtest_issue_15337
def runtest_issue_15337(language, backend):
# NOTE : autowrap was originally designed to only accept an iterable for
# the kwarg "helpers", but in issue 10274 the user mistakenly thought that
# if there was only a single helper it did not need to be passed via an
# iterable that wrapped the helper tuple. There were no tests for this
# behavior so when the code was changed to accept a single tuple it broke
# the original behavior. These tests below ensure that both now work.
a, b, c, d, e = symbols('a, b, c, d, e')
expr = (a - b + c - d + e)**13
exp_res = (1. - 2. + 3. - 4. + 5.)**13
f = autowrap(expr, language, backend, args=(a, b, c, d, e),
helpers=('f1', a - b + c, (a, b, c)))
numpy.testing.assert_allclose(f(1, 2, 3, 4, 5), exp_res)
f = autowrap(expr, language, backend, args=(a, b, c, d, e),
helpers=(('f1', a - b, (a, b)), ('f2', c - d, (c, d))))
numpy.testing.assert_allclose(f(1, 2, 3, 4, 5), exp_res)
示例13: test_autowrap_store_files_issue_gh12939
def test_autowrap_store_files_issue_gh12939():
x, y = symbols('x y')
tmp = './tmp'
try:
f = autowrap(x + y, backend='dummy', tempdir=tmp)
assert f() == str(x + y)
assert os.access(tmp, os.F_OK)
finally:
shutil.rmtree(tmp)
示例14: propensities_as_function
def propensities_as_function(self):
all_symbols = self.species + self.parameters
wrapping_func = lambda x: autowrap(x, args=all_symbols, language='C', backend='Cython')
wrapped_functions = map(wrapping_func, self.propensities)
def f(*args):
ans = np.array([w_f(*args) for w_f in wrapped_functions])
return ans
return f
示例15: runtest_autowrap_matrix_matrix
def runtest_autowrap_matrix_matrix(language, backend):
has_module('numpy')
expr = Eq(C[i, j], A[i, k]*B[k, j])
matmat = autowrap(expr, language, backend)
# compare with numpy's dot product
M1 = numpy.random.rand(10, 20)
M2 = numpy.random.rand(20, 15)
M3 = numpy.dot(M1, M2)
assert numpy.sum(numpy.abs(M3 - matmat(M1, M2))) < 1e-13