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

Python sympy.cse函数代码示例

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

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



在下文中一共展示了cse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_issue_7840

def test_issue_7840():
    # daveknippers' example
    C393 = sympify( \
        'Piecewise((C391 - 1.65, C390 < 0.5), (Piecewise((C391 - 1.65, \
        C391 > 2.35), (C392, True)), True))'
    )
    C391 = sympify( \
        'Piecewise((2.05*C390**(-1.03), C390 < 0.5), (2.5*C390**(-0.625), True))'
    )
    C393 = C393.subs('C391',C391)
    # simple substitution
    sub = {}
    sub['C390'] = 0.703451854
    sub['C392'] = 1.01417794
    ss_answer = C393.subs(sub)
    # cse
    substitutions,new_eqn = cse(C393)
    for pair in substitutions:
        sub[pair[0].name] = pair[1].subs(sub)
    cse_answer = new_eqn[0].subs(sub)
    # both methods should be the same
    assert ss_answer == cse_answer

    # GitRay's example
    expr = sympify(
        "Piecewise((Symbol('ON'), Equality(Symbol('mode'), Symbol('ON'))), \
        (Piecewise((Piecewise((Symbol('OFF'), StrictLessThan(Symbol('x'), \
        Symbol('threshold'))), (Symbol('ON'), true)), Equality(Symbol('mode'), \
        Symbol('AUTO'))), (Symbol('OFF'), true)), true))"
    )
    substitutions, new_eqn = cse(expr)
    # this Piecewise should be exactly the same
    assert new_eqn[0] == expr
    # there should not be any replacements
    assert len(substitutions) < 1
开发者ID:asmeurer,项目名称:sympy,代码行数:35,代码来源:test_cse.py


示例2: test_cse_single2

def test_cse_single2():
    # Simple substitution, test for being able to pass the expression directly
    e = Add(Pow(x+y,2), sqrt(x+y))
    substs, reduced = cse(e, optimizations=[])
    assert substs == [(x0, x+y)]
    assert reduced == [sqrt(x0) + x0**2]
    assert isinstance(cse(Matrix([[1]]))[1][0], Matrix)
开发者ID:Enchanter12,项目名称:sympy,代码行数:7,代码来源:test_cse.py


示例3: test_issue_11230

def test_issue_11230():
    # a specific test that always failed
    a, b, f, k, l, i = symbols('a b f k l i')
    p = [a*b*f*k*l, a*i*k**2*l, f*i*k**2*l]
    R, C = cse(p)
    assert not any(i.is_Mul for a in C for i in a.args)

    # random tests for the issue
    from random import choice
    from sympy.core.function import expand_mul
    s = symbols('a:m')
    # 35 Mul tests, none of which should ever fail
    ex = [Mul(*[choice(s) for i in range(5)]) for i in range(7)]
    for p in subsets(ex, 3):
        p = list(p)
        R, C = cse(p)
        assert not any(i.is_Mul for a in C for i in a.args)
        for ri in reversed(R):
            for i in range(len(C)):
                C[i] = C[i].subs(*ri)
        assert p == C
    # 35 Add tests, none of which should ever fail
    ex = [Add(*[choice(s[:7]) for i in range(5)]) for i in range(7)]
    for p in subsets(ex, 3):
        p = list(p)
        was = R, C = cse(p)
        assert not any(i.is_Add for a in C for i in a.args)
        for ri in reversed(R):
            for i in range(len(C)):
                C[i] = C[i].subs(*ri)
        # use expand_mul to handle cases like this:
        # p = [a + 2*b + 2*e, 2*b + c + 2*e, b + 2*c + 2*g]
        # x0 = 2*(b + e) is identified giving a rebuilt p that
        # is now `[a + 2*(b + e), c + 2*(b + e), b + 2*c + 2*g]`
        assert p == [expand_mul(i) for i in C]
开发者ID:asmeurer,项目名称:sympy,代码行数:35,代码来源:test_cse.py


示例4: derive_solution

