Advertisement
Dmitrey15

Untitled

Jan 23rd, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.23 KB | None | 0 0
  1. try:
  2.     import numpypy as N
  3. except:
  4.     import numpy as N
  5.  
  6. def empty_like(a, dtype=None, order='K', subok=True):
  7.     '''
  8.    Return a new array with the same shape and type as a given array.
  9.    
  10.    Parameters
  11.    ----------
  12.    a : array_like
  13.        The shape and data-type of `a` define these same attributes of the
  14.        returned array.
  15.    dtype : data-type, optional
  16.        Overrides the data type of the result.
  17.    order : {'C', 'F', 'A', or 'K'}, optional
  18.        Added for compatibility with cpython numpy, currently ignored.
  19.        In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
  20.        'F' means F-order, 'A' means 'F' if ``a`` is Fortran contiguous,
  21.        'C' otherwise. 'K' means match the layout of ``a`` as closely
  22.        as possible.
  23.    subok : bool, optional.
  24.        If True, then the newly created array will use the sub-class
  25.        type of 'a', otherwise it will be a base-class array. Defaults
  26.        to True.
  27.    
  28.    Returns
  29.    -------
  30.    out : ndarray
  31.        Array of uninitialized (arbitrary) data with the same
  32.        shape and type as `a`.
  33.    
  34.    See Also
  35.    --------
  36.    ones_like : Return an array of ones with shape and type of input.
  37.    zeros_like : Return an array of zeros with shape and type of input.
  38.    empty : Return a new uninitialized array.
  39.    ones : Return a new array setting values to one.
  40.    zeros : Return a new array setting values to zero.
  41.    
  42.    Notes
  43.    -----
  44.    This function does *not* initialize the returned array; to do that use
  45.    `zeros_like` or `ones_like` instead.  It may be marginally faster than
  46.    the functions that do set the array values.
  47.  
  48.    Examples
  49.    --------
  50.    >>> a = ([1,2,3], [4,5,6])                         # a is array-like
  51.    >>> np.empty_like(a)
  52.    array([[-1073741821, -1073741821,           3],    #random
  53.           [          0,           0, -1073741821]])
  54.    >>> a = np.array([[1., 2., 3.],[4.,5.,6.]])
  55.    >>> np.empty_like(a)
  56.    array([[ -2.00000715e+000,   1.48219694e-323,  -2.00000572e+000],#random
  57.           [  4.38791518e-305,  -2.00000715e+000,   4.17269252e-309]])
  58.    
  59.    '''
  60.     if hasattr(a, 'shape') and hasattr(a, 'dtype'):
  61.         # may be ndarray, matrix, sparse matrix or their subclass
  62.         r = N.empty(a.shape, a.dtype if dtype is None else dtype)
  63.         Class = a.__class__
  64.     elif isinstance(a, (list, tuple)):
  65.         # we can't use len(), because a may be list of lists or like that
  66.         tmp = N.asarray(a)
  67.         Class = tmp.__class__
  68.         r = N.empty(tmp.shape, tmp.dtype if dtype is None else dtype)
  69.     else:
  70.         r = N.empty((), type(a))
  71.         Class = a.__class__ if hasattr(a,'__class__') else object
  72.     if subok and issubclass(Class, N.ndarray) and a.__class__ != r.__class__:
  73.         assert 0, 'empty_like with ndarray subclass output is unimplemented yet'
  74.         #r.__class__ = a.__class__ and other tried tricks doesn't work yet
  75.     return r
  76.  
  77. def zeros_like(a, dtype=None, order='K', subok=True):
  78.     '''
  79.    zeros_like(a, dtype=None, order='K', subok=True)
  80.    Return an array of zeros with the same shape and type as a given array.
  81.    
  82.    With default parameters, is equivalent to ``a.copy().fill(0)``.
  83.    
  84.    Parameters
  85.    ----------
  86.    a : array_like
  87.        The shape and data-type of `a` define these same attributes of
  88.        the returned array.
  89.    dtype : data-type, optional
  90.        Overrides the data type of the result.
  91.    order : {'C', 'F', 'A', or 'K'}, optional
  92.        Added for compatibility with cpython numpy, currently ignored.
  93.        In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
  94.        'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
  95.        'C' otherwise. 'K' means match the layout of `a` as closely
  96.        as possible.
  97.    
  98.    Returns
  99.    -------
  100.    out : ndarray
  101.        Array of zeros with the same shape and type as `a`.
  102.    
  103.    See Also
  104.    --------
  105.    ones_like : Return an array of ones with shape and type of input.
  106.    empty_like : Return an empty array with shape and type of input.
  107.    zeros : Return a new array setting values to zero.
  108.    ones : Return a new array setting values to one.
  109.    empty : Return a new uninitialized array.
  110.    
  111.    Examples
  112.    --------
  113.    >>> x = np.arange(6)
  114.    >>> x = x.reshape((2, 3))
  115.    >>> x
  116.    array([[0, 1, 2],
  117.           [3, 4, 5]])
  118.    >>> np.zeros_like(x)
  119.    array([[0, 0, 0],
  120.           [0, 0, 0]])
  121.  
  122.    >>> y = np.arange(3, dtype=np.float)
  123.    >>> y
  124.    array([ 0.,  1.,  2.])
  125.    >>> np.zeros_like(y)
  126.    array([ 0.,  0.,  0.])
  127.    '''
  128.     res = empty_like(a, dtype=dtype, order=order, subok=subok)
  129.     res.fill(0)
  130.     return res
  131.  
  132. def ones_like(a, dtype=None, order='K', subok=True):
  133.     '''
  134.    ones_like(a, dtype=None, order='K', subok=True)
  135.    Return an array of ones with the same shape and type as a given array.
  136.    
  137.    With default parameters, is equivalent to ``a.copy().fill(0)``.
  138.    
  139.    Parameters
  140.    ----------
  141.    a : array_like
  142.        The shape and data-type of `a` define these same attributes of
  143.        the returned array.
  144.    dtype : data-type, optional
  145.        Overrides the data type of the result.
  146.    order : {'C', 'F', 'A', or 'K'}, optional
  147.        Added for compatibility with cpython numpy, currently ignored.
  148.        In CPython numpy it overrides the memory layout of the result. 'C' means C-order,
  149.        'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
  150.        'C' otherwise. 'K' means match the layout of `a` as closely
  151.        as possible.
  152.    
  153.    Returns
  154.    -------
  155.    out : ndarray
  156.        Array of ones with the same shape and type as `a`.
  157.    
  158.    See Also
  159.    --------
  160.    ones_like : Return an array of ones with shape and type of input.
  161.    empty_like : Return an empty array with shape and type of input.
  162.    zeros : Return a new array setting values to zero.
  163.    ones : Return a new array setting values to one.
  164.    empty : Return a new uninitialized array.
  165.    
  166.    Examples
  167.    --------
  168.    >>> x = np.arange(6)
  169.    >>> x = x.reshape((2, 3))
  170.    >>> x
  171.    array([[0, 1, 2],
  172.           [3, 4, 5]])
  173.    >>> np.ones_like(x)
  174.    array([[1, 1, 1],
  175.           [1, 1, 1]])
  176.  
  177.    >>> y = np.arange(3, dtype=np.float)
  178.    >>> y
  179.    array([ 0.,  1.,  2.])
  180.    >>> np.ones_like(y)
  181.    array([ 1.,  1.,  1.])
  182.    '''
  183.     res = empty_like(a, dtype=dtype, order=order, subok=subok)
  184.     res.fill(1)
  185.     return res
  186.  
  187. if __name__ == '__main__':
  188.     for a in [N.array(1), N.array([1, 2, 3]), N.array([[2.0, 4.0],[3,6]]),  N.ones((3, 4, 5)), N.zeros((3, 4, 5, 6))]:
  189.         b = empty_like(a)
  190.         assert b.shape == a.shape and a.dtype == b.dtype
  191.         b = ones_like(a)
  192.         assert b.shape == a.shape and a.dtype == b.dtype
  193.         b = zeros_like(a)
  194.         assert b.shape == a.shape and a.dtype == b.dtype
  195.     for a in [1, 1.0, N.int16(1), N.float32(1)]:
  196.         b = empty_like(a)
  197.         # b.dtype is not equal to type(a) for a=1, because b.dtype is int64 while type(a) is int
  198.         assert b.shape == () and b.size == 1
  199.         b = ones_like(a)
  200.         assert b.shape == () and b.size == 1
  201.         b = zeros_like(a)
  202.         assert b.shape == ()  and b.size == 1
  203.     print('passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement