Sympy: Multiplications of exponential rather than exponential of sum

爱⌒轻易说出口 提交于 2019-12-10 21:27:30

问题


I'm searching how to tell SymPy to use a multiplication of exponentials rather than an exponential of a sum. That is, it currently gives me exp(a + b) and I would want to get exp(a)*exp(b). There must be a fairly easy way but I can't seem to find it.


回答1:


You could use the expand() function to show the expression with multiplication of bases rather than the sum of exponents:

>>> from sympy import *
>>> a, b = symbols('a b')
>>> expr = exp(a + b)
>>> expr
exp(a + b)
>>> expr.expand()
exp(a)*exp(b)

The documentation for this function is here. The relevant parts are summarised below:

sympy.core.function.expand(e, deep=True, modulus=None, power_base=True, power_exp=True, mul=True, log=True, multinomial=True, basic=True, **hints)

Expand an expression using methods given as hints.

Hints evaluated unless explicitly set to False are: basic, log, multinomial, mul, power_base, and power_exp ...

It's clear that power_exp is the relevant hint:

power_exp

Expand addition in exponents into multiplied bases.

>>> exp(x + y).expand(power_exp=True)
exp(x)*exp(y)
>>> (2**(x + y)).expand(power_exp=True)
2**x*2**y

Turning it to False leaves the expression unchanged:

>>> expr.expand(power_exp=False)
exp(a + b)


来源:https://stackoverflow.com/questions/28117770/sympy-multiplications-of-exponential-rather-than-exponential-of-sum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!