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

P. Dubois dubois@kristen.llnl.gov
Wed, 1 Nov 1995 10:56:19 +0800

```This is a very nice facility already and has an amazing amount of
functionality. What follows are some notes I made on it.
Other members of the MATRIX-SIG have probably played enough before with the
idea to realize what the examples don't show: the subscript notation
for multiple dimensions works perfectly...I'm stunned.

a. The already implemented list of functions/reduction operators is
impressive. We have also found useful:
1. A function equal to max(m) - min(m), often used as a reduction operator
in higher dimensions. We call it ptp (peak-to-peak).

2. ranf(m) fills m with uniform random numbers on (0.0, 1.0)

3. A function reshape(x,n,m,...) equal to a copy of x reshaped as (n,m,...)
The decision to make reshape a status-altering method is fine, but
often the need for a reshape is in the context of some temporary
value needed in an expression. Some of the clever rank stuff
mitigates this need but I bet this still comes in very useful.
Actually, our function of this kind also duplicates copies of
x as needed provided length(x) divides the new size.

4. The existing ones(n,m) will return a matrix of shape (n,m)
filled with 1.0; equally useful would be one where the i,j element is
1.0 iff i==j, 0.0 otherwise.

b. compress(condition, x) = those elements of x corresponding to those
elements of condition that are "true". condition and x must have
the same length (or x scalar), result is a one-dimensional vector with
length equal to the number of true elements in condition. Naturally this
is most useful if x < y returns a vector instead of a scalar.
Most used form however is with a condition of the form x > 0. or some
similar comparison to a scalar, which are expressible already in Python
if we take "true" in this sense to be the normal Python meaning.

c. My Basis users love:
where(condition,x,y) is shaped like condition and has elements of x and
y where condition is respectively true or false; we allow broadcast of
scalars x or y, so some thought about ranks should reveal the equivalent
notion here. Often used in the form where(z, x/z, 1.e99); in this case
we rely on a convention that something/0.0 = Infinity, where Infinity is
some variable the users can set to suit them, but anyway the idea is that
the computation x/z goes at compiled speed and does not cause an exception.
We haven't found this perfect and of course would prefer that the
computation is simply not done at components where z is false. Given
the genius you guys have already displayed ... (Yes, you can do this
with a for loop, this is just a speed-freak need).

d. One curiosity is the notation something[(a,b,c)]. This is required
because something[a,b,c] is a syntax error. But perhaps Python could
allow the latter interpreted as the former. For if you do x=[1.,2.,3.]
then trying x[(0,1,2)] produces a perfectly sensible error message.
And it would be in the spirit of tuples not always needing the parentheses.
(That sound you hear is the shot of a large caliber weapon as Guido
justifiably shoots a newbie for making a suggested language change before
he can even use half the language yet; in fact, I'm tempted to shoot
myself, but I'm enjoying Python too much to die yet.)

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org