Table of Contents

Module: numpytools py4cs/numpytools.py

Some tools on top of NumPy. Transparent use of Numeric and numarray. The module also has some extensions to NumPy.

Imported modules   
import operator
import os
import sys
Functions   
_doctest
exp_robust
isequence
sequence
solve_tridiag_linear_system
test_ArrayGen
wrap2callable
  _doctest 
_doctest ()

  exp_robust 
exp_robust ( x )

Exponential function without over/under-flow. Works with arrays.

  isequence 
isequence (
        start=0,
        stop=None,
        inc=1,
        )

Generate integers from start to (and including) stop, with increment of inc. Alternative to range/xrange.

  sequence 
sequence (
        min=0.0,
        max=None,
        inc=1.0,
        type=Float,
        return_type='NumPyArray',
        )

Generate numbers from min to (and including) max, with increment of inc. Alternative to arange/arrayrange.

  solve_tridiag_linear_system 
solve_tridiag_linear_system ( A,  b )

Solve a tridiagonal linear system of the form A[0,1]*x[0] + A[0,2]*x[1] = 0 A[1,0]*x[0] + A[1,1]*x[1] + A[1,2]*x[2] = 0 ... ... A[k,0]*x[k-1] + A[k,1]*x[k] + A[k,2]*x[k+1] = 0 ... A[n-2,0]*x[n-3] + A[n-2,1]*x[n-2] + A[n-2,2]*x[n-1] = 0 ... A[n-1,0]*x[n-2] + A[n-1,1]*x[n-1] = 0

That is, the diagonal is stored in A[:,1], the subdiagonal is stored in A[1:,0], and the superdiagonal is stored in A[:n-2,2].

The storage is not memory friendly in Python/C (diagonals in the columns of A, but when A is sent to F77 for high-performance computing, a copy is taken and the F77 routine works with the same algorithm and hence optimal Fortran storage.

  test_ArrayGen 
test_ArrayGen ()

  wrap2callable 
wrap2callable ( f,  **kwargs )

Allow constants, string formulas, discrete data points, user-defined functions and (callable) classes to be wrapped in a new callable function. That is, all the mentioned data structures can be used as a function, usually of space and/or time. (kwargs is used for string formulas)

>>> from py4cs.numpytools import >>> f1 = wrap2callable(2.0) >>> f1(0.5) 2.0 >>> f2 = wrap2callable('1+2x') >>> f2(0.5) 2.0 >>> f3 = wrap2callable('1+2t', independent_variables=t) >>> f3(0.5) 2.0 >>> f4 = wrap2callable('a+bt') >>> f4(0.5) Traceback (most recent call last): ... NameError: name a is not defined >>> f4 = wrap2callable(a+b*t, independent_variables=t, a=1, b=2) >>> f4(0.5) 2.0

>>> x = seq(0,1,0.5); y=1+2x >>> f5 = wrap2callable((x,y)) >>> f5(0.5) 2.0 >>> def myfunc(x): return 1+2x >>> f6 = wrap2callable(myfunc) >>> f6(0.5) 2.0 >>> f7 = wrap2callable(lambda x: 1+2x) >>> f7(0.5) 2.0 >>> class MyClass: def __call__(self, x): return 1+2x

>>> myclass = MyClass() >>> f8 = wrap2callable(myclass) >>> f8(0.5) 2.0 >>> # 3D functions: >>> f9 = wrap2callable('1+2x+3y+4z', independent_variables=(x,y,z)) >>> f9(0.5,1/3.,0.25) 4.0 >>> # discrete 3D data: >>> y = seq(0,1,0.5); z = seq(-1,0.5,0.1) >>> xc = x[:,NewAxis,NewAxis]; yc = y[NewAxis,:,NewAxis] >>> zc = z[NewAxis,NewAxis,:] >>> def myfunc3(x,y,z): return 1+2x+3y+4z

>>> values = myfunc3(xc,yc,zc) >>> f10 = wrap2callable((x,y,z,values)) >>> f10(0.5,1/3.,0.25) 4.0 >>>

Exceptions   
TypeError, 'f of type %s is not callable' % type( f )
Classes   

ArrayGen

NoneArrayObject

Generalization of None for arrays.

WrapDiscreteData2Callable

Turn discrete data on a uniform grid into a callable function,

WrapNo2Callable

Turn a number (constant) into a callable function.


Table of Contents

This document was automatically generated on Sun Mar 14 15:31:01 2004 by HappyDoc version 2.1