I have an unknown pdf f[x], and I need to express integrals involving f[x] as expansions of the moments of f[x]. For example:

gau[x_, v_] = ((2*Pi*v)^(-1/2))*E^-((x^2)/(2*v));
ns = Series[gau[x - z, v], {v, Infinity, 4}] // Normal;
p = Integrate[f[z]*ns, {z, -Infinity, Infinity}]

How can I us pattern matching to express p in terms of m[1], m[2], m[3]..., where Integrate[f[x]*x^n,{x,-Infinity,Infinity}]->m[n]?

I need this to work even if the variable of integration is something other than x, and I need it to make the substitution ONLY when n is a non-negative integer.


  • $\begingroup$ Your code defines p as an integral over z but you wish it to be expressed as a sum of integrals over x. This seems inconsistent. $\endgroup$
    – bbgodfrey
    Mar 1, 2015 at 3:39

1 Answer 1


Instead of using replacement rules or pattern matching, I would extract the coefficients directly:

Total@MapIndexed[#1 m[First@#2 - 1] &, CoefficientList[ns, z]]

This extracts a list of the coefficients of the powers of z in ns, then multiplies each one of them by m[n] (I subtract one since index 1 corresponds to m[0]), finally totaling all the terms together.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.