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....
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.
...
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:
...
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
...
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 ...
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$....
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 ...
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)$$
...
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-...
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 ...
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
...
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 ...
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.
...
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:
...
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 ...
15
votes
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 ...
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 ...
14
votes
Accepted
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 ...
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 ...
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 ...
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 ...
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:
...
13
votes
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
...
13
votes
Most efficient way to build block / banded SparseArray
Not faster, but maybe easier to implement
...
12
votes
Accepted
Elegant method of generating this matrix?
Update: here Table is faster and more user-friendly then Array.
...
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:
...
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] ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sparse-arrays × 302matrix × 111
list-manipulation × 67
performance-tuning × 67
linear-algebra × 60
bugs × 23
eigenvalues × 11
memory × 10
parallelization × 8
export × 7
replacement × 7
crash × 7
import × 6
associations × 6
functions × 5
graphs-and-networks × 5
finite-element-method × 5
tensors × 5
differential-equations × 4
numerics × 4
pattern-matching × 4
algorithm × 4
data-structures × 4
matlab × 4
librarylink × 4