Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to solve approximately 100 equations in the same number of variables. I wrote a little Mathematica function to do it. It works fine, but usually only 2 to 10 variables end up being something other than 0 and it's hard to read the solution since there are 90+ instances of something like "hn[0]->0". Is there any easy way to show only the variables that are something other than 0 in the solution? The variable names are similar to above but others might include "hk[3]->0", "hnPk[7]->0", and so on. All start with the letter h and all are arrays, but there is quite a bit of variety.

Oh, and all the nonzero variables in the solutions are going to be some variable is some linear combination of others at least.

EDIT: For example, here is one output from a small case:

{h0[0] -> 0, hnP3k[0] -> 0, hnP2k[0] -> 0, hnPk[0] -> 0, hn[0] -> 0, hnMk[0] -> -hk[0]}

I want to see only

{hnMk[0] -> -hk[0]}

which tells me the full solution but is much simpler.


share|cite|improve this question
Would any downvoters like to explain why they downvoted? It seems to be a perfectly good question to me. Tell me something wrong with it. Is it because it's not "math"? If so, why is "mathematica" one of the keywords allowed here? And, who would know the most about software that does math than mathematicians? – Graphth Nov 27 '11 at 23:55
up vote 2 down vote accepted

If I had

rules = {h0[0] -> 0, hnP3k[0] -> 0, hnP2k[0] -> 0, hnPk[0] -> 0, hn[0] -> 0, hnMk[0] -> -hk[0]}

then an application of

DeleteCases[rules, _ -> 0|0.]

(which should handle both approximate and exact zeroes) ought to work.

share|cite|improve this answer

Here's how to get rid of rules that have 0's

result = Solve[{x + 1 == 0, y == 0}, {x, y}]
removeZeroRules[rules_] := Select[rules, Last@# =!= 0 &]
removeZeroRules /@ result

If you just want to get variable names corresponding to non-zero values on right hand size, you could replace removeZeroRules with getNonZeroVars defined as

getNonZeroVars[rules_] := 
 Cases[rules, HoldPattern[x_ -> Except[0]] :> x]
share|cite|improve this answer
Thank you for your answer but it does not work quite right. It could very well be that it works how you expected but that I did not provide enough detail of what I am looking for. So, above, I added a sample output and what I want it to look like. In this case, your getNonZeroVars function returns {hnMk[0]} instead of {hnMk[0] -> -hk[0]}. The removeZeroRules returns {}. Would you be able to change them slightly to achieve my goal? Thanks so much for your help. – Graphth Apr 4 '11 at 15:24
@Numth fixed, I changed != to =!= – Yaroslav Bulatov Apr 6 '11 at 4:07

There may be other ways, but you can pretend $x\to 0$ is a list and address the $0$ as $(x\to0)[[2]]$.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.