Numerical Python

Numerical Python

David Ascher Paul F. Dubois Konrad Hinsen Jim Hugunin Travis Oliphant

Legal Notice

Numerical Python and this manual are an Open Source software project. This manual was originally written under the sponsorship of Lawrence Livermore National Laboratory. Numerical Python was written by a wide variety of people, principally Jim Hugunin when he was a student at MIT. LLNL has released this manual and its contributions to Numerical Python under the following terms. Numerical Python is generally released under the terms of the Python license.

Copyright (c) 1999. The Regents of the University of California. All rights reserved. Permission to use, copy, modify, and distribute this software for any purpose without fee is hereby granted, provided that this entire notice is included in all copies of any software which is or includes a copy or modification of this software and in all copies of the supporting documentation for such software. This work was produced at the University of California, Lawrence Livermore National Laboratory under contract no. W-7405-ENG-48 between the U.S. Department of Energy and The Regents of the University of California for the operation of UC LLNL. DISCLAIMER This software was prepared as an account of work sponsored by an agency of the United States Government. Neither the United States Government nor the University of California nor any of their employees, makes any warranty, express or implied, or assumes any liability or responsibility for the accuracy, completeness, or usefulness of any information, apparatus, product, or process disclosed, or represents that its use would not infringe privately-owned rights. Reference herein to any specific commercial products, process, or service by trade name, trademark, manufacturer, or otherwise, does not necessarily constitute or imply its endorsement, recommendation, or favoring by the United States Government or the University of California. The views and opinions of authors expressed herein do not necessarily state or reflect those of the United States Government or the University of California, and shall not be used for advertising or product endorsement purposes.

ii

Table of Contents

1. Introduction................................................................................ 9

Where to get information and code ..............................................................10 Acknowledgments ........................................................................................10

2. Installing NumPy...................................................................... 12

Testing the Python installation .....................................................................12 Testing the Numeric Python Extension Installation......................................12 Installing NumPy ..........................................................................................13

3. The NumTut package .............................................................. 14

Testing the NumTut package .......................................................................14 Possible reasons for failure:.........................................................................14

Win32 ...................................................................................................14 Unix ......................................................................................................15

4. High-Level Overview ............................................................... 16

Array Objects ...............................................................................................16 Universal Functions......................................................................................17 Convenience Functions................................................................................17 RandomArray ...............................................................................................18 FFT...............................................................................................................18 LinearAlgebra...............................................................................................19

5. Array Basics............................................................................. 20

Basics...........................................................................................................20 Creating arrays from scratch........................................................................21

array() and typecodes ..........................................................................21 Multidimensional Arrays .......................................................................22 Creating arrays with values specified `on-the-fly'.........................................26 zeros() and ones() ................................................................................26 arrayrange()..........................................................................................26 Creating an array from a function: fromfunction().................................27 identity()................................................................................................29

Coercion and Casting...................................................................................29 Automatic Coercions and Binary Operations .......................................29 Deliberate up-casting: The asarray function.........................................30 The typecode value table .....................................................................30 Consequences of silent upcasting........................................................31 Deliberate casts (potentially down): the astype method .......................31

Operating on Arrays .....................................................................................32 Simple operations.................................................................................32

Getting and Setting array values..................................................................33 Slicing Arrays ...............................................................................................34

6. Ufuncs ...................................................................................... 36

What are Ufuncs? ........................................................................................36 Ufuncs can operate on any Python sequence......................................37 Ufuncs can take output arguments.......................................................37 Ufuncs have special methods ..............................................................37 The reduce ufunc method ........................................................................ 37 The accumulate ufunc method ................................................................ 38 The outer ufunc method .......................................................................... 38 The reduceat ufunc method ..................................................................... 39 Ufuncs always return new arrays .........................................................39

Which are the Ufuncs?.................................................................................39 Unary Mathematical Ufuncs (take only one argument) ........................39 Binary Mathematical Ufuncs.................................................................39 Logical Ufuncs......................................................................................39 Ufunc shorthands .................................................................................40

7. Pseudo Indices ........................................................................ 42

8. Array Functions ....................................................................... 44

take(a, indices, axis=0) ................................................................................44 transpose(a, axes=None).............................................................................45 repeat(a, repeats, axis=0) ............................................................................46 choose(a, (b0, ..., bn))..................................................................................46 ravel(a) .........................................................................................................46 nonzero(a) ....................................................................................................46 where(condition, x, y) ...................................................................................47 compress(condition, a, axis=0) ....................................................................47 diagonal(a, k=0) ...........................................................................................47 trace(a, k=0) .................................................................................................47 searchsorted(a, values)................................................................................47 sort(a, axis=-1) .............................................................................................48 argsort(a, axis=-1) ........................................................................................48 argmax(a, axis=-1), argmin(a, axis=-1) ........................................................49 fromstring(string, typecode) .........................................................................49 dot(m1, m2)..................................................................................................49 matrixmultiply(m1, m2).................................................................................49 clip(m, m_min, m_max)................................................................................49 indices(shape, typecode=None)...................................................................50 swapaxes(a, axis1, axis2) ............................................................................50 concatenate((a0, a1, ... , an), axis=0) ..........................................................51 innerproduct(a, b).........................................................................................51

iv

?

array_repr() ..................................................................................................51 array_str().....................................................................................................51 resize(a, new_shape)...................................................................................51 diagonal(a, offset=0, axis1=-2, axis2=-1).....................................................52 repeat ............................................................................ (a, counts, axis=0)52 convolve (a, v, mode=0)...............................................................................52 cross_correlate (a, v, mode=0) ....................................................................53 where (condition, x, y) ..................................................................................53 identity(n) .....................................................................................................53 sum(a, index=0) ...........................................................................................53 cumsum(a, index=0) ....................................................................................53 product(a, index=0) ......................................................................................54 cumproduct(a, index=0) ...............................................................................54 alltrue(a, index=0) ........................................................................................54 sometrue(a, index=0) ...................................................................................54

9. Array Methods.......................................................................... 55

itemsize()......................................................................................................55 iscontiguous()...............................................................................................55 typecode() ....................................................................................................55 byteswapped()..............................................................................................55 tostring() .......................................................................................................55 tolist() ...........................................................................................................56

10. Array Attributes ..................................................................... 57

flat ................................................................................................................57 real and imaginary........................................................................................57

11. Special Topics........................................................................ 59

Subclassing..................................................................................................59 Code Organization .......................................................................................59

Numeric.py and friends ........................................................................59 UserArray.py ........................................................................................60 Matrix.py ...............................................................................................60 Precision.py ..........................................................................................60 ArrayPrinter.py .....................................................................................60 Mlab.py.................................................................................................60

bartlett(M) ............................................................................................... 60 blackman(M) ........................................................................................... 60 corrcoef(x, y=None) ................................................................................ 60 cov(m,y=None)........................................................................................ 60 cumprod(m) ............................................................................................. 60 cumsum(m).............................................................................................. 60 diag(v, k=0) ............................................................................................. 60 diff(x, n=1) .............................................................................................. 61 eig(m) ...................................................................................................... 61 eye(N, M=N, k=0, typecode=None)........................................................ 61 fliplr(m) ................................................................................................... 61 flipud(m).................................................................................................. 61 hamming(M)............................................................................................ 61 hanning(M).............................................................................................. 61 kaiser(M, beta)......................................................................................... 61

v

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download