Questions tagged [sparse-arrays]

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

328 questions
11
votes
2 answers

Efficient by-element updates to SparseArrays

I have a very large SparseArray called A. What is the most efficient way to update say element {i,j} with value x to the value f[x] ? I worry about memory usage and code speed if I need to make a very large number of updates. I've seen Leonid's…
Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
9
votes
1 answer

How can I efficiently ArrayFlatten a matrix of sparse matrices?

I have a matrix of sparse matrices that I want to convert to a sparse block matrix. So, basically I want what ArrayFlatten does, except that I want the output to be sparse. So e.g.: sp = SparseArray[{{2, 2} -> 1.}, {50, 50}]; sparseArrayFlatten[ { …
Niki Estner
  • 36,101
  • 3
  • 92
  • 152
7
votes
1 answer

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: tst = SparseArray[{1, 0, 1, 0, 1}] - SparseArray[{1, 0, 1, 0,…
dcutrell
  • 73
  • 6
7
votes
1 answer

Opposite function to Normal

Is there any function opposite to Normal? For example, let as say that I have an SparseMatrix A, then, let as say that for convenience for a certain operation like Packing, I convert it into an Array by doing A=Normal[A]; and afterwards, I want A…
Mencia
  • 1,324
  • 12
  • 26
7
votes
0 answers

Is there a way to make SparseArray distinguish between background values of different precision that are numerically equal?

To illustrate what I mean, consider the following: SparseArray[{{1} -> 1, {5} -> 1.}, Automatic, 0] // Normal {1, 0, 0, 0, 1.} As you can see, the distinction between 1 and 1. is preserved. Now consider: SparseArray[{{1} -> 1, {5} -> 1.},…
Sjoerd Smit
  • 23,370
  • 46
  • 75
7
votes
2 answers

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 Properties? How and when should they be used? ) s["Background"] a but is there a direct way to change it? We can of course…
george2079
  • 38,913
  • 1
  • 43
  • 110
6
votes
1 answer

DeleteDuplicates without destroying SparseArray

When I use DeleteDuplicates to delete equal rows of a sparse array, the output is not a sparse array anymore. In my application, the sparse arrays are huge but very sparse, so converting back and forth is quite annoying. Is there a version of, or…
Gert
  • 1,530
  • 8
  • 22
6
votes
1 answer

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"] {{1, 1}, {2, 2}} How to make it notice this?
MeMyselfI
  • 1,116
  • 5
  • 12
5
votes
2 answers

Efficiently construct a SparseArray of a given "shape"

I have a given SparseArray, let's call it A. I want to construct another SparseArray, let's call it B, which has the same "shape" as A. Right now, I construct B by applying a certain defining function on the positions of the elements of A. So…
coussin
  • 361
  • 1
  • 8
5
votes
3 answers

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 matrix looks…
Fraccalo
  • 6,057
  • 13
  • 28
5
votes
1 answer

How does AdjacencyLists work on SparseArray

I have been working with large SparseArray and have noticed the properties are very very useful and fast. I wanted to manipulate my large sparse array in ways similar to the properties. Looking at the full form seemed to give some clues. If I have a…
ADDonut
  • 139
  • 6
4
votes
0 answers

Efficient storage of Kronecker sum of circulant matrices+diagonal matrix

I have a square matrix of the form $$M=D+\overset{N}{\underset{n=1}\bigoplus}R_n,$$ where $R_n$ are circulant matrices, $D$ is a diagonal matrix and $\oplus$ is Kronecker sum. Its size is quite big, but as can be seen from its form, it's in fact not…
Ruslan
  • 7,152
  • 1
  • 23
  • 52
4
votes
3 answers

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 one: MaxSpar[matr_] := Module[{curr, ms = 0}, …
mavzolej
  • 693
  • 4
  • 10
3
votes
1 answer

Transforming a huge rectangular table to Sparse Array

I am interested in the most efficient way to transform a huge rectangular array to a sparse array structure. Consider a numeric rectangular table that is rather sparse, with zero positions being set to Null rather than 0, which saves time and memory…
Yasha Gindikin
  • 458
  • 3
  • 7
3
votes
1 answer

Product of non-zero elements in sparse array

How can I multiply the non-zero elements of a SparseArray? Example: a = SparseArray[{1 -> 1, 3 -> 2, 7 -> 3}] Times @@ a (* this returns 0, but I need 6! *)
Danvil
  • 1,505
  • 1
  • 10
  • 17
1
2