Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove higher orders terms from mathemtica's result

539 views
Skip to first unread message

kem

unread,
Jul 29, 2008, 6:10:33 AM7/29/08
to
Hi,
Given this:

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


Andrzej Kozlowski

unread,
Jul 30, 2008, 11:17:20 AM7/30/08
to


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

Alois Steindl

unread,
Jul 30, 2008, 11:17:41 AM7/30/08
to
kem <kem...@gmail.com> writes:

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


Jean-Marc Gulliet

unread,
Jul 30, 2008, 11:18:25 AM7/30/08
to
kem wrote:

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


0 new messages