# How to define a function that counts the exponents

I want to define a function $F$ described as follows:

1. The input of $F$ is a polynomial such as

m*u*x^4*y + n*v*x^2*y^3


where $m$ and $n$ are constants, and $u$, $v$, $x$ and $y$ are variables.

2. Assume that $F$ receives a monomial. Then $F$ counts the number of exponents of $x$ and $y$ in the following way.

F(x^k*y^l) = (k, k+l).


More generally,

F(m*u*x^k*y^l) = m*(k, k+l).


Here the constant $m$ is multiplied in front of the 2-tuple, while we ignore the variable $u$ because we just want to count the exponents of $x$ and $y$ only. Hence it holds that for instance

F(x^4) = (4,4)
F(x^2y^3) = (2,5)


and

F(2xy^2) = 2(1,3).

3. Finally, suppose that $F$ receives a polynomial. In this case,

F(mux^4*y + nvx^2*y^3) = m*(4,5) + n*(2,5).


As a matter of fact, I prefer not to compute the result $m*(4,5) + n*(2,5)$ so that it becomes $(4m+2n, 5m+5n)$.

Could you help me in defining this function? Thank you in advance!

• Would you please explain the logic of why m is used but u is discarded? Oct 10, 2014 at 1:00
• Also why F(x^4) should yield (4,4) rather than (4,0). Oct 10, 2014 at 1:01
• The only characters $m$ and $n$ serve as constants. ($m$ and $n$ denote quantities like the dimension of the problem.) Except them, every character is regarded as a variable. In particular $u$, $v$, $x$ and $y$ are considered to be variables. I want to count the exponents of $x$ and $y$ only, discarding the other variables $u$ and $v$. Oct 10, 2014 at 1:16
• Also, I want to regard $x^4$ as $x^4y^0$. Since F(x^ky^l) = (k, k+l), we should have $F(x^4) = F(x^4y^0) = (4, 4+0) = (4,4)$. Thank you for your comment! Oct 10, 2014 at 1:19

I propose

pol = m*u*x^4*y + n*v*x^2*y^3

Defer@*Times @@@ CoefficientRules[pol /. {u|v -> 1, x -> x y}, {x, y}] // Total
(* {4, 5} m + {2, 5} n *)


Use Composition[Defer, Times] instead of Defer@*Times if you have version 9 or earlier.

A more general definition:

vars = {x, y};
ignore = {u, v};

Defer@Times@## &[#2, Accumulate@#[[;; Length[vars]]]] & @@@
CoefficientRules[pol, Join[vars, ignore]] // Total
(* n {2, 5} + m {4, 5} *)

• Thank you so much! I will try to understand your suggestion. Oct 10, 2014 at 1:28
• The answer is what I really wanted. I sincerely appreciate for it! Oct 10, 2014 at 4:33
• This doesn't appear to yield the desired output for x^k*y^l or m*u*x^k*y^l, the first two examples. Oct 10, 2014 at 13:29
• @Mr.Wizard, unfortunately, CoefficientRules works with integer powers only. Oct 10, 2014 at 15:18
• I guess that's not a problem for the OP since he Accepted it. Vote restored. Oct 10, 2014 at 16:37

There is an internal function that might be of use here.

pol = m*u*x^4*y + n*v*x^2*y^3;
GroebnerBasisDistributedTermsList[pol, {u, v, x, y}][]

(* Out= {{{1, 0, 4, 1}, m}, {{0, 1, 2, 3}, n}} *)
`
• Thanks :) It seems good for me. I will check it out. Oct 12, 2014 at 23:30