5
$\begingroup$

For a term in a polynomial, say

387 a1^4 a2^3 x^3 y^7 z^100 w^364

what is the most efficient way to extract the coefficient of this term, i.e. 387?

$\endgroup$
1
  • 2
    $\begingroup$ Put all variables equal to 1? $\endgroup$ Commented May 15, 2016 at 0:43

8 Answers 8

7
$\begingroup$

Another way:

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;

vars = Variables[poly];
exps = Exponent[poly, vars];

Coefficient[poly, Times @@ (vars^exps)]
387

or

Cancel[poly/(Times @@ (vars^exps))]
387

p.s. In general, you'd want to hit your polynomial with MonomialList if it's not a proper monomial.

And just for fun, here's an overly complicated solution

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;
vars = Variables[poly];

Times @@ ((# D[Log[poly], #]) + 1 & /@ vars) * 
  Fold[Integrate[#1, {#2, 0, 1}] &, poly, vars]
387
$\endgroup$
6
$\begingroup$
lookMaNoXYZ = 1 & @@@ # &;

lookMaNoXYZ[ x^2 y^6 ]

1

lookMaNoXYZ[10 x^2  Pi y^6 / 4]

(5 π)/2

lookMaNoXYZ[x^2 55. y^6]

55.

$\endgroup$
4
  • 1
    $\begingroup$ ... i am also puzzled how/why this works. $\endgroup$
    – kglr
    Commented May 12, 2016 at 21:47
  • $\begingroup$ So it replaces all the powers with 1's... obviously $\endgroup$
    – swish
    Commented May 12, 2016 at 22:19
  • 2
    $\begingroup$ Try lookMaNoXYZ[Sqrt[2/3] x^5 y^7]. $\endgroup$ Commented May 12, 2016 at 22:52
  • 1
    $\begingroup$ To extend the comment by J. M.: lookMaNoXYZ fails whenever parts of the coefficient are not AtomQ. $\endgroup$
    – Karsten7
    Commented May 13, 2016 at 14:31
6
$\begingroup$

Example:

(*Example 1*)   
Select[387 a1^4 a2^3 x^3 y^7 z^100 w^364, IntegerQ]

(*Example 2*)
Select[x^2 y^6, IntegerQ]

Output:

(*Output 1*)
387

(*Output 2*)
1

Reference:

Select
IntegerQ

$\endgroup$
4
  • $\begingroup$ This is so beautiful, thank you. $\endgroup$
    – Wenzhe
    Commented May 12, 2016 at 20:41
  • $\begingroup$ I am sure there are other solutions but this should do it. Glad it helped $\endgroup$ Commented May 12, 2016 at 20:42
  • 1
    $\begingroup$ It works only if the head is Times, for example Select[x^2, IntegerQ] returns 2. $\endgroup$
    – swish
    Commented May 12, 2017 at 23:04
  • $\begingroup$ Multiplying by Unique[] adds very little overhead: Select[x^2 Unique[], IntegerQ] or in general Select[monomial * Unique[], NumericQ]. Select is the fastest way I've found. $\endgroup$
    – Michael E2
    Commented May 13, 2017 at 15:40
4
$\begingroup$
ClearAll[cF]
cF = # /. Thread[Variables[#] -> 1] &;

cF[x^2 y^6 ]

1

cF [367 a1^4 a2^3 x^3   y^7 z^100 w^364 ]

367

$\endgroup$
0
4
$\begingroup$
poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;

CoefficientRules[poly][[1, 2]]

387

$\endgroup$
1
  • $\begingroup$ CoefficientRules[poly] is{{4, 3, 364, 3, 7, 100} -> 387}. Also, FromCoefficientRules[%, Variables[poly]] == poly is True $\endgroup$
    – user1066
    Commented May 13, 2016 at 5:58
2
$\begingroup$

Using some undocumented functionality:

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;
GroebnerBasis`DistributedTermsList[poly, Variables[poly]][[1, 1, 2]]
   387

poly2 = Sqrt[2/3] x^5 y^7;
GroebnerBasis`DistributedTermsList[poly2, Variables[poly2]][[1, 1, 2]]
   Sqrt[2/3]

GroebnerBasis`DistributedTermsList[x^3 y^2, {x, y}][[1, 1, 2]]
   1
$\endgroup$
2
$\begingroup$

I would use FactorTermsList:

First @ FactorTermsList[387 a1^4 a2^3 x^3 y^7 z^100 w^364]

387

$\endgroup$
1
$\begingroup$

for your particular example, this is quite simple:

FirstCase[387 a1^4 a2^3 x^3 y^7 z^100 w^364, _Integer]
(* 387 *)

this case works even with integer being not in the first position:

FirstCase[a1^4 a2^3 x^3 387 y^7 z^100 w^364, _Integer]
(* 387 *)

Also, as @MichaelE2 suggested perhaps the shortest answer:

First[a1^4 a2^3 x^3 y^7 387 z^100 w^364]
(* 387 *) 
$\endgroup$
2
  • 3
    $\begingroup$ For this particular example, First[387 a1^4 a2^3 x^3 y^7 z^100 w^364] is even simpler. :) $\endgroup$
    – Michael E2
    Commented May 12, 2017 at 22:37
  • $\begingroup$ Agreed @MichaelE2 $\endgroup$
    – Ali Hashmi
    Commented May 13, 2017 at 9:14

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.