def randint(low, high, size, dtype=int64): """ Generate a pdarray with random values in a specified range. Parameters ---------- low : int The low value (inclusive) of the range high : int The high value (exclusive for int, inclusive for float) of the range size : int The length of the returned array dtype : {int64, float64, bool} The dtype of the array Returns ------- pdarray Values drawn uniformly from the specified range having the desired dtype Notes ----- Calling randint with dtype=float64 will result in uniform non-integral floating point values. Examples -------- >>> ak.randint(0, 10, 5) array([5, 7, 4, 8, 3]) >>> ak.randint(0, 1, 3, dtype=ak.float64) array([0.92176432277231968, 0.083130710959903542, 0.68894208386667544]) >>> ak.randint(0, 1, 5, dtype=ak.bool) array([True, False, True, True, True]) """ # TO DO: separate out into int and float versions # TO DO: float version should accept non-integer low and high if not all((np.isscalar(low), np.isscalar(high), np.isscalar(size))): raise TypeError("all arguments must be scalars") dtype = akdtype(dtype) # normalize dtype # check dtype for error if dtype.name not in DTypes: raise TypeError("unsupported dtype {}".format(dtype)) if isinstance(low, int) and isinstance(high, int) and isinstance( size, int): kind, itemsize = translate_np_dtype(dtype) repMsg = generic_msg("randint {} {} {} {}".format( low, high, size, dtype.name)) return create_pdarray(repMsg) else: raise TypeError("min,max,size must be int {} {} {}".format( low, high, size))
def randint(low, high, size, dtype=int64): """ Generate a pdarray with random values in a specified range. Parameters ---------- low : int The low value (inclusive) of the range high : int The high value (exclusive for int, inclusive for float) of the range size : int The length of the returned array dtype : {int64, float64, bool} The dtype of the array Returns ------- pdarray Values drawn uniformly from the specified range having the desired dtype Notes ----- Calling randint with dtype=float64 will result in uniform non-integral floating point values. Examples -------- >>> ak.randint(0, 10, 5) array([5, 7, 4, 8, 3]) >>> ak.randint(0, 1, 3, dtype=ak.float64) array([0.92176432277231968, 0.083130710959903542, 0.68894208386667544]) >>> ak.randint(0, 1, 5, dtype=ak.bool) array([True, False, True, True, True]) """ if not all((np.isscalar(low), np.isscalar(high), np.isscalar(size))): raise TypeError("all arguments must be scalars") if resolve_scalar_dtype(size) != 'int64': raise TypeError("size must be integer") if size < 0 or high < low: raise ValueError("Incompatible arguments") dtype = akdtype(dtype) # normalize dtype # check dtype for error if dtype.name not in DTypes: raise TypeError("unsupported dtype {}".format(dtype)) lowstr = NUMBER_FORMAT_STRINGS[dtype.name].format(low) highstr = NUMBER_FORMAT_STRINGS[dtype.name].format(high) sizestr = NUMBER_FORMAT_STRINGS['int64'].format(size) repMsg = generic_msg("randint {} {} {} {}".format(sizestr, dtype.name, lowstr, highstr)) return create_pdarray(repMsg)
def ones(size: int, dtype: type = float64) -> pdarray: """ Create a pdarray filled with ones. Parameters ---------- size : int Size of the array (only rank-1 arrays supported) dtype : {float64, int64, bool} Resulting array type, default float64 Returns ------- pdarray Ones of the requested size and dtype Raises ------ TypeError Raised if the supplied dtype is not supported or if the size parameter is neither an int nor a str that is parseable to an int. See Also -------- zeros, ones_like Examples -------- >>> ak.ones(5, dtype=ak.int64) array([1, 1, 1, 1, 1]) >>> ak.ones(5, dtype=ak.float64) array([1, 1, 1, 1, 1]) >>> ak.ones(5, dtype=ak.bool) array([True, True, True, True, True]) """ if not np.isscalar(size): raise TypeError("size must be a scalar, not {}".\ format(size.__class__.__name__)) dtype = akdtype(dtype) # normalize dtype # check dtype for error if cast(np.dtype, dtype).name not in numericDTypes: raise TypeError("unsupported dtype {}".format(dtype)) kind, itemsize = translate_np_dtype(dtype) repMsg = generic_msg("create {} {}".format( cast(np.dtype, dtype).name, size)) a = create_pdarray(cast(str, repMsg)) a.fill(1) return a
def zeros(size : int_scalars, dtype : type=np.float64) -> pdarray: """ Create a pdarray filled with zeros. Parameters ---------- size : int_scalars Size of the array (only rank-1 arrays supported) dtype : all_scalars Type of resulting array, default float64 Returns ------- pdarray Zeros of the requested size and dtype Raises ------ TypeError Raised if the supplied dtype is not supported or if the size parameter is neither an int nor a str that is parseable to an int. See Also -------- ones, zeros_like Examples -------- >>> ak.zeros(5, dtype=ak.int64) array([0, 0, 0, 0, 0]) >>> ak.zeros(5, dtype=ak.float64) array([0, 0, 0, 0, 0]) >>> ak.zeros(5, dtype=ak.bool) array([False, False, False, False, False]) """ if not np.isscalar(size): raise TypeError("size must be a scalar, not {}".\ format(size.__class__.__name__)) dtype = akdtype(dtype) # normalize dtype # check dtype for error if cast(np.dtype,dtype).name not in NumericDTypes: raise TypeError("unsupported dtype {}".format(dtype)) repMsg = generic_msg(cmd="create", args="{} {}".format( cast(np.dtype,dtype).name, size)) return create_pdarray(repMsg)
def ones(size, dtype=float64): """ Create a pdarray filled with ones. Parameters ---------- size : int Size of the array (only rank-1 arrays supported) dtype : {float64, int64, bool} Resulting array type, default float64 Returns ------- pdarray Ones of the requested size and dtype See Also -------- zeros, ones_like Examples -------- >>> ak.ones(5, dtype=ak.int64) array([1, 1, 1, 1, 1]) >>> ak.ones(5, dtype=ak.float64) array([1, 1, 1, 1, 1]) >>> ak.ones(5, dtype=ak.bool) array([True, True, True, True, True]) """ if not np.isscalar(size): raise TypeError("size must be a scalar, not {}".format(type(size))) dtype = akdtype(dtype) # normalize dtype # check dtype for error if dtype.name not in DTypes: raise TypeError("unsupported dtype {}".format(dtype)) kind, itemsize = translate_np_dtype(dtype) repMsg = generic_msg("create {} {}".format(dtype.name, size)) a = create_pdarray(repMsg) a.fill(1) return a
def zeros(size, dtype=np.float64): """ Create a pdarray filled with zeros. Parameters ---------- size : int Size of the array (only rank-1 arrays supported) dtype : {float64, int64, bool} Type of resulting array, default float64 Returns ------- pdarray Zeros of the requested size and dtype See Also -------- ones, zeros_like Examples -------- >>> ak.zeros(5, dtype=ak.int64) array([0, 0, 0, 0, 0]) >>> ak.zeros(5, dtype=ak.float64) array([0, 0, 0, 0, 0]) >>> ak.zeros(5, dtype=ak.bool) array([False, False, False, False, False]) """ if not np.isscalar(size): raise TypeError("size must be a scalar, not {}".format(type(size))) dtype = akdtype(dtype) # normalize dtype # check dtype for error if dtype.name not in DTypes: raise TypeError("unsupported dtype {}".format(dtype)) kind, itemsize = translate_np_dtype(dtype) repMsg = generic_msg("create {} {}".format(dtype.name, size)) return create_pdarray(repMsg)
def randint(low: Union[int, float], high: Union[int, float], size: int, dtype=int64, seed: Union[None, int] = None) -> pdarray: """ Generate a pdarray of randomized int, float, or bool values in a specified range. Parameters ---------- low : Union[int,float] The low value (inclusive) of the range high : Union[int,float] The high value (exclusive for int, inclusive for float) of the range size : int The length of the returned array dtype : {int64, float64, bool} The dtype of the array Returns ------- pdarray Values drawn uniformly from the specified range having the desired dtype Raises ------ TypeError Raised if dtype.name not in DTypes, size is not an int, low or if not a scalar ValueError Raised if size < 0 or if high < low Notes ----- Calling randint with dtype=float64 will result in uniform non-integral floating point values. Examples -------- >>> ak.randint(0, 10, 5) array([5, 7, 4, 8, 3]) >>> ak.randint(0, 1, 3, dtype=ak.float64) array([0.92176432277231968, 0.083130710959903542, 0.68894208386667544]) >>> ak.randint(0, 1, 5, dtype=ak.bool) array([True, False, True, True, True]) """ if not all((np.isscalar(low), np.isscalar(high), np.isscalar(size))): raise TypeError("all arguments must be scalars") if resolve_scalar_dtype(size) != 'int64': raise TypeError("The size parameter must be an integer") if resolve_scalar_dtype(low) not in RANDINT_TYPES: raise TypeError("The low parameter must be an integer or float") if resolve_scalar_dtype(high) not in RANDINT_TYPES: raise TypeError("The high parameter must be an integer or float") if size < 0 or high < low: raise ValueError("size must be > 0 and high > low") dtype = akdtype(dtype) # normalize dtype # check dtype for error if dtype.name not in DTypes: raise TypeError("unsupported dtype {}".format(dtype)) lowstr = NUMBER_FORMAT_STRINGS[dtype.name].format(low) highstr = NUMBER_FORMAT_STRINGS[dtype.name].format(high) sizestr = NUMBER_FORMAT_STRINGS['int64'].format(size) repMsg = generic_msg("randint {} {} {} {} {}".\ format(sizestr, dtype.name, lowstr, highstr, seed)) return create_pdarray(cast(str, repMsg))
def randint(low: Union[int, float], high: Union[int, float], size: int, dtype=int64, seed: int = None) -> pdarray: """ Generate a pdarray of randomized int, float, or bool values in a specified range bounded by the low and high parameters. Parameters ---------- low : Union[int,float] The low value (inclusive) of the range high : Union[int,float] The high value (exclusive for int, inclusive for float) of the range size : int The length of the returned array dtype : {int64, float64, bool} The dtype of the array seed : int Index for where to pull the first returned value Returns ------- pdarray Values drawn uniformly from the specified range having the desired dtype Raises ------ TypeError Raised if dtype.name not in DTypes, size is not an int, low or high is not an int or float, or seed is not an int ValueError Raised if size < 0 or if high < low Notes ----- Calling randint with dtype=float64 will result in uniform non-integral floating point values. Examples -------- >>> ak.randint(0, 10, 5) array([5, 7, 4, 8, 3]) >>> ak.randint(0, 1, 3, dtype=ak.float64) array([0.92176432277231968, 0.083130710959903542, 0.68894208386667544]) >>> ak.randint(0, 1, 5, dtype=ak.bool) array([True, False, True, True, True]) >>> ak.randint(1, 5, 10, seed=2) array([4, 3, 1, 3, 4, 4, 2, 4, 3, 2]) >>> ak.randint(1, 5, 3, dtype=ak.float64, seed=2) array([2.9160772326374946, 4.353429832157099, 4.5392023718621486]) >>> ak.randint(1, 5, 10, dtype=ak.bool, seed=2) array([False, True, True, True, True, False, True, True, True, True]) """ if size < 0 or high < low: raise ValueError("size must be > 0 and high > low") dtype = akdtype(dtype) # normalize dtype # check dtype for error if dtype.name not in DTypes: raise TypeError("unsupported dtype {}".format(dtype)) lowstr = NUMBER_FORMAT_STRINGS[dtype.name].format(low) highstr = NUMBER_FORMAT_STRINGS[dtype.name].format(high) sizestr = NUMBER_FORMAT_STRINGS['int64'].format(size) repMsg = generic_msg("randint {} {} {} {} {}".\ format(sizestr, dtype.name, lowstr, highstr, seed)) return create_pdarray(repMsg)