Dear Samuel,

almost by accident, I just noticed the following:

def to_matrix_1(self):
    entries = { (v-1, i): 1 for i, v in enumerate(self) }
    return MatrixSpace(ZZ, len(self), sparse = True)(entries)

def to_matrix_2(self):
    entries = { (v-1, i): 1 for i, v in enumerate(self) }
    return matrix(ZZ, len(self), len(self), entries, sparse = True)

sage: %timeit [to_matrix_1(pi) for pi in Permutations(8)]
1 loop, best of 3: 6.37 s per loop
sage: %timeit [to_matrix_2(pi) for pi in Permutations(8)]
1 loop, best of 3: 13.2 s per loop

Is there any drawback to the faster method?

Martin

Am Montag, 11. September 2017 17:21:11 UTC+2 schrieb Samuel Lelievre:
>
> Mon 2017-09-11 10:07:01 UTC-5, Martin R:
>
> > Is there a recommended (fast) way to turn input
> > into a (square) matrix (with entries in ZZ)?
> > Currently I do "corner = matrix(corner)"
>
> Suppose you defined
>
>     m_list = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>
> and you want to turn m_list into a 3 by 3 matrix over ZZ.
>
> Then I recommend this:
>
>     m = matrix(ZZ, 3, m_list)
>
> By comparison, if you do the more lazy
>
>      m = matrix(m_list)
>
> Sage will have to figure out by itself what is the appropriate
> base ring and dimension to use. Figuring out the dimension
> is fast enough but for figuring out the base ring means looking
> for a common parent for all the elements, a waste of time
> if you already know what base ring you want.
>
> Note that
>
>     m = matrix(ZZ, 3, m_list)
>
> will also work if you defined m_list using
>
>     m_list = [0, 1, 2, 3, 4, 5, 6, 7, 8]
>
> If you need to construct a lot of matrices in the same matrix space,
> you might want to first define
>
>     M = MatrixSpace(ZZ, 3)
>
> and then for each matrix you create, do
>
>     m = M(m_list)
>
> Thus you avoid creating an instance of the matrix space each time
> you create a matrix.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to