Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import numpypy as N
- except:
- import numpy as N
- def empty_like(a):
- '''
- returns empty array of same shape and type to a.
- Elements of the array may be arbitrary, different from zero.
- Parameters
- ----------
- a : array-like
- Input ndarray (or its subclass), list, typle or number.
- Returns
- -------
- out : ndarray
- ndarray of same shape and type to `a`.
- Examples
- --------
- empty_like(array([24, 20]))
- empty_like(array([[2.0, 4.0],[3,6]]))
- empty_like([[2.0, 4.0],[3,6]])
- '''
- if hasattr(a, 'shape') and hasattr(a, 'dtype'):
- # may be ndarray, matrix, sparse matrix or their subclass
- return N.empty(a.shape, a.dtype)
- if isinstance(a, (list, tuple)):
- tmp = N.asarray(a)
- # we can't use len(), because a may be list of lists or like that
- return N.empty(tmp.shape, tmp.dtype)
- # TODO: if not isscalar - raise bug
- # however, isscalar is unimplemented yet
- #assert type(a) in (float, int), 'this type is unimplemented for empty_like yet'
- return N.empty(1, type(a))
- def ones_like(a):
- '''
- returns array of ones with same shape and type to a.
- Elements of the array may be arbitrary, different from zero.
- Parameters
- ----------
- a : array-like
- Input ndarray (or its subclass), list, typle or number.
- Returns
- -------
- out : ndarray
- ndarray of same shape and type to `a`.
- Examples
- --------
- ones_like(array([24, 20]))
- ones_like(array([[2.0, 4.0],[3,6]]))
- ones_like([[2.0, 4.0],[3,6]])
- '''
- if hasattr(a, 'shape') and hasattr(a, 'dtype'):
- # may be ndarray, matrix, sparse matrix or their subclass
- return N.ones(a.shape, a.dtype)
- if isinstance(a, (list, tuple)):
- tmp = N.asarray(a)
- # we can't use len(), because a may be list of lists or like that
- return N.ones(tmp.shape, tmp.dtype)
- # TODO: if not isscalar - raise bug
- #assert type(a) in (float, int), 'this type is unimplemented for ones_like yet'
- return N.ones(1, type(a))
- def zeros_like(a):
- '''
- returns array of zeros with same shape and type to a.
- Elements of the array may be arbitrary, different from zero.
- Parameters
- ----------
- a : array-like
- Input ndarray (or its subclass), list, typle or number.
- Returns
- -------
- out : ndarray
- ndarray of same shape and type to `a`.
- Examples
- --------
- zeros_like(array([24, 20]))
- zeros_like(array([[2.0, 4.0],[3,6]]))
- zeros_like([[2.0, 4.0],[3,6]])
- '''
- if hasattr(a, 'shape') and hasattr(a, 'dtype'):
- # may be ndarray, matrix, sparse matrix or their subclass
- return N.zeros(a.shape, a.dtype)
- if isinstance(a, (list, tuple)):
- tmp = N.asarray(a)
- # we can't use len(), because a may be list of lists or like that
- return N.zeros(tmp.shape, tmp.dtype)
- # TODO: if not isscalar - raise bug
- #assert type(a) in (float, int), 'this type is unimplemented for zeros_like yet'
- return N.zeros(1, type(a))
- if __name__ == '__main__':
- 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))]:
- b = empty_like(a)
- assert b.shape == a.shape and a.dtype == b.dtype
- b = ones_like(a)
- assert b.shape == a.shape and a.dtype == b.dtype
- b = zeros_like(a)
- assert b.shape == a.shape and a.dtype == b.dtype
- for a in [1, 1.0]:
- b = empty_like(a)
- # b.dtype is not equal to type(a) for a=1, because b.dtype is int64 while type(a) is int
- assert b.shape == (1, ) #and b.dtype == type(a)
- b = ones_like(a)
- assert b.shape == (1, ) #and b.dtype == type(a)
- b = zeros_like(a)
- assert b.shape == (1, ) #and b.dtype == type(a)
- print('passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement