Python and generic programming

Ian Bicking ianb at colorstudy.com
Sat Nov 20 09:25:16 CET 2004


Jive Dadson wrote:
> If you have, for example, several data-types for matrices, how do you
> write code that will automatically find the right routine for quickly
> multiplying a vector by a diagonal matrix represented as a vector, and
> automatically call the right code for multiplying by a sparse matrix
> represented by some sparse coding, etc?

I haven't been following this thread, but Phillip Eby's recent 
implementation of generic functions should be mentioned if it hasn't 
been already:

http://dirtsimple.org/2004/11/generic-functions-have-landed.html

It might look something like this:

@dispatch.on('m1', 'm2')
def matrix_mul(m1, m2):
     "Multiply two matrices"

@matrix_mul.when(VectorMatrix, SparseMatrix)
def matrix_mul_sparse(m1, m2):
     "Special implementation"


-- 
Ian Bicking  /  ianb at colorstudy.com  / http://blog.ianbicking.org



More information about the Python-list mailing list