@@ -3701,19 +3701,23 @@ def arange_fill(inputs, output, offset):
37013701 start , _ , step = inputs
37023702 start += offset [0 ] * step
37033703 stop = start + lout * step
3704- # use linspace to have finer control over exclusion of endpoint
3705- output [:] = np .linspace (start , stop , num = lout , endpoint = False , dtype = output .dtype )
3704+ if (stop - start ) // step == lout : # USE ARANGE IF POSSIBLE (2X FASTER)
3705+ output [:] = np .arange (start , stop , step , dtype = output .dtype )
3706+ else : # use linspace to have finer control over exclusion of endpoint for float types
3707+ output [:] = np .linspace (start , stop , lout , endpoint = False , dtype = output .dtype )
3708+
3709+ NUM = int ((stop - start ) / step )
37063710
37073711 if step is None : # not array-api compliant but for backwards compatibility
37083712 step = 1
37093713 if stop is None :
37103714 stop = start
37113715 start = 0
3712- if not shape :
3713- shape = (int (( stop - start ) / step ),)
3716+ if shape is None :
3717+ shape = (max ( NUM , 0 ),)
37143718 else :
37153719 # Check that the shape is consistent with the start, stop and step values
3716- if math .prod (shape ) != int (( stop - start ) / step ) :
3720+ if math .prod (shape ) != NUM :
37173721 raise ValueError ("The shape is not consistent with the start, stop and step values" )
37183722 if dtype is None :
37193723 dtype = (
@@ -3723,13 +3727,10 @@ def arange_fill(inputs, output, offset):
37233727 )
37243728 dtype = _check_dtype (dtype )
37253729
3726- if is_inside_new_expr ():
3730+ if is_inside_new_expr () or NUM < 0 :
37273731 # We already have the dtype and shape, so return immediately
37283732 return blosc2 .zeros (shape , dtype = dtype )
37293733
3730- if (stop - start ) / step < 0 : # return empty array
3731- return blosc2 .empty ((0 ,), dtype = dtype )
3732-
37333734 lshape = (math .prod (shape ),)
37343735 lazyarr = blosc2 .lazyudf (arange_fill , (start , stop , step ), dtype = dtype , shape = lshape )
37353736
@@ -3795,26 +3796,25 @@ def linspace_fill(inputs, output, offset):
37953796 step = (stop - start ) / (num - 1 ) if endpoint and num > 1 else (stop - start ) / num
37963797 # Compute proper start and stop values for the current chunk
37973798 # except for 0th iter, have already included start_ in prev iter
3798- start_ = start + offset [0 ] * step if offset [ 0 ] == 0 else start + ( offset [ 0 ] + 1 ) * step
3799+ start_ = start + offset [0 ] * step
37993800 stop_ = start_ + lout * step
3800- if offset [0 ] + lout == num : # reached end
3801+ if offset [0 ] + lout == num : # reached end, include stop if necessary
38013802 output [:] = np .linspace (start_ , stop , lout , endpoint = endpoint , dtype = output .dtype )
3802- else : # always include start and stop
3803- output [:] = np .linspace (start_ , stop_ , lout , endpoint = True , dtype = output .dtype )
3804-
3805- if num < 0 :
3806- raise ValueError ("num must be nonnegative." )
3803+ else :
3804+ output [:] = np .linspace (start_ , stop_ , lout , endpoint = False , dtype = output .dtype )
38073805
3808- if shape is None and num is None :
3809- raise ValueError ("Either `shape` or `num` must be specified." )
3810- if shape is None : # num is not None
3806+ if shape is None :
3807+ if num is None :
3808+ raise ValueError ("Either `shape` or `num` must be specified." )
3809+ # num is not None
38113810 shape = (num ,)
3812- else : # num is none
3813- num = math .prod (shape )
3814-
3815- # check compatibility of shape and num
3811+ else :
3812+ num = math .prod (shape ) if num is None else num
3813+ # check compatibility of shape and num
38163814 if math .prod (shape ) != num :
38173815 raise ValueError ("The specified shape is not consistent with the specified num value" )
3816+ if num < 0 :
3817+ raise ValueError ("num must be nonnegative." )
38183818
38193819 if dtype is None :
38203820 dtype = (
@@ -3829,10 +3829,8 @@ def linspace_fill(inputs, output, offset):
38293829 # We already have the dtype and shape, so return immediately
38303830 return blosc2 .zeros (shape , dtype = dtype ) # will return empty array for num == 0
38313831
3832- lshape = (math .prod (shape ),)
3833-
38343832 inputs = (start , stop , num , endpoint )
3835- lazyarr = blosc2 .lazyudf (linspace_fill , inputs , dtype = dtype , shape = lshape )
3833+ lazyarr = blosc2 .lazyudf (linspace_fill , inputs , dtype = dtype , shape = ( num ,) )
38363834 if len (shape ) == 1 :
38373835 # C order is guaranteed, and no reshape is needed
38383836 return lazyarr .compute (** kwargs )
0 commit comments