#18312: Construction of a sparse matrix from sparse vectors does not exploit
sparseness
---------------------------------------------+------------------------
       Reporter:  nthiery                    |        Owner:
           Type:  defect                     |       Status:  new
       Priority:  major                      |    Milestone:  sage-6.7
      Component:  linear algebra             |   Resolution:
       Keywords:  sparse matrix constructor  |    Merged in:
        Authors:                             |    Reviewers:
Report Upstream:  N/A                        |  Work issues:
         Branch:                             |       Commit:
   Dependencies:                             |     Stopgaps:
---------------------------------------------+------------------------
Changes (by nthiery):

 * keywords:   => sparse matrix constructor


Old description:

> {{{
> sage: n = 10^3
> sage: F = FreeModule(QQ, n, sparse=True)
> sage: v = F.an_element()
> sage: vectors = [v]*n
>
> sage: %time a = matrix(vectors, sparse=True)
> CPU times: user 2.01 s, sys: 38.4 ms, total: 2.05 s
> Wall time: 1.96 s
> }}}
>
> Compare with the construction of the same matrix through an
> intermediate dictionary:
>
> {{{
> sage: %time b = matrix(QQ, n, {(i,j): c for i,v in enumerate(vectors) for
> j,c in v.iteritems()}, sparse=True)
> CPU times: user 7.33 ms, sys: 124 µs, total: 7.46 ms
> Wall time: 6.06 ms
> sage: a == b
> True
> }}}
>
> Running `%prun` shows that the input vectors are converted to dense
> lists and back (gasp), which gives a complexity of `n^2` instead of
> linear in the number of nonzero entries as would be expected.

New description:

 {{{
 sage: n = 10^3
 sage: F = FreeModule(QQ, n, sparse=True)
 sage: v = F.an_element()
 sage: vectors = [v]*n

 sage: %time a = matrix(vectors, sparse=True)
 CPU times: user 2.01 s, sys: 38.4 ms, total: 2.05 s
 Wall time: 1.96 s
 }}}

 Compare with the construction of the same matrix through an
 intermediate dictionary:

 {{{
 sage: %time b = matrix(QQ, n, {(i,j): c for i,v in enumerate(vectors) for
 j,c in v.iteritems()}, sparse=True)
 CPU times: user 7.33 ms, sys: 124 µs, total: 7.46 ms
 Wall time: 6.06 ms
 sage: a == b
 True
 }}}

 Running `%prun` shows that the input vectors are converted to dense
 lists and back (gasp!), which gives a complexity of `n^2` instead of
 linear in the number of nonzero entries as would be expected.

 See also: #10312

--

--
Ticket URL: <http://trac.sagemath.org/ticket/18312#comment:1>
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