问题
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
, andpower_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