• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python autowrap.autowrap函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sympy.utilities.autowrap.autowrap函数的典型用法代码示例。如果您正苦于以下问题:Python autowrap函数的具体用法?Python autowrap怎么用?Python autowrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了autowrap函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:AdrianPotter,项目名称:sympy,代码行数:7,代码来源:test_autowrap.py


示例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})
开发者ID:asmeurer,项目名称:sympy,代码行数:13,代码来源:test_autowrap.py


示例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"
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:14,代码来源:test_autowrap.py


示例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"
开发者ID:101man,项目名称:sympy,代码行数:14,代码来源:test_autowrap.py


示例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")
开发者ID:A-turing-machine,项目名称:sympy,代码行数:34,代码来源:test_autowrap.py


示例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
开发者ID:qsnake,项目名称:sympy,代码行数:7,代码来源:test_autowrap.py


示例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)
开发者ID:jcockayne,项目名称:bayesian_pdes,代码行数:31,代码来源:sympy_helpers.py


示例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"
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:17,代码来源:test_autowrap.py


示例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
开发者ID:gitter-badger,项目名称:pygom,代码行数:46,代码来源:ode_utils.py


示例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)
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:9,代码来源:test_autowrap.py


示例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]
            )
开发者ID:hyharry,项目名称:PPFem,代码行数:18,代码来源:lagrange_basis.py


示例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)
开发者ID:asmeurer,项目名称:sympy,代码行数:18,代码来源:test_autowrap.py


示例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)
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:9,代码来源:test_autowrap.py


示例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
开发者ID:lukauskas,项目名称:means,代码行数:10,代码来源:problems.py


示例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
开发者ID:AdrianPotter,项目名称:sympy,代码行数:10,代码来源:test_autowrap.py


示例16: test_autowrap_store_files

def test_autowrap_store_files():
    x, y = symbols('x y')
    tmp = tempfile.mkdtemp()
    TmpFileManager.tmp_folder(tmp)

    f = autowrap(x + y, backend='dummy', tempdir=tmp)
    assert f() == str(x + y)
    assert os.access(tmp, os.F_OK)

    TmpFileManager.cleanup()
开发者ID:cklb,项目名称:sympy,代码行数:10,代码来源:test_autowrap.py


示例17: runtest_autowrap_matrix_vector

def runtest_autowrap_matrix_vector(language, backend):
    has_module('numpy')
    x, y = symbols('x y', cls=IndexedBase)
    expr = Eq(y[i], A[i, j]*x[j])
    mv = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M = numpy.random.rand(10, 20)
    x = numpy.random.rand(20)
    y = numpy.dot(M, x)
    assert numpy.sum(numpy.abs(y - mv(M, x))) < 1e-13
开发者ID:AdrianPotter,项目名称:sympy,代码行数:11,代码来源:test_autowrap.py


示例18: __init__

    def __init__(self, loss_type):
        """
        set up symbolic derivations
        """

        from sympy.utilities.autowrap import autowrap

        self.x = x = sympy.Symbol("x")
        self.y = y = sympy.Symbol("y")
        self.cx = cx = sympy.Symbol("cx")
        self.cy = cy = sympy.Symbol("cy")
        self.r = r = sympy.Symbol("r")
        
        if loss_type == "eucledian_squared":
            self.fun = (sympy.sqrt((x-cx)**2 + (y-cy)**2) - r)**2

        if loss_type == "eucledian_abs":
            self.fun = sympy.sqrt((sympy.sqrt((x-cx)**2 + (y-cy)**2) - r)**2 + 0.001)

        if loss_type == "algebraic_squared":
            self.fun = ((x-cx)**2 + (y-cy)**2 - r)**2


        #TODO replace x**2 with x*x
        self.fun = self.fun.expand(deep=True)
        sympy.pprint(self.fun)

        self.d_cx = self.fun.diff(cx).expand(deep=True)
        self.d_cy = self.fun.diff(cy).expand(deep=True)
        self.d_r = self.fun.diff(r).expand(deep=True)


        # generate native code
        native_lang = "C"

        # generate native code
        if native_lang == "fortran":
            self.c_fun = autowrap(self.fun, language="F95", backend="f2py")
            self.c_d_cx = autowrap(self.d_cx)
            self.c_d_cy = autowrap(self.d_cy)
            self.c_d_r = autowrap(self.d_r)
        else:
            self.c_fun = autowrap(self.fun, language="C", backend="Cython", tempdir=".")
            self.c_d_cx = autowrap(self.d_cx, language="C", backend="Cython", tempdir=".")
            self.c_d_cy = autowrap(self.d_cy, language="C", backend="Cython", tempdir=".")
            self.c_d_r = autowrap(self.d_r, language="C", backend="Cython", tempdir=".")

        self.grads = {"cx": self.d_cx, "cy": self.d_cy, "r": self.d_r}
        self.c_grads = {"cx": self.c_d_cx, "cy": self.c_d_cy, "r": self.c_d_r}
开发者ID:cwidmer,项目名称:GRED,代码行数:49,代码来源:fit_sphere.py


示例19: runtest_autowrap_matrix_vector

def runtest_autowrap_matrix_vector(language, backend):
    has_module('numpy')
    A, x, y = map(IndexedBase, ['A', 'x', 'y'])
    n, m = symbols('n m', integer=True)
    i = Idx('i', m)
    j = Idx('j', n)
    expr = Eq(y[i], A[i, j]*x[j])
    mv = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M = numpy.random.rand(10, 20)
    x = numpy.random.rand(20)
    y = numpy.dot(M, x)
    assert numpy.sum(numpy.abs(y - mv(M, x))) < 1e-13
开发者ID:qsnake,项目名称:sympy,代码行数:14,代码来源:test_autowrap.py


示例20: runtest_autowrap_matrix_matrix

def runtest_autowrap_matrix_matrix(language, backend):
    has_module('numpy')
    A, B, C = map(IndexedBase, ['A', 'B', 'C'])
    n, m, d = symbols('n m d', integer=True)
    i = Idx('i', m)
    j = Idx('j', n)
    k = Idx('k', d)
    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
开发者ID:qsnake,项目名称:sympy,代码行数:15,代码来源:test_autowrap.py



注:本文中的sympy.utilities.autowrap.autowrap函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python codegen.codegen函数代码示例发布时间:2022-05-27
下一篇:
Python utilities.sift函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap