Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [sparse-arrays]

Questions on the construction and manipulation of sparse arrays in Mathematica, with functions like SparseArray[] and Band[].

Filter by
Sorted by
Tagged with
5
votes
2answers
132 views

Fastest way to update rows of an array of SparseArrays

I have an array of sparse arrays, say ...
0
votes
0answers
22 views

Filling Regions (Blocks) of a Binary Matrix with Different Probabilities

This is an extension of the following question: How to generate binary array whose elements with values 1 are randomly drawn, where the most elegant answer was posted in the comments: ...
4
votes
1answer
83 views

Efficiently calculating matrix exponential of a recursively generated matrix

Given a matrix $A$ of size $N \times N$, I want to evaluate the function \begin{equation} F(t) = |Tr(\exp^{-i A t})|^2 \end{equation} as a function of $t$. Here, $A[n]$ is constructed recursively ...
2
votes
1answer
39 views

Efficient Linear Operators on SparseArrays

I have a rank four SparseArray of dimension {40, 40, 40, 40}. I wish to apply a linear operator to it, whose form I know abstractly. That is, I have a simple closed form for the action of the operator ...
2
votes
1answer
63 views

How to RowReduce a sparse matrix?

I want to compute the reduced row echelon form of a sparse matrix, but RowReduce returns a dense matrix. Is there a build-in function that computes the reduced form ...
4
votes
3answers
98 views

Sparse matrix processing: flip sign of top-left entries of the matrix

I have a 12k X 12k sparse array (~1% density) and I need to: take the square root of all the elements flip the sign of all the elements in the top-left half of the matrix For example, say that the ...
6
votes
1answer
98 views

Finding the null space of a large sparse matrix

I am trying to compute the null space of a large $n\times n$ sparse matrix. No matter how large the dimension of the matrix is, it will always look like as below: The figure on the left shows the ...
4
votes
0answers
63 views

ParametricNDSolveValue with SparseArrays

I want to solve large, sparse matrix systems with ParametricNDSolveValue and was hoping to use SparseArray to speed up the computation. However I find that ParametricNDSolveValue doesn't seem to work ...
3
votes
1answer
64 views

Signed cell adjacency matrices (boundary operators)

In How to obtain the cell-adjacency graph of a mesh? there are some great answers that show how to obtain the cell adjacency matrix of a mesh. How would one generalize these to incorporate the ...
17
votes
1answer
374 views

Efficiently defining a SparseArray function

I've got a fairly large SparseMatrix (~60,000 entries in a ~10,000-by-10,000 matrix) that depends on two parameters (im1 and <...
6
votes
1answer
131 views

Speeding up tensor contractions and multiplication

Consider a tensor $T\in\mathbb{R}^{N\times N\times N\times M}$ and two vectors $x,y\in\mathbb{R}^N$. I want to compute the $N\times M$ vector defined by $X_{ij}=\operatorname{tr}(x^\top T_{:,:,i,j}y)=\...
1
vote
0answers
26 views

Computing the invariant factors of a sparse matrix

Let M be a sparse $n\times m$ integral matrix, where $nm$ is very large. The goal is to compute its invariant factors. The naïve approach ...
7
votes
1answer
79 views

SparseArray: how to efficiently extract/delete many entries?

This is a follow-up of this question. We have a large $m\!\times\!n$ matrix stored as a SparseArray a and a list of positions pos...
18
votes
3answers
358 views

Generate coefficient array from general formula of linear equation system

This is a problem coming out in the implementation of finite difference method (FDM). Here is a toy example. Suppose we want to solve the boundary value problem (BVP) $$y''(x)=\sin(x),\ y(0)=0,\ y(...
10
votes
3answers
533 views

Optimising subdiagonal shift matrix generation time

I want a square matrix $A$ such that $$A_{i,i-1} = 1 \ \forall i\\ A_{i,j} = 0 \text{ for } \ j \ne i-1$$ I have tried ...
5
votes
2answers
153 views

Lowest Magnitude Eigenvalues of Large Sparse Matrices

I am trying to find the first three lowest eigenvalues of large sparse matrices of size range $10^3 - 10^5$. The matrices depend on some parameter $x$, so I first construct the matrices and then use ...
0
votes
0answers
27 views

Inverse of Sparse Diagonal Array not Sparse

Kind of bummed that when I take the inverse of a matrix that is a diagonal sparse array, the result in not a sparse array. Further, the time to compute the inverse is the same as the time to compute ...
0
votes
1answer
55 views

Time used by Mathematica to calculate tridiagonal matrix

I have a question. I need to find the eigenvalues and eigenvectors of a tridiagonal matrix of size NxN. Can you tell me how much time does Mathematica need to do that in minutes? for Size N ...
4
votes
5answers
514 views

How to generate binary array whose elements with values 1 are randomly drawn

I am trying to generate a binary matrix (whose elements are 0 or 1) of dimension 20x20. To do this, I want to supply as input the number of matrix elements that will take value 1. After, I want to ...
13
votes
2answers
884 views

Solving “Resistance between two nodes on a grid” problem in Mathematica

In the context of resistor networks and finding the (equivalent) resistance between two arbitrary nodes, I am trying to learn how to write a generic approach in Mathematica, generic as in an approach ...
10
votes
2answers
654 views

Quickly creating a sparse array

The motivation for my question is that I'm creating very large, very sparse symmetric matrices and finding the largest eigenvalue. (Think upwards of $10^5\times 10^5$ size matrices, with at most $10^...
0
votes
0answers
32 views

Selecting some vectors from a matrix

I am trying to write a code for band matrices that store only the nonzero vectors. For example if I have tridiagonal matrix of size 10, I want to store only the three diagonals as a vectors. n here ...
11
votes
4answers
841 views

Set every element to zero in matrix unless it's 1 or 1/2

I have the following code: ...
8
votes
0answers
105 views

Matrix-free Arnoldi method for eigensystems

I am solving a generalized eigenvalue problem $$\mathbf S\,\mathbf x = \lambda\,\mathbf M\,\mathbf x$$ w/ $\mathbf S := \mathbf B\,\mathbf A^{-1}\,\mathbf B^{T}$, and $\mathbf A$ is a sparse ...
1
vote
1answer
227 views

Bottom-most left-most entries in a sparse array

I have a list bdrs of SparseArrays, which represent the boundary matrices of a chain complex $R^{d_0}\overset{\partial_1}{\...
2
votes
1answer
120 views

Indices of nonzero rows in a sparse array

Given $A\!\in\!\mathbb{Z}^{m\times n}$ stored as a=SparseArray[{...},{m,n}], how can I obtain the list of all indices $i\!\in\!\{1,\ldots,m\}$ for which the $i$-th ...
0
votes
1answer
67 views

Read text files as sparse arrays and concatenate in a loop

I would like to read multiple text files and build a sparse array by concatenating row-wise one after the other. The text files are ordered in the following format: \begin{matrix} 0, & 0, &...
4
votes
1answer
162 views

Delete zeros from a sparse array [duplicate]

Why doesn't SparseArray automatically remove zero entries? The code ...
1
vote
2answers
60 views

Building a sparse array from given lists the 2nd case

Similar to the 1st case, now given a list ...
1
vote
1answer
23 views

Building a sparse array from given lists

Given some lists ...
4
votes
3answers
290 views

Sparsity of a sparse array without converting it to a regular one

My goal is to find such properties of a sparse matrix as the maximum/average number of non-zero elements per row. The brute-force way of doing this is via converting the sparse array into a regular ...
32
votes
1answer
1k views

Complex eigenvalues from a sparse Hermitian matrix

Bug introduced in 9.0 or earlier and persisting through 12.0. I notice in the following example that wrong complex eigenvalues are resulted if calculating from a Hermitian sparse matrix, which should ...
2
votes
0answers
59 views

How to construct a time-dependent matrix quickly?

In the process of discretization of a 4D PDE, I need to construct a final sparse matrix $B$, which is very large (I denoted by $\text{size}$ here) and time-dependent, viz, some of its entries change ...
5
votes
1answer
233 views

Parallelize the construction of sparse matrices

I'm trying to parallelize a few constructions of chain complexes (given as a list of sparse matrices). I have a function that for any column (or row) computes the list of nonzero entries in that ...
2
votes
1answer
61 views

Creating an appropriate Sparse Matrix

i have a 6$\times$6 matrix of the form ...
1
vote
1answer
49 views

Speed up construction of large matrix with if statements

I am constructing a matrix of the form $$ T_{i_aj_ak_al_a,i_bj_bk_bl_b} = T_{i_ai_b}\delta_{j_aj_b}\delta_{k_ak_b}\delta_{l_al_b} + T_{j_aj_b}\delta_{i_ai_b}\delta_{k_ak_b}\delta_{l_al_b} + T_{k_ak_b}\...
0
votes
0answers
32 views

How to add the duplicated positions in sparse array? [duplicate]

When we use the SparseArray in such form: SparseArray[{pos1,pos2}->{val}] We know that we don't have duplication of positions in pos1 and pos2. I mean that we ...
3
votes
2answers
48 views

Using elements of a list as SparseArray patterns: “can't be used as part specification”

I don't have much experience with SparseArray, and the search here did not give me an answer to this. I'm trying to numerically compute a certain spectrum, and the ...
0
votes
0answers
26 views

SparseArray with width or height 0

I can store the zero matrix (=module morphism) $R^2\overset{a}{\leftarrow} R^3$ as a=SparseArray[{},{2,3}]. But $R^0\overset{a}{\leftarrow} R^3$ stored as ...
1
vote
1answer
153 views

Sparse matrices dot multiplication

I have two huge sparse matrices, and I am interested in dot multiplication them. But I have a memory issue so a notebook is a shutdown. I am using the 11.3 version. Any suggestion on how to avoid ...
1
vote
2answers
90 views

Filtering sparse matrix

I am interested in filtering a sparse matrix, where the values are between zero and one. I do it in the following manner. ...
4
votes
0answers
170 views

Loopy Belief Propagation

I look for an efficient way to implement a Belief Propagation in Pairwise Markov Random Field. Each node is a binary random variable, and the unary potential function is ...
2
votes
1answer
86 views

Save a list as a value in the sparse matrix

I am interested in save a list (the size of the list is two) of numbers in the sparse matrix, but this is not supported. Maybe you familiar with some workarounds or any suggestion on another data ...
5
votes
2answers
362 views

Replace values in sparse matrix

I am interested in updating the values of the sparse matrix. I have a list of matrix element which I am interested in updating and a list of new values. Currently, I do it in the following manner: <...
1
vote
1answer
128 views

SparseArray: accesing nonzero row and column entries

I have an enormous $m\!\times\!n$ sparse matrix $d$ with $l$ nonzero entries, stored as d=SparseArray[{{i1,j1}->w1,...,{il,jl}->wl},{m,n}];. How do I access ...
9
votes
5answers
464 views

Replace diagonal elements in sparse matrix

I am interested in replacing all elements in a diagonal of a sparse matrix with zero. Currently, I do it in the following manner: ...
6
votes
1answer
362 views

How to find k largest element and their position in a matrix?

I have a sparse matrix, and I am interested in find k largest elements and their positions in the matrix. I do it in the following manner: ...
1
vote
1answer
96 views

Optimization of a slow function - reason to why it is slow

Edit: Okay, my question was viewed quite a few times but not answered so I assume I phrased/presented in a confusing way. I'll try to rephrase my problem to make it more accessible because luckily I ...
6
votes
1answer
107 views

Efficiently fill sparse matrix involving BSpline

Context In order to implement regularised fitting in 2 or 3 dimensions (as is done say here) using BSplineBasis one needs to evaluate a basis over a set of data. ...
6
votes
1answer
65 views

How to prevent a SparseArray entry from being specified when it coincides with the default value?

Mathematica doesn't notice when an entry coincides with the default value: (IdentityMatrix[2, SparseArray] - IdentityMatrix[2, SparseArray])["NonzeroPositions"] ...