def derive_solution():
    from sympy import symbols, Matrix, cse, cos, sin, Abs, Rational,acos,asin

    cs,K,tec,nu,phase,sigma_phase,alpha,beta,tec_p,cs_p,sigma_tec,sigma_cs = symbols('cs K tec nu phase sigma_phase alpha beta tec_p cs_p sigma_tec sigma_cs', real=True)
    g = K*tec/nu + cs*alpha

    L = Abs(g - phase)/sigma_phase + beta*((tec - tec_p)**Rational(2)/sigma_tec**Rational(2)/Rational(2) + (cs -  cs_p)**Rational(2)/sigma_cs**Rational(2)/Rational(2))

    req,res = cse(L,optimizations='basic')
    for line in req:
        print("{} = {}".format(line[0],line[1]).replace("Abs","np.abs").replace("cos","np.cos").replace("sin","np.sin").replace("sign","np.sign"))
    print("{}".format(res[0]).replace("Abs","np.abs").replace("cos","np.cos").replace("sin","np.sin").replace("sign","np.sign"))
    print()

    grad = Matrix([sigma_tec**Rational(2)*L.diff(tec), sigma_cs**Rational(2)*L.diff(cs)])
    req,res = cse(grad,optimizations='basic')
    for line in req:
        print("{} = {}".format(line[0],line[1]).replace("Abs","np.abs").replace("cos","np.cos").replace("sin","np.sin").replace("sign","np.sign"))
    print("{}".format(res[0]).replace("Abs","np.abs").replace("cos","np.cos").replace("sin","np.sin").replace("sign","np.sign"))
    print()

    H = Matrix([[L.diff(tec).diff(tec),L.diff(tec).diff(cs)],[L.diff(cs).diff(tec),L.diff(cs).diff(cs)]])

    req,res = cse(H,optimizations='basic')
    for line in req:
        print("{} = {}".format(line[0],line[1]).replace("Abs","np.abs").replace("cos","np.cos").replace("sin","np.sin").replace("sign","np.sign"))
    print("{}".format(res[0]).replace("Abs","np.abs").replace("cos","np.cos").replace("sin","np.sin").replace("sign","np.sign"))
开发者ID:Joshuaalbert,项目名称:RadioAstronomyThings,代码行数:27,代码来源:tec_solver.py


示例5: test_derivative_subs

def test_derivative_subs():
    y = Symbol("y")
    f = Function("f")
    assert Derivative(f(x), x).subs(f(x), y) != 0
    assert Derivative(f(x), x).subs(f(x), y).subs(y, f(x)) == Derivative(f(x), x)
    # issues 1986, 1938
    assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative)
    assert cse(Derivative(f(x, y), x) + Derivative(f(x, y), y))[1][0].has(Derivative)
开发者ID:catchmrbharath,项目名称:sympy,代码行数:8,代码来源:test_subs.py


示例6: test_bypass_non_commutatives

def test_bypass_non_commutatives():
    A, B, C = symbols('A B C', commutative=False)
    l = [A*B*C, A*C]
    assert cse(l) == ([], l)
    l = [A*B*C, A*B]
    assert cse(l) == ([], l)
    l = [B*C, A*B*C]
    assert cse(l) == ([], l)
开发者ID:asmeurer,项目名称:sympy,代码行数:8,代码来源:test_cse.py


示例7: test_cse_MatrixSymbol

def test_cse_MatrixSymbol():
    # MatrixSymbols have non-Basic args, so make sure that works
    A = MatrixSymbol("A", 3, 3)
    assert cse(A) == ([], [A])

    n = symbols('n', integer=True)
    B = MatrixSymbol("B", n, n)
    assert cse(B) == ([], [B])
开发者ID:asmeurer,项目名称:sympy,代码行数:8,代码来源:test_cse.py


示例8: test_cse_ignore

def test_cse_ignore():
    exprs = [exp(y)*(3*y + 3*sqrt(x+1)), exp(y)*(5*y + 5*sqrt(x+1))]
    subst1, red1 = cse(exprs)
    assert any(y in sub.free_symbols for _, sub in subst1), "cse failed to identify any term with y"

    subst2, red2 = cse(exprs, ignore=(y,))  # y is not allowed in substitutions
    assert not any(y in sub.free_symbols for _, sub in subst2), "Sub-expressions containing y must be ignored"
    assert any(sub - sqrt(x + 1) == 0 for _, sub in subst2), "cse failed to identify sqrt(x + 1) as sub-expression"
开发者ID:asmeurer,项目名称:sympy,代码行数:8,代码来源:test_cse.py


示例9: test_cse_not_possible

def test_cse_not_possible():
    # No substitution possible.
    e = Add(x, y)
    substs, reduced = cse([e])
    assert substs == []
    assert reduced == [x + y]
    # issue 6329
    eq = meijerg((1, 2), (y, 4), (5,), [], x) + meijerg((1, 3), (y, 4), (5,), [], x)
    assert cse(eq) == ([], [eq])
开发者ID:brajeshvit,项目名称:virtual,代码行数:9,代码来源:test_cse.py


示例10: test_cse_not_possible

def test_cse_not_possible():
    # No substitution possible.
    e = Add(x, y)
    substs, reduced = cse([e], optimizations=[])
    assert substs == []
    assert reduced == [x + y]
    # issue 3230
    eq = (meijerg((1, 2), (y, 4), (5,), [], x) + \
          meijerg((1, 3), (y, 4), (5,), [], x))
    assert cse(eq) == ([], [eq])
开发者ID:StefenYin,项目名称:sympy,代码行数:10,代码来源:test_cse.py


示例11: test_derivative_subs

def test_derivative_subs():
    y = Symbol('y')
    f = Function('f')
    assert Derivative(f(x), x).subs(f(x), y) != 0
    assert Derivative(f(x), x).subs(f(x), y).subs(y, f(x)) == \
        Derivative(f(x), x)
    # issues 5085, 5037
    assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative)
    assert cse(Derivative(f(x, y), x) +
               Derivative(f(x, y), y))[1][0].has(Derivative)
开发者ID:josephwillard,项目名称:sympy,代码行数:10,代码来源:test_subs.py


示例12: test_issue_10228

def test_issue_10228():
    assert cse([x*y**2 + x*y]) == ([(x0, x*y)], [x0*y + x0])
    assert cse([x + y, 2*x + y]) == ([(x0, x + y)], [x0, x + x0])
    assert cse((w + 2*x + y + z, w + x + 1)) == (
        [(x0, w + x)], [x0 + x + y + z, x0 + 1])
    assert cse(((w + x + y + z)*(w - x))/(w + x)) == (
        [(x0, w + x)], [(x0 + y + z)*(w - x)/x0])
    a, b, c, d, f, g, j, m = symbols('a, b, c, d, f, g, j, m')
    exprs = (d*g**2*j*m, 4*a*f*g*m, a*b*c*f**2)
    assert cse(exprs) == (
        [(x0, g*m), (x1, a*f)], [d*g*j*x0, 4*x0*x1, b*c*f*x1])
开发者ID:alexako,项目名称:sympy,代码行数:11,代码来源:test_cse.py


示例13: test_cse_single

def test_cse_single():
    # Simple substitution.
    e = Add(Pow(x + y, 2), sqrt(x + y))
    substs, reduced = cse([e])
    assert substs == [(x0, x + y)]
    assert reduced == [sqrt(x0) + x0**2]

    subst42, (red42,) = cse([42])  # issue_15082
    assert len(subst42) == 0 and red42 == 42
    subst_half, (red_half,) = cse([0.5])
    assert len(subst_half) == 0 and red_half == 0.5
开发者ID:asmeurer,项目名称:sympy,代码行数:11,代码来源:test_cse.py


示例14: test_derivative_subs

def test_derivative_subs():
    y = Symbol('y')
    f = Function('f')
    assert Derivative(f(x), x).subs(f(x), y) != 0
    # need xreplace to put the function back, see #13803
    assert Derivative(f(x), x).subs(f(x), y).xreplace({y: f(x)}) == \
        Derivative(f(x), x)
    # issues 5085, 5037
    assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative)
    assert cse(Derivative(f(x, y), x) +
               Derivative(f(x, y), y))[1][0].has(Derivative)
开发者ID:Lenqth,项目名称:sympy,代码行数:11,代码来源:test_subs.py


示例15: test_derivative_subs

def test_derivative_subs():
    x = Symbol('x')
    y = Symbol('y')
    f = Function('f')
    assert Derivative(f(x), x).subs(f(x), y) != 0
    assert Derivative(f(x), x).subs(f(x), y).subs(y, f(x)) == \
        Derivative(f(x), x)
    # issues 1986, 1938
    assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative)
    assert cse(Derivative(f(x, y), x) +
               Derivative(f(x, y), y))[1][0].has(Derivative)
开发者ID:malikdiarra,项目名称:sympy,代码行数:11,代码来源:test_subs.py


示例16: test_subtraction_opt

def test_subtraction_opt():
    # Make sure subtraction is optimized.
    e = (x-y)*(z-y) + exp((x-y)*(z-y))
    substs, reduced = cse([e], optimizations=[(cse_opts.sub_pre,cse_opts.sub_post)])
    assert substs == [(x0, x - y), (x1, y - z), (x2, x0*x1)]
    assert reduced == [-x2 + exp(-x2)]
    assert cse(-(x - y)*(z - y) + exp(-(x - y)*(z - y))) == \
        ([(x0, (x - y)*(y - z))], [x0 + exp(x0)])
    # issue 978
    n = -1 + 1/x
    e = n/x/(-n)**2 - 1/n/x
    assert cse(e) == ([], [0])
开发者ID:Enchanter12,项目名称:sympy,代码行数:12,代码来源:test_cse.py


示例17: test_cse_single2

def test_cse_single2():
    # Simple substitution, test for being able to pass the expression directly
    e = Add(Pow(x + y, 2), sqrt(x + y))
    substs, reduced = cse(e)
    assert substs == [(x0, x + y)]
    assert reduced == [sqrt(x0) + x0**2]
    substs, reduced = cse(Matrix([[1]]))
    assert isinstance(reduced[0], Matrix)

    subst42, (red42,) = cse(42)  # issue 15082
    assert len(subst42) == 0 and red42 == 42
    subst_half, (red_half,) = cse(0.5)  # issue 15082
    assert len(subst_half) == 0 and red_half == 0.5
开发者ID:asmeurer,项目名称:sympy,代码行数:13,代码来源:test_cse.py


示例18: test_pow_invpow

def test_pow_invpow():
    assert cse(1 / x ** 2 + x ** 2) == ([(x0, x ** 2)], [x0 + 1 / x0])
    assert cse(x ** 2 + (1 + 1 / x ** 2) / x ** 2) == ([(x0, x ** 2), (x1, 1 / x0)], [x0 + x1 * (x1 + 1)])
    assert cse(1 / x ** 2 + (1 + 1 / x ** 2) * x ** 2) == ([(x0, x ** 2), (x1, 1 / x0)], [x0 * (x1 + 1) + x1])
    assert cse(cos(1 / x ** 2) + sin(1 / x ** 2)) == ([(x0, x ** (-2))], [sin(x0) + cos(x0)])
    assert cse(cos(x ** 2) + sin(x ** 2)) == ([(x0, x ** 2)], [sin(x0) + cos(x0)])
    assert cse(y / (2 + x ** 2) + z / x ** 2 / y) == ([(x0, x ** 2)], [y / (x0 + 2) + z / (x0 * y)])
    assert cse(exp(x ** 2) + x ** 2 * cos(1 / x ** 2)) == ([(x0, x ** 2)], [x0 * cos(1 / x0) + exp(x0)])
    assert cse((1 + 1 / x ** 2) / x ** 2) == ([(x0, x ** (-2))], [x0 * (x0 + 1)])
    assert cse(x ** (2 * y) + x ** (-2 * y)) == ([(x0, x ** (2 * y))], [x0 + 1 / x0])
开发者ID:brajeshvit,项目名称:virtual,代码行数:10,代码来源:test_cse.py


示例19: test_subtraction_opt

def test_subtraction_opt():
    # Make sure subtraction is optimized.
    e = (x - y) * (z - y) + exp((x - y) * (z - y))
    substs, reduced = cse([e], optimizations=[(cse_opts.sub_pre, cse_opts.sub_post)])
    assert substs == [(x0, (x - y) * (y - z))]
    assert reduced == [-x0 + exp(-x0)]
    e = -(x - y) * (z - y) + exp(-(x - y) * (z - y))
    substs, reduced = cse([e], optimizations=[(cse_opts.sub_pre, cse_opts.sub_post)])
    assert substs == [(x0, (x - y) * (y - z))]
    assert reduced == [x0 + exp(x0)]
    # issue 4077
    n = -1 + 1 / x
    e = n / x / (-n) ** 2 - 1 / n / x
    assert cse(e, optimizations=[(cse_opts.sub_pre, cse_opts.sub_post)]) == ([], [0])
开发者ID:brajeshvit,项目名称:virtual,代码行数:14,代码来源:test_cse.py


示例20: test_dont_cse_tuples

def test_dont_cse_tuples():
    from sympy import Subs

    f = Function("f")
    g = Function("g")

    name_val, (expr,) = cse(Subs(f(x, y), (x, y), (0, 1)) + Subs(g(x, y), (x, y), (0, 1)))

    assert name_val == []
    assert expr == (Subs(f(x, y), (x, y), (0, 1)) + Subs(g(x, y), (x, y), (0, 1)))

    name_val, (expr,) = cse(Subs(f(x, y), (x, y), (0, x + y)) + Subs(g(x, y), (x, y), (0, x + y)))

    assert name_val == [(x0, x + y)]
    assert expr == Subs(f(x, y), (x, y), (0, x0)) + Subs(g(x, y), (x, y), (0, x0))
开发者ID:brajeshvit,项目名称:virtual,代码行数:15,代码来源:test_cse.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python sympy.denom函数代码示例发布时间:2022-05-27
下一篇:
Python sympy.csch函数代码示例发布时间: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