DynamicVector is a Python class designed to combine the flexibility of Python lists with the computational efficiency of NumPy arrays. It allows for dynamic resizing, list-like manipulation, and full access to NumPy’s powerful numerical operations.
- Dynamic Resizing: Automatically expands as new elements are appended or inserted, mimicking Python lists.
- NumPy Integration: Access to all NumPy array operations and methods via the
viewproperty. - List-Like Functionality: Supports common list operations such as append, insert, and pop, making it highly versatile.
- Optimized for Performance: Takes advantage of NumPy’s speed and memory efficiency for handling large datasets.
Ensure that numpy is installed in your environment. If not, you can install it using:
(note, this module was only tested against numpy>2.0)
pip install numpyTo install the module
pip install --upgrade git+https://github.com/ScottBoyce-Python/DynamicVector.gitor you can clone the respository with
git clone https://github.com/ScottBoyce-Python/DynamicVector.gitand then move the file DynamicVector/DynamicVector.py to wherever you want to use it.
Below are examples showcasing how to create and interact with a DynamicVector.
fromDynamicVectorimportDynamicVector# Initialize with a listvec=DynamicVector.from_values([1, 2, 3]) # integer vector with three valuesprint(vec) # Output: DynamicVector([1, 2, 3])print(repr(vec)) # Output: DynamicVector([1, 2, 3], dtype=int32)# Access the underlying NumPy array via the 'view' propertyprint(vec.view) # Output: [1 2 3]print(vec[:]) # Output: [1 2 3] -> same as vec.view# Perform NumPy operationsvec+=1print(vec) # Output: DynamicVector([2, 3, 4])vec[1] =99print(vec) # Output: DynamicVector([ 2, 99, 4])vec[1:len(vec)] =8# set element 2 and 3 to the value of 8.print(vec) # Output: DynamicVector([2, 8, 8])vec[:] = [2, 3, 4]
print(vec) # Output: DynamicVector([2, 3, 4])# Append elements dynamicallyvec.append(5) # Fast operationprint(vec) # Output: DynamicVector([2, 3, 4, 5])vec.extend([7, 8, 9]) # Fast operationprint(vec) # Output: DynamicVector([1, 2, 3, 5, 7, 8, 9])# Insert at a specific indexvec.insert(1, 10)
print(vec) # Output: DynamicVector([ 1, 10, 2, 3, 5, 7, 8, 9])# Insert at a specific indexvec.insert_values(3, [97, 98])
print(vec) # Output: DynamicVector([ 1, 10, 2, 97, 98, 3, 5, 7, 8, 9])# Remove and return the last elementprint(vec) # Output: DynamicVector([ 1, 10, 2, 97, 98, 3, 5, 7, 8, 9])last_elem=vec.pop() # Fast operationprint(vec) # Output: DynamicVector([ 1, 10, 2, 97, 98, 3, 5, 7, 8])print(last_elem) # Output: 9third_element=vec.pop(2)
print(vec) # Output: DynamicVector([ 1, 10, 97, 98, 3, 5, 7, 8])print(third_element) # Output: 2# Slice behaves like NumPy arrayssliced_vec=vec[1:3]
print(sliced_vec) # Output: [10 97]vec[2:5] = [51, 52, 53]
print(vec) # Output: DynamicVector([ 1, 10, 51, 52, 53, 5, 7, 8])vec[[1, 3, 5]] = [-1, -2, -3]
print(vec) # Output: DynamicVector([ 1, -1, 51, -2, 53, -3, 7, 8])This project uses pytest and pytest-xdist for testing. Tests are located in the tests folder. To run tests, install the required packages and execute the following command:
pip install pytest pytest-xdist
pytest # run all tests, note options are set in the pyproject.toml fileNote, that the pyproject.toml file is configured to run pytest with the following arguments:
[tool.pytest.ini_options]
# agressive parallel optionsaddopts = "-ra --dist worksteal -nauto"This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
Scott E. Boyce