On Thursday 07 May 2009, Christophe Oosterlynck wrote:
> Hi,
>
> in a cython project of mine, I need to do some intensive work on
> arrays and then use these list in a matrix product. Doing the
> intensive work on the arrays goes smoothly when using C arrays (using
> malloc). Afterwards I convert the C array to a python list and I
> construct a matrix over GF(2) from this array so I can use Sage's
> quick GF(2) matrix multiplication. I need to do this several times and
> it seems that constructing a matrix from a list is slowing everything
> down.
Is the array a bit array or an int array with zeros and ones? If it is the
former you might be able to just use some pointer arithmetic to convince M4RI
that your array is its matrix. If it is the latter, it will be much quicker
to write the C ints to the matrix directly instead of going through the list,
something like this:
cdef Py_ssize_t i, j
for i from 0 <= i < x:
for j from 0 <= j < y:
mzd_write_bit(self._entries, i, j, a[i][j] % 2)
if a is your two dimensional array.
> I want to combine my algorithm to do a transformation on an array with
> the possibility to do a matrix multiplication with the list I got from
> this algorithm. Is calling matrix(GF(2), x, z, list) the best way to
> do this? I find it very slow...
The code which does the conversion is this:
for i from 0 <= i < self._nrows:
if PyErr_CheckSignals(): raise KeyboardInterrupt
for j from 0 <= j < self._ncols:
mzd_write_bit(self._entries,i,j, int(entries[k]) % 2)
k = k + 1
The %2 is stupid and shouldn't be required but I'd need to fix mzd_write_bit
first (it explicitly checks for 1 instead of !=0). I don't know how expensive
the PyErr_CheckSignals() but the generic entries lookup + the int() are
certainly expensive.
Bottomline: it is quite generic code which could be made somewhat faster with
some care. However, the solution above which avoids all Python data
structures directly will always be fastest.
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://www.informatik.uni-bremen.de/~malb
_jab: [email protected]
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---