Advertisement
Dmitrey15

Untitled

Jan 18th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. try:
  2.     import numpypy as N
  3. except:
  4.     import numpy as N
  5.  
  6. def empty_like(a):
  7.     '''
  8.    returns empty array of same shape and type to a.
  9.    Elements of the array may be arbitrary, different from zero.
  10.    
  11.    Parameters
  12.    ----------
  13.    a : array-like
  14.        Input ndarray (or its subclass), list, typle or number.
  15.        
  16.    Returns
  17.    -------
  18.    out : ndarray
  19.        ndarray of same shape and type to `a`.
  20.  
  21.    Examples
  22.    --------
  23.    empty_like(array([24, 20]))
  24.    empty_like(array([[2.0, 4.0],[3,6]]))
  25.    empty_like([[2.0, 4.0],[3,6]])
  26.    '''
  27.     if hasattr(a, 'shape') and hasattr(a, 'dtype'):
  28.         # may be ndarray, matrix, sparse matrix or their subclass
  29.         return N.empty(a.shape, a.dtype)
  30.    
  31.     if isinstance(a, (list, tuple)):
  32.         tmp = N.asarray(a)
  33.         # we can't use len(), because a may be list of lists or like that
  34.         return N.empty(tmp.shape, tmp.dtype)
  35.    
  36.     # TODO: if not isscalar - raise bug
  37.     # however, isscalar is unimplemented yet
  38.     #assert type(a) in (float, int), 'this type is unimplemented for empty_like yet'
  39.     return N.empty(1, type(a))
  40.  
  41.  
  42. def ones_like(a):
  43.     '''
  44.    returns array of ones with same shape and type to a.
  45.    Elements of the array may be arbitrary, different from zero.
  46.    
  47.    Parameters
  48.    ----------
  49.    a : array-like
  50.        Input ndarray (or its subclass), list, typle or number.
  51.        
  52.    Returns
  53.    -------
  54.    out : ndarray
  55.        ndarray of same shape and type to `a`.
  56.  
  57.    Examples
  58.    --------
  59.    ones_like(array([24, 20]))
  60.    ones_like(array([[2.0, 4.0],[3,6]]))
  61.    ones_like([[2.0, 4.0],[3,6]])
  62.    '''
  63.     if hasattr(a, 'shape') and hasattr(a, 'dtype'):
  64.         # may be ndarray, matrix, sparse matrix or their subclass
  65.         return N.ones(a.shape, a.dtype)
  66.    
  67.     if isinstance(a, (list, tuple)):
  68.         tmp = N.asarray(a)
  69.         # we can't use len(), because a may be list of lists or like that
  70.         return N.ones(tmp.shape, tmp.dtype)
  71.    
  72.     # TODO: if not isscalar - raise bug
  73.     #assert type(a) in (float, int), 'this type is unimplemented for ones_like yet'
  74.     return N.ones(1, type(a))
  75.  
  76. def zeros_like(a):
  77.     '''
  78.    returns array of zeros with same shape and type to a.
  79.    Elements of the array may be arbitrary, different from zero.
  80.    
  81.    Parameters
  82.    ----------
  83.    a : array-like
  84.        Input ndarray (or its subclass), list, typle or number.
  85.        
  86.    Returns
  87.    -------
  88.    out : ndarray
  89.        ndarray of same shape and type to `a`.
  90.  
  91.    Examples
  92.    --------
  93.    zeros_like(array([24, 20]))
  94.    zeros_like(array([[2.0, 4.0],[3,6]]))
  95.    zeros_like([[2.0, 4.0],[3,6]])
  96.    '''
  97.     if hasattr(a, 'shape') and hasattr(a, 'dtype'):
  98.         # may be ndarray, matrix, sparse matrix or their subclass
  99.         return N.zeros(a.shape, a.dtype)
  100.    
  101.     if isinstance(a, (list, tuple)):
  102.         tmp = N.asarray(a)
  103.         # we can't use len(), because a may be list of lists or like that
  104.         return N.zeros(tmp.shape, tmp.dtype)
  105.    
  106.     # TODO: if not isscalar - raise bug
  107.     #assert type(a) in (float, int), 'this type is unimplemented for zeros_like yet'
  108.     return N.zeros(1, type(a))
  109.  
  110.  
  111. if __name__ == '__main__':
  112.     for a in [N.array([1, 2, 3]), N.array([[2.0, 4.0],[3,6]]),  N.ones((3, 4, 5)), N.zeros((3, 4, 5, 6))]:
  113.         b = empty_like(a)
  114.         assert b.shape == a.shape and a.dtype == b.dtype
  115.         b = ones_like(a)
  116.         assert b.shape == a.shape and a.dtype == b.dtype
  117.         b = zeros_like(a)
  118.         assert b.shape == a.shape and a.dtype == b.dtype
  119.     for a in [1, 1.0]:
  120.         b = empty_like(a)
  121.         # b.dtype is not equal to type(a) for a=1, because b.dtype is int64 while type(a) is int
  122.         assert b.shape == (1, ) #and b.dtype == type(a)
  123.         b = ones_like(a)
  124.         assert b.shape == (1, ) #and b.dtype == type(a)
  125.         b = zeros_like(a)
  126.         assert b.shape == (1, ) #and b.dtype == type(a)
  127.     print('passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement