6
$\begingroup$

I have a polynomial like this:

ser = 1 - x/2 - x^2/8 - x^3/16 + y/2 + (3 x y)/4 + (23 x^2 y)/16 + ( 27 x^3 y)/32 - y^2/8 - (31 x y^2)/16 - (127 x^2 y^2)/64 - ( 351 x^3 y^2)/128 + y^3/16 + (35 x y^3)/32 + (407 x^2 y^3)/128 + ( 1915 x^3 y^3)/256;

Now I want to extract the terms with total degree smaller than 4. Using patterns, I can realize the goal.

Cases[ser, (_*x^m_.*y^n_. /; m + n < 5) | (_?NumericQ*y^j_. /; j < 5) | (_?NumericQ*x^i_. /; i < 5) | _?NumericQ]

But I think my method is a little tedious. I have two questions: 1. can my method be simplified? 2. are there other simple methods to do the job?

$\endgroup$
5
$\begingroup$

You can use Jens's method from this answer:

expr[x_, y_] := 1 - x/2 - x^2/8 - x^3/16 + y/2 + (3 x y)/4 + (23 x^2 y)/16 + (27 x^3 y)/32 -
                y^2/8 - (31 x y^2)/16 - (127 x^2 y^2)/64 - (351 x^3 y^2)/128 + y^3/16 +
                (35 x y^3)/32 + (407 x^2 y^3)/128 + (1915 x^3 y^3)/256

expr2[x_, y_] = Normal[Series[expr[x t, y t], {t, 0, 4}]] /. t -> 1

   (* 1 + 1/2 (-x + y) + 1/8 (-x^2 + 6 x y - y^2) + 1/16 (-x^3 + 23 x^2 y - 31 x y^2 + y^3) +
   1/64 (54 x^3 y - 127 x^2 y^2 + 70 x y^3) *)

To obtain the OP's result

List @@ Expand[expr2[x, y]]

(* {1, -(x/2), -(x^2/8), -(x^3/16), y/2, (3 x y)/4, (23 x^2 y)/16, (
 27 x^3 y)/32, -(y^2/8), -((31 x y^2)/16), -(127/64) x^2 y^2, y^3/16, (
 35 x y^3)/32} *)
$\endgroup$
4
$\begingroup$
Sort @ Select[Exponent[# /. y -> x, x] < 5 &] @ MonomialList[ser]

TeXForm @ %

$\left\{1,-\frac{x}{2},-\frac{x^2}{8},-\frac{x^3}{16},\frac{y}{2},\frac{3 x y}{4},\frac{23 x^2 y}{16},\frac{27 x^3 y}{32},-\frac{y^2}{8},-\frac{31 x y^2}{16},-\frac{127}{64} x^2 y^2,\frac{y^3}{16},\frac{35 x y^3}{32}\right\}$

| improve this answer | |
$\endgroup$
2
$\begingroup$

You could also use CoefficientArrays

v = {x, y};
c = CoefficientArrays[ser, v]
result = {c[[1]], c[[2]].v, c[[3]].v.v, c[[4]].v.v.v, c[[5]].v.v.v.v} // Expand

$$ \left\{1,\frac{y}{2}-\frac{x}{2},-\frac{x^2}{8}+\frac{3 x y}{4}-\frac{y^2}{8},\frac{23 x^2 y}{16}-\frac{x^3}{16}-\frac{31 x y^2}{16}+\frac{y^3}{16},-\frac{127 x^2 y^2}{64}+\frac{27 x^3 y}{32}+\frac{35 x y^3}{32}\right\} $$

| improve this answer | |
$\endgroup$
2
$\begingroup$

You can adapt Yode's method from his answer to: Removing terms of certain degree in multivariable polynomial

var={x,y};
Sort@Select[MonomialList[ser], Tr[Exponent[#, var]] < 5 &]

$$\left\{1,-\frac{x}{2},-\frac{x^2}{8},-\frac{x^3}{16},\frac{y}{2},\frac{3 x y}{4},\frac{23 x^2 y}{16},\frac{27 x^3 y}{32},-\frac{y^2}{8},-\frac{31 x y^2}{16},-\frac{127}{64} x^2 y^2,\frac{y^3}{16},\frac{35 x y^3}{32}\right\}$$

[N.B.: Your code gives total degree <5, while your language said "smaller than 4", so I replicated the former.]

| improve this answer | |
$\endgroup$
2
$\begingroup$

You can use CoefficientList and select necessary items in the matrix:

ser = 
  1 - x/2 - x^2/8 - x^3/16 + 
   y/2 + (3 x y)/4 + (23 x^2 y)/16 + (27 x^3 y)/32 - 
   y^2/8 - (31 x y^2)/16 - (127 x^2 y^2)/64 - (351 x^3 y^2)/128 + 
   y^3/16 + (35 x y^3)/32 + (407 x^2 y^3)/128 + (1915 x^3 y^3)/256;

PolynomialQ[ser, x]
(* True *)

coef = CoefficientList[ser, {x, y}]
(* {{1, 1/2, -(1/8), 1/16}, {-(1/2), 3/4, -(31/16), 35/
  32}, {-(1/8), 23/16, -(127/64), 407/128}, {-(1/16), 27/
  32, -(351/128), 1915/256}} *)

List of necessary coefficients:

selectedCoef = Reverse@LowerTriangularize[Reverse@coef, 1]
(* {{1, 1/2, -(1/8), 1/16}, {-(1/2), 3/4, -(31/16), 35/
  32}, {-(1/8), 23/16, -(127/64), 0}, {-(1/16), 27/32, 0, 0}} *)

Now you can build polynomial from this coefficients:

coefList = Expand@Fold[FromDigits[Reverse[#1], #2] &, selectedCoef, {x, y}]
(* 1 - x/2 - x^2/8 - x^3/16 + y/2 + (3 x y)/4 + (
 23 x^2 y)/16 + (27 x^3 y)/32 - y^2/8 - (31 x y^2)/16 - (
 127 x^2 y^2)/64 + y^3/16 + (35 x y^3)/32 *)

To get the result in form of list:

Sort@MonomialList[coefList]
(* {1, -(x/2), -(x^2/8), -(x^3/16), y/2, (3 x y)/4, (
 23 x^2 y)/16, (
 27 x^3 y)/32, -(y^2/8), -((31 x y^2)/
  16), -(127/64) x^2 y^2, y^3/16, (35 x y^3)/32} *)
| improve this answer | |
$\endgroup$

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.