# Ignore parts of an equation with multiple variable in Mathematica

I want to use the linear version of a somewhat big equation which is output by my Mathematica code. For simplicity I will use the following example equation here:

Test = 3 x + x y + 8 y


Now, I want to keep only the first order terms, so that for x that will be 3 and for y that will be 8. I have tried to get those using

Coefficient[Test, x]
Coefficient[Test, y]


However these give me for x:

3+y


and for y:

8+x


Is there any way that I can use Mathematica to ignore the terms that depend in both x and y simultaneously? I have tried doing y x = 0 before I use coefficient but that doesn't work either since it only implies that one of them has to be zero but not which one.

You can use a scaling variable to do this:

Test = 3 x+x y+8 y
Normal @ Series[Test /. v:x|y->v t, {t, 0, 1}] /. t->1


3 x + 8 y + x y

3 x + 8 y

• Series can be a pain in the neck sometimes. I prefer to use PadeApproximant[..., {t, 0, {1, 0}}] instead of Normal @ Series[..., {t, 0, 1}]. – AccidentalFourierTransform Jun 2 '18 at 22:11

Looks more complicated than working with Series, but if you are going to work a lot with polynomials, then CoefficientRules and FromCoefficientRules might get useful anyways.

FromCoefficientRules[
Cases[
CoefficientRules[3 x + x y + 8 y, {x, y}],
Rule[a_, b_] /; (Total[a] <= 1)
],
{x, y}
]


3 x + 8 y

• Thanks, But, this seem to work well for two arguments but I may have more than 3 even, any way this could be addapted? – Mac Jun 2 '18 at 20:51
• Just give your new expression as first argument to CoefficientRules and the list of variables as second arguments to CoefficientRules and FromCoefficientRules (e.g., {x,y,z,...} instead of {x,y}). See also the documentation of FromCoefficientRules, CoefficientRules, and Cases. – Henrik Schumacher Jun 2 '18 at 21:06

I think that CoefficientRules and FromCoefficientRules is a good choice here.

CoefficientRules[3 x + x y + 8 y, {x, y}]

(*{{1, 1} -> 1, {1, 0} -> 3, {0, 1} -> 8}*)


Keep only the first order terms

DeleteCases[%, HoldPattern[a_ -> _] /; Total[a] >= 2]

(*{{1, 0} -> 3, {0, 1} -> 8}*)


Finally construct the function in terms of $x$ and $y$

FromCoefficientRules[%, {x, y}]

(*3 x + 8 y*)


Changing the pattern inside DeleteCases you can modify the function the way you want.

Assuming polynomials are to be linearized at the origin, here are a few ways using CoefficientArrays:

Homogeneous linear part:

CoefficientArrays[test][[2]].Variables[test2]
(*  3 x + 8 y  *)


Degree at most 1 part:

test2 = 3 x + x y + 8 y + 2;
ca = CoefficientArrays[test];
ca[[1]] + [[2]].Variables[test2]
(*  2 + 3 x + 8 y  *)


For arbitrary degree (2 variants):

deg = 1;
With[{vars = Variables[test2]},
Total[
Nest[Dot[#, vars] &, #, ArrayDepth[#]] & /@
CoefficientArrays[test2][[;; deg + 1]]
]]
(*  2 + 3 x + 8 y  *)

Inner[
Function[{a, v}, Nest[#.v &, a, ArrayDepth[a]]],
CoefficientArrays[test2][[;; deg + 1]],
{1, Variables[test2]},
Plus
]
(*  2 + 3 x + 8 y  *)


to ignore the terms that depend on both x and y simultaneously

ClearAll[f]
f = Replace[# ,  e_ /; And @@ (InternalDependsOnQ[e, #] & /@ {##2}) :> 0,  ∞] &;

f[3 x + x y + 8 y, x, y]


3 x + 8 y

f[3 x + 5 2^x  y + 8 y, x, y]


3 x + 8 y

a simple way.

Test = 3 x + x y + 8 y // Expand;

vars=Variables[Test];

prodterms=Apply[Times, Subsets[vars, {2,Length[vars]}], 2]

(*   {x y}   *)

(*   3 x + 8 y   *)

Select[Test, FreeQ[#, x y] &]


3 x + 8 y

Select[Test, MemberQ[#, x ] &]
`

3 x + x y

• This is just a reminder, that your question pops up in the "Low Quality" posts because it contains no description at all. I know that these two small expressions are self-explanatory and a description is hardly needed, but others might down-vote it because of this. – halirutan Jun 14 '18 at 21:51