[PYTHON MATRIX-SIG] Speed...cg...and more!

James Hugunin jjh@mama-bear.lcs.mit.edu
Mon, 13 Nov 95 11:20:16 -0500

> 1)

See my previous post.

> 2) When writing mathematical stuff with matrices one uses very often
>    the multiplication (in the Matrix sense) with transposed matrices.
>    Something like:
>    s  = Multiply(A.transpose(), r)

The current implementation of matrix multiply sucks!  I do very little  
linear algebra work, so I haven't gotten around to doing this properly yet.   
I'll bear this in mind when I do get around to doing things right.  The  
following is the current implementation of matrix multiplication.

def Multiply(a,b):
	return add.inner(multiply, a, b.transpose())

You should be able to use:
s = add.inner(multiply, A, r).transpose()

I'm sure that the extra transpose at the end will add very little overhead  
to your code (compared to the matrix multiplication itself).

Thanks for the CG code, this is exactly the sort of stuff I'd love to have  
people start building into standard libraries!

> 3) As a matter of style, perhaps we should discuss how to write such things.
>    I guess as soon as the matrixmodule will be in the standard distribution
>    there will be a lot of matlab style writing. We should try to keep the
>    procedures, classes ... as uniform as possible, so that everybody can
>    rely on a certain behaviour. We want to be better than Matlab with such a
>    powerfull Language as Python, don't we?

Good point.  I'd love to see people out there write some proposed style  
guides for matrix functions!

> 4) Last but not least I need a simple example how to use matrixobjects
>    writing own extension.

This is currently very poorly supported in the current 0.11 alpha release  
(expect this to change in later releases).  Look at matrixmodule.c  
matrix_sort() for a much more complicated example than your own.  Also,  
matrices are stored as contiguous memory blocks with a single pointer, to  
get the traditional C **, you need to generate it yourself (this too will be  
supported in a friendly way when I have time).

> 5) Are the handouts of the mentionned talk about the matrix module available
> for    all? Would be nice!

I agree!

And from your followup:

>     f=lambda i, j: sin(i)*cos(j)
>     a=Matrix(1000,1000, func=f)  #square 1000x1000 matrix

Here's how I'd do that using ofuncs

i = mrange(1000)
j = mrange(1000)
a = multiply.outer(sin(i), cos(j))

>    b=a=Matrix_d(10,10,10, func=lambda i, j, k: sin(i)*cos(j)*exp(k)
i = mrange(10)
j = mrange(10)
k = mrange(10)
a1 = multiply.outer(sin(i), cos(j)) #note, this tempory is only needed for clarity

b = a = multiply.outer(a1, exp(k))

Okay, I admit that this is uglier than you'd like, but it runs nearly as  
fast as the hand-coded C would.  It might be possible to write some sort of  
matrix constructor function that could "compile" to this ofunc form on the  


MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org