Your solutions sol
are all of type complex(numeric)
. You can select only the real ones with type,numeric
, ie.
restart;
mu := 3.986*10^5: T:= 8*60*60:
eq := T = 2*Pi*sqrt(a^3/mu):
sol := solve(eq,a);
20307.39319, -10153.69659 + 17586.71839 I, -10153.69659 - 17586.71839 I
select( type, [sol], numeric );
[20307.39319]
By using the multiple argument calling form of the select
command we here can avoid using a custom operator as the first argument. You won't notice it for your small example, but it should be more efficient to do so. Other commands such as map
perform similarly, to avoid having to make an additional function call for each individual test.
The types numeric
and complex(numeric)
cover real and complex integers, rationals, and floats.
The types realcons
and complex(realcons)
includes the previous, but also allow for an application of evalf
done during the test. So Int(sin(x),x=1..3)
and Pi
and sqrt(2)
are all of type realcons
since following an application of evalf
they become floats of type numeric
.
The above is about types. There are also properties to consider. Types are properties, but not necessarily vice versa. There is a real
property, but no real
type. The is
command can test for a property, and while it is often used for mixed numeric-symbolic tests under assumptions (on the symbols) it can also be used in tests like yours.
select( is, [sol], real );
[20307.39319]
It is less efficient to use is
for your example. If you know that you have a collection of (possibly non-real) floats then type,numeric
should be an efficient test.
And, just to muddy the waters... there is a type nonreal
.
remove( type, [sol], nonreal );
[20307.39319]