58 votes

What are SparseArray Properties? How and when should they be used?

Introduction This post is long overdue as I have been repeatedly asked to explain code of mine containing these things. As I see increased use of this construct by others perhaps it is past due also....
user avatar
  • 265k
58 votes
Accepted

A geometric multigrid solver for Mathematica?

Background Details about multigrid solvers can be found in this pretty neat script by Volker John. That's basically the source from which I drew the information to implement the V-cycle solver below. ...
user avatar
34 votes
Accepted

Complex eigenvalues from a sparse Hermitian matrix

Very good observation. Indeed, this issue is really frustrating. To single out the issue: It seems that Arnoldi's method is to blame: ...
user avatar
29 votes
Accepted

Speeding up generation of block diagonal matrix

You are right, it can be done in a fraction of second. One can explicitly construct an array of indexes ...
user avatar
  • 42.9k
22 votes
Accepted

Efficiently defining a SparseArray function

The way the code is written, you can neither exploit packed arrays nor any vectorization. There are two major reasons: Using Rule prevents using packed arrays ...
user avatar
21 votes
Accepted

Sparse Cholesky Decomposition

LinearSolve[] actually computes a permuted Cholesky decomposition; that is, it performs the decomposition $\mathbf P^\top\mathbf A\mathbf P=\mathbf G^\top\mathbf G$....
user avatar
21 votes
Accepted

Implementation of MATLAB's "numgrid" function

The source code of some of the MATLAB functions is available to its users. In this example, if you type edit numgrid, its file will be open, and to my surprise, it ...
user avatar
  • 7,249
20 votes
Accepted

Analytic determinant of a sparse 25x25 matrix?

Using the properties of Block matrices: $$\det\begin{pmatrix}\mathbf A&\mathbf B\\\mathbf C&\mathbf D\end{pmatrix}=\det(\mathbf A)\det\left(\mathbf D-\mathbf C\mathbf A^{-1}\mathbf B\right)$$ ...
user avatar
19 votes

A geometric multigrid solver for Mathematica?

3D Example The problem with direct solvers is that starting in 3 dimensions, their performance for dealing with matrices stemming from PDEs drops rapidly. This is why I wanted to show at least one 3-...
user avatar
19 votes
Accepted

Solving "Resistance between two nodes on a grid" problem in Mathematica

In addition to Carl Woll's post: Computing the pseudoinverse of a the graph Laplacian matrix (a.k.a. the KirchhoffMatrix) is very expensive and in general leads to ...
user avatar
18 votes
Accepted

How to use SparseArray in CUDA?

Yes, it is possible! There is a WorframGPULibrary (WGL), which I discover recently. It is undocumented, however there are beautiful examples in ...
user avatar
  • 42.9k
17 votes
Accepted

Quickly creating a sparse array

You can use the first documented usage for SparseArray: So what you want to do is collect all of the rules {i,j}->val before ...
user avatar
  • 63.6k
16 votes
Accepted

Most efficient way to build block / banded SparseArray

This is definitely faster. It took me some time to figure out the combinatorics and there might still be some potential for improvement within the compiled functions. ...
user avatar
16 votes

Solving "Resistance between two nodes on a grid" problem in Mathematica

Based on rcampion2012's answer to Efficient Implementation of Resistance Distance for graphs?, you could use: ...
user avatar
  • 125k
15 votes
Accepted

What are pattern sparse arrays and how do I use them?

Pattern sparse arrays are used, for example, in NDSolve to specify the structure of a Jacobian. This can save significant time while integrating a differential equation. In other words pattern sparse ...
user avatar
  • 36.8k
15 votes

Optimising subdiagonal shift matrix generation time

...
user avatar
  • 350k
14 votes
Accepted

SparseArray output triggers "unsafe dynamic content" warning

In version 10 Wolfram introduced some dynamic panels in output objects. These are intended to provide some useful brief information (I guess) about what is stored. Because these panels are dynamic ...
user avatar
14 votes
Accepted

Why won't SparseArray let me store values with the head List?

The basic reason is that once you convert a tensor expression into a SparseArray, you've "given control" of all levels of that expression to ...
user avatar
14 votes
Accepted

How to convert a sparse matrix to a list of sparse arrays?

List @@ s will do the trick: ...
user avatar
  • 67.3k
14 votes
Accepted

Why is IdentityMatrix not defined as a SparseArray by default?

The fastest way to get the identity matrix as a sparse array is simply this: IdentityMatrix[10000, SparseArray]; // AbsoluteTiming Here are just some thoughts on ...
user avatar
  • 95.4k
14 votes

Using dense matrix is faster than sparse matrix in LinearSolve

You cannot benefit from SparseArray if your matrix is not sparse! The data structure storing a SparseArray needs a certain ...
user avatar
14 votes
Accepted

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

Be careful with assigning a "form" (e.g. MatrixForm) to a variable. Another observation is that a is a ...
user avatar
  • 12.5k
14 votes
Accepted

Reusing PARDISO symbolic factorization

No, this is not built-in and I have been waiting quite long for such a feature. PardisoLink With the help of Szabolcs's package "LTemplate`" I had written such an interface for the Intel MKL ...
user avatar
13 votes
Accepted

How can I efficiently ArrayFlatten a matrix of sparse matrices?

Everything needs to be of the same precision including the background element. Then this: ...
user avatar
  • 36.8k
13 votes

Speeding up generation of block diagonal matrix

There is an undocumented built-in solution: ...
user avatar
13 votes
Accepted

What are valid settings for the TreatRepeatedEntries sparse array option?

The complete list is (V11.2) First or symbolic 0 Total or symbolic 1 List or symbolic 2 ...
user avatar
  • 36.8k
13 votes

Most efficient way to build block / banded SparseArray

Not faster, but maybe easier to implement ...
user avatar
12 votes
Accepted

Elegant method of generating this matrix?

Update: here Table is faster and more user-friendly then Array. ...
user avatar
  • 42.9k
12 votes
Accepted

When are sparse arrays effective?

You have shown that when the arrays are not sparse, using SparseArray is futile. Let's look at a case when it is sparse: ...
user avatar
  • 65.8k
12 votes
Accepted

Efficiently populate a Sparsearray for a set of rules for a constrained basis

There are a couple of things that can be improved: Binary search can simply be replaced by a lookup table that is very cheap to build. Moreover, IntegerPart[i/len] ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible