[MATRIX-SIG] Newbie Q.

Konrad Hinsen hinsen@ibs.ibs.fr
Tue, 1 Jul 1997 10:25:02 +0200


>  I am new to NumPy and there has to be a easy way to do this than I can think of.
> >>> a
> array([1, 2, 3])
> >>> b
> array([2, 4, 6])
> >>> c
> array([[1, 2, 3],
>        [2, 4, 6]])
> >>> concatenate((c,a))  # Here's my problem they are different row shape.
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "/opt/gnu/lib/python1.4/NumPy/Numeric.py", line 58, in concatenate
>     return multiarray.concatenate(a)
> ValueError: arrays must have same number of dimensions

You must add the missing dimension to a before concatenating. The
easiest solution is:

>>> concatenate((c, a[NewAxis,:]))
array([[1, 2, 3],
       [2, 4, 6],
       [1, 2, 3]])

> but that sounds extremely silly. In my past in Gauss:
> d = c | a 
> would have done the job.

I don't know much about Gauss. Does it treat all arrays as
two-dimensional, like Matlab? In that case there is never a dimension
mismatch.

> In addition, in Gauss
> Horizontal would be e = a~b
> Transposing c would have been 
> t = c'
> dot product would be
> d = a .* b'
> This to me faster than writing transpose(c) or concatenate((a,b)1), etc...

Gauss is a special array language, whereas Python is a general-purpose
language with arrays. If Python had a short notation for all its
domains of application, it would be a complete mess. So it follows the
"spell-everything-out" approach (like Mathematica).

> Is there shorter method in Python? I guess I could write a emacs key. Does
> anyone have one already?

Functions are objects in Python, so you can create shorthands by assignment:

  TR = transpose
  CC = concatenate
  A = array

  CC(TR(a), A([2, 3, 4]))

You can then put all your favourite abbreviations into a module and
import from it. But don't expect everyone else to understand your
private notation!
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________