Contents

I NumPy from Python 12

1 Origins of NumPy 13

2 Object Essentials 18

2.1 Data-Type Descriptors 20

2.2 Basic indexing (slicing) 23

2.3 Memory Layout of ndarray 26

2.3.1 Contiguous Memory Layout 26

2.3.2 Non-contiguous memory layout 28

2.4 Universal Functions for arrays 30

2.5 Summary of new features 32

2.6 Summary of differences with Numeric 34

2.6.1 First-step changes 34

2.6.2 Second-step changes 37

2.6.3 Updating code that uses Numeric using alter_codeN 38

2.6.4 Changes to think about 39

2.7 Summary of differences with Numarray 40

2.7.1 First-step changes 41

2.7.1.1 Import changes 41

2.7.1.2 Attribute and method changes 42

2.7.2 Second-step changes 43

2.7.3 Additional Extension modules 43

3 The Array Object 45

3.1 ndarray Attributes 45

3.1.1 Memory Layout attributes 46

3.1.2 Data Type attributes 50

3.1.3 Other attributes 51

3.1.4 Array Interface attributes 52

3.2 ndarray Methods 55

3.2.1 Array conversion 55

3.2.2 Array shape manipulation 60

3.2.3 Array item selection and manipulation 62

3.2.4 Array calculation 66

3.3 Array Special Methods 72

3.3.1 Methods for standard library functions 72

3.3.2 Basic customization 73

3.3.3 Container customization 75

3.3.4 Arithmetic customization 76

3.3.4.3 Unary operations 79

3.4 Array indexing 80

3.4.1 Basic Slicing 80

3.4.2 Advanced selection 82

3.4.2.1 Integer 82

3.4.3 Flat Iterator indexing 85

4 Basic Routines 86

4.1 Creating arrays 86

4.2 Operations on two or more arrays 91

4.3 Printing arrays 94

4.4 Functions redundant with methods 95

4.5 Dealing with data types 96

5 Additional Convenience Routines 98

5.1 Shape functions 98

5.2 Basic functions 102

5.3 Polynomial functions 110

5.4 Set Operations 113

5.5 Array construction using index tricks 114

5.6 Other indexing devices 117

5.7 Two-dimensional functions 118

5.8 More data type functions 120

5.9 Functions that behave like ufuncs 123

5.10 Miscellaneous Functions 123

5.11 Utility functions 126

6 Scalar objects 128

6.1 Attributes of array scalars 129

6.2 Methods of array scalars 131

6.3 Defining New Types 132

7 Data-type (dtype) Objects 133

7.1 Attributes 134

7.2 Construction 136

7.3 Methods 139

8 Standard Classes 141

8.1 Special attributes and methods recognized by NumPy 142

8.2 Matrix Objects 143

8.3 Memory-mapped-file arrays 145

8.4 Character arrays (numpy.char) 146

8.5 Record Arrays (numpy.rec) 147

8.6 Masked Arrays (numpy.ma) 151

8.7 Standard container class 152

8.8 Array Iterators 152

8.8.1 Default iteration 153

8.8.2 Flat iteration 153

8.8.3 N-dimensional enumeration 154

8.8.4 Iterator for broadcasting 154

9 Universal Functions 156

9.1 Description 156

9.1.1 Broadcasting 157

9.1.2 Output type determination 157

9.1.3 Use of internal buffers 158

9.1.4 Error handling 158

9.1.5 Optional keyword arguments 159

9.2 Attributes 160

9.3 Casting Rules 161

9.4 Methods 162

9.4.1 Reduce 164

9.4.2 Accumulate 164

9.4.3 Reduceat 165

9.5 Available ufuncs 167

9.5.1 Math operations 167

9.5.2 Trigonometric functions 170

9.5.3 Bit-twiddling functions 171

9.5.4 Comparison functions 172

9.5.5 Floating functions 174

10 Basic Modules 177

10.1 Linear Algebra (linalg) 177

10.2 Discrete Fourier Transforms (fft) 180

10.3 Random Numbers (random) 184

10.3.1 Discrete Distributions 185

10.3.2 Continuous Distributions 187

10.3.3 Miscellaneous utilities 194

10.4 Matrix-specific functions (matlib) 194

10.5 Ctypes utiltity functions (ctypeslib) 194

11 Testing and Packaging 196

11.1 Testing 196

11.2 NumPy Distutils 199

11.2.1 misc.util 199

11.2.2 Other modules 206

11.3 Conversion of .src files 208

11.3.1 Fortran files 208

11.3.1.1 Named repeat rule 208

11.3.1.2 Short repeat rule 208

11.3.1.3 Pre-defined names 209

11.3.2 Other files 209

11 C-API 211

12 New Python Types and C-Structures 212

12.1 New Python Types Defined 213

12.1.2 PyArrayDescr.Type 215

12.1.3 PyUFunc.Type 223

12.1.4 PyArraylter.Type 226

12.1.5 PyArrayMultilter.Type 227

12.1.6 PyArrayFlags.Type 228

12.1.7 ScalarArrayTypes 228

12.2 Other C-Structures 229

12.2.2 Py Array .Chunk 230

12.2.3 PyArrayInterface 230

12.2.4 Internally used structures 232

12.2.4.1 PyUFuncLoopObject 232

12.2.4.2 PyUFuncReduceObject 232

12.2.4.3 PyUFUncXoopld 232

12.2.4.4 PyArrayMapIter.Type 232

13 Complete API 233

13.1 Configuration defines 233

13.1.1 Guaranteed to be defined 233

13.1.2 Possible defines 234

13.2 Array Data Types 235

13.2.1 Enumerated Types 235

13.2.2 Defines 236

13.2.2.1 Max and min values for integers 236

13.2.2.2 Number of bits in data types 236

