3

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.
Nasser
  • 12,849
  • 6
  • 52
  • 104

2 Answers2

3

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]
acer
  • 6,671
  • 15
  • 15
  • I think `is` is better. When I tried: `r1:=Pi,-1+2*I,-1-2*I;` then `select(type,[r1],numeric);` did not pick the `Pi` out. But `select(is,[r1],real);` did. And since I am basically looking for a way to pick all values from a list or set that are not complex. So `is` with `real` seems to be the better solution so far than using `type` – Nasser Mar 08 '14 at 00:38
0

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.

dnl-blkv
  • 2,037
  • 1
  • 17
  • 22
  • thanks. But that is not what I am asking. Supposed the list was generated from something other than solving an equation? and one still wants to select the real values in that list? In addition, one might not want to restrict the solver, but simply filter the result later. Might want to use the other solution as well. So this does not answer my question. But thanks. – Nasser Mar 06 '14 at 02:37
  • @Nasser, consider checking the updated version. You may find some useful information there! – dnl-blkv Mar 06 '14 at 03:01