[PYTHON MATRIX-SIG] Some notes on the matrix class

Jim Fulton, U.S. Geological Survey jfulton@usgs.gov
Wed, 01 Nov 1995 16:54:58 -0500

On Wed, 1 Nov 95 16:02:31 -0500 
James Hugunin said:
> > Couldn't this be done by implementing a "numeric" type (at the C
> > level) that has a range of [0..1]?  It could have two objects...
> >
> > Also, what exactly is the advantage of having a Boolean type?  (Am I
> > showing my age here? :-)
> What I'm asking for is to be able to create a new python object in C, and  
> to include boolean methods for that object.  So, just like I can implement  
> my matrix_add function for the syntax "a+b" when a and b are matrices  
> (modulo coercion), I'd really like to be able to implement a matrix_greater  
> function for the syntax "a>b" when a and b are matrices (still modulo  
> coercion).  This seems completely in the original spirit of the language.
> The reason for this is that if a = [1,2,3], and b = [2,2,2], I'd like to  
> define a>b = [0,0,1].  (where all of the above are matrices).  This is  
> useful in an astounding range of matrix computations, and there is no  
> sensible definition of a>b for matrices that returns a scalar boolean value.  
> ie. the traditional discrete delta function is elegantly expressed as
> def delta(i): return i==0
> delta([-2,-1,0,1,2,3,4]) -> [0,0,1,0,0,0,0]
> It's also useful for "vectorizing" traditional operations like filter.  If  
> x is a vector,
> filter(lambda x: x<2 and x>0, x) <--> x.filter(x<2 and x>0)
> except the second is orders of magnitude faster for matrices.
> I understand that this is a major change to some of the more complicated  
> parts of python, so there are good practical reasons that it won't be done  
> anytime soon.  Nevertheless, I would love though to have you agree that this  
> would be a good feature if somebody ever has the time to add it.

I think what you want to do is best done with a separate function.

I'm against having comparison operators return non-boolean values.
I think comparison operators should always either return boolean, or
raise an exception if an operation is not supported.

With your proposal, would [1,2,3]==[0,2,3] return [0,1,1]?  Then you
would lose the ability to say: "if a == b:"

I'd much rather be able to say a particular type supports == and !=
and does not support <, <=, >, and >=.


MATRIX-SIG  - SIG on Matrix Math for Python

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