# Select Solution with real (positive, etc) coefficients - ignoring variables

I often need to solve equations for numerical coefficients while preserving numerous variables (e.g. to preserve scaling relations). Thus I end up with a set of solutions like

$$\left\{-\frac{(0.156113\, +0.0384783 i) d^{6/13} f^{6/13} m^{16/13} r^{9/13}}{g^{9/13} L^{6/13}}, -\frac{(0.156113\, -0.0384783 i) d^{6/13} f^{6/13} m^{16/13} r^{9/13}}{g^{9/13} L^{6/13}}, [...] , \frac{0.160785 d^{6/13} f^{6/13} m^{16/13} r^{9/13}}{g^{9/13} L^{6/13}}\right\}$$

where [...] is possible lots more, ugly solutions --- while I just want the last one (for example).

How can I select only the solution with the type of numerical coefficient I want (e.g. Real, or real & positive, etc)?

Solve[eq,var,dom] doesn't seem to work with 'inexact coefficients', and trying things like 'Select[...]' seem to have the same problem.

I've found I can brute force proper selection if I just replace every variable with unity (i.e. {Sols} /.f->1 /.d->1 /.m->1 $...$ But that realls sucks...

Any help would be appreciated, thanks!

• how do you obtain these solutions in the first place? Have you tried NSolve (which you can ask for real solutions)? – acl May 22 '12 at 17:44
• The exact same problem results from NSolve... it can't handle those criteria for variables. – DilithiumMatrix May 22 '12 at 21:32
• I don't understand the need for the numerical part in your solution. Can't you just solve your equations with everything symbolic while adding an extra constraint with respect to the part that you currently have as a number? – Sjoerd C. de Vries May 23 '12 at 8:13

You can use Trace to pick out the coefficients. There may be a more elegant way but this seems to work.

sol={(1.233-0.23I)a^(1/2) b^(3/2),(1.233-0.343I)a^(1/2) b^(3/2),
(234.234)a^(1/2) b^(3/2)};
coeff=Map[TraceScan[Identity,#][]&,sol];
rule=Select[coeff,( Im[#]<0)&];      (* Select only negative Im coefficients *)
Extract[sol,Position[coeff,#]&/@rule]

{{(1.233 -0.23 I) Sqrt[a] b^(3/2)},{(1.233 -0.343 I) Sqrt[a] b^(3/2)}}

• It looks like the question asks for the solutions that contain a specific type of number. So it should return the solution, not the number. – Sjoerd C. de Vries May 22 '12 at 21:11
• I updated the code to return the solution. – azdahak May 22 '12 at 21:23
• Thanks, this works... I'm a little disappointed if its the cleanest solution Mathematica can muster – DilithiumMatrix May 22 '12 at 21:34

I am sure there will be some input that will break the following method, but here is a way to use Pick or Select:

Let

sols = {-(((0.25 + 0.5 I) x^2 y^3)/z), ((3 + I) x^3 y)/w^2, 3 - 4 I, (5. x^5 Sqrt[y])/z^3, 2, -2.5, 4 x^2};


First, note that you can get the rules to replace variables with 1s using

thrd = Thread[Variables[sols] -> 1]
(* ==>  {w -> 1, x -> 1, y -> 1, z -> 1} *)


and use this in to get the "coefficients" in a single step:

sols /. thrd
(* ==> {-0.25 - 0.5 I, 3 + I, 3 - 4 I, 5., 2, -2.5, 4}  *)


Now, you can use Select as follows:

Select[sols, myConditions[#] &@(# /. thrd) &]


where myConditions is any pure function specifying your criteria.

Examples:

Select[sols, (Element[#, Integers]) &@(# /. thrd) &]
(*==> {2, 4 x^2} *)
Select[sols, (Element[#, Complexes] && Re[#] > 2) &@(# /. thrd) &]
(* ==> {((3 + I) x^3 y)/w^2, 3 - 4 I, (5. x^5 Sqrt[y])/z^3, 4 x^2} *)
Select[sols, (Element[#, Complexes] && Im[#] < 0) &@(# /. thrd) &]
(* ==> {-(((0.25 + 0.5 I) x^2 y^3)/z), 3 - 4 I} *)


Alternatively, you get the same results using Pick as follows:

Pick[sols, Element[#, Integers] & /@ (sols/.thrd)]
Pick[sols, (Element[#, Complexes] && Re[#] > 2) & /@ (sols/.thrd)]
Pick[sols, (Element[#, Complexes] && Im[#] < 0) & /@ (sols/.thrd)]

• It looks like the question asks for the solutions that contain a specific type of number. So it should return the solution, not the number. – Sjoerd C. de Vries May 22 '12 at 21:11
• @Sjoerd, hope this one is not too far off the mark:) – kglr May 22 '12 at 21:40