### LU Factorization and Linear System Solving with CSR Matrices Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Illustrates how to solve a linear system Ax = b where A is a CSR sparse matrix using the `` operator. It also covers computing the LU factorization of a sparse matrix and reusing it for multiple solves, including in-place solves and updating the factorization with a modified matrix. ```julia using SparseMatricesCSR using LinearAlgebra # Create sparse matrix (must be square and non-singular) I = [1, 1, 2, 2, 2, 3, 3] J = [1, 2, 1, 2, 3, 2, 3] V = [4.0, 1.0, -1.0, 4.0, 1.0, -1.0, 4.0] csr = sparsecsr(I, J, V, 3, 3) # Right-hand side vector b = [1.0, 2.0, 3.0] # Solve linear system: csr * x = b x = csr \ b println(x) # Solution vector # Compute LU factorization lu_fact = lu(csr) # Reuse factorization for multiple solves b2 = [4.0, 5.0, 6.0] x2 = lu_fact \ b2 # In-place solve with existing factorization y = similar(b) ldiv!(y, lu_fact, b) println(y) # Same as x # Update factorization with modified matrix (in-place) csr2 = copy(csr) csr2[1, 1] = 5.0 lu!(lu_fact, csr2) # Reuse memory ``` -------------------------------- ### Create 0-based CSR Matrix for C/Fortran Interoperability Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Illustrates the creation of a 0-based Compressed Sparse Row (CSR) matrix, which is beneficial for interfacing with external libraries (like C or Fortran) that commonly use 0-based indexing. It shows how to specify 0-based indexing and inspect the internal 0-based storage properties, while noting that element access still uses Julia's 1-based indexing. ```julia using SparseMatricesCSR # Create 0-based CSR matrix (useful for interfacing with C/Fortran libraries) I = [1, 2, 3, 3] J = [1, 2, 1, 3] V = [10.0, 20.0, 30.0, 40.0] # Specify 0-based indexing using Val(0) csr0 = sparsecsr(Val(0), I, J, V, 3, 3) # Check indexing base println(getBi(csr0)) # Output: 0 println(getoffset(csr0)) # Output: 1 (offset to convert from 1-based to 0-based) # Element access still uses Julia's 1-based indexing println(csr0[1, 1]) # Output: 10.0 # Internal storage uses 0-based indexing println(csr0.rowptr) # Row pointers: 0-based offsets println(csr0.colval) # Column values: 0-based indices ``` -------------------------------- ### Create and Iterate CSR Sparse Matrix Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Demonstrates how to create a CSR sparse matrix from row, column, and value arrays. It then shows how to iterate over each row, access the range of non-zero elements, and retrieve the column indices and values of these non-zero elements. ```julia # Create sparse matrix I = [1, 1, 2, 2, 3, 3] J = [1, 3, 2, 3, 1, 3] V = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] csr = sparsecsr(I, J, V, 3, 3) # Iterate over each row for row in 1:size(csr, 1) println("Row $row:") # Get range of non-zero indices for this row nz_range = nzrange(csr, row) # Access non-zero values and column indices for nz_idx in nz_range col = colvals(csr)[nz_idx] # Column index (1-based) val = nonzeros(csr)[nz_idx] # Value println(" Column $col: $val") end end ``` -------------------------------- ### Convert Between CSC and CSR Sparse Matrix Formats Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Shows how to convert between Julia's standard `SparseMatrixCSC` (Compressed Sparse Column) format and the `SparseMatrixCSR` format provided by this library. It covers constructing a CSR matrix from an existing CSC matrix, constructing a CSR matrix from a dense matrix, and converting a CSR matrix back to CSC format, including an efficient method using transpose. ```julia using SparseMatricesCSR using SparseArrays # Create standard Julia sparse matrix (CSC format) I = [1, 2, 3, 1, 2, 3] J = [1, 1, 1, 2, 2, 3] V = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] csc = sparse(I, J, V, 3, 3) # Convert CSC to CSR csr = SparseMatrixCSR(csc) println(typeof(csr)) # Output: SparseMatrixCSR{1, Float64, Int64} # Convert from dense matrix dense = [1.0 0.0 2.0; 0.0 3.0 0.0; 4.0 0.0 5.0] csr_from_dense = sparsecsr(dense) # Convert CSR back to CSC (via transpose) csc_back = sparse(transpose(collect(csr))) # Direct construction from transposed CSC csr_efficient = SparseMatrixCSR(transpose(csc)) ``` -------------------------------- ### Create 1-based CSR Matrix from Row/Column/Value Vectors Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Demonstrates how to construct a 1-based Compressed Sparse Row (CSR) matrix using vectors for row indices, column indices, and their corresponding values. It shows element access and retrieving matrix properties. This is the default behavior when creating a CSR matrix. ```julia using SparseMatricesCSR # Define sparse matrix using row indices, column indices, and values I = [1, 1, 2, 2, 2, 3, 3] # Row indices J = [1, 2, 1, 2, 3, 2, 3] # Column indices V = [4.0, 1.0, -1.0, 4.0, 1.0, -1.0, 4.0] # Values # Create 1-based CSR matrix (default) csr = sparsecsr(I, J, V) # Matrix structure: # 4.0 1.0 0.0 # -1.0 4.0 1.0 # 0.0 -1.0 4.0 # Access elements println(csr[1, 1]) # Output: 4.0 println(csr[2, 3]) # Output: 1.0 # Get matrix properties println(size(csr)) # Output: (3, 3) println(nnz(csr)) # Output: 7 (number of stored values) println(issparse(csr)) # Output: true ``` -------------------------------- ### Convert CSR Matrix Between Indexing Bases Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Demonstrates how to convert a CSR sparse matrix between 1-based and 0-based indexing using the `convert` function. This process also allows for changing the data types of the matrix elements (values and indices) during the conversion. ```julia using SparseMatricesCSR # Create 1-based CSR matrix I = [1, 2, 3, 2] J = [1, 2, 3, 3] V = [1.0, 2.0, 3.0, 4.0] csr1 = sparsecsr(I, J, V, 3, 3) # Convert to 0-based indexing csr0 = convert(SparseMatrixCSR{0, Float64, Int64}, csr1) println(getBi(csr0)) # Output: 0 println(csr0[2, 2]) # Output: 2.0 (element access still 1-based in Julia) # Internal arrays now use 0-based indexing println(csr0.rowptr[1]) # First row pointer is 0 println(csr0.colval) # Column indices are 0-based # Convert back to 1-based csr1_back = convert(SparseMatrixCSR{1, Float64, Int64}, csr0) println(getBi(csr1_back)) # Output: 1 # Change value type during conversion csr_float32 = convert(SparseMatrixCSR{1, Float32, Int32}, csr1) println(eltype(csr_float32)) # Output: Float32 ``` -------------------------------- ### Create and Manipulate Symmetric Sparse Matrix (CSR) Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Demonstrates the creation of a symmetric sparse matrix in CSR format, creating a deep copy, and filling all stored entries with a specific value. Assumes `I`, `J`, and `V` define the sparse matrix structure. ```julia sym_csr = symsparsecsr(I, J, V, 3, 3) sym_copy = copy(sym_csr) fillstored!(sym_csr, 1.0) ``` -------------------------------- ### Copy and Modify CSR Sparse Matrices Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Shows how to create a deep copy of a CSR sparse matrix using the `copy` function, ensuring that modifications to the copy do not affect the original matrix. It also demonstrates using `fillstored!` to efficiently update all stored non-zero values in a matrix to a specific value. ```julia using SparseMatricesCSR using LinearAlgebra # Create original matrix I = [1, 1, 2, 3] J = [1, 2, 2, 3] V = [1.0, 2.0, 3.0, 4.0] csr = sparsecsr(I, J, V, 3, 3) # Deep copy csr_copy = copy(csr) csr_copy[1, 1] = 100.0 println(csr[1, 1]) # Output: 1.0 (original unchanged) println(csr_copy[1, 1]) # Output: 100.0 # Fill all stored entries with a value fillstored!(csr, 5.0) println(nonzeros(csr)) # Output: [5.0, 5.0, 5.0, 5.0] ``` -------------------------------- ### Find Non-Zero Entries in CSR Matrix Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Shows how to extract all non-zero entries from a CSR sparse matrix into separate arrays for row indices, column indices, and values using the `findnz` function. It also demonstrates counting the total number of non-zero entries with `nnz`, counting entries satisfying a predicate with `count`, and counting all stored entries. ```julia using SparseMatricesCSR # Create sparse matrix I = [1, 2, 3, 1, 3] J = [1, 2, 2, 3, 3] V = [10.0, 20.0, 30.0, 40.0, 50.0] csr = sparsecsr(I, J, V, 3, 3) # Extract row indices, column indices, and values rows, cols, vals = findnz(csr) println("Rows: $rows") # Output: [1, 1, 2, 3, 3] println("Columns: $cols") # Output: [1, 3, 2, 2, 3] println("Values: $vals") # Output: [10.0, 40.0, 20.0, 30.0, 50.0] # Count non-zero entries println(nnz(csr)) # Output: 5 # Count entries satisfying a predicate positive_count = count(v -> v > 25.0, csr) println(positive_count) # Output: 3 # Count all stored entries (including structural zeros) total_stored = count(csr) println(total_stored) # Output: 5 ``` -------------------------------- ### Create Symmetric CSR Sparse Matrix Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Demonstrates creating a symmetric CSR sparse matrix by providing only the entries for the upper triangle and diagonal. The `symsparsecsr` function automatically handles the symmetry, allowing access to elements as if the lower triangle were also present, even though it's not explicitly stored. ```julia using SparseMatricesCSR # Create symmetric matrix (only store upper triangle) # Define entries for upper triangle and diagonal I = [1, 1, 1, 2, 2, 3] # Row indices J = [1, 2, 3, 2, 3, 3] # Column indices (J >= I) V = [4.0, 1.0, 2.0, 5.0, 3.0, 6.0] # Values # Create symmetric CSR matrix (1-based by default) sym_csr = symsparsecsr(I, J, V, 3, 3) # Access uses symmetry automatically println(sym_csr[1, 2]) # Output: 1.0 println(sym_csr[2, 1]) # Output: 1.0 (same as [1,2] due to symmetry) # Only upper triangle is stored println(nnz(sym_csr)) # Output: 6 (only upper triangle entries) # Matrix-vector multiplication accounts for symmetry x = [1.0, 2.0, 3.0] y = sym_csr * x println(y) # Uses both upper and lower triangle in computation ``` -------------------------------- ### Scale Sparse Matrix Entries (CSR) Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Scales all entries of a CSR sparse matrix by a constant factor. Assumes the matrix `csr` is already defined. Prints the non-zero elements after scaling. ```julia rmul!(csr, 2.0) println(nonzeros(csr)) ``` -------------------------------- ### Perform Matrix-Vector Multiplication with CSR Matrices Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Demonstrates matrix-vector multiplication using `SparseMatrixCSR`. It includes standard multiplication (`csr * x`), in-place multiplication (`mul!(y, csr, x)`), and in-place multiplication with scaling (`mul!(y, csr, x, α, β)`), which computes `y = α*A*x + β*y`. This operation is often efficient for CSR matrices when the vector `x` is accessed sequentially. ```julia using SparseMatricesCSR using LinearAlgebra # Create sparse matrix I = [1, 1, 2, 2, 3] J = [1, 2, 2, 3, 3] V = [2.0, 1.0, 3.0, 1.0, 4.0] csr = sparsecsr(I, J, V, 3, 3) # Vector to multiply x = [1.0, 2.0, 3.0] # Standard multiplication y = csr * x println(y) # Output: [4.0, 9.0, 12.0] # In-place multiplication: y = A*x y = zeros(3) mul!(y, csr, x) println(y) # Output: [4.0, 9.0, 12.0] # In-place with scaling: y = α*A*x + β*y y = [1.0, 1.0, 1.0] α, β = 2.0, 0.5 mul!(y, csr, x, α, β) # Computes: y = 2.0 * csr * x + 0.5 * y println(y) # Output: [8.5, 18.5, 24.5] ``` -------------------------------- ### Symmetrize a Non-Symmetric CSR Matrix Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Explains how to convert a non-symmetric CSR matrix into a symmetric one using `symsparsecsr` with the `symmetrize=true` option. This method averages the corresponding upper and lower triangle entries to create the symmetric matrix, while diagonal entries remain unchanged. ```julia using SparseMatricesCSR # Create non-symmetric input (full matrix) I = [1, 1, 2, 2, 1, 2] # Includes both upper and lower entries J = [1, 2, 1, 2, 2, 1] V = [4.0, 3.0, 5.0, 6.0, 7.0, 9.0] # Different values in upper/lower # Symmetrize by averaging: (A + A^T) / 2 sym_csr = symsparsecsr(I, J, V, 2, 2; symmetrize=true) # Result stores symmetrized values # Entry (1,2) = Entry (2,1) = (3.0 + 9.0) / 2 = 6.0 println(sym_csr[1, 2]) # Output: 6.0 println(sym_csr[2, 1]) # Output: 6.0 # Diagonal entries are not averaged println(sym_csr[1, 1]) # Output: 4.0 println(sym_csr[2, 2]) # Output: 6.0 ``` -------------------------------- ### Access and Modify Sparse Matrix Elements in CSR Format Source: https://context7.com/gridap/sparsematricescsr.jl/llms.txt Explains how to access and modify elements within a `SparseMatrixCSR`. It shows reading elements using standard Julia 1-based indexing, modifying existing non-zero entries, and highlights that attempting to set a value at a position outside the current sparsity pattern (i.e., where the value is zero) will result in an error. It also covers accessing internal storage arrays and modifying all stored values using functions like `fillstored!` and `rmul!`. ```julia using SparseMatricesCSR # Create sparse matrix I = [1, 1, 2, 3, 3] J = [1, 3, 2, 1, 3] V = [1.0, 2.0, 3.0, 4.0, 5.0] csr = sparsecsr(I, J, V, 3, 3) # Read elements (1-based Julia indexing) println(csr[1, 1]) # Output: 1.0 println(csr[1, 2]) # Output: 0.0 (not stored) # Modify existing non-zero entries csr[1, 1] = 10.0 println(csr[1, 1]) # Output: 10.0 # Attempting to set zero entries throws error try csr[1, 2] = 5.0 # This position is not in the sparsity pattern catch e println(e) # Output: ArgumentError("Trying to set an entry outside sparsity pattern") end # Access internal storage directly println(nonzeros(csr)) # Get array of stored values println(colvals(csr)) # Get array of column indices # Modify all stored values fillstored!(csr, 1.0) # Set all stored entries to 1.0 rmul!(csr, 2.0) # Multiply all stored entries by 2.0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.