|
1 | 1 | # MultidimensionalSparseArrays.jl |
2 | 2 |
|
3 | | -[](https://github.com/raphasampaio/MultidimensionalSparseArrays.jl/actions/workflows/CI.yml) |
4 | | -[](https://codecov.io/github/raphasampaio/multidimensionalsparsearrays.jl) |
5 | | -[](https://github.com/JuliaTesting/Aqua.jl) |
| 3 | +[](https://github.com/gcalderone/MultidimensionalSparseArrays.jl/actions/workflows/CI.yml) |
| 4 | +[](https://codecov.io/gh/gcalderone/MultidimensionalSparseArrays.jl) |
| 5 | +[](https://github.com/JuliaTesting/Aqua.jl) |
6 | 6 |
|
7 | | -## Introduction |
| 7 | +A Julia package for multidimensional sparse arrays. |
8 | 8 |
|
9 | | -## Getting Started |
| 9 | +## Overview |
10 | 10 |
|
11 | | -### Installation |
| 11 | +`MultidimensionalSparseArrays.jl` provides a `SparseArray` type that efficiently stores and manipulates multidimensional arrays with a high proportion of zero elements. Unlike dense arrays, `SparseArray` only stores non-zero values, significantly reducing memory consumption for sparse data. |
| 12 | + |
| 13 | +This package is designed to be a flexible and intuitive tool for scientific computing, data analysis, and any domain where large, sparse multidimensional data structures are common. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- **Memory Efficiency:** Only non-zero elements are stored, making it ideal for high-dimensional sparse data. |
| 18 | +- **Arbitrary Dimensionality:** Create sparse arrays of any number of dimensions. |
| 19 | +- **Intuitive Indexing:** Use standard Julia indexing to access and modify elements. |
| 20 | +- **Comprehensive API:** A rich set of functions for creating, manipulating, and analyzing sparse arrays. |
| 21 | +- **Interoperability:** Easily convert between `SparseArray` and dense `Array` types. |
| 22 | +- **Arithmetic Operations:** Perform element-wise arithmetic (+, -, *) on sparse arrays. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +The package can be installed with the Julia package manager. From the Julia REPL, type `]` to enter the Pkg REPL mode and run: |
| 27 | + |
| 28 | +```julia |
| 29 | +pkg> add MultidimensionalSparseArrays |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### Creating a SparseArray |
| 35 | + |
| 36 | +You can create a `SparseArray` in several ways: |
| 37 | + |
| 38 | +```julia |
| 39 | +using MultidimensionalSparseArrays |
| 40 | + |
| 41 | +# Create an empty 3x4x2 sparse array of Float64 |
| 42 | +A = SparseArray{Float64}(3, 4, 2) |
| 43 | + |
| 44 | +# Create from a dense array (only non-zero elements are stored) |
| 45 | +dense_array = [1 0 3; 0 0 0; 2 0 0] |
| 46 | +B = SparseArray(dense_array) |
| 47 | + |
| 48 | +# Create a sparse array of zeros |
| 49 | +C = spzeros(Int, 5, 5) |
| 50 | + |
| 51 | +# Create a sparse array of ones |
| 52 | +D = spones(2, 2) |
| 53 | +``` |
| 54 | + |
| 55 | +### Accessing and Modifying Elements |
| 56 | + |
| 57 | +Use standard array indexing to get and set elements. Accessing an unset element will throw a `BoundsError`. |
12 | 58 |
|
13 | 59 | ```julia |
14 | | -julia> ] add MultidimensionalSparseArrays |
| 60 | +A = SparseArray{Float64}(3, 3) |
| 61 | + |
| 62 | +# Set values |
| 63 | +A[1, 1] = 10.0 |
| 64 | +A[3, 2] = -5.0 |
| 65 | + |
| 66 | +# Get values |
| 67 | +value = A[1, 1] # returns 10.0 |
| 68 | + |
| 69 | +# Check if an index has a stored value |
| 70 | +hasindex(A, 1, 1) # true |
| 71 | +hasindex(A, 1, 2) # false |
| 72 | + |
| 73 | +# To get a value with a default for unset indices |
| 74 | +get(A, (1, 2), 0.0) # returns 0.0 |
15 | 75 | ``` |
16 | 76 |
|
17 | | -### Example 1: |
| 77 | +### Handling Sparsity |
| 78 | + |
| 79 | +Several functions are provided to work with the sparse nature of the array: |
18 | 80 |
|
19 | | -### Example 2: |
| 81 | +```julia |
| 82 | +# Get the number of non-zero elements |
| 83 | +nnz(B) |
| 84 | + |
| 85 | +# Get the sparsity of the array (fraction of zero elements) |
| 86 | +sparsity(B) |
| 87 | + |
| 88 | +# Get the stored indices and values |
| 89 | +indices = stored_indices(B) |
| 90 | +values = stored_values(B) |
| 91 | +pairs = stored_pairs(B) |
| 92 | + |
| 93 | +# Find the indices and values of non-zero elements |
| 94 | +(I, V) = findnz(B) |
| 95 | + |
| 96 | +# Remove stored zeros to save memory |
| 97 | +compress!(B) |
| 98 | +``` |
| 99 | + |
| 100 | +### Arithmetic Operations |
| 101 | + |
| 102 | +Element-wise arithmetic operations are supported: |
| 103 | + |
| 104 | +```julia |
| 105 | +A = SparseArray([1 0; 0 4]) |
| 106 | +B = SparseArray([0 2; 3 0]) |
| 107 | + |
| 108 | +# Addition |
| 109 | +C = A + B # [1 2; 3 4] |
| 110 | + |
| 111 | +# Subtraction |
| 112 | +D = A - B # [1 -2; -3 4] |
| 113 | + |
| 114 | +# Scalar multiplication |
| 115 | +E = A * 2 # [2 0; 0 8] |
| 116 | +``` |
| 117 | + |
| 118 | +### Utility Functions |
| 119 | + |
| 120 | +- `to_dense(A)`: Convert a `SparseArray` to a dense `Array`. |
| 121 | +- `dropstored!(A, val)`: Remove all stored entries equal to `val`. |
| 122 | +- `spzeros`, `spones`, `spfill`: Create sparse arrays with specific values. |
| 123 | + |
| 124 | +## API Reference |
| 125 | + |
| 126 | +The following are the key exports of the package: |
| 127 | + |
| 128 | +- `SparseArray`: The multidimensional sparse array type. |
| 129 | +- `nnz`: Get the number of non-zero elements. |
| 130 | +- `sparsity`: Get the fraction of zero elements. |
| 131 | +- `stored_indices`, `stored_values`, `stored_pairs`: Iterators for stored elements. |
| 132 | +- `spzeros`, `spones`, `spfill`: Constructors for sparse arrays. |
| 133 | +- `findnz`: Find non-zero elements. |
| 134 | +- `dropstored!`, `compress!`: Memory management functions. |
| 135 | +- `hasindex`: Check for a stored value at an index. |
| 136 | +- `to_dense`: Convert to a dense array. |
| 137 | + |
| 138 | +For more details, please refer to the docstrings of the individual functions. |
| 139 | + |
| 140 | +## Code Formatting |
| 141 | + |
| 142 | +This project uses `JuliaFormatter.jl` to ensure a consistent code style. To format your code locally, run the following command from the root of the project: |
| 143 | + |
| 144 | +```bash |
| 145 | +julia --project=format format/format.jl |
| 146 | +``` |
20 | 147 |
|
21 | 148 | ## Contributing |
22 | 149 |
|
23 | | -Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request. |
| 150 | +Contributions are welcome! Please feel free to submit a pull request or open an issue. |
| 151 | + |
| 152 | +## License |
| 153 | + |
| 154 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments