Pregunta

Suppose one needs to select the real solutions after solving some equation.

Is this the correct and optimal way to do it, or is there a better one?

restart;
mu  :=  3.986*10^5; T:= 8*60*60:
eq  :=  T = 2*Pi*sqrt(a^3/mu):
sol :=  solve(eq,a);

select(x->type(x,'realcons'),[sol]);

I could not find real as type. So I used realcons. At first I did this:

select(x->not(type(x,'complex')),[sol]);

which did not work, since in Maple 5 is considered complex! So ended up with no solutions.

 type(5,'complex');
 (* true *)

Also I could not find an isreal() type of function. (unless I missed one)

Is there a better way to do this that one should use?

update: To answer the comment below about 5 not supposed to be complex in maple.

restart;
type(5,complex);
                              true
type(5,'complex');
                              true

interface(version);
Standard Worksheet Interface, Maple 18.00, Windows 7, February 

From help

The type(x, complex) function returns true if x is an expression of the form 
a + I b, where a (if present) and b (if present) are finite and of type realcons.
¿Fue útil?

Solución

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]

Otros consejos

The one possibility is to restrict the domain before the calculation takes place.

Here is an explanation on the Maplesoft website regarding restricting the domain: 4 Basic Computation

UPD: Basically, according to this and that, 5 is NOT considered complex in Maple, so there might be some bug/error/mistake (try checking what may be wrong there).

For instance, try putting complex without quotes.

Your way seems very logical according to this.

UPD2: According to the Maplesoft Website, all the type checks are done with type() function, so there is rather no isreal() function.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top