ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 27 Jun 2019 15:47:31 -0500Convert real matrix into Integer matrixhttp://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/Suppose I have a real matrix with integer entries
A = matrix(RR,[[1,2,3],[4,5,6]])
I would like to convert it to an Integer matrix (ZZ). I can do it element by element but is there a function that will do this for the entire matrix. Doing B=ZZ(A) will return an error message
unable to coerce <type 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'> to an integer.
Sun, 27 Jan 2019 09:34:47 -0600http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/Answer by parzan for <p>Suppose I have a real matrix with integer entries</p>
<p>A = matrix(RR,[[1,2,3],[4,5,6]])</p>
<p>I would like to convert it to an Integer matrix (ZZ). I can do it element by element but is there a function that will do this for the entire matrix. Doing B=ZZ(A) will return an error message</p>
<p>unable to coerce <type 'sage.matrix.matrix_generic_dense.matrix_generic_dense'=""> to an integer.</p>
http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?answer=45204#post-id-45204It seems that the most efficient method is `A.apply_map(ZZ,ZZ)` (in terms of running time).
Note this will not work if you have non-integers in the matrix, then you can use `A.apply_map(int,ZZ)` or `A.apply_map(round,ZZ)` to round the entries.Sun, 27 Jan 2019 09:43:47 -0600http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?answer=45204#post-id-45204Comment by brianko for <p>It seems that the most efficient method is <code>A.apply_map(ZZ,ZZ)</code> (in terms of running time).</p>
<p>Note this will not work if you have non-integers in the matrix, then you can use <code>A.apply_map(int,ZZ)</code> or <code>A.apply_map(round,ZZ)</code> to round the entries.</p>
http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?comment=47014#post-id-47014This appears to be the correct answer. None of the methods in the previous answer will do the conversion between RR and ZZ. I would upvote, but silly karma policies prevent it.Thu, 27 Jun 2019 15:47:31 -0500http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?comment=47014#post-id-47014Comment by tmonteil for <p>It seems that the most efficient method is <code>A.apply_map(ZZ,ZZ)</code> (in terms of running time).</p>
<p>Note this will not work if you have non-integers in the matrix, then you can use <code>A.apply_map(int,ZZ)</code> or <code>A.apply_map(round,ZZ)</code> to round the entries.</p>
http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?comment=45215#post-id-45215You should be very careful with timings here, since in all cases, most of the time is spent in creating the parent ; compare:
sage: A = matrix(RR,[[1,2,3],[4,5,6]])
sage: %time A.apply_map(ZZ,ZZ)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 1.49 ms
[1 2 3]
[4 5 6]
And in another fresh Sage session (to avoid possible caching):
sage: A = matrix(RR,[[1,2,3],[4,5,6]])
sage: P = MatrixSpace(ZZ,2,3)
sage: %time A.apply_map(ZZ,ZZ)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 367 µs
[1 2 3]
[4 5 6]
Note that each parent is only created once in a session. Once it is created, calling `P(A)` seems slightly faster than `A.apply_map(ZZ,ZZ)`, probably because Sage does not even have to guess an find the parent.Sun, 27 Jan 2019 14:31:09 -0600http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?comment=45215#post-id-45215Answer by tmonteil for <p>Suppose I have a real matrix with integer entries</p>
<p>A = matrix(RR,[[1,2,3],[4,5,6]])</p>
<p>I would like to convert it to an Integer matrix (ZZ). I can do it element by element but is there a function that will do this for the entire matrix. Doing B=ZZ(A) will return an error message</p>
<p>unable to coerce <type 'sage.matrix.matrix_generic_dense.matrix_generic_dense'=""> to an integer.</p>
http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?answer=45205#post-id-45205First, the "standard" way to do this is to change the base ring of the matrix as follows;
sage: B = A.change_ring(ZZ) ; B
[1 2 3]
[4 5 6]
sage: B.parent()
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
If `P` is a parent and `E` is some element, `P(E)` tries to convert `E` as an element of `P`. Hence, when you do `B = ZZ(A)`, Sage tries to transform the matrix into an integer, which leads to an error. If you want to use such a conversion, the parent `P` you are looking for is the space of integer matrices of size 2*3 :
sage: P = MatrixSpace(ZZ,2,3)
sage: P
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: P(A)
[1 2 3]
[4 5 6]
Or directly:
sage: MatrixSpace(ZZ,2,3)(A)
[1 2 3]
[4 5 6]
Note that it leads to the same result as with the `change_ring` method:
sage: P(A) == B
True
sage: P == B.parent()
True
Sun, 27 Jan 2019 10:27:49 -0600http://ask.sagemath.org/question/45203/convert-real-matrix-into-integer-matrix/?answer=45205#post-id-45205