13.2.2.3 Bit-width references to enumerated typenums . . . . 237

13.2.2.4 Integer that can hold a pointer 237

13.2.3 C-type names 237

13.2.3.1 Boolean 237

13.2.3.2 (Un)Signed Integer 237

13.2.3.3 (Complex) Floating point 238

13.2.3.4 Bit-width names 238

13.2.4 Printf Formatting 238

13.3 Array API 239

13.3.1 Array structure and data access 239

13.3.1.1 Data access 240

13.3.2 Creating arrays 241

13.3.2.1 From scratch 241

13.3.2.2 From other objects 244

13.3.3 Dealing with types 249

13.3.3.1 General check of Python Type 249

13.3.3.2 Data-type checking 251

13.3.3.3 Converting data types 254

13.3.3.4 New data types 256

13.3.3.5 Special functions for PyArray.OBJECT 257

13.3.4 Array flags 258

13.3.4.1 Basic Array Flags 258

13.3.4.2 Combinations of array flags 259

13.3.4.3 Flag-like constants 259

13.3.4.4 Flag checking 260

13.3.5 Array method alternative API 261

13.3.5.1 Conversion 261

13.3.5.2 Shape Manipulation 263

13.3.5.3 Item selection and manipulation 265

13.3.5.4 Calculation 268

13.3.6 Functions 270

13.3.6.1 Array Functions 270

13.3.6.2 Other functions 272

13.3.7 Array Iterators 273

13.3.8 Broadcasting (multi-iterators) 274

13.3.9 Array Scalars 276

13.3.10 Data-type descriptors 278

13.3.11 Conversion Utilities 280

13.3.11.1 For use with PyArg_ParseTuple 280

13.3.11.2 Other conversions 282

13.3.12 Miscellaneous 283

13.3.12.1 Importing the API 283

13.3.12.2 Internal Flexibility 284

13.3.12.3 Memory management 285

13.3.12.4 Threading support 285

13.3.12.5 Priority 287

13.3.12.6 Default buffers 287

13.3.12.7 Other constants 287

13.3.12.8 Miscellaneous Macros 288

13.3.12.9 Enumerated Types 289

13.4 UFunc API 289

13.4.1 Constants 289

13.4.2 Macros 290

13.4.3 Functions 290

13.4.4 Generic functions 293

13.5 Importing the API 295

14 How to extend NumPy 297

14.1 Writing an extension module 297

14.2 Required subroutine 298

14.3 Defining functions 299

14.3.1 Functions without keyword arguments 300

14.3.2 Functions with keyword arguments 301

14.3.3 Reference counting 302

14.4 Dealing with array objects 303

14.4.1 Converting an arbitrary sequence object 304

14.4.2 Creating a brand-new ndarray 307

14.4.3 Getting at ndarray memory and accessing elements of the ndarray 308

14.5 Example 309

15 Beyond the Basics 311

15.1 Iterating over elements in the array 311

15.1.1 Basic Iteration 311

15.1.2 Iterating over all but one axis 313

15.1.3 Iterating over multiple arrays 313

15.1.4 Broadcasting over multiple arrays 314

15.2 Creating a new universal function 315

15.3 User-defined data-types 318

15.3.1 Adding the new data-type 319

15.3.2 Registering a casting function 319

15.3.3 Registering coercion rules 320

15.3.4 Registering a ufunc loop 321

15.4 Subtyping the ndarray in C 322

15.4.1 Creating sub-types 322

15.4.2 Specific features of ndarray sub-typing 323

15.4.2.1 The „array_finalize_ method 323

15.4.2.2 The __array_priority_ attribute 324

16 Using Python as glue 325

16.1 Calling other compiled libraries from Python 326

16.2 Hand-generated wrappers 327

16.3 f2py 327

16.3.1 Creating source for a basic extension module 328

16.3.2 Creating a compiled extension module 328

16.3.3 Improving the basic interface 329

16.3.4 Inserting directives in Fortran source 330

16.3.5 A filtering example 331

16.3.6 Calling f2py from Python 332

16.3.7 Automatic extension module generation 333

16.3.8 Conclusion 333

16.4.1 Speed up code involving arrays (also see scipy.numexpr) . . . 334

16.4.3 Simplify creation of an extension module 337

16.4.4 Conclusion 338

16.5.1 Pyrex-add 340

16.5.2 Pyrex-filter 341

16.5.3 Conclusion 342

16.6 ctypes 343

16.6.1 Having a shared library 344

16.6.2 Loading the shared library 345

16.6.3 Converting arguments 346

16.6.4 Calling the function 347

16.6.5 Complete example 348

16.6.6 Conclusion 352

16.7 Additional tools you may find useful 353

16.7.1 SWIG 353

16.7.3 Boost Python 354

16.7.4 Instant 355

16.7.5 PyInline 356

16.7.6 PyFort 356

17 Code Explanations 357

17.1 Memory model 357

17.2 Data-type encapsulation 358

17.3 N-D Iterators 359

17.4 Broadcasting 359

17.5 Array Scalars 360

17.6 Advanced ("Fancy") Indexing 361

17.6.1 Fancy-indexing check 361

17.6.2 Fancy-indexing implementation 362

17.6.2.1 Creating the mapping object 362

17.6.2.2 Binding the mapping object 362

17.6.2.3 Getting (or Setting) 363

17.7 Universal Functions 363

17.7.1 Setup 364

17.7.2 Function call 365

17.7.2.1 One Loop 366

17.7.2.2 StridedLoop 366

17.7.2.3 Buffered Loop 366

17.7.3 Final output manipulation 367

17.7.4 Methods 367

17.7.4.2 Reduce 368

17.7.4.3 Accumulate 369

17.7.4.4 Reduceat 369

0 0

Post a comment