如何使用SymPy替换模式

时间:2021-03-19 20:24:02

In my code I need to substitute ALL the expressions of sin(g(t)) (g being a continuous function) to g(t) (it's the tight angle approximation). This is a sample of what I get from my code:

在我的代码中,我需要将sin(g(t))的所有表达式(g是连续函数)替换为g(t)(它是紧密角度近似)。这是我从代码中获得的一个示例:

-29.4*sin(2*t) - 19.6*sin(f(t)) + 4.0*Derivative(f(t), t)**2

I need to substitute both sin(f(t)) and sin(2*t). Not just one of them and sin(2*t) changes, (sin(f(t)) is always the same). Is there a simpler way than adding an extra variable for what's inside the sin or isn't there?

我需要替换sin(f(t))和sin(2 * t)。不仅仅是其中一个和sin(2 * t)的变化,(sin(f(t))总是相同的)。有没有比为罪中的内容添加额外变量更简单的方法还是不存在?

1 个解决方案

#1


5  

Is this what you are trying to do?

这是你想要做的吗?

import sympy as sp

t = sp.symbols('t')
f = sp.Function('f')

expr_v1 = -29.4*sp.sin(2*t) - 19.6*sp.sin(f(t)) + 4.0*sp.Derivative(f(t), t)**2
print('expr_v1 = ', expr_v1)

expr_v2 = expr_v1.replace(sp.sin, lambda *args: args[0])
print('expr_v2 = ', expr_v2)
expr_v1 =  -29.4*sin(2*t) - 19.6*sin(f(t)) + 4.0*Derivative(f(t),t)**2
expr_v2 =  -58.8*t - 19.6*f(t) + 4.0*Derivative(f(t), t)**2

#1


5  

Is this what you are trying to do?

这是你想要做的吗?

import sympy as sp

t = sp.symbols('t')
f = sp.Function('f')

expr_v1 = -29.4*sp.sin(2*t) - 19.6*sp.sin(f(t)) + 4.0*sp.Derivative(f(t), t)**2
print('expr_v1 = ', expr_v1)

expr_v2 = expr_v1.replace(sp.sin, lambda *args: args[0])
print('expr_v2 = ', expr_v2)
expr_v1 =  -29.4*sin(2*t) - 19.6*sin(f(t)) + 4.0*Derivative(f(t),t)**2
expr_v2 =  -58.8*t - 19.6*f(t) + 4.0*Derivative(f(t), t)**2