简体   繁体   English

Sympy中的f(x).subs()替换(python)

[英]f(x).subs() substitution in sympy (python)

I defined symbols a and f. 我定义了符号a和f。 I'm expecting to use "a(3).subs(a,f)" to get f(3), but instead I got a(3). 我期望使用“ a(3).subs(a,f)”来获取f(3),但我却得到了a(3)。 What's wrong with it? 它出什么问题了?

a, f = symbols('a f')

a(3).subs(a,f)

您已将f定义为一个函数,然后将其替换为symbols()的返回值。

You can use replace to change the function. 您可以使用replace更改功能。 Here are some examples. 这里有些例子。

import sympy as sp

f = sp.Function('f')
g = sp.Function('g')
x,y = sp.symbols('x, y')

f(x).replace(f, g)

g(x)

f(x).replace(f, sp.sin)

sin(x)

f(x,y).replace(f, lambda *args: sp.exp(-5 * args[0] + args[1] ))

exp(-5*x + y)

The documentation provides an extensive list of additional examples. 文档提供了大量其他示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM