Types

You will note that Maple is not a typed language like C++ or Java:

[> vari := 17; # 'vari' is assigned an integer

$vari := 17$

[> vari := 17/5; # 'vari' is now assigned a fraction

$vari := \frac{17}{5}$

[> vari := 17.5; # 'vari' is now assigned a float

$vari := 17.5$

Up to this point, we have simply used values and expressions and vectors appropriately; however, if you've been experimenting, you'll also understand that Maple is capable of determining if an argument is appropriate or not; for example:

[> igcd( 15/4, 18, 9 );
> igcd( 15/4, 18/7 );
Error, invalid input: igcd received 15/4, which is not valid for its 1st argument

Maple does have types, but checking the type of an object is an explicit operation. The simplest types are specified by an appropriate symbol, and the first we will look at is the type integer. Any integer is of this type, and this includes rational numbers where the denominator divides the numerator:

[> type( 17, 'integer' );

$\textit{true}$

[> type( 25/5, 'integer' );

$\textit{true}$

[> type( 25/4, 'integer' );

$\textit{false}$

Integer types

In addition to the type integer, there area few others:

The last is a little peculiar, but recall that $3$ is also a function in Maple, so why not make it a type, too?

[> type( 1 + 2, '3' );

$\textit{true}$

[> type( 3.0, '3' ); # 3.0 is a floating point number, not the integer '3'

$\textit{false}$

[> type( x + 3, '3' ); # 'x + 3' is an algebraic expression

$\textit{false}$

Rational types

A ratio of two integers that does not simplify to an integer is of type fraction, and all integers and fractions are of type rational. Like integers, every fractional number is a type that matches only itself.

[> type( 3, 'rational' );

$\textit{true}$

[> type( 3, 'fraction' );

$\textit{false}$

[> type( 3/2, 'fraction' );

$\textit{true}$

[> type( 9/6, '3/2' );

$\textit{true}$

Floating-point types

All floating-point numbers of type float.

Numeric types

Anything that is of type integer, fraction or float is of type numeric. The types positive, negative, nonnegative and nonpositive include anything of type numeric that also satisfies the additional constraint of being greater than zero, less than zero, greater than or equal to zero, or less than or equal to zero, respectively. Unlike integers, there is no type specifically for a positive fraction or a positive float.

Complex types

Type complex is actually very difficult to explain, and is not really all that useful. There are, however, subtypes as follows. If you take any of the types that we have discussed to this point, and ask if an expression is of type complex(typename), then is is true if:

For example,

[> type( 3 - 4*j, 'complex(integer)' );

$\textit{true}$


[> type( 3 - 4*j, 'complex(positive)' );

$\textit{false}$

[> type( 3 - 4/7*j, 'complex(rational)' );

$\textit{true}$

Data structures

There are also types for our data structures. For example, there is a type list:

[> type( 3 - 4*j, 'list' );

$\textit{false}$

[> type( [3 - 4*j, 5, 7 - 2/3*j], 'list' );

$\textit{true}$

Like complex(typename), you can also provide the type list, and it will therefore check if the argument is a list where all elements are of that given type:

[> type( [3 - 4*j, 5, 7 - 2/3*j], 'list(integer)' );

$\textit{false}$

[> type( [3, 5, -2, 5], 'list(integer)' );

$\textit{true}$

Note that the empty list [] is of type list(typename) for any type.

This is also true for the type set:

[> type( {}, 'set' );

$\textit{false}$

[> type( {3 - 4*j, 5, 7 - 2/3*j}, 'set' );

$\textit{false}$

[> type( {3 - 4*j, 5, 7 - 2/3*j}, 'set(rational)' );

$\textit{false}$

[> type( [3, 5/2, -2, 5/9], 'list(rational)' );

$\textit{true}$

There are two additional types associated with lists:

For example,

[> type( [3, 5/2, -2], '[rational, rational, rational]' );

$\textit{true}$

[> type( [3, 5/2, -2, 1/2], '[rational, rational, rational]' );

$\textit{false}$

[> type( [[1, 2], [3, 4], [5, 6]], 'listlist' );

$\textit{true}$

[> type( [[1, 2.5], [3, 4.4], [5, 6.7]], 'listlist(float)' );

$\textit{false}$

[> type( [[1.0, 2.5], [3.0, 4.4], [5.0, 6.7]], 'listlist(float)' );

$\textit{true}$

Expression sequences

In Maple, there is no type for an expression sequence, and the type(...) function does not allow an expression sequence as a first argument. If you know that a symbol is assigned an expression sequence, you can wrap that symbol in a list constructor, and then test if the result is an appropriate list:

[> prim_nums := 2, 3, 5, 7, 11, 13, 17, 19, 23, 29:
[> type( [a], 'list(integer)' ); # is 'a' an expression sequence of integers?

$\textit{true}$

[> rand_nums := 0.0162, 0.434, 0.695, 0.875, 0.178:
[> type( [b], 'list(float)' );

$\textit{true}$