Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

Thanks

share|improve this question
1  
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
add comment

3 Answers

up vote 1 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|improve this answer
add comment

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|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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.