[SciPy-user] FW: Vector orientation (pedantry?)

Alexander Schmolck a.schmolck at gmx.net
Mon Nov 25 15:05:39 EST 2002


Stephen Walton <stephen.walton at csun.edu> writes:

> 
> >Well, note that a row and column vector is nothing more than a 1xN or
> > Nx1 array (as opposed to a what I'd call a 'flat' N-array).
> 
> So [1,2,3] denotes a 'flat' 3-array to Numeric, while [[1,2,3]] denotes
> 1-by-3 array?  Not sure I understand the distinction.


I'm not sure how best to explain this (I don't know your background, other
than that you are likely to be pretty clued about math), but I'll try:


These are not the same:

 scalar   1x vector        1x1 matrix  1x1x1 array  1x1x1x1 array
 1        --> [1] ->       [[1]],      [[[1]]],     [[[[1]]]]    etc.

because apart from storing the same single numerical value, they have
different information about their structure (viz. how many, if any, axes there
are). If this doesn't make sense, try indexing these constructs:

1[0]   ==> TypeError as it makes no sense to index a number
[1][0] ==> 1
[[1]][0] ==> [1] etc.

If you have one index, you index through what's enclosed by the outermost pair
of '[',']'s, so:

[1,2,3][0] (which I could maybe more clearly write as 3 rows:

[1,
 2,
 3][0]
)

is 1

and [[1],
     [2],
     [3]][0]

is [1],

wheras

[[1,2,3]][0] is [1,2,3]

Does that start to make sense now?

In matlab everything really is a matrix, so 1 and [1] are really just a
shorthand for [[1]], and indexing is a hack. This is neither pretty nor does
it scale well.



> 
> I agree that SciPy is awkward for linear algebra, but it's mainly syntax
> in my case.  

I perfectly aggree. I especially find that things like:

 dot(transpose(X), dot(V, transpose(U))) * x

quickly render code completely unreadable. That's why with my matrix class it
looks like:

 X.T ._* (V ._* U.T) * x

where the pseudo-operator  '._*' is the dot-product.

> <HERESY> I find typing 'x=[1,2,3;4,5,6;7,8,9]' much more
> straightforward than 'x=array([[1,2,3],[4,5,6],[7,8,9]]), especially
> since Python/SciPy doesn't seem to have a 'blink matching brackets'
> option. </HERESY>

well this:

 >>> from Numeric import dot, array
 >>> dot(array([[1],[2],[0.00003]]), array([[1,2,3]]))
array([[  1.00000000e+00,   2.00000000e+00,   3.00000000e+00],
       [  2.00000000e+00,   4.00000000e+00,   6.00000000e+00],
       [  3.00000000e-05,   6.00000000e-05,   9.00000000e-05]])


would become:

 >>> from nummat import matrix as mat
 >>> mat('1;2;0.00003') ._* mat('1,2,3')

 matrix('''
  1.00000   2.00000   3.00000
  2.00000   4.00000   6.00000
  0.00003   0.00006   0.00009
 ''')


I like to think that both input and output are markedly preferable in the
second case (and much more so for complicated expressions) :)

(displayed above is matlab style formatting, Numeric.array-style formatting is
also an option; plus really large matrices don't clutter up the screen by
default, e.g.:

>>> mat.ones((1000,10000))
 <1000 x 10000 matrix of type Int32>)


Note that you could also use Matrix.py, which also allows you to write

  >>> Matrix('1, 2, 3')

The main advantages over Matrix.py that already comes with Numeric is that my
matrix class is (or at least aims to be) completely compatible with Numeric
arrays (i.e.  you should be able to use one of my matrix objects anywhere
where you would you could use a Numeric array (of array rank-2, of course)),
provides much more funcationality (among many other things matlab-style output
formatting and good syntax for *both* elementwise and matrixwise operations,
plus it can use an optional library that makes dot-products for big matrices
40 times or so faster).

There are lots of other features and plenty of test-code, but I've lacked the
time to package it up and iron out a few things before I put it on the web.
There are also at least two disadvantages, a) it needs at lest python
2.2 and b) there is a bug in Numeric that makes it really slow, but I've got a
simple and safe patch that fixes that.  As I said, I'll be happy to send
interested parties a preview of the code. OK, enough with the shameless
advertisment for now :)

alex


-- 
Alexander Schmolck     Postgraduate Research Student
                       Department of Computer Science
                       University of Exeter
A.Schmolck at gmx.net     http://www.dcs.ex.ac.uk/people/aschmolc/




More information about the SciPy-User mailing list