[SciPy-user] C extension to manipulate sparse lil matrix
Hoyt Koepke
hoytak@cs.ubc...
Fri Jan 9 00:33:23 CST 2009
Hello Andy,
I don't know if I can be of much help answering your questions, but
here's a few thoughts:
> I want to move some time critical bits of code for hidden Markov
> models from python to C. I've written code that works and uses sparse
> matrices. Next, I want to implement the "backward" algorithm in C.
<snip>
> I think I could glean what I need from
> that example. Since I'm new to C extensions, I'd like to see type
> checking and reference counting done right too.
Have you tried using cython ((http://www.cython.org)? It makes
writing C code extensions almost as painless as typing your variables,
works well with numpy arrays, and handles all the messy stuff for you.
If your goal is to learn the ins and outs of how python works with
extensions, then stick with c. But if you just want to optimize your
code, you can't beat cython. In particular, see
http://wiki.cython.org/tutorials/numpy for how to work with numpy.
> I would be grateful if someone posted C code that interchanged two
> rows of a lil sparse matrix.
I'm not sure it's what you're looking for, I might recommend using an
intermediate index mapping array, and make all your accesses to the
sparse matrix go through it. in other words, have m be a bijective
map on the indices, and use something SM.row[ m[i] ] to access stuff.
mapping indices are easy to swap in C or cython. Then at the end, do
the whole transformation either in python or on the index arrays of a
csr or csc matrix all at once.
Some other experts on the list might have a better way though,
--Hoyt
++++++++++++++++++++++++++++++++++++++++++++++++
+ Hoyt Koepke
+ University of Washington Department of Statistics
+ http://www.stat.washington.edu/~hoytak/
+ hoytak@gmail.com
++++++++++++++++++++++++++++++++++++++++++
More information about the SciPy-user
mailing list