0
$\begingroup$

How to extract symbolic outputs of Solve that evaluate to positive at given values of parameters? For example, if sol = {x, y} /.Solve outputs {{a, a}, {b, a}, {a, b}, {b, b}} and only {a, a} has both elements positive at {a -> 1, b -> -1}, then the desired output is {a,a}.

My attempted code (simple example)

Clear[sol, solindex, a, b]
sol = {x, y} /. 
  Solve[{(x - a)*(x - b) == 0, (y - ay)*(y - by) == 0}, {x, y}]
solindex = 
 Boole[Positive[(sol /. {a -> 1, b -> -1,ay->2,by->-2})[[All, 2]]]]*
  Boole[Positive[(sol /. {a -> 1, b -> -1,ay->2,by->-2})[[All, 1]]]]
 Extract[sol, solindex]

outputs Symbol, or in the more complicated real problem I am trying to solve, gives the error Extract: position specification... not applicable.

Based on answers to list manipulation questions, I also tried

Pick[sol, (# /. {a -> 1, b -> -1}) & > 0]
Select[sol, (# /. {a -> 1, b -> -1}) > 0 &]
solindex2 = 
 Positive[{Boole[Positive[(sol /. {a -> 1, b -> -1})[[All, 2]]]]*
Boole[Positive[(sol /. {a -> 1, b -> -1})[[All, 1]]]]}]
Extract[sol, solindex2]

and get an error or empty output.

$\endgroup$

1 Answer 1

3
$\begingroup$

Not sure whether I got your point, but given:

out = sol /. {a -> 1, b -> -1, ay -> 2, by -> -2}

then taking elements with second part positive is:

Select[sol /. {a -> 1, b -> -1, ay -> 2, by -> -2}, Last[#] > 0 &]
(* {{1, 2}, {-1, 2}} *)

Extracting elements with both parts positive, e.g.

Pick[out, And @@@ (Positive /@ out)]
(* {{1, 2}} *)
$\endgroup$
1
  • $\begingroup$ A small modification to your answer: Clear[sol, out, a, b, ay, by] sol = {x, y} /. Solve[{(x - a)*(x - b) == 0, (y - ay)*(y - by) == 0}, {x, y}] out = sol /. {a -> 1, b -> -1, ay -> 2, by -> -2}; Pick[sol, And @@@ (Positive /@ out)] does what I want: picks the solution where both elements evaluate to positive. $\endgroup$ Sep 9, 2019 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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