File: UsersGuide.txt

package info (click to toggle)
python-numarray 1.5.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 11,384
  • sloc: ansic: 113,864; python: 22,422; makefile: 197; sh: 11
file content (397 lines) | stat: -rw-r--r-- 13,253 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
A User's Guide to the differences between numarray and Numeric.

For the most part, one can use the existing Numeric manual
as a manual for numarray (we are in the process of editing
the Numeric manual for use with numarray). The following
should be read to understand what differences there are between
numarray and Numeric. The emphasis is given to differences
between common capabilities; at the end, there are explanations
of new capabilities.

*****
Types
*****

In Numeric there are two ways of referring to the type of an array,
either by using a character code (one character string) or a type
name. For example, one can specify a Numeric float as either
Float, Float32, or 'f'. In fact Float and Float32 are simply
module variables that have value 'f'.

In numarray, types are represented by type objects and not
character codes. As with Numeric there is a module variable Float32,
but now it represents an instance of a FloatingType class.
For example, if x is a Float32 array

x.type() will return a FloatingType instance associated with 32 bit
floats (instead of using x.typecode() as is done in Numeric).
So the following will not work in numarray:

if x.typecode() == 'f':

rather, use:

if x.type() == Float32

[all examples presume "from numarray import *" has been used instead
of "import numarray"]

The advantage of the new scheme is that other kinds of tests become
simpler. The type classes are heirarchical so one can easiy test to
see if the array is an integer array. For example:

if isinstance(x.type(), IntegralType):

or

if isinstance(x.type(), UnsignedIntegralType):

This is the type heirarchy


                                 NumericType
                                 |  |  |  |
                                /   |   \  \______________________
                    ___________/    |    \_________               |
                   |                |              |              |
              IntegralType     BooleanType    FloatingType   ComplexType
               |        |           |           |       |      |     |
              /          \        (Bool)   (Float32) (Float64) |     |
             /            \                                    |     |
            |              |                          (Complex64)
(Complex128)
UnsignedIntegralType   SignedIntegralType
   |         |          |       |       |
(UInt8)   (UInt16)   (Int8)  (Int16) (Int32)
   |         |          |       |       |
    \       /            \      |      /
   UnsignedType            SignedType


In the above, parentheseis indicate instances of a type class. All of
of the Int type classes inherit from Numeric type and one of SignedType
or UnsignedType (multiple inheritance). Thus one can use isinstance()
to see if an array is in a more general class of types.

Numarray defines a number of aliases for the above types. In particular:

              Aliases
Bool
Int8          '1', "i1", "Byte"
Int16         's', "i2", "Short"
Int32         'i', "i4", "Int"
UInt8              "u1", "UByte"
UInt16             "u2", "UShort"
Float32       'f', "f4", "Float"
Float64       'd', "f8", "Double"
Complex64     'F', "c8", "Complex"
Complex128    'D', "c16"

(This is a bit of a lie, the single character "Numeric" codes are not
implemented, but are very easily added)

The aliases are generally accepted whereever a type class is when
used as an argument for a type parameter or keyword. For example
the following are all equivalent:

x = array([2,3], 'f')      # doesnt' work yet
x = array([2,3], Float32)
x = array([2,3], "Float")
x = array([2,3], "f4")

The unsigned integer types have no corresponding type in Numeric,
nor does Bool.

*************
Type Coercion
*************

In expressions involving only arrays, the normal coercion rules
apply (i.e., the same as Numeric). However, the same rules do
not apply to binary operations between arrays and Python scalars
in certain cases. If the kind of number is the same for the array
and scalar (e.g., both are integer types or both are float types),
then the type of the output is determined by the precision of the
array, not the scalar. Some examples will best illustrate:

Scalar type  *  Array type        Numeric result type    numarray result
type
   Int            Int16                Int32                    Int16
   Int            Int8                 Int32                    Int8
   Float          Int8                 Float64                  Float64
   Float          Float32              Float64                  Float32

The change in the rules was made so that it would be easy to preserve the
precision of arrays in expressions involving scalars. Previous solutions
with Numeric were either quite awkward (using a function to create a rank-0
array from a scalar with the desired type) or surprising (the savespace
attribute, that never allowed type coercion). The problem arises because
Python has a limited selection of scalar types. This appears to be the
best solution though it admittedy may surprise some who are used to the
classical type coercion model.

Another twist on type coercion appears when combining a signed int with
an unsigned in. For example, combining an Int16 with an UInt16 results
in an Int32 since neither type can hold the range of the other (we have not
implemented UInt32 since we haven't implemented Int64)

****************
Array Attributes
****************

Numeric arrays have some public attributes. Numarray arrays
have none. All changes to an array's state must be made
through accessor methods. Specifically, Numeric has the following
attributes:

Numeric        numarray accessor method(s)
Attribute

shape      --> shape()  (i.e., specify new shape through method arg instead
of
                         assigning to shape attribute)
flat       --> flat()
real       --> real()   (set capability not implemented yet, but will be)
imag       --> imag()   (ditto)
savespace  --> not used, no equivalent functionality (xxx check this)

Private attributes:

Numarray arrays have lots of them (all preceded by underscores).
Don't mess with them. Changing them in ways inconsistent with
other attributes can result in Python crashing. Only those
contributing to the underlying system code should access these
attributes. Even for read-only purposes you should rely on the
public accesor methods (lest we change the details of the
underlying attributes on you; you were warned!)

*****************
Other differences
*****************

Warning and error messages may have changed.

There are no doubt many other differences (mostly minor we hope)
that we have not discovered (or have forgotten).  Please let us
know about them so we can properly document them.

****************
New capabilities
****************

Index Arrays:

Arrays supplied as arguments to subscripts have
special meaning. If the array is of Bool type, then the indexing will be
treated as the equivalent of the compress function. If the array is of
an Integer type, then a take or put operation is implied. We will
generalize the existing take and put as follows:

If ind1, ind2,...indN are index arrays whose values indicate the index
into another array then

x[ind1, ind2]

forms a new array with the same shape as ind1, ind2 (they all must be
broadcastable to the same shape) and values such:

result[i,j,k] = x[ind1[i,j,k], ind2[i,j,k]]

In this example, ind1, ind2 are index arrays with 3 dimensions (but they
could have an arbitrary number of dimensions).

To illustrate with some specific examples:


>>> # simple index array example
>>> x = 2*arange(10)
>>> ind = array([3,6,2,4,4])
>>> x[ind]

array([ 6, 12,  4,  8,  8])


>>> # index a 2-d array
>>> x = arange(12)
>>> x = reshape(x,(3,4))
>>> x

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

>>> ind1 = array([2,1])
>>> ind2 = array([0,2])
>>> x[ind1, ind2]

array([8, 6])


>>> # multidimensional index arrays
>>> ind1 = array([[2,2],[1,0]])
>>> ind2 = array([[2,1],[0,1]])
>>> x[ind1, ind2]

array([[10,  9],
       [ 4,  1]])


>>> # Mindblowing combination of multidimensional index arrays with
>>> # partial indexing. Strap on your seatbelts.
>>> x[ind1]

array([[[ 8,  9, 10, 11],
   [ 8,  9, 10, 11]],

  [[ 4,  5,  6,  7],
   [ 0,  1,  2,  3]]])

Note that in this last example, each index in the single index array
(ind1) is treated as though x were given only one index. For each
of these 'single' indices, a 1-d array is returned, thus the combination
of the 2 dimensions in the index array combined with the leftover
dimension in the array being indexed produces a 3 dimensional
array.

When using constants for
some of the index positions, then the result uses that constant for all
values. Slices and strides (at least initially) will not be permitted in
the same subscript as index arrays. So


>>> x[ind1, 2]

array([[10, 10],
       [ 6,  2]])

would be legal, but


>>> x[ind1, 1:3]

Traceback (most recent call last):
[...]
    raise IndexError("Cannot mix arrays and slices as indices")
IndexError: Cannot mix arrays and slices as indices

would not be.

Similarly for assignment:

x[ind1, ind2, ind3] = values

will form a new array such that:

x[ind1[i,j,k], ind2[i,j,k], ind3[i,j,k]] = values[i,j,k]

The index arrays and the value array must be broadcast consistent.
(As an example: ind1.shape()=(5,4), ind2.shape()=(5,),
ind3.shape()=(1,4), and values.shape()=(1).

# Index put example, using broadcasting and illustrating that Python
# integer sequences work as indices also.

>>> x = zeros((10,10))
>>> x[[2,5,6],array([0,1,9,3])[:,NewAxis]] = 111
>>> x

array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  [111, 111,   0, 111,   0,   0,   0,   0,   0, 111],
  [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  [111, 111,   0, 111,   0,   0,   0,   0,   0, 111],
  [111, 111,   0, 111,   0,   0,   0,   0,   0, 111],
  [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0]])


If indices are repeated, the last value encountered will be stored. When
index values are out of range they will be clipped to the appropriate
range. That is to say, negative indices will not have the same meaning
by default [This will change!]. Use of the equivalent take and put
functions will allow other interpretations of the indices (raise
exceptions for out of bounds indices, allow negative indices to work
backwards as they do when used individually, or for indices to wrap
around). The same behavior applies for functions such as choose and
where. [We are planning to change indexing so that negative indices
have the traditional Python interpretation]


>>> x = 2*arange(10)
>>> x[[0, 5, 100, 5]] = [1000, 1005, 1100, 2005]
>>> x

array([1000,    2,    4,    6,    8, 2005,   12,   14,   16, 1100])

Output arguments for Ufunc methods.

Reduce, accumulate, and outer accept an array as the ouput argument.
Such arguments must be of the same type as the expected output
(xxx is this correct)

Arbitrary types for Ufunc output arrays.

Like Numeric, numarray accepts an output array as an output argument
for Ufuncs. Unlike Numeric, the array may have any type (automatic
conversion is performed on the output).

tofile and fromfile capability

There is a fromfile function that creates an array directly
from a file (based on the undocumented file method "readinto").
This avoids the need to read data in through a string. Likewise,
there is a tofile method for arrays to do the reverse.

Examples:

x = fromfile("greatdata.dat", Float32, (100,100))

will create a Float32 100x100 data from the file greatdata.dat.
If one want to read an array offset into the file, open the
file first, seek or otherwise move to the beginning of the
data and then call the function.

f = open("greatdata.dat")
f.seek(2500)
x = from file(f, Float32, (100,100))

(2*x).tofile("doubledgreatdata.dat")

Likewise, tofile will work with fileobjects and one can write multiple
arrays to a single output file that way.

Memory-mapped files:

It is possible to memory map a file and refer to its contents
with Numarray

[Details to be provided later.]

Record Arrays:

[Documentation forthcoming]

Character Arrays:

[Documentation forthcoming]

New Array properties:

It is possible to create an array where the values are intrinsically
byteswapped. Normally we expect that this property will be set by
a function that takes its data from a file and recognizes that the
data are byteswapped and decides for whatever reason (e.g., memory
mapping) that it should not byteswap the data in place. This is not
a property we expect most users to be concerned with explicitly, but
primarily for those that write software that creates arrays from
memory mapped files or read only sources.

It is possible to create arrays with arbitray byte offests and strides
between elements. Since such arrays may have data elements that are
"nonaligned". As with byteswapping, we do not expect users to deal
with this issue explicitly much; it is more for those that write
function that create record or other inhomogeneous, but regular, arrays.