On Monday 11 July 2011, Simon King wrote: > Hi Martin, Hi Simon, > On 10 Jul., 20:38, Martin Albrecht <martinralbre...@googlemail.com> > > wrote: > > Which makes sense because we always return a zero matrix when we allocate > > in M4RI, so copy() is alloc + memcpy while just creating it is just an > > alloc. > > How does that work? I learnt the hard way: Allocating memory does not > necessarily mean that the memory is initialised by zeroes. Perhaps the > other matrix classes can benefit from M4RI's way to create initialised > memory?
We are not doing anything special except for calling calloc which zeros the memory or we memset() the memory to zero which should still be faster than a memcpy(). > > I guess one approach could be a class attribute which determines whether > > a cache should be used or not (i.e. by type). Another one would decide > > based on some dimension (I don't know which since that depends on the > > implementation heavily). > > According to Robert, the fastest way to create an empty matrix depends > not only on the class but on the dimensions. My main concern is that > doing an elaborate test - repeated each time a zero matrix is created > - would considerably slow things down. Therefore, my first suggestion > was to introduce a static method of the matrix classes (see post > above). > > But I forgot that the test actually needs to be done only once for > each matrix class: It is an ideal candidate for a lazy attribute of > MatrixSpace_generic! Hence, we may do something like > @lazy_attribute > def _copy_zero(self): > if self.__is_sparse: > return False > if self.__matrix_class == > sage.matrix.matrix_mod2_dense.Matrix_mod2_dense: > return False > if self.__matrix_class == > sage.matrix.matrix_modn_dense.Matrix_modn_dense: > if self._nrows>... and self._ncols>...: > return False > else: > return True > ...<do something for matrices over ZZ and so on>... > return True > > def __call__(self,...): > ... > if entries is None or entries == 0: > if self._copy_zero: # faster to copy than to create a new > one. > return self.zero_matrix().__copy__() > else: > return self.__matrix_class(self, {}, coerce=coerce, > copy=copy) Well, you are still adding one conditional jump (if self._copy_zero), but I'll give this approach a try. Cheers, Martin -- name: Martin Albrecht _pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99 _otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF _www: http://martinralbrecht.wordpress.com/ _jab: martinralbre...@jabber.ccc.de -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org