2

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()?

Sohail Si
  • 2,750
  • 2
  • 22
  • 36

1 Answers1

1

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
smichr
  • 16,948
  • 2
  • 27
  • 34
  • The solution using `as_independent` worked for me! Just need to be careful the expression is not like `coeff*exp(e1)*exp(e2)`. I used `simplify()` for this purpose. This answers the question "How to isolate the exponent" (2nd part). Matching large formulas using `match` remains unsolved, but not my problem really. ` def decomp_exp(x): exp = simplify(exp) coeff, e = x.as_independent(exp) assert isinstance(e, exp) expon = e.exp assert not isinstance(coeff, exp) assert simplify(coeff*exp(expon) - x) == 0 return coeff, expon ` – Sohail Si Aug 30 '17 at 14:58