5
$\begingroup$

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?

$\endgroup$
3
  • 3
    $\begingroup$ Select[Solutions, Min[Values[#]] >= 0 &] $\endgroup$
    – Bob Hanlon
    Feb 6 at 0:31
  • 1
    $\begingroup$ 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. $\endgroup$
    – Syed
    Feb 6 at 5:07
  • $\begingroup$ 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+) $\endgroup$
    – user170231
    Feb 6 at 18:43

4 Answers 4

5
$\begingroup$

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

final

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.

$\endgroup$
5
$\begingroup$

Another way using Table, If and FreeQ:

Table[If[FreeQ[# < 0 & /@ (#[[All, 2]] & /@ sols)[[i]], True] === False, 
Nothing, sols[[i]]], {i, 1, Length[sols]}]
$\endgroup$
3
$\begingroup$
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.}}
$\endgroup$
2
$\begingroup$
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.}

    } *)  
$\endgroup$
1
  • $\begingroup$ Also Select[Solutions, NoneTrue[Values[#], Negative]&] $\endgroup$
    – user1066
    Feb 6 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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