Skip to main content

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
63 votes
2 answers
3k views

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

A strange undocumented form of SparseArray is increasingly used in answers on this site: ...
Mr.Wizard's user avatar
  • 272k
13 votes
2 answers
1k views

How to interpret the FullForm of a SparseArray?

SparseArrays are atomic objects, but they do have a FullForm which reveals information about them. What is the meaning of the ...
Szabolcs's user avatar
  • 236k
19 votes
2 answers
781 views

Additive SparseArray Assembly

The goal is to assemble a SparseArray in an additive fashion. Let us assume we have a large List of indices (some will be ...
leibs's user avatar
  • 758
24 votes
3 answers
8k views

Find the eigenvector associated with the smallest eigenvalue, not smallest in magnitude

I am trying to find the eigenvector of a $20000 \times 20000$ sparse matrix associated with the smallest eigenvalue. I realized that the smallest eigenvalue might be negative; for example, if the ...
SlipStream's user avatar
20 votes
3 answers
2k views

Efficient way to combine SparseArray objects?

I have several SparseArray objects, say sa11, sa12, sa21, sa22, which I would like to combine into the equivalent of {{sa11, sa12}, {sa21, sa22}}. As an example, I ...
b.gates.you.know.what's user avatar
8 votes
2 answers
239 views

How to relate memory usage with occupied positions of SparseArrays?

What is the relation of memory usage of a SparseArray and the number of its occupied positions? Let's say you build a $100000000 \times 10$ ...
Hp Radojewski Schäfer Von's user avatar
28 votes
4 answers
2k views

Speeding up generation of block diagonal matrix

I'm struggling with the following problem. I have $48$ square matrices (full, filled with real machine precision numbers, thus are packed, all different) of size $128$. I wolud like to place them on a ...
mmal's user avatar
  • 3,518
11 votes
2 answers
2k views

Efficient by-element updates to SparseArrays

I have a very large SparseArray called A. What is the most efficient way to update say element ...
Vitaliy Kaurov's user avatar
23 votes
3 answers
2k views

Using ReplaceAll on SparseArray

I'm using SparseArray in a notebook in which I am doing complex conjugation manually, i.e. writing $\sqrt{-1}$ as i and applying ...
matimo2's user avatar
  • 503
11 votes
2 answers
880 views

Speeding up construction of simple tridiagonal matrix

I have the following code to construct a tridiagonal matrix: ...
acl's user avatar
  • 19.9k
20 votes
1 answer
763 views

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

When you attempt to define a SparseArray with elements that have the head List it complains: ...
Mr.Wizard's user avatar
  • 272k
3 votes
1 answer
1k views

Import Excel sheet into 3D array?

I have an excel spreadsheet that I would like to plot in a 3D graph using mathematica. The X and Y values are the location of the cell, and the Z value is the number of the scale. How can I import ...
Sponge Bob's user avatar
53 votes
2 answers
2k views

A geometric multigrid solver for Mathematica?

Cross posted to community.wolfram.com Mathematica ships a variety of linear solvers through the interface LinearSolve[A, b, Method -> method] the most ...
Henrik Schumacher's user avatar
12 votes
1 answer
330 views

Fast way to reassemble SparseArrays

I often have to deal with nonlinear shape optimization problems involving curves or surfaces surfaces. A classical example is Plateau's problem. No matter if I use gradient-based techniques (such as ...
Henrik Schumacher's user avatar
7 votes
1 answer
334 views

Adding two SparseArrays produces zeros in the reported "NonzeroValues"

When two SparseArrays are added together and new zero values are created, these new zero values are reported as "NonzeroValues". Example, produced with Mathematica version 10.2: ...
dcutrell's user avatar
5 votes
3 answers
1k views

SparseArray row operations

Given a large (very) sparse matrix, A, how can I efficiently "operate" on only the nonzeros in a given row? For example: For each row in A, generate a list of column indices that have a magnitude (...
leibs's user avatar
  • 758
23 votes
0 answers
1k views

LinearSolve and Krylov Method options

I need to solve a large, sparse, linear system. At some point Method -> "Multifrontal" fails and a bit later also "Pardiso" ...
unlikely's user avatar
  • 7,123
17 votes
2 answers
289 views

Is it OK that SameQ considers expressions identical, although they have different structure?

...
Vladimir Reshetnikov's user avatar
17 votes
1 answer
519 views

Adding three integer sparse matrices is very slow. Adding only two is fast

Bug introduced in 9.0.0 and fixed in 10.0.0 Adding more than two sparse matrices in one step in Mathematica 9 is very slow (in fact I couldn't even wait for it to finish). Here's an example. Let's ...
Szabolcs's user avatar
  • 236k
11 votes
2 answers
537 views

Building sparse array from packed arrays

Suppose I have two large packed arrays a and b, that respectively contain the position of the elements of a square matrix (i.e. <...
VanillaSpinIce's user avatar
7 votes
2 answers
468 views

directly change the background value of a SparseArray?

given a SparseArray s = SparseArray[{1, 10} -> 1, {1, 10}, a] I can extract the background value (see What are SparseArray ...
george2079's user avatar
5 votes
4 answers
476 views

Creating large matrices from sub blocks

I am trying to generate a matrix from square blocks. Effectively, I have three blocks, call them $A_{diag}$, $A_{up}$, $A_{down}$, and a block of zeroes, which I'll just call $0$. I have some integer $...
Kai's user avatar
  • 2,099
2 votes
2 answers
2k views

Create a matrix of matrices using Band and ArrayFlatten [duplicate]

I can easily write a simple Band matrix using: SparseArray[{Band[{2, 1}] -> a}, {Dim, Dim}] where a is a number. Now, I ...
altroware's user avatar
  • 274
29 votes
2 answers
1k views

Wrong eigenvalues from a sparse matrix

Bug introduced after 5.0, in or before 8.0 and persisting through 13.3. I notice in the following example that wrong smallest 2 eigenvalues are resulted if calculating from a sparse matrix. But it ...
xiaohuamao's user avatar
  • 4,728
27 votes
2 answers
2k views

Using the Krylov method for Solve: Speeding up a SparseArray calculation

I'm trying to implement this Total Variation Regularized Numerical Differentiation (TVDiff) code in Mathematica (which I found through this SO answer): essentially, I want to differentiate noisy data. ...
tkott's user avatar
  • 4,949
15 votes
2 answers
285 views

Bug with SparseArray selector in Pick

Bug introduced in version 8 or earlier and fixed in 10.0 I have created a notebook with two cells. This is the content of the first: ...
Adam Reith's user avatar
12 votes
3 answers
675 views

Most efficient way to build block / banded SparseArray

I have a matrix that I generate with a procedure that looks like this: ...
b3m2a1's user avatar
  • 46.9k
10 votes
1 answer
192 views

SparseArray output triggers "unsafe dynamic content" warning

The first basic example in the documentation of SparseArray: ...
Chris Grant's user avatar
6 votes
1 answer
137 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"] ...
MeMyselfI's user avatar
  • 1,116
3 votes
1 answer
1k views

How to get the determinant and inverse of a large sparse symmetric matrix?

For example, the following is a $12\times 12$ symmetric matrix. Det and Inverse take too much time and don't even work on my ...
Orders's user avatar
  • 1,247
2 votes
1 answer
2k views

How to find position of non-zero elements in SparseArray without converting to a dense object

Overview So I have a SparseArray, sparse, constructed from a list of rules, with the dimension options. Given a row, I want to know where the values are non-zero. ...
SumNeuron's user avatar
  • 5,422
20 votes
1 answer
2k views

Sparse Cholesky Decomposition

I am working with square matrices with a special form, which for large rank ($> 100,000$) would be best stored and manipulated as a SparseArray. I believe that ...
Eric Brown's user avatar
  • 4,426
20 votes
1 answer
436 views

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

I found a few references to something called a "pattern sparse array" that does not contain any values, only positions. What is this data structure? How and when do I use it? From the documentation ...
Szabolcs's user avatar
  • 236k
19 votes
3 answers
619 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(1)=...
xzczd's user avatar
  • 67k
19 votes
1 answer
2k views

How to use SparseArray in CUDA?

Sparse arrays are widely used in many scientific applications where memory and time are strictly limited. For a considerable speedup one can use CUDA. Since Mathematica 8 we can conveniently use CUDA ...
ybeltukov's user avatar
  • 43.8k
16 votes
6 answers
1k views

Matrix Rotation

If I have a matrix of any size, say $\begin{pmatrix} 72 & 32 & 64 \\ 18 & 8 & 16 \\ 63 & 28 & 56 \\ \end{pmatrix}$ $\begin{pmatrix} 72 & 32 \\ 18 & 8 \\ 63 &...
Prashant Bhate's user avatar
16 votes
2 answers
473 views

Temporal database reconstruction in mathematica

<EDIT> after some internet research I found the database domain that deals with this kind of thing: Temporal Databases. I think my question therefore becomes how to reconstruct a temporal ...
Hagmonk's user avatar
  • 558
15 votes
2 answers
1k 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 ...
user avatar
12 votes
2 answers
880 views

Analytic determinant of a sparse 25x25 matrix?

I would like to compute the analytic determinant of the following sparse matrix ...
Kagaratsch's user avatar
12 votes
1 answer
4k views

How to work around Mathematica hanging when showing a large SparseArray?

Bug introduced in 9.0 and fixed in 9.0.1 In Mathematica 9, if I display any large SparseArray object in the default way (which looks something like ...
Szabolcs's user avatar
  • 236k
11 votes
1 answer
197 views

What are valid settings for the TreatRepeatedEntries sparse array option?

There is a system option that controls how to build sparse arrays when a position is assigned more than one value. The default setting is First, meaning that all ...
Szabolcs's user avatar
  • 236k
10 votes
5 answers
890 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: ...
Kiril Danilchenko's user avatar
10 votes
1 answer
2k views

Exporting a Large Multidimensional Sparse Array

I'm trying to export a sparse array from Mathematica to share with collaborators who primarily use Matlab. The sparse array in question is 4 dimensional, (72 x 93 x 94 x 172) with ~4M non-zero ...
s0rce's user avatar
  • 9,652
9 votes
1 answer
569 views

Calculating the rank of a huge sparse array

By virtue of the suggestion in my previous question, I constructed the sparse matrix whose size is $518400 \times 86400$, mostly filled with $0$ and $\pm 1$. Now I want to calculate its rank. Since <...
Joonho Kim's user avatar
9 votes
1 answer
313 views

SparseArray to list of lists (column entries) efficiently?

Suppose we have a very big very sparse matrix, e.g.: ...
Leo's user avatar
  • 1,155
8 votes
1 answer
204 views

Rank of singular, large, sparse matrices

I need to find the rank (and eventually do the Gaussian elimination) of a large, sparse, non-square matrix of integers. There are a few methods in Mathematica to find the rank of a non-square matrix (...
Albercoc's user avatar
  • 998
8 votes
2 answers
5k views

How to export an output matrix obtained in mathematica to a matrix in python with the correct format?

I have a large sparse matrix as the output and I want to save the output and open it as a matrix in python. The output is a 6000 x 6000 matrix and I don't know how to make changes to the file such ...
jazzytortoise's user avatar
8 votes
0 answers
146 views

LibraryLink: Create SparseArray directly from row pointers and column indices

In Mathematica, sparse arrays use a CSR representation. Suppose I already have data in this format. Is there a way to create an MSparseArray from it directly with ...
Szabolcs's user avatar
  • 236k
8 votes
3 answers
1k views

How to create a large sparse block matrix

I need to generate a very large sparse block matrix, with blocks consisting only of ones along the diagonal. I have tried several ways of doing this, but I seem to always run out of memory. The ...
Jesper's user avatar
  • 81
8 votes
3 answers
5k views

Constructing Tri-Diagonal Matrices

Fractional Calculus Course, we are instructed to create an $n \times n$ Tri-Diagonal matrix in the form of: \begin{array} a A &= \begin{bmatrix} 2 & -1 & 0 & 0 & ... &...
Kevin_H's user avatar
  • 233