I have an expression with each term therein either containing at least one $n_i \in \left\{ n_1 , n_2 \dots , n_{5} \right\}$ or no $n_i$ at all. I would like to easily eliminate all the terms that are of the latter nature, i.e I would like to reduce the expression down to one containing only terms each with at least an $n_i$.
For example, I would replace
$$\frac{n_1 n_2}{n_3^2 n_4}c_1 + c_2 + \frac{1}{n_5}c_3$$ with $$\frac{n_1 n_2}{n_3^2 n_4}c_1 + \frac{1}{n_5}c_3,$$ eliminating the term with no $n_i$ and keeping those with at least one $n_i$. The $c_i$ are just $n_i$ independent variables.
Here is an expression snippet (note the expr in its entirety is too large to be presented), but the snippet below includes both $n_i$ dependent and $n_i$ independent pieces.
expr = -(s^4/(2 n2 n4 r (r - s) (r + s)^3)) + q/((-r + s) (r + s)^3) - (n1 q)/(2 n2 (-r + s) (r + s)^3) - (n2 q)/(2 n1 (-r + s) (r + s)^3) + (n1 q)/(2 n3 (r + s) (r + s)^3) - (n2 q)/(n3 (-r + s) (r + s)^3) + (n2^2 q)/(2 n1 n3 (-r + s) (r + s)^3) + (3 n1 q^2)/(8 n3 r (-r + s) (r + s)^3) - (n1^2 q^2)/(8 n2n3 r (-r + s) (r + s)^3) - (3 n2 q^2)/(8 n3 r (-r + s) (r + s)^3) + (n2^2 q^2)/(8 n1 n3 r (-r + s) (r + s)^3) + r/((-r + s) (r + s)^3);
I've looked into Filter out all terms not involving a given variable but I couldn't find a way to generalise the answer there with the case keep = b;
to more than one symbol or to make terms
denote an expression rather than a brace of expressions, e.g
keep = {n1,n2,n3,n4,n5}; (*keep expressions with this symbol in it*)
lst = DeleteDuplicates[Cases[#, _Symbol, Infinity]] & /@ expr
Pick[expr, MemberQ[#, keep] & /@ lst]
FreeQ
? Also have a look atNothing
. $\endgroup$FreeQ
is justTrue
orFalse
.Nothing
would be good but the expressions are large so to manually replace each $n_i$ independent term with Nothing would be tedious. $\endgroup$FreeQ
should be used together with the conditional operator. Please have in mind that people here request that you include Mma code of your expression along with the code showing your trials. Otherwise, they may appear reluctant to help you. $\endgroup$FreeQ
is used within an operator in the SE question I linked to in my question but I can't seem to find a way to generalise that. $\endgroup$