# How do I erase sets of solutions from Solve based on criteria?

I have this list

Solutions = {{"C1" -> 0., "C2" -> 0., "C3" -> 0., "L1" -> 0., "L2" -> 0.,
"L3" -> 0., "Rin" -> -1.09141*10^22}, {"C1" -> 0., "C2" -> 0.,
"C3" -> 0., "L1" -> 0., "L2" -> 0., "L3" -> 0.,
"Rin" -> -1.09141*10^22}, {"C1" -> 0., "C2" -> 0., "C3" -> 0.,
"L1" -> 0., "L2" -> 0., "L3" -> 0., "Rin" -> -22777.6}, {"C1" -> 0.,
"C2" -> 0., "C3" -> 0., "L1" -> 0., "L2" -> 0., "L3" -> 0.,
"Rin" -> -22777.6}, {"C1" -> 0., "C2" -> 0., "C3" -> 0., "L1" -> 0.,
"L2" -> 0., "L3" -> 0., "Rin" -> 59.7556}, {"C1" -> 0., "C2" -> 0.,
"C3" -> 0., "L1" -> 0., "L2" -> 0., "L3" -> 0.,
"Rin" -> 59.7556}, {"C1" -> 0., "C2" -> 0., "C3" -> 0., "L1" -> 0.,
"L2" -> 0., "L3" -> 0., "Rin" -> 100.}}


I want to eliminate any solution with a negative element. I have tried to do

Solutions = Select[Solutions, FreeQ[#, NegativeReals] &]


It does not work, gives me the empty set as a solution. How to fix this?

• Select[Solutions, Min[Values[#]] >= 0 &] Feb 6 at 0:31
• Not related to the question, but could you specify the domain as NonNegativeReals so that these solutions would not be generated in the first place? I deleted my previous comment as a PositiveReals domain would exclude all zero valued solutions.
– Syed
Feb 6 at 5:07
• You could also include the positive condition in Solve. For instance, compare Solve[x^2 + y^2 == 1 && x - y == 1, {x, y}] with Solve[x^2 + y^2 == 1 && x - y == 1 && {x, y} >= 0, {x, y}] (or as Syed suggested, the domain options if you have version 12.0+) Feb 6 at 18:43

If I understand correctly, you can create a list

list = Values[Solutions] // Rationalize[#, 0] &;


and exclude any sublists with one negative element

nonneg =Pick[list, UnitStep @@@ list, 1]


Edit

Of course, after having the above you can do

params = ((Solutions // Rationalize[#, 0] &) /. Rule -> (#1 &))[[1]];
Thread[params -> #] & /@ nonneg


to get

And of course, after we have explained the logic we can write a one-liner

Thread[First[((Solutions // Rationalize[#, 0] &) /.
Rule -> (#1 &))] -> #] & /@
Pick[Values[Solutions] // Rationalize[#, 0] &,
UnitStep @@@ Values[Solutions] // Rationalize[#, 0] &, 1]


where Solutions is taken from the OP.

Another way using Table, If and FreeQ:

Table[If[FreeQ[# < 0 & /@ (#[[All, 2]] & /@ sols)[[i]], True] === False,
Nothing, sols[[i]]], {i, 1, Length[sols]}]

Pick[#, Values@# ∈ NonNegativeReals] & /@ Solutions


Or

Select[Values@# ∈ NonNegativeReals &][Solutions]


Result:

{{"C1" -> 0., "C2" -> 0., "C3" -> 0., "L1" -> 0., "L2" -> 0.,
"L3" -> 0., "Rin" -> 59.7556}, {"C1" -> 0., "C2" -> 0., "C3" -> 0.,
"L1" -> 0., "L2" -> 0., "L3" -> 0., "Rin" -> 59.7556}, {"C1" -> 0.,
"C2" -> 0., "C3" -> 0., "L1" -> 0., "L2" -> 0., "L3" -> 0.,
"Rin" -> 100.}}

Replace[Solutions, x_ /; AnyTrue[Values[x],Negative] -> Nothing,{1}]

(* {
{C1 -> 0., C2 -> 0., C3 -> 0., L1 -> 0., L2 -> 0., L3 -> 0., Rin -> 59.7556},

{C1 -> 0., C2 -> 0., C3 -> 0., L1 -> 0., L2 -> 0., L3 -> 0., Rin -> 59.7556},

{C1 -> 0., C2 -> 0., C3 -> 0., L1 -> 0., L2 -> 0., L3 -> 0., Rin -> 100.}

} *)

• Also Select[Solutions, NoneTrue[Values[#], Negative]&] Feb 6 at 17:00