Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very simple question which appears not to have already been answered on this forum. Is there built-in functionality that returns the degree of a multivariable polynomial? For example if the polynomial is

s1 + s2^2 s3 + s3^2 s4^7

I want Mathematica to return 9. If there is no automatic solution, could somebody please suggest a simple (and fast) implementation?

EDIT: I removed my "solution" because it actually only works for simple examples. I tested out the four solutions presented so far on a degree 20 polynomial in 6 variables (ByteCount[poly] = 2006352). I used AbsoluteTiming to determine that the answer I chose is the fastest, with a run-time of 53.06 s for 1000 evaluations. This is quite a bit faster than the closest competitor's run-time of 283.76 s for 1000 evaluations.

UPDATE: I tested the improved version of the solution (the one with random reals) and found that the run-time is now slightly worse but still far better than the competition at 90.52 s for 1000 evaluations.

share|improve this question
up vote 10 down vote accepted


 Exponent[# /. Thread[Variables[#] -> \[FormalX]], \[FormalX]] &[s1 + s2^2 s3 + s3^2 s4^7]
 (* 9 *)


UPDATE: As noted by Daniel in the comments, the original function gives the wrong result if cancellations occur after all variables are replaced by the same symbol. To fix this issue, one can modify the function as

Exponent[# /. ((# -> \[FormalX] RandomReal[]) & /@ Variables[#]), \[FormalX]] & 

(per Daniel's suggestion), or as

Max@Exponent[MonomialList[#] /.Thread[Variables[#] -> \[FormalX]], \[FormalX]]& 
share|improve this answer
In practice, one would use a formal variable for safety... – J. M. Nov 20 '12 at 12:30
@J.M. thank you; updated using your suggestion. – kglr Nov 20 '12 at 12:35
Use random reals times \[FormalX] to avoid accidental cancellations. – Daniel Lichtblau Nov 20 '12 at 15:55
I learned this the hard way, trying to figure out why a definite integral I knew we were unable to do was instead giving zero. Now zero is, for a number of reasons, a "common" wrong answer in that part of the computational world. But this one was not caused by any of the usual suspects. The thing was split into two pieces, both of which failed. You can guess what $Failed-$Failed gives. – Daniel Lichtblau Nov 20 '12 at 20:04
Also safe would be : Exponent[# /. Thread[Variables[#] -> (\[FormalX] Variables[#]]), \[FormalX]] &[s1 + s2^2 s3 + s3^2 s4^7] – Rolf Mertig Nov 20 '12 at 22:35
Max[Plus @@@ CoefficientRules[#][[All, 1]]] &@ (s1 + s2^2 s3 + s3^2 s4^7)

=> 9


J.M. (in a comment) suggests the following neat modification:

Max[Cases[CoefficientRules[s1 + s2^2 s3 + s3^2 s4^7], v_?VectorQ :> Total[v], 2]]
share|improve this answer
Alternatively: Max[Cases[CoefficientRules[s1 + s2^2 s3 + s3^2 s4^7], v_?VectorQ :> Total[v], 2]] – J. M. Nov 20 '12 at 12:29
That's a very elegant method – user 106 Nov 20 '12 at 12:59
This is the method I was about to add, +1. – rcollyer Nov 20 '12 at 14:12

Here's an implementation adapted from a routine by Eric Weisstein:

AlgebraicDegree[eqn_, vars_List] := Max[Total[
      GroebnerBasis`DistributedTermsList[eqn /. Equal :> Subtract, vars][[1, All, 1]], {2}]]

AlgebraicDegree[eqn_] := AlgebraicDegree[eqn, Variables[eqn]]


AlgebraicDegree[s1 + s2^2 s3 + s3^2 s4^7]
share|improve this answer

OP's MonomialList[] approach could certainly be simplified somewhat:

Max[Composition[Total[#[[All, 2]]] &, Rest, FactorList] /@
    MonomialList[s1 - s2^2 s3 + 3 s3^2 s4^7]]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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