# Extracting a product of factors from an expression in unevaluated form

Suppose I have an expression like so:

$\qquad {\rm expr}=2\,i\,{\rm Gamma}(4)\,\pi\, 9.93 f(x)\,g(y) \frac{h(x, 4y)^3}{11 q}$

What is best (most succinct) way to extract out only the product of numbers?

$\qquad 2\,i\,{\rm Gamma}(4)\,\pi\,9.93\,\frac{1}{11}$

I would want this the code to work for as generic an expression as possible in the sense that it should pick all the numbers(Real or Complex or Transcendental or Special Constants).

Edit:

Select[expr,NumericQ]
(*0. + 10.8327 I*)


almost seems to work but not quite as it performs the product and simplifies. Any way to HoldForm here?

• Look up FactorTerms[]. Mar 11, 2018 at 18:09
• @J.M. Any suggestions following the edit? Wasn't able to make much out of FactorTerms[] Mar 13, 2018 at 19:05
• Well, did you look at what expr looked like after storing that expression? Why were you expecting that the arithmetic won't be performed? Mar 13, 2018 at 19:10
• @J.M. Sure, the expression has been evaluated in the assignment itself. I wanted to find a way to extract out all the numeric factors in the form they appear in the expression. Is there a way to do that? So, if my input expression was the one I wrote as in the example, the output should have the unevaluated form of the numeric subexpression. Mar 13, 2018 at 19:42

UPDATE

Following function is more efficient and it captures terms in the denominator:

extractCoefficient=


Result:

In[364]:= extractCoefficient[2I Gamma[4] Pi 9.93 f[x] g[y] h[x,4 y]^3/(11q)]
Out[364]= 2*I*Gamma[4]*\[Pi]*9.93*1/11


OLD CODE

Following function, albeit ugly, does the job.

extractCoefficient=
//.Hold[a_]Hold[b_]:> Hold[a b])
,HoldAll];


In[152]:= extractCoefficient[2I Gamma[4] Pi 9.93 f[x] g[y] h[x,4 y]^3/(11q)]

Out[152]= (((2 9.93) I) \[Pi]) Gamma[4]

• It misses the $1/11$ factor Mar 14, 2018 at 8:33
• Corrected it now. Mar 14, 2018 at 8:47