1
$\begingroup$

I need a function, which will collect terms next to the same rational power of a given variable. For the input

RCollect[3xy^(3/2)+2/y+zx/y-y/x+2*x^z y^(3/2),y]

the desired output is

(2+zx)y^(-1)-y/x+(3x+2x^z)y^(3/2).

I'm aware that output is ambiguous when powers of the RCollect variables are unspecified RCollect[2 x^k1+3 x^k2]. In such a case, the function should collect terms next to x^k1 and x^k2 separately.

$\endgroup$

1 Answer 1

2
$\begingroup$

Let me first advise that the expressions like xz Mathematica understands as a new variable, rather than a product. To define a product, you should instead write either x z (with white space) or x*z.

There is no need for a special function for your task. Collect makes the job like follows: Here is your expression:

expr = 3 x y^(3/2) + 2/y + z x/y - y/x + 2*x^z y^(3/2);

Let us first prepare a list for Collect:

lst = (Map[Exponent[#, y] &, List @@ expr] // DeleteDuplicates) /. 
  x_ -> y^x

(*  {1/y, y, y^(3/2)} *)

Now:

Collect[expr, lst]

(*  -(y/x) + (3 x + 2 x^z) y^(3/2) + (2 + x z)/y  *)

Have fun!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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