;ò
ÎÑAHc           @   sÅ  d  Z  d k l Z d e i d Z e i Z d d d d d d	 d
 d d d d d d d d d d d d d d d d d g Z d k Z d k	 Z	 d k
 Z
 d k Z d k Z d k Te d g ƒ d d  Z Z Z e	 i d! ƒ Z d" Z d# „  Z d$ „  Z d  d% „ Z d  d& „ Z d' „  Z d( „  Z d) „  Z d* „  Z d+ „  Z d, „  Z d- „  Z d. d/ „ Z e  e  d0 „ Z! d1 „  Z" d. d2 d3 „ Z# d. d  d4 „ Z$ d5 d6 „ Z% e	 i d7 ƒ d8 „ Z& d9 „  Z' d: „  Z( d S(;   sä   
A set of convenient utilities for numerical work.

Most of this module requires Numerical Python or is meant to be used with it.
See http://www.pfdubois.com/numpy for details.

$Id: numutils.py 958 2005-12-27 23:17:51Z fperez $(   s   Releases   %s <%s>s   Fernandos   sum_flats	   mean_flats   rms_flats	   base_reprs   binary_reprs   amins   amaxs   amaps
   zeros_likes
   empty_likes   franges   diagonal_matrixs   identitys   fromfunction_kws   log2s   ispower2s   norms   l1norms   l2norms   exp_safes   infs   inftys   Infinitys   NumericN(   s   *i   f0.0i    f2.2250738585072014e-308f1.7976931348623157e+308c         C   s>   t  |  ƒ t j o t t |  t t ƒ ƒ Sn t i |  ƒ Sd S(   sÐ   Compute exponentials which safely underflow to zero.

    Slow but convenient to use. Note that NumArray will introduce proper
    floating point exception handling with access to the underlying
    hardware.N(   s   types   xs	   ArrayTypes   exps   clips   exp_safe_MINs   exp_safe_MAXs   math(   s   x(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   exp_safe4   s     c         G   s   t  t |  | Œ ƒ Sd S(   s¨   amap(function, sequence[, sequence, ...]) -> array.

    Works like map(), but it returns an array.  This is just a convenient
    shorthand for Numeric.array(map(...))N(   s   arrays   maps   fns   args(   s   fns   args(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   amap@   s     c         C   s   t  i t |  ƒ | ƒ Sd S(   sB   amin(m,axis=0) returns the minimum of m along dimension axis.
    N(   s   minimums   reduces   asarrays   ms   axis(   s   ms   axis(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   aminG   s     c         C   s   t  i t |  ƒ | ƒ Sd S(   sB   amax(m,axis=0) returns the maximum of m along dimension axis.
    N(   s   maximums   reduces   asarrays   ms   axis(   s   ms   axis(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   amaxL   s     c         C   s   t  |  i |  i ƒ  ƒ Sd S(   sÈ   Return an array of zeros of the shape and typecode of a.

    If you don't explicitly need the array to be zeroed, you should instead
    use empty_like(), which is faster as it only allocates memory.N(   s   zeross   as   shapes   typecode(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys
   zeros_likeQ   s     c         C   s   t  |  i |  i ƒ  ƒ Sd S(   s  Return an empty (uninitialized) array of the shape and typecode of a.

    Note that this does NOT initialize the returned array.  If you require
    your array to be initialized, you should use zeros_like().

    This requires Numeric.empty(), which appeared in Numeric 23.7.N(   s   emptys   as   shapes   typecode(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys
   empty_likeY   s     c         C   s8   |  i ƒ  o t i |  i ƒ Sn t i t |  ƒ ƒ Sd S(   s„   Return the sum of all the elements of a, flattened out.

    It uses a.flat, and if a is not contiguous, a call to ravel(a) is made.N(   s   as   iscontiguouss   Numerics   sums   flats   ravel(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   sum_flatc   s     c         C   s   t  |  ƒ t t |  ƒ ƒ Sd S(   s8   Return the mean of all the elements of a, flattened out.N(   s   sum_flats   as   floats   size(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys	   mean_flatm   s     c         C   s1   t  i t t |  ƒ d ƒ t t |  ƒ ƒ ƒ Sd S(   sD   Return the root mean square of all the elements of a, flattened out.i   N(   s   maths   sqrts   sum_flats   absolutes   as   floats   size(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   rms_flatr   s     c         C   s   t  t |  ƒ ƒ Sd S(   s¥   Return the l1 norm of a, flattened out.

    Implemented as a separate function (not a call to norm() for speed).

    Ref: http://mathworld.wolfram.com/L1-Norm.htmlN(   s   sum_flats   absolutes   a(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   l1normw   s     c         C   s!   t  i t t |  ƒ d ƒ ƒ Sd S(   s¥   Return the l2 norm of a, flattened out.

    Implemented as a separate function (not a call to norm() for speed).

    Ref: http://mathworld.wolfram.com/L2-Norm.htmli   N(   s   maths   sqrts   sum_flats   absolutes   a(   s   a(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   l2norm€   s     i   c         C   sJ   | d d f j o t t |  ƒ i ƒ Sn t t |  ƒ | ƒ d | Sd S(   sˆ  norm(a,p=2) -> l-p norm of a.flat

    Return the l-p norm of a, considered as a flat array.  This is NOT a true
    matrix norm, since arrays of arbitrary rank are always flattened.

    p can be a number or one of the strings ('inf','Infinity') to get the
    L-infinity norm.

    Ref: http://mathworld.wolfram.com/VectorNorm.html
         http://mathworld.wolfram.com/L-Infinity-Norm.htmls   infs   Infinityf1.0N(   s   ps   maxs   absolutes   as   flats   sum_flat(   s   as   p(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   norm‰   s    
 c         K   sÈ   | i d d ƒ | d d j } | t j o |  d } d }  n | t j o
 d } n y& | d } | |  t | | ƒ } Wn1 t	 j
 o% t
 t | |  | | ƒ ƒ } n Xt | ƒ | |  Sd S(   s&  frange([start,] stop[, step, keywords]) -> array of floats

    Return a Numeric array() containing a progression of floats. Similar to
    arange(), but defaults to a closed interval.

    frange(x0, x1) returns [x0, x0+1, x0+2, ..., x1]; start defaults to 0, and
    the endpoint *is included*. This behavior is different from that of
    range() and arange(). This is deliberate, since frange will probably be
    more useful for generating lists of points for function evaluation, and
    endpoints are often desired in this use. The usual behavior of range() can
    be obtained by setting the keyword 'closed=0', in this case frange()
    basically becomes arange().

    When step is given, it specifies the increment (or decrement). All
    arguments can be floating point numbers.

    frange(x0,x1,d) returns [x0,x0+d,x0+2d,...,xfin] where xfin<=x1.

    frange can also be called with the keyword 'npts'. This sets the number of
    points the list should contain (and overrides the value 'step' might have
    been given). arange() doesn't offer this option.

    Examples:
    >>> frange(3)
    array([ 0.,  1.,  2.,  3.])
    >>> frange(3,closed=0)
    array([ 0.,  1.,  2.])
    >>> frange(1,6,2)
    array([1, 3, 5])
    >>> frange(1,6.5,npts=5)
    array([ 1.   ,  2.375,  3.75 ,  5.125,  6.5  ])
    s   closedi   i    f0.0f1.0s   nptsN(   s   kws
   setdefaults   endpoints   xfins   Nones   xinis   deltas   nptss   floats   KeyErrors   ints   rounds   arange(   s   xinis   xfins   deltas   kws   endpoints   npts(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   frangeš   s      



#c         C   s   |  t t |  ƒ ƒ Sd S(   sW   Return square diagonal matrix whose non-zero elements are given by the
    input array.N(   s   diags   identitys   len(   s   diag(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   diagonal_matrixÔ   s     s   lc         C   sO   t  |  f | d | ƒ} x+ t |  ƒ D] } | f | } d | | <q& W| Sd S(   s×  identity(n,r) returns the identity matrix of shape (n,n,...,n) (rank r).

    For ranks higher than 2, this object is simply a multi-index Kronecker
    delta:
                        /  1  if i0=i1=...=iR,
    id[i0,i1,...,iR] = -|
                        \  0  otherwise.

    Optionally a typecode may be given (it defaults to 'l').

    Since rank defaults to 2, this function behaves in the default case (when
    only n is given) like the Numeric identity function.s   typecodei   N(   s   zeross   ns   ranks   typecodes   idens   ranges   is   idx(   s   ns   ranks   typecodes   idens   idxs   i(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   identityÚ   s      c         C   s¨   d } |  | j  o" | d | d | t |  ƒ Sn t t i |  ƒ t i | ƒ ƒ } t | ƒ | } t |  | ƒ } | | t |  | | | t | d | ƒ ƒ Sd S(   s8   Return the representation of a number in any given base.s$   0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZi   i    N(   s   charss   numbers   bases   paddings   ints   maths   logs   max_exponents   longs	   max_powers
   lead_digits	   base_reprs   max(   s   numbers   bases   paddings
   lead_digits   charss   max_exponents	   max_power(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys	   base_reprî   s     ""i  c         C   s²   |  d | >j  p t ‚ t t i | |  g t | d d d ƒ ƒ } t t i | | d g ƒ } | i
 d ƒ o d Sn | | i d ƒ } d i t t | ƒ ƒ i d d ƒ Sd S(	   s7  Return the binary representation of the input number as a string.

    This is more efficient than using base_repr with base 2.

    Increase the value of max_length for very large numbers. Note that on
    32-bit machines, 2**1023 is the largest integer power of 2 which can be
    converted to a Python float.l    i   iÿÿÿÿi   i    s    s   LN(   s   numbers
   max_lengths   AssertionErrors   maps   operators   rshifts   ranges   shiftss   mods   digitss   counts   indexs   joins   reprs   replace(   s   numbers
   max_lengths   digitss   shifts(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   binary_reprú   s      f2.0c         C   sq   y t  |  ƒ d } Wn* t t f j
 o t i |  ƒ | Sn. Xd | j o t i |  ƒ | Sn t | ƒ Sd S(   s«   Return the log(x) in base 2.
    
    This is a _slow_ function but which is guaranteed to return the correct
    integer value if the input is an ineger exact power of 2.i   s   1N(	   s   binary_reprs   xs   bin_ns   AssertionErrors	   TypeErrors   maths   logs   ln2s   len(   s   xs   ln2s   bin_n(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   log2  s     c         C   s3   t  |  ƒ d } d | j o d Sn t | ƒ Sd S(   sŒ   Returns the log base 2 of n if n is a power of 2, zero otherwise.

    Note the potential ambiguity if n==1: 2**0==1, interpret accordingly.i   s   1i    N(   s   binary_reprs   ns   bin_ns   len(   s   ns   bin_n(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   ispower2  s
     c         K   s   |  t t | ƒ ƒ |  Sd S(   sG  Drop-in replacement for fromfunction() from Numerical Python.
 
    Allows passing keyword arguments to the desired function.

    Call it as (keywords are optional):
    fromfunction_kw(MyFunction, dimensions, keywords)

    The function MyFunction() is responsible for handling the dictionary of
    keywords it will recieve.N(   s   functions   tuples   indicess
   dimensionss   kwargs(   s   functions
   dimensionss   kwargs(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   fromfunction_kw&  s    	 ()   s   __doc__s   IPythons   Releases   authorss
   __author__s   licenses   __license__s   __all__s   __main__s   maths   operators   syss   Numerics   arrays   infs   inftys   Infinitys   logs   exp_safe_MINs   exp_safe_MAXs   exp_safes   amaps   amins   amaxs
   zeros_likes
   empty_likes   sum_flats	   mean_flats   rms_flats   l1norms   l2norms   norms   Nones   franges   diagonal_matrixs   identitys	   base_reprs   binary_reprs   log2s   ispower2s   fromfunction_kw("   s   exp_safes   exp_safe_MINs   __main__s   operators   infs   rms_flats   binary_reprs   fromfunction_kws   franges   Infinitys   __all__s
   empty_likes   ispower2s   maths   inftys   log2s	   mean_flats   __license__s
   zeros_likes
   __author__s   syss   l2norms   amaps   Releases   sum_flats   diagonal_matrixs   amaxs   identitys   amins   exp_safe_MAXs   Numerics	   base_reprs   norms   l1norm(    (    sA   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/numutils.pys   ?   sB   		N
									
	
						:		