Data Type Coercion in Maple 16
Coercion refers to the ability to pass one type of data to a procedure and have it receive a different type. The passed data is coerced or converted to the new type behind the scenes. The user of the procedure benefits by being able to use a larger variety of data types, which may all look alike. The author of the procedure benefits by being able to focus on handling only the best data type suited to the task.
p 1,2, 3,4 / p( 1234)
Using Maple Package Commands: LinearAlgebra
Declaring a Parameter that uses Coercion
Standardizing Array Bounds
Adding Your Own Coercions
Flexible Extended Syntax Using coerce()
There are various LinearAlgebra routines that expect Matrices for parameters. Before the introduction of data type coercion these routines would raise errors when given Arrays or lists. With data type coercion, these routines are now capable of accepting a wider range of inputs.
LinearAlgebra:-Rank Array 1,2 , 3,4 ;
LinearAlgebra:-Determinant 6, 7 ,8,9 ;
Various other routines in Maple's library have been updated to make use of coercion. No knowledge of programming is necessary to make use of this new feature. Simply using the same commands will now work on a wider variety of data types.
Coercion could involve anything from changing integers to floats, or names to strings, or lists to matrices to name a few. The author of the target procedure can focus on dealing with one core data-structure, while leveraging parameter processing to allow other acceptable types to also be supported. Using data type coercion makes authoring code easier by declaring only the type you want and letting Maple take care of the conversions.
p := proc( m::~Matrix )
p Array 1, 2 , 3, 4 ;
p Vectorrow 5, 6,7 ;
p 1,2, 3, 4, 5, 6 ;
Adding a ~ prefix to the m::~Matrix parameter specification in the example above tells Maple it will accept something similar to a Matrix. You can now pass in a Vector. Behind the scenes an n-element column vector will be coerced into a nx1 matrix. In this case, there is no conversion or data copying going on; rather, an alias, or different view on the same data, is applied. Hence the term "coercion", the data structure is coerced into the right form.
One important aspect of rtable to rtable
coercion is that it provides a way to control the lower-bound options for Arrays. Maple allows the flexibility of using your choice of lower bound when an Array is declared, but it is often seen that code is not prepared to handle arrays that have their index starting at anything other than 1. Using coercion, you can specify that a 1-based Array is expected.
In this example the 2x2 array passed in had lower bounds of 2 and 6. During the calling of procedure 'p', an alias was created so that B has a new view on the data. Inside the procedure B can be used as an ordinary 1-based array.
p := proc( B::~Array(1..,1..) )
A ≔ Array2..3,6..7:
There are times when you may want something other than a 1-based array. For example, when translating code written in another language, like C, it might be easier if you use 0-based arrays. In the following example, M[0,0] is the first array element inside the procedure, while outside the procedure the caller can continue to use the default for Maple arrays and matrices. The coercion is done in the most efficient way possible, without copying the data.
p := proc( M::~Array(0..,0..) )
p 1,2;3,4 ;
Coercion in the examples above applies when, for a given type T, there exists a command, ~T. Currently ~Array, ~Matrix, and ~Vector exist as top-level built-in procedures in Maple. You can add additional ~ commands as top-level commands or as module exports.
The following example shows how to declare a private coercion routine inside a module. The use of "ModuleApply" makes the module work the same way as a procedure, but because it is a module, other methods and data can be associated with it. The method highlighted in red declares the way floats will be converted to rationals. The method highlighted in blue uses the parameter r -- declared as coercible type ~rational. When this procedure is called with a float argument, the procedure body sees only the coerced rational.
Frac := module()
local ModuleApply, ~rational;
~rational := proc( a::float ) convert(a,rational); end;
ModuleApply := proc( r::~rational ) frac(r); end;
The long form of the coerce() parameter declaration lets you control exactly what types are candidates for coercion and how they will be coerced or converted. Inside the coerce() declaration will be a sequence of types and/or procedures. The procedures may be declared inline as "arrow" procedures, usually containing explicit types for each procedure.
A simple way to allow either a name or string as the first argument of the procedure, but only ever have to deal with the string form.
p := proc( s::coerce( string, (s::name)->convert(s,string) ) )
This procedure accepts a single argument which gets returned unchanged. When this procedure is called given s as a string, it matches the first coercion, which is simply a type match to string. The return value is still a string -- the argument is passed through unchanged.
When the same procedure is given s as a name, the second coercion s->convert(s,string) is applied. This is executed as inline code, and transforms the passed argument into a string. The return value here is also a string -- the parameter is changed before executing any code in the body of the procedure.
If some other kind of argument is passed, say an integer, then none of the coercion procedures match. Because 's' is an expected parameter, that did not match the expected type, an error will be raised.
Error, invalid input: p expects its 1st argument, s, to be of type string or coercible via proc (s::name) options operator, arrow; convert(s, string) end proc, but received expect+an+Error
For additional information see the following help topics:
Procedure Parameter Modifiers
~Array, ~Matrix, ~Vector
Download Help Document