Skip to main content
added 150 characters in body
Source Link
march
  • 24.2k
  • 2
  • 46
  • 102

The issue is that MonomialList and friends only work if the variables have integer powers, and so v^a2, for instance, is a problem.

Update

Another way, with pattern matching and Collect:

Collect[P, Times @@@ Rest@Subsets@{x^(_ : 1), y^(_ : 1), v^(_ : 1)}]

Original Solution

Here's hack that's a little ugly but should generally work:

Total@Module[{i = 2, rule},
  rule = Thread[# -> (# /. v^_ :> v^i++)] &@Cases[P, v^_, Infinity];
  MonomialList[Expand@PMonomialList[P /. rule, {x, y, v}] /. Reverse /@ rule
 ]
(* -d1 d2 + (-3 + d1 + d2) x + a2 b2 v^a2 x + a3 b3 v^a3 x + (d1 + d2) y - 2 x y - y^2 *)

The point is to replace any instance of v^(something) with a unique v^(integer), make the replacement v^(something) -> v^(integer) in the expression, construct the desired expression using Total@MonomialList, and then undoing the replacement.

The issue is that MonomialList and friends only work if the variables have integer powers, and so v^a2, for instance, is a problem.

Here's hack that's a little ugly but should generally work:

Total@Module[{i = 2, rule},
  rule = Thread[# -> (# /. v^_ :> v^i++)] &@Cases[P, v^_, Infinity];
  MonomialList[Expand@P /. rule, {x, y, v}] /. Reverse /@ rule
 ]
(* -d1 d2 + (-3 + d1 + d2) x + a2 b2 v^a2 x + a3 b3 v^a3 x + (d1 + d2) y - 2 x y - y^2 *)

The point is to replace any instance of v^(something) with a unique v^(integer), make the replacement v^(something) -> v^(integer) in the expression, construct the desired expression using Total@MonomialList, and then undoing the replacement.

The issue is that MonomialList and friends only work if the variables have integer powers, and so v^a2, for instance, is a problem.

Update

Another way, with pattern matching and Collect:

Collect[P, Times @@@ Rest@Subsets@{x^(_ : 1), y^(_ : 1), v^(_ : 1)}]

Original Solution

Here's hack that's a little ugly but should generally work:

Total@Module[{i = 2, rule},
  rule = Thread[# -> (# /. v^_ :> v^i++)] &@Cases[P, v^_, Infinity];
  MonomialList[P /. rule, {x, y, v}] /. Reverse /@ rule
 ]
(* -d1 d2 + (-3 + d1 + d2) x + a2 b2 v^a2 x + a3 b3 v^a3 x + (d1 + d2) y - 2 x y - y^2 *)

The point is to replace any instance of v^(something) with a unique v^(integer), make the replacement v^(something) -> v^(integer) in the expression, construct the desired expression using Total@MonomialList, and then undoing the replacement.

Source Link
march
  • 24.2k
  • 2
  • 46
  • 102

The issue is that MonomialList and friends only work if the variables have integer powers, and so v^a2, for instance, is a problem.

Here's hack that's a little ugly but should generally work:

Total@Module[{i = 2, rule},
  rule = Thread[# -> (# /. v^_ :> v^i++)] &@Cases[P, v^_, Infinity];
  MonomialList[Expand@P /. rule, {x, y, v}] /. Reverse /@ rule
 ]
(* -d1 d2 + (-3 + d1 + d2) x + a2 b2 v^a2 x + a3 b3 v^a3 x + (d1 + d2) y - 2 x y - y^2 *)

The point is to replace any instance of v^(something) with a unique v^(integer), make the replacement v^(something) -> v^(integer) in the expression, construct the desired expression using Total@MonomialList, and then undoing the replacement.