简体   繁体   English

Python3 - Sympy:扩展trig函数的产品

[英]Python3 - Sympy: expand products of trig functions

I cannot find a way to have SymPy expand products like cos(a)*cos(b) into sum of trig functions of sum of angles. 我找不到让SymPy将cos(a)*cos(b)等产品扩展为角度和的三角函数之和的方法。

from sympy import *
init_printing()
wrf,wlo,t = symbols('\omega_RF \omega_LO t')
c = cos(wrf*t)*cos(wlo*t)
expand_trig(c)

Keeps the product intact. 保持产品完好无损。 simplify(c) or trigsimp(c) also do not give any alternative form. simplify(c)trigsimp(c)也不提供任何替代形式。

I would like to have cos(a)*cos(b) to be expanded to 1/2*(cos(a+b) + cos(ab)) ...any hints? 我想将cos(a)*cos(b)扩展为1/2*(cos(a+b) + cos(ab)) ...任何提示?

Per the docstring, help(sympy.fu) , 根据文档字符串, help(sympy.fu)

fu will try to minimize the objective function measure . fu会尽量减少目标函数的measure By default this first minimizes the number of trig terms and then minimizes the number of total operations. 默认情况下,这首先最小化trig项的数量,然后最小化总操作的数量。

However, if you pass 但是,如果你通过

measure=lambda x: -x.count_ops()

then fu will try to maximize the op count . 那么fu 会尝试最大化操作次数


import sympy as sy
sy.init_printing()
wrf, wlo, t = sy.symbols('\omega_RF \omega_LO t')
c = sy.cos(wrf*t)*sy.cos(wlo*t)
print(sy.fu(c, measure=lambda x: -x.count_ops()))

yields 产量

cos(\omega_LO*t - \omega_RF*t)/2 + cos(\omega_LO*t + \omega_RF*t)/2

Alternatively, you could call the Fu transformation TR8 directly : 或者,您可以直接调用Fu转换TR8

from sympy.simplify.fu import TR8
print(TR8(c))

yields the same result. 产生相同的结果。 The docstring, help(sys.modules['sympy.simplify.fu']) explains the transformations available. docstring, help(sys.modules['sympy.simplify.fu'])解释了可用的转换。 Here is the summary; 这是摘要; check the docstring for more information: 检查docstring以获取更多信息:

TR0 - simplify expression
TR1 - sec-csc to cos-sin
TR2 - tan-cot to sin-cos ratio
TR2i - sin-cos ratio to tan
TR3 - angle canonicalization
TR4 - functions at special angles
TR5 - powers of sin to powers of cos
TR6 - powers of cos to powers of sin
TR7 - reduce cos power (increase angle)
TR8 - expand products of sin-cos to sums
TR9 - contract sums of sin-cos to products
TR10 - separate sin-cos arguments
TR10i - collect sin-cos arguments
TR11 - reduce double angles
TR12 - separate tan arguments
TR12i - collect tan arguments
TR13 - expand product of tan-cot
TRmorrie - prod(cos(x*2**i), (i, 0, k - 1)) -> sin(2**k*x)/(2**k*sin(x))
TR14 - factored powers of sin or cos to cos or sin power
TR15 - negative powers of sin to cot power
TR16 - negative powers of cos to tan power
TR22 - tan-cot powers to negative powers of sec-csc functions
TR111 - negative sin-cos-tan powers to csc-sec-cot

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

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