I am trying to extract the exponent in formulas that resemble the Gaussian PDF, in form of F*exp(E)
. The function match()
from SymPy works for simple cases but not for more complicated formulas such as below.
from __future__ import division
from sympy import *
δ, b, σ, m, s, μ, x = symbols('δ b σ m s μ x', real=True)
gauss = 1/σ/sqrt(2*pi)*exp( -(x-μ)**2/σ/σ/2 )
big = sqrt(2*σ**2*x + 2*s**2)* exp(
b**2/(2*x*(σ**2*x + s**2)) - b**2/(2*x*s**2) - b*μ/(σ**2*x + s**2) + b*δ/s**2 + μ**2*x/(2*σ**2*x + 2*s**2) - μ**2/(2*σ**2) + μ*δ/σ**2 - x*δ**2/(2*s**2) - δ**2/(2*σ**2)
) / (2*sqrt(pi)*σ*s)
F, E = Wild('F', exclude=[exp,]), Wild('E', exclude=[exp,])
pprint( gauss)
print("match:")
pprint( gauss.match(F*exp(E)))
pprint( big )
print("match:")
pprint( big.match(F*exp(E))) # Fails to match and returns None.
The formula for big
is obviously in the form of F*exp(E) but match()
does not work on it. How can I extract the exponent and the normalisation factor from Gauss-like formulas?
How can I isolate the exponent in an equation with exp()
?
Only use the exclusion on Wild F. Then the E will match the exp part of the expression. This works for gauss but not big -- I don't know why. So, althernatively, you can still use the exclusion only on Wild F and use m = foo.match(E*F)
-- the exponent will be E.exp
.
Another way to crack this Mul without matching is to use as_independent
coeff, e = foo.as_independent(exp)
expo = e.exp