3
$\begingroup$

I have a list with many expressions as elements. I want to remove the list elements which do not contain certain variables. For example, I would like to remove the element which does not contain x1 in the list I below:

I = {2x1 + 3x2 - 5x1^2, 7x2 - 3x2^2, 2x1 + 5x2 - 9x3}

to become

I = {2x1 + 3x2 - 5x1^2, 2x1 + 5x2 - 9x3}.

Also, I would like to combine conditions to check if the element does not contain any of a list of variables, and remove it if so. For example if neither x1 nor x2 in I[[i]], then remove I[[i]].

Any help is appreciated.

$\endgroup$
6
$\begingroup$

Removing elements which do not contain x1 (i.e. keeping elements which do contain x1)

expr = {2 x1 + 3 x2 - 5 x1^2, 7 x2 - 3 x2^2, 2 x1 + 5 x2 - 9 x3};
Select[expr, MemberQ[Variables[#], x1] &]

{2 x1 - 5 x1^2 + 3 x2, 2 x1 + 5 x2 - 9 x3}

Keeping elements which contain at least one of the variables from a list

(* changing example list to include a term which contains neither `x1` or x2` *)
expr = {2 x1 + 3 x2 - 5 x1^2, 7 x2 - 3 x2^2, 2 x1 + 5 x2 - 9 x3, x3 + x4};
wanted = {x1, x2};

Select[expr, 
 Table[MemberQ[Variables[#], v], {v, wanted}] /. List -> Or &]

{2 x1 - 5 x1^2 + 3 x2, 7 x2 - 3 x2^2, 2 x1 + 5 x2 - 9 x3}

$\endgroup$
6
$\begingroup$
list = {2 x1 + 3 x2 - 5 x1^2, 7 x2 - 3 x2^2, 2 x1 + 5 x2 - 9 x3, Sin[x1 + x4] + x5};

You can use FreeQ with DeleteCases, Cases, Select and Pick as follows:

DeleteCases[_?(FreeQ[x1])]@list

Cases[_?(Not@*FreeQ[x1])]@list

Select[Not@*FreeQ[x1]]@list

Pick[list, Not @* FreeQ[x1] /@ list]

All methods above return

{2 x1 - 5 x1^2 + 3 x2, 2 x1 + 5 x2 - 9 x3, x5 + Sin[x1 + x4]}

To delete cases that does not contain x2 or x3 (to keep only those case that contain both x2 and x3) use Alternatives (|) in the pattern inside FreeQ:

wanted =  {x2, x3};
alt = Alternatives @@ wanted;

DeleteCases[_?(FreeQ[alt])]@list

Cases[_?(Not@*FreeQ[alt])]@list

Select[Not@*FreeQ[alt]]@list

Pick[list, Not @* FreeQ[alt] /@ list]

All four methods give

{2 x1 - 5 x1^2 + 3 x2, 7 x2 - 3 x2^2, 2 x1 + 5 x2 - 9 x3}

You can also use a combination of Level and ContainsNone or ContainsAny as follows:

DeleteCases[_?(ContainsNone[Level[#, {-1}], wanted] &)]@list

Cases[_?(ContainsAny[Level[#, {-1}], wanted] &)]@list

Select[ContainsAny[Level[#, {-1}], wanted] &]@list

Pick[#, ContainsAny[Level[#, {-1}], wanted] & /@ #] &@list

to get

{2 x1 - 5 x1^2 + 3 x2, 7 x2 - 3 x2^2, 2 x1 + 5 x2 - 9 x3}

Note: Variables "gives a list of all independent variables in a polynomial":

Variables /@ list

{{x1, x2}, {x2}, {x1, x2, x3}, {x5, Sin[x1 + x4]}}

$\endgroup$
  • $\begingroup$ Thanks for the answer. I will have to accept the first answer since it was posted earlier and also because it provides a better way for dealing with many variables to exclude. Great answer though. $\endgroup$ – Bran Nov 2 at 16:54
  • $\begingroup$ @Bran, you are welcome. $\endgroup$ – kglr Nov 2 at 17:40

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.