#18231: constructing matrices is very slow
-------------------------------------+-------------------------------------
       Reporter:  vdelecroix         |        Owner:
           Type:  defect             |       Status:  needs_review
       Priority:  major              |    Milestone:  sage-6.7
      Component:  linear algebra     |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Vincent Delecroix  |    Reviewers:
Report Upstream:  N/A                |  Work issues:
         Branch:                     |       Commit:
  u/vdelecroix/18231                 |  715f49ecab8ae8bb66725be77da51ce6eb8f8738
   Dependencies:                     |     Stopgaps:
-------------------------------------+-------------------------------------
Description changed by vdelecroix:

Old description:

> The problem is in the code which looks like the following
> {{{
> def test1():
>     K = QuadraticField(2)
>     for _ in range(100):
>         i = randint(2,3)
>         j = randint(2,3)
>         m = matrix(K, i, j, range(i*j))
> }}}
> It causes the various matrix spaces to rebuilt almost each time! If we
> compare with
> {{{
> def test2():
>     K = QuadraticField(2)
>     M = {(i,j): MatrixSpace(K,i,j) for i in range(2,4) for j in
> range(2,4)}
>     for _ in range(100):
>         i = randint(2,3)
>         j = randint(2,3)
>         m = M[i,j](range(i*j))
> }}}
> then we got
> {{{
> sage: %timeit test1()
> 10 loops, best of 3: 55 ms per loop
> sage: %timeit test2()
> 100 loops, best of 3: 6.49 ms per loop
> }}}
>
> This is one of the reason why the creation of Polyhedron is so slow (see
> #18213).
>
> In the branch we:
> - replace some old Cython code in `matrix_generic_dense` and `matrix0`
> and do some optimization
> - move the function `_get_matrix_class` in `MatrixSpace` out of the class
> (as it depends only on the base ring and the sparseness)
> - In the class `MatrixSpace` the argument `__matrix_class` is now
> `_matrix_class`. That way, it is possible in other part of Sage to
> optimize the creation of matrices by calling directly
> {{{
> sage: M = MatrixSpace(ZZ,3,2)
> sage: M._matrix_class([1,5,3,-1,2,3], coerce=False, copy=False)
> }}}
> - we simplify the main element constructor of matrix, namely
> `MatrixSpace.matrix` and gain a significant speedup.

New description:

 The problem is in the code which looks like the following
 {{{
 def test1():
     K = QuadraticField(2)
     for _ in range(100):
         i = randint(2,3)
         j = randint(2,3)
         m = matrix(K, i, j, range(i*j))
 }}}
 It causes the various matrix spaces to rebuilt almost each time! If we
 compare with
 {{{
 def test2():
     K = QuadraticField(2)
     M = {(i,j): MatrixSpace(K,i,j) for i in range(2,4) for j in
 range(2,4)}
     for _ in range(100):
         i = randint(2,3)
         j = randint(2,3)
         m = M[i,j](range(i*j))
 }}}
 then we got
 {{{
 sage: %timeit test1()
 10 loops, best of 3: 55 ms per loop
 sage: %timeit test2()
 100 loops, best of 3: 6.49 ms per loop
 }}}

 This is one of the reason why the creation of Polyhedron is so slow (see
 #18213).

 In the branch we:
 - replace some old Cython code in `matrix_generic_dense` and `matrix0` and
 do some optimization
 - move the function `_get_matrix_class` in `MatrixSpace` out of the class
 (as it depends only on the base ring and the sparseness)
 - In the class `MatrixSpace` the argument `__matrix_class` is now
 `_matrix_class`. That way, it is possible in other part of Sage to
 optimize the creation of matrices by calling directly
 {{{
 sage: M = MatrixSpace(ZZ,3,2)
 sage: M._matrix_class([1,5,3,-1,2,3], coerce=False, copy=False)
 }}}
 - we simplify the main element constructor of matrix, namely
 `MatrixSpace.matrix` and gain a significant speedup.

 follow up: #18258

--

--
Ticket URL: <http://trac.sagemath.org/ticket/18231#comment:9>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

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

Reply via email to