K = (1+vy) (zx+wx) + vx (zy+wy);
Expand[K^2]
Is it possible to remove automatically the 3rd and 4th orders from the
result of 'Expand[K^2]' ?
By 3rd order I mean terms like: 2 wy zy vx^2 etc. (all terms with sum
of the degrees of derivatives of u,v,w is = 3)
By 4th order: wy^2 vx^2 etc. (sum of degrees of u,v,w = 4)
Thanks
Actually, there are many ways, but no single function that would do
this. Here are a couple of short programs, which get rid of terms od
degree >= 2
K = (1 + v y) (z x + w x) + v x (z y + w y);
Block[{vars = Variables[K], t},
Normal[(K /. Thread[vars -> t vars]) + O[t]^3] /. t -> 1]
w x + z x
Block[{vars = Variables[K], m},
m = Take[CoefficientArrays[K, vars] // Normal, 3];
m[[1]] + m[[2]].vars + m[[3]].vars.vars]
w x + z x
Andrzej Kozlowski
Hello,
isn't 2 wy zy vx^2 a 4th order term? (I admit, that I don't understand
your quatities sufficiently well. What is zy?)
You could multiply all variables with a weighting factor
epsilon, calculate a Series in epsilon, Normalize, and set
epsilon=1. This also helps, if you need other weighting factors
(eg. consider w itself as 2nd order).
Good luck
Alois
The following expression will do it:
Plus @@ Pick[List @@ expr,
Total@Exponent[#, Variables[expr]] & /@ List @@ expr, 0 | 1 | 2]
Here a step by step, visual, explanation of how it works:
In[1]:= K = (1 + vy) (zx + wx) + vx (zy + wy);
In[2]:= expr = Expand[K^2]
Out[2]= wx^2 + 2 vy wx^2 + vy^2 wx^2 + 2 vx wx wy + 2 vx vy wx wy +
vx^2 wy^2 + 2 wx zx + 4 vy wx zx + 2 vy^2 wx zx + 2 vx wy zx +
2 vx vy wy zx + zx^2 + 2 vy zx^2 + vy^2 zx^2 + 2 vx wx zy +
2 vx vy wx zy + 2 vx^2 wy zy + 2 vx zx zy + 2 vx vy zx zy + vx^2 zy^2
In[3]:= Variables[expr]
Out[3]= {wx, vy, vx, wy, zx, zy}
In[4]:= List @@ expr
Out[4]= {wx^2, 2 vy wx^2, vy^2 wx^2, 2 vx wx wy, 2 vx vy wx wy, vx^2
wy^2, 2 wx zx, 4 vy wx zx, 2 vy^2 wx zx, 2 vx wy zx, 2 vx vy wy zx,
zx^2, 2 vy zx^2, vy^2 zx^2, 2 vx wx zy, 2 vx vy wx zy, 2 vx^2 wy zy,
2 vx zx zy, 2 vx vy zx zy, vx^2 zy^2}
In[5]:= Exponent[#, Variables[expr]] & /@ List @@ expr
Out[5]=
{{2, 0, 0, 0, 0, 0}, {2, 1, 0, 0, 0, 0}, {2, 2, 0, 0, 0, 0},
{1, 0, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 0}, {0, 0, 2, 2, 0, 0},
{1, 0, 0, 0, 1, 0}, {1, 1, 0, 0, 1, 0}, {1, 2, 0, 0, 1, 0},
{0, 0, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 2, 0},
{0, 1, 0, 0, 2, 0}, {0, 2, 0, 0, 2, 0}, {1, 0, 1, 0, 0, 1},
{1, 1, 1, 0, 0, 1}, {0, 0, 2, 1, 0, 1}, {0, 0, 1, 0, 1, 1},
{0, 1, 1, 0, 1, 1}, {0, 0, 2, 0, 0, 2}}
In[6]:= Total@Exponent[#, Variables[expr]] & /@ List @@ expr
Out[6]= {2, 3, 4, 3, 4, 4, 2, 3, 4, 3, 4, 2, 3, 4, 3, 4, 4, 3, 4, 4}
In[7]:= Pick[List @@ expr, Total@Exponent[#, Variables[expr]] & /@ List
@@ expr, 0 | 1 | 2]
Out[7]= {wx^2, 2 wx zx, zx^2}
In[8]:= Plus @@ Pick[List @@ expr,
Total@Exponent[#, Variables[expr]] & /@ List @@ expr, 0 | 1 | 2]
Out[8]= wx^2 + 2 wx zx + zx^2
Regards,
-- Jean-Marc