Ticket #10628: 10628-speedup-matrix-creation

File 10628-speedup-matrix-creation, 1.6 KB (added by mderickx, 10 years ago)
Line 
1# HG changeset patch
2# User Maarten Derickx <m.derickx.student@gmail.com>
3# Date 1327941723 28800
4# Node ID 91d8fbde9e26a59ef62c88d0a0802c268cf25496
5# Parent  75b29dbaa700aa3a5dd4e5619d4742a50404f8a3
6#10628 this makes matrix initialisation of lists of lists much faster by removing sum(list,[]) type stuff which is of O(n^2) in len(list)
7
8diff --git a/sage/matrix/matrix_space.py b/sage/matrix/matrix_space.py
9--- a/sage/matrix/matrix_space.py
10+++ b/sage/matrix/matrix_space.py
11@@ -533,7 +533,10 @@
12                                 e[(j,i)] = x
13                 entries = e
14             else:
15-                entries = sum([v.list() for v in entries],[])
16+                tmp=[]
17+                for v in entries:
18+                    tmp.extend(v)
19+                entries = tmp
20 
21         if rows is None:
22             rows = True
23@@ -1333,16 +1336,12 @@
24                     raise ValueError("a matrix from %s cannot be converted to "
25                                      "a matrix in %s!" % (x.parent(), self))
26         if isinstance(x, list) and len(x) > 0:
27-            if isinstance(x[0], list):
28-                x = sum(x,[])
29-            elif hasattr(x[0], "is_vector"): # TODO: is this the best way to test that?
30+            if isinstance(x[0], (list,tuple)) or hasattr(x[0], "is_vector"):# TODO: is this the best way to test is_vector?
31                 e = []
32                 for v in x:
33-                    e = e + v.list()
34-                copy = False # deep copy?
35+                    e.extend(v)
36                 x = e
37-            elif isinstance(x[0], tuple):
38-                x = list(sum(x,()))
39+
40 
41             if not rows:
42                 new_x = []