Questions tagged [sparse-arrays]

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

15
votes
0answers
417 views

Wrong eigenvalues from a sparse matrix

Bug introduced after 5.0, in or before 8.0 and persisting through 12.0. I notice in the following example that wrong smallest 2 eigenvalues are resulted if calculating from a sparse matrix. But it ...
0
votes
1answer
56 views

Matrix manipulation instead of loops

I have the following matrix form $[J]$: $$ \begin{bmatrix} \{ v[3],p + v[2]\}\\ \{v[3], p + v[2], 2 p + v[1]\} \\ \{v[3], p + v[2],2 p + v[1], 3 p + v[0]\}\\ \{...
0
votes
0answers
35 views

3D Sparse array [duplicate]

Following the 3D matrix representation shown here Vitaliy Kaurov's answer for "Plot 3D boolean Array or matrix in Mathematica" Is it possible to use a SparseArray ...
7
votes
3answers
256 views

Generating a (sparse?) array from data

I have a list: {{pos_1,val_1},{pos_2,val_2},...} and would like to generate an array of dimension d, with values ...
3
votes
0answers
37 views

NetTrain doesn't accept sparse input?

The following code fails to execute: ...
11
votes
3answers
296 views

Most efficient way to build block / banded SparseArray

I have a matrix that I generate with a procedure that looks like this: ...
4
votes
3answers
91 views

efficient replacing of certain diagonal parts of array/matrix

in writing some code for FEM i need an efficient way to replace parts of the diagonal of a sparse array. Certain positions in the diagonal of the array (given in a list) should be changed to "1". An ...
3
votes
4answers
182 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 $...
6
votes
1answer
69 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 ...
4
votes
1answer
124 views

Partitioning a sparse matrix into sparse sub-matrices

Given a normal matrix, for example m = Array[a,{4,4}] which has dimensions {4,4}, I can partition it by using: ...
5
votes
0answers
99 views

Reproducible kernel crash using Eigensystem with Arnoldi on SparseArray

Using Arnoldi method to find some eigenvalues (say 20), with shift value taken in the middle of the spectrum, of a large sparse matrix the Kernel crashes with no error returned. The crash occurs if ...
4
votes
1answer
517 views

Using dense matrix is faster than sparse matrix in LinearSolve [closed]

I'm doing FEM with Mathematica and have to deal with LinearSolve of a large matrix inside a Newton loop constantly. Hence I need to speed up the computation efficiency of LinearSolve. I'm aware of ...
11
votes
3answers
430 views

Why can't I reshape to any dimensions?

Bug introduced in 11.0 and fixed in 11.3.0 I am trying to reshape a $2 \times 2 \times 2$ array into a $n \times m$ array. It seems that if the matrix is defined via ...
7
votes
0answers
73 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 ...
1
vote
0answers
25 views

Is it safe to change MSparseArrays which lack column indices?

I am using sparse arrays with LibraryLink. I noticed that when MSparseArray_fromExplicitPositions and MSparseArray_fromMTensor ...
3
votes
0answers
65 views

Trying to create rank-1 SparseArray through LibraryLink causes crash

Bug introduced in 10.0 or earlier and fixed in 11.3 When I try to create a 2D SparseArray through LibraryLink's MSparseArray_fromExplicitPositions, it works fine. ...
4
votes
0answers
60 views

Why is SparseArray atomic?

This answer tells us that functions like ReplaceAll do not straightforwardly work for the entries of sparse arrays because a ...
0
votes
0answers
240 views

Inverse of a sparse 21x21 symbolic matrix

I am trying to compute the inverse of the following symmetric sparse(density 0.37) 21x21 matrix involving symbolic expressions in the entries(the variables ...
2
votes
3answers
154 views

How I can generate a matrix from a set of terms?

I want to build a matrix, from the following set $S = \{a b , a c\}$ S = {a b , a c} where the terms of S represent the raw of the matrix ...
0
votes
0answers
134 views

Find Eigenvalues close to a Target for a Generalised Eigenvalue problem of the type Ax=λBx with A and B very sparse

So I have been using Mathematica as a tool to discretise large linear systems of PDEs and cast them as algebraic eigenvalue problems involving very large sparse matrices. These are usually complex and ...
6
votes
1answer
211 views

Why does SparseArray allow some patterns but not others?

When building a SparseArray, patterns of the form {i_, i_} and {i_, j_} are fine, but ...
6
votes
2answers
117 views

Using sparse matrices when value depends on position

I am working with solving a linear system that becomes a tridiagonal matrix. In order to speed up the process for large matrices, I want to use sparse matrices. My problem is that the values are not ...
3
votes
3answers
766 views

How to create a lists of matrices?

I would like to creat a list of matrices from the following rules: 1- All matrices of the list have diagonal equal to zero. 2- The first matrix have 2x2 dimension, ...
2
votes
2answers
126 views

Creating a list of arrays according to given rules

I want to create a list of matrices from the following rules: All matrices of the list have diagonal equal to zero. The first element of the list is a 2x2 matrix, having elements are given by ...
0
votes
1answer
100 views

Parse large ASCII matrix

I am parsing a large ASCII sparse matrix, output of PETSc, but my current implementation is impossibly slow. I don't know how I could speed that up. The ASCII file of a sparse PETSc ...
3
votes
1answer
241 views

Problems with RandomChoice

Consider an Matrix of dimension n1xn1. I would like to get a list with n2 distinct matrix (of dimension ...
6
votes
2answers
212 views

Why the sparse array is ill-conditioned?

Bug introduced in 8.0 or earlier and persisting through 11.1.0 or later Given that I have a knot vector $\mathbf U$ and a set of parameter values $\mathbf T$: ...
2
votes
1answer
136 views

Build sparse matrix in mathematica knowing position i,j and value to store, from file

I have a file formatted in this way: Row Column Value i j Vij i= INT Number row where in sparse matrix I want the ...
3
votes
2answers
156 views

Merging SparseArrays (or normal arrays)

I have a list of SparseArrays, and I want to combine them into one array that concatenates their rules. I don't want to connect them together like ...
6
votes
1answer
138 views

Multiplying SparseArray by negative number Crashes Kernel

Bug introduced in 9.0 and persisting through 11.0 Can anyone reproduce the following bug, or figure out a work-around. The following crashes the kernel on both Mathematica 10 and 11, on my macs, ...
6
votes
1answer
126 views

Nonsparse SparseArray crashes the Kernel

Bug introduced in 10.0 and fixed in 11.1 On Windows 10, 64 bit, version 11.0.1, my kernel just started crashing for no reason as for as I can see. Does/should the following crash your kernel? ...
3
votes
0answers
53 views

Map entries of StructuredArray

I have the following defintion arr = SymmetrizedArray[{1, 2} -> 1, {4, 4}, Antisymmetric[{1, 2}]] which gives a ...
0
votes
0answers
46 views

How to modify the default value of a `SparseArray` without intermediate non sparse matrices [duplicate]

How to modify the default value of a SparseArray? All of my ideas doesn't work, because of the ReadProtected attribute. Is there ...
6
votes
2answers
202 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 ...
7
votes
1answer
201 views

Bug? Strange behaviour of FullSimplify when checking an equality of SparseArray's

Edit (15 Dec '16): bug noticed in v11.0 and reported as CASE:3796403. Edit (03 May '17): bug fixed in v11.1 (verified in v11.1.1). I stumbled upon the following strange behaviour. Disclaimer: I ...
7
votes
4answers
403 views

How to find position where a sequence drops off to zero

Suppose I have a sequence seq = {0,0,0,1,2,3,1,0,0,0,4,5,8,0} and I want to find the first position after which the sequence drops to zero (which in this case, is ...
0
votes
1answer
208 views

How to build a histogram from the mean of 100 matrices

I have a matrix that looks like this: ...
0
votes
0answers
27 views

Sparse (Weighted) Adjacency Matrix & SparseArray: encoding multiple weights at a given edge [duplicate]

Context Below is just an arbitrary example. It is not perfect, but hopefully it serves the point of what I would like to accomplish. Say you have a sparse adjacency matrix, $\bf{A}$, where a value ...
2
votes
1answer
83 views

How to fill in matrices by enforcing a condition on each entry quickly?

In the process of solving a 3D time-dependent PDE, I need to fill several matrices which their sizes are quite high. These matrices are constructed based on the radii of the mesh nodes in the 3 ...
5
votes
3answers
116 views

Sparse array approach to generate a Colm Mulcahy matrix

I wrote: ...
2
votes
1answer
826 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. ...
1
vote
1answer
183 views

How to create a matrix with some conditions?

I want to create an $n \times n$ matrix (eg. $n=5$) using the following steps: 1) The entries aii (i = 1, ..., n) that form the main diagonal of a square matrix are 0 (aii=0); In C++, the element of ...
5
votes
1answer
214 views

Pardiso Linear Solve Method

It seems that "LinearSolveMethod" -> {"Pardiso"} is not available on the Raspberry Pi 3 version (10.3, 32-bit Linux, ARM) of Mathematica. Is there a package that ...
1
vote
0answers
42 views

Looking for a package for data on (sparse) uniform grids

I have large amounts of sparse 2d and 3d "volumetric" data, i.e. fixed data associated with positions in a uniform (integer coordinate) grid. I think of it as partial function $$f: \mathbb Z^n \to D.$...
4
votes
1answer
154 views

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 ...
6
votes
2answers
2k 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 ...
4
votes
1answer
437 views

Why am I losing precision in computation of eigenvectors of sparse matrices?

I compute the eigenvectors and eigenvalues of the same matrix in its dense and sparse version. The eigenvalues are the same to great precision, but the eigenvectors are not: when I compute the ...
1
vote
1answer
136 views

Converting a list of lists of indices into a sparse matrix of ones and zeros

I have data formatted like {{a,b},{c,d},{e,f}} where each entry is an integer. I need to turn this in to a sparse array with entries ...
4
votes
0answers
88 views

Why isn't SparseArray working in this case?

This works: SparseArray[{{1, 1} -> 1, {2, 2} -> 2}]\ (* {{1, 0}, {0, 2}} *) This works: ...
6
votes
1answer
198 views

Efficiently compute this Table of NullSpace

I have two $(2n,2n)$ matrices, $A_1$ and $A_2$, and I would like to compute $$\ker(A_1^p A_2^q -I)$$ for $p,q\leq 2n$. Both matrices are orthogonal and have exactly four non-zeros values on each ...