[Numpy-discussion] untenable matrix behavior in SVN

Gael Varoquaux gael.varoquaux@normalesup....
Sat Apr 26 13:01:43 CDT 2008


On Sat, Apr 26, 2008 at 02:01:45PM -0400, Alan G Isaac wrote:
>     >>> A
>     matrix([[1,  2],
>             [ 3,  4]])
>     >>> A = np.mat(x)
>     >>> for row in A:
>     ...  for col in row:
>     ...   print col
>     ...
>     [[1  2]]
>     [[3 4]]

> So are you saying that one should not be able to iterate 
> through to the elements of a matrix?

I kinda expect this was the real issue.

I think the only way to get consistent behavior for such code is either

* to use a syntax similar to dictionnaries:

    for row in A.rows():
	for col in row.cols()

  I actually think this is much better than the code you currently use,

* or implement row and column objects.

The problem in your code is that you do not distinguish iterating over
rows and columns, and in linear algebra these are different objects. The
two solutions I propose do this distinction. My favorite one is the first
one (the rows and cols methods).

Cheers,

Gaël


More information about the Numpy-discussion mailing list