|
3.5.5 Type conversion and casting
Type conversion
Assignments convert the type of the right-hand side to the type of the
left-hand side of the assignment, if possible. Operators and functions
which require certain types of operands can also implicitly convert the
type of an expression. It is, for example, possible to multiply a
polynomial by an integer because the integer is automatically converted to
a polynomial. Type conversions do not act transitively. Possible
conversions are:
|
1. intvec | ==> intmat
|
|
2. poly | ==> ideal
|
|
3. bigint | ==> ideal
|
|
4. int | ==> ideal
|
|
5. intmat | ==> matrix
|
|
6. ideal | ==> matrix
|
|
7. module | ==> matrix
|
|
8. number | ==> matrix
|
|
9. poly | ==> matrix
|
|
10. vector | ==> matrix
|
|
11. bigint | ==> matrix
|
|
12. int | ==> matrix
|
|
13. intvec | ==> matrix
|
|
14. ideal | ==> module
|
|
15. matrix | ==> module
|
|
16. vector | ==> module
|
|
17. bigint | ==> number
|
|
18. int | ==> number
|
|
19. number | ==> poly
|
|
20. bigint | ==> poly
|
|
21. int | ==> poly
|
|
22. list | ==> resolution
|
|
23. poly | ==> vector
(p ==> p*gen(1) )
|
|
24. bigint | ==> vector
|
|
25. int | ==> vector
(i ==> i*gen(1) )
|
|
26. int | ==> bigint
|
|
27. int | ==> intvec
|
|
28. string | ==> link
|
|
29. resolution | ==> list
|
|
30. intvec | ==> bigintvec
|
Type casting
An expression can be casted to another type by using a type cast
expression:
type ( expression ) .
Possible type casts are:
|
to | from
|
|
bigint | expression int , number , poly
|
|
bigintvec | expression int , bigint
|
|
ideal | expression lists of int , number , poly
|
|
ideal | int , matrix , module , number , poly , vector
|
|
int | number , poly
|
|
intvec | expression lists of int , intmat , bigintvec
|
|
intmat | intvec (see intmat type cast)
|
|
list | expression lists of any type
|
|
matrix | module , ideal ,
vector , matrix .
There are two forms to convert something to a matrix: if matrix(
expression ) is used then the size of the matrix is determined
by the size of expression.
But matrix( expression , m , n ) may also be
used - the result is a
matrix (see matrix type cast)
|
|
module | expression lists of int , number ,
poly , vector
|
|
module | ideal , matrix , vector
|
|
number | poly
|
|
poly | int , number
|
|
ring | list (the inverse of ringlist )
|
|
string | any type (see string type cast)
|
Example:
| ring r=0,x,(c,dp);
number(3x);
==> 0
number(poly(3));
==> 3
ideal i=1,2,3,4,5,6;
print(matrix(i));
==> 1,2,3,4,5,6
print(matrix(i,3,2));
==> 1,2,
==> 3,4,
==> 5,6
vector v=[1,2];
print(matrix(v));
==> 1,
==> 2
module(matrix(i,3,2));
==> _[1]=[1,3,5]
==> _[2]=[2,4,6]
// generators are columns of a matrix
|
|