Discussion: Introducing new operators for matrix computation

Huaiyu Zhu hzhu at localhost.localdomain
Mon Jul 17 02:12:35 EDT 2000


On Sat, 15 Jul 2000 23:38:30 +0100, Gareth McCaughan <gjm11 at g.local> wrote:
>Huaiyu Zhu wrote:
>> How would you write this in list compresension (in less than 10 lines)?
>> 
>> B*(sin(A*x+b).*(A*y)/3)/C
>> 
>> Note that * is matrix multiplication and .* is elementwise.  Note that C is
>> a matrix so the / is matrixwise.  If you want to write everything as for
>> loops it takes at least 30 lines, without any decent error analysis.
>
>    B*[p*q/3 for p in A*x+b, q in A*y]/C

Someone else had already given a similar answer.  But more work is needed
for such things to work:

The x, b and y could be matrices, so p and q need to be double loops.

The [ ... ] need to be a double lists.  Is list comprehension defined for
this?

B*[..]/C would not work without a cast from double list to matrix.

For all these errors, what would the error messages look like?  Do they
involve the dummy indices p and q?  A previous answer also reused the name b
for the dummy variable, which would make the error even more obscure.

Besides, the main point of using matrix is to be free from specifying loops
over indices with dummy names.  Compare this double loop with extra syntax
with a single operator .* and you may wonder why this is considered at all.

Since this is going way off original topic I'll stop here.  I admit that I'm
at fault to a large extent.

As to the question of what's the precedence of the proposed .* like
operators, they are the same as their ordinary counterparts, because they
become identical when the operands are numbers or 1x1 matrices.


Huaiyu



More information about the Python-list mailing list