len() should always return something

Erik Max Francis max at alcyone.com
Sun Jul 26 12:11:02 CEST 2009

Steven D'Aprano wrote:
> I'm curious what those applications are, because regular multiplication 
> behaves differently depending on whether you have a 1x1 matrix or a 
> scalar:
> [[2]]*[[1, 2, 3], [2, 3, 4]] is not defined
> 2*[[1, 2, 3], [2, 3, 4]] = [[2, 4, 6], [2, 6, 8]]
> I'm curious as to what these applications are, and what they're actually 
> doing. Kronecker multiplication perhaps? Do you have some examples of 
> those applications?

The most obvious example would be the matrix algebra equivalent of the 
calculation of the dot product, the equivalent of a dot product in 
normal vector algebra.  If you have two 3-vectors (say), u = (u_1, u_2, 
u_3) and v = (v_1, v_2, v_3), then the dot product is the sum of the 
pairwise products of these terms, u dot v = sum_i u_i v_i = u_1 v_1 + 
u_2 v_2 + u_3 v_3.

Nothing revolutionary there.  The matrix way of writing this wouldn't 
obviously work, since multiplying two nx1 or (1xn) matrixes by each 
other is invalid.  But you _can_ multiply a nx1 matrix by an 1xn matrix, 
and you get the equivalent of the dot product:

	u v^T = ( u dot v ),

where the right hand side of the equation is formally a 1x1 matrix 
(intended to be indicated by the parentheses), but you can see how it is 
useful to think of it as just a scalar, because it's really just a 
matrix version of the same thing as a dot product.

This stretches out to other kinds of applications, where, say, in tensor 
calculus, you can think of a contravariant vector as being transformed 
into a covariant vector by the application of the matrix tensor, which 
is written as lowering an index.  The components of the contravariant 
vector can be thought of as a column vector, while the components of a 
covariant vector can be represented with a row vector.  The application 
  of the metric tensor to a contravariant vector turns it into a row 
vector, and then contracting over the two vectors gives you the inner 
product as above.  The applications and contractions can all be 
visualized as matrix multiplications.  (Contrary to a popularization, 
tensors _aren't_ generalizations of matrices, but their components can be.)

It's a bunch of other examples like that where you end up with a 1x1 
matrix that really represents a scalar, and since that was intended 
there really isn't a lot of efficacy to treat it as a separate entity. 
Especially if you're dealing with a special-purpose language where 
everything is really a form of an generalized array representation of 
something _anyway_.

Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   Scars are like memories. We do not have them removed.
    -- Chmeee

More information about the Python-list mailing list