1
$\begingroup$

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!

$\endgroup$
  • $\begingroup$ Would you please explain the logic of why m is used but u is discarded? $\endgroup$ – Mr.Wizard Oct 10 '14 at 1:00
  • $\begingroup$ Also why F(x^4) should yield (4,4) rather than (4,0). $\endgroup$ – Mr.Wizard Oct 10 '14 at 1:01
  • $\begingroup$ 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$. $\endgroup$ – Seunghyeok Oct 10 '14 at 1:16
  • $\begingroup$ 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! $\endgroup$ – Seunghyeok Oct 10 '14 at 1:19
2
$\begingroup$

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} *)
$\endgroup$
  • $\begingroup$ Thank you so much! I will try to understand your suggestion. $\endgroup$ – Seunghyeok Oct 10 '14 at 1:28
  • $\begingroup$ The answer is what I really wanted. I sincerely appreciate for it! $\endgroup$ – Seunghyeok Oct 10 '14 at 4:33
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Oct 10 '14 at 13:29
  • $\begingroup$ @Mr.Wizard, unfortunately, CoefficientRules works with integer powers only. $\endgroup$ – ybeltukov Oct 10 '14 at 15:18
  • $\begingroup$ I guess that's not a problem for the OP since he Accepted it. Vote restored. $\endgroup$ – Mr.Wizard Oct 10 '14 at 16:37
4
$\begingroup$

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

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

(* Out[176]= {{{1, 0, 4, 1}, m}, {{0, 1, 2, 3}, n}} *)
$\endgroup$
  • $\begingroup$ Thanks :) It seems good for me. I will check it out. $\endgroup$ – Seunghyeok Oct 12 '14 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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