\chapter{Array Attributes}
\label{cha:array-attributes}

There are four public array attributes; however, they are only available 
in Python 2.2 or later. There are array methods that may be used instead. The
attributes are \code{shape, flat, real,} and \code{imaginary}.


\begin{memberdesc}[numarray]{shape}
   Accessing the \member{shape} attribute is equivalent to calling the
   \method{getshape} method; it returns the shape tuple.  Assigning a value to
   the shape attribute is equivalent to calling the \method{setshape} method.
\begin{verbatim}
>>> print a
[[0 1 2]
 [3 4 5]
 [6 7 8]]
>>> print a.shape
(3,3)
>>> a.shape = ((9,))
>>> print a.shape
(9,)
\end{verbatim}
\end{memberdesc}


\begin{memberdesc}[numarray]{flat}
   \label{mem:numarray:flat}
   Accessing the flat attribute of an array returns the flattened, or
   \method{ravel}ed version of that array, without having to do a function
   call.  This is equivalent to calling the \method{getflat} method. The
   returned array has the same number of elements as the input array, but it is
   of rank-1. One cannot set the flat attribute of an array, but one can use
   the indexing and slicing notations to modify the contents of the array:
\begin{verbatim}
>> print a
[[0 1 2]
 [3 4 5]
 [6 7 8]]
>> print a.flat
0 1 2 3 4 5 6 7 8]
>> a.flat[4] = 100
>> print a
[[  0   1   2]
 [  3 100   5]
 [  6   7   8]]
>> a.flat = arange(9,18)
>> print a
[[ 9 10 11]
 [12 13 14]
 [15 16 17]]
\end{verbatim}
\end{memberdesc}


\begin{memberdesc}[numarray]{real}
\end{memberdesc}
\begin{memberdesc}[numarray]{imag}
\end{memberdesc}
\begin{memberdesc}[numarray]{imaginary}
   These attributes exist only for complex arrays. They return respectively
   arrays filled with the real and imaginary parts of the elements. The
   equivalent methods for getting and setting these values are
   \method{getreal}, \method{setreal}, \method{getimag}, and \method{setimag}.
   \method{getimaginary} and \method{setimaginary} are synonyms for
   \method{getimag} and \method{setimag} respectively, and \method{.imag} is a
   synonym for \method{.imaginary}.  The arrays returned are not contiguous
   (except for arrays of length 1, which are always contiguous).
   The attributes \member{real}, \member{imag}, and \member{imaginary} are 
   modifiable:
\begin{verbatim}
>>> print x
[ 0.             +1.j               0.84147098+0.54030231j 0.90929743-0.41614684j]
>>> print x.real
[ 0.          0.84147098  0.90929743]
>>> print x.imag
[ 1.          0.54030231 -0.41614684]
>>> x.imag = arange(3)
>>> print x
[ 0.        +0.j  0.84147098+1.j  0.90929743+2.j]
>>> x = reshape(arange(10), (2,5)) + 0j # make complex array
>>> print x
[[ 0.+0.j  1.+0.j  2.+0.j  3.+0.j  4.+0.j]
 [ 5.+0.j  6.+0.j  7.+0.j  8.+0.j  9.+0.j]]
>>> print x.real
[[ 0.  1.  2.  3.  4.]
 [ 5.  6.  7.  8.  9.]]
>>> print x.type(), x.real.type()
Complex64 Float64
>>> print x.itemsize(), x.imag.itemsize()
16 8
\end{verbatim}
\end{memberdesc}


%% Local Variables:
%% mode: LaTeX
%% mode: auto-fill
%% fill-column: 79
%% indent-tabs-mode: nil
%% ispell-dictionary: "american"
%% reftex-fref-is-default: nil
%% TeX-auto-save: t
%% TeX-command-default: "pdfeLaTeX"
%% TeX-master: "numarray"
%% TeX-parse-self: t
%% End:
