-1
$\begingroup$

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

  • 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?2011-11-27

3 Answers 3

2

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.

1

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] 
  • 0
    @Numth fixed, I changed != to =!=2011-04-06
0

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