Yesterday I wrote about sparse arrays for Python using dictionaries. I couldn't get it out of my head so tonight I actually made a complete module containing a class for sparse n-dimensional arrays. The module supports any number of dimensions and any size. Sparray has all basic operations like adding, subtracting, multiplication, division, mod, pow, printing, sum() and can easily be converted to full NumPy arrays.
The sparray module can be downloaded from here. Running sparray.py will show a series of examples and print the results in the console.
Enjoy. Bug reports and feedback welcome!
Below is some output from the example:
The sparray module can be downloaded from here. Running sparray.py will show a series of examples and print the results in the console.
Enjoy. Bug reports and feedback welcome!
Below is some output from the example:
adding...
[[ 3. 3. 3.]
[ 3. 13. 3.]
[ 3. 3. 3.]]
subtracting...
[[-3. -3. -3.]
[-3. 7. -3.]
[-3. -3. -3.]]
multiplication...
[[ 0. 0. 0.]
[ 0. 30. 0.]
[ 0. 0. 0.]]
[[ 9. 9. 9.]
[ 9. 9. 9.]
[ 9. 9. 9.]]
division...
[[ 0. 0. 0.]
[ 0. 3. 0.]
[ 0. 0. 0.]]
mod...
[[ 0. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 0.]]
power...
[[ 0. 0. 0.]
[ 0. 1000. 0.]
[ 0. 0. 0.]]
iadd...
[[ 3. 3. 3.]
[ 3. 13. 3.]
[ 3. 3. 3.]]
sum of elements...
74
mix with NumPy arrays...
[[ 6. 6. 6.]
[ 6. 26. 6.]
[ 6. 6. 6.]]
Frobenius norm...
601.0