Skip to content

Commit 2a9370a

Browse files
committed
add arange constructor method
1 parent 60a25cb commit 2a9370a

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

DynamicVector/DynamicVector.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,112 @@ def zeros(cls, *args, size=None, dtype=None, capacity=None, grow_use_add=None, g
360360
dyn._size = size
361361
return dyn
362362

363+
@classmethod
364+
def arange(
365+
cls, *args, start=None, stop=None, step=None, dtype=None, capacity=None, grow_use_add=None, grow_add=None
366+
):
367+
"""
368+
Create a DynamicVector from evenly spaced values within a given interval
369+
and specified data type (dtype). If dtype is not given, then it is set to `np.int32`.
370+
371+
DynamicVector.zeros(start=1, stop, step=1, dtype=np.int32, capacity=8, *, grow_use_add=None, grow_add=None)
372+
373+
`arange` can be called with a varying number of positional arguments:
374+
375+
- `arange(stop)`: Values are generated within the half-open interval [0, stop)
376+
(in other words, the interval including start but excluding stop).
377+
- `arange(start, stop)`: Values are generated within the half-open interval [start, stop).
378+
- `arange(start, stop, step)` Values are generated within the half-open interval [start, stop),
379+
with spacing between values given by step.
380+
- `arange(start, stop, step, dtype)` Values are generated within the half-open interval [start, stop),
381+
with spacing between values given by step and has data type, dtype.
382+
383+
`arange` can also use keyword argument `start`, `stop`, `step` and `dtype`.
384+
However, if a positional argument of the same name is used, as described above,
385+
then only the keywords that are not one of the position arguments names can be used.
386+
Such as:
387+
388+
- `arange(stop)`: Cannot use the keyword `stop`.
389+
- `arange(start, stop)`: Cannot use the keywords `start`, `stop`.
390+
- `arange(start, stop, step)` Cannot use the keywords `start`, `stop`, `step`,.
391+
- `arange(start, stop, step, dtype)` Cannot use the keywords `start`, `stop`, `step`, `dtype`.
392+
393+
For integer arguments the function is roughly equivalent to the Python built-in range,
394+
but returns an DynamicVector rather than a range instance.
395+
396+
When using a non-integer step, such as 0.1, it is often better to use `DynamicVector.linspace`.
397+
398+
Parameters:
399+
start (int or float, optional): The starting value of the sequence, defaults to 0.
400+
stop (int or float): The end value of the sequence.
401+
step (int or float, optional): The increment (step size). Defaults to 1.
402+
dtype (type, optional): The type of the output values. Defaults to `np.int32`.
403+
404+
Returns:
405+
DynamicVector of evenly spaced dtype values from given start, stop, and step.
406+
407+
Examples:
408+
DynamicVector.arange(5) -> DynamicVector([0, 1, 2, 3, 4])
409+
DynamicVector.arange(2, 5) -> DynamicVector([2, 3, 4])
410+
DynamicVector.arange(2, 10, step=2) -> DynamicVector([2, 4, 6, 8])
411+
DynamicVector.arange(0, 5, step=0.5) -> DynamicVector([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5])
412+
"""
413+
narg = len(args)
414+
if narg > 5:
415+
raise TypeError(f"arange() takes from 0 to 5 positional arguments but {narg} were given")
416+
if narg == 0 and stop is None:
417+
raise TypeError("DynamicVector.arange() must specify at least one argument or use the `stop=` keyword.")
418+
if narg == 1 and stop is not None:
419+
raise TypeError("DynamicVector.arange() cannot specify 1 positional argument and the `stop=` keyword.")
420+
if narg == 2 and (stop is not None or start is not None):
421+
raise TypeError(
422+
"DynamicVector.arange() cannot specify 2 positional arguments and the `stop=` or `start=` keywords."
423+
)
424+
if narg == 3 and (stop is not None or start is not None or step is not None):
425+
raise TypeError(
426+
"DynamicVector.arange() cannot specify 3 positional arguments "
427+
"and the `stop=`, `start=`, or `step=` keywords."
428+
)
429+
if narg == 4 and (stop is not None or start is not None or step is not None or dtype is not None):
430+
raise TypeError(
431+
"DynamicVector.arange() cannot specify 4 positional arguments "
432+
"and the `stop=`, `start=`, `step=`, or `dtype=` keywords."
433+
)
434+
if narg == 5 and (
435+
stop is not None or start is not None or step is not None or dtype is not None or capacity is not None
436+
):
437+
raise TypeError(
438+
"DynamicVector.arange() cannot specify 5 positional arguments "
439+
"and the `stop=`, `start=`, `step=`, `dtype=`, or `capacity=` keywords."
440+
)
441+
# set up default values
442+
if start is None:
443+
start = 0
444+
if step is None:
445+
step = 1
446+
if dtype is None:
447+
dtype = np.int32
448+
if capacity is None:
449+
capacity = 8
450+
451+
if narg == 1:
452+
stop = args[0]
453+
elif narg == 2:
454+
start, stop = args
455+
elif narg == 3:
456+
start, stop, step = args
457+
elif narg == 4:
458+
start, stop, step, dtype, capacity = args
459+
460+
tmp = np.arange(start, stop, step, dtype)
461+
dim = tmp.size
462+
capacity = max(dim, capacity)
463+
464+
dyn = cls(dtype, capacity, grow_use_add=grow_use_add, grow_add=grow_add)
465+
dyn._size = dim
466+
dyn._data[:dim] = tmp
467+
return dyn
468+
363469
@property
364470
def size(self) -> int:
365471
"""Returns the current size of the vector."""

0 commit comments

Comments
 (0)