Simplifying Presentation of Solutions in Mathematica

256 Views Asked by At

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

3

There are 3 best solutions below

0
On BEST ANSWER

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.

2
On

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
On

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