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
0
votes
1answer
39 views

Construct a matrix from blocks of matrices

I am trying to find eigen values of a matrix system where entries of the matrix are also matrix. This is how my matrix looks: $$ M=\begin{bmatrix} H[j,j] & T[j,j+1] & 0 &0 &....
2
votes
2answers
84 views

Elegant way to construct a matrix with symbolic entries

I would like to construct a matrix $G$ composed of block matrices $G^{(i)}$ as defined entry-wise below stacked vertically as $$G = \begin{bmatrix} G^{(1)} \\ G^{(2)} \\ \vdots \\ G^{(i)} \\ \vdots \\ ...
2
votes
1answer
39 views

How do I pass indices to a function when constructing a SparseArray?

I am trying to speed up my Mathematica code, so instead of testing and calculating the same thing again and again I want to save the result in a SparseArray. The goal is to generate a matrix which I ...
0
votes
1answer
57 views

Import and export a list of SparseArrays efficiently?

I have a list of integers dims and a list of SparseArrays bdrs (representing a chain complex ...
0
votes
0answers
44 views

How to find the rank of a sparse-array list without memory leaks?

I have a very wide list of sparse arrays (I basically just used the numbers as an encoding mechanism for vectors of polynomials), and I want to find out whether they're linearly independent. How ...
7
votes
1answer
143 views

Efficiently populate sparse matrix with band structure

I'm trying to efficiently populate elements of a very large (2^20 x 2^20) symmetric matrix with 1s - luckily the matrix is very sparse, <0.1% filling. Further, the matrix has a very well defined ...
9
votes
3answers
223 views

Multiplying two complex SparseArray objects, yielding empty SparseArray crashes kernel

Problem: When I multiply together two sparse matrices that should give back the 0 matrix, where at least one element among the two is complex and at least one is not an exact number, the kernel ...
6
votes
1answer
115 views

SparseArray to list of lists (column entries) efficiently?

Suppose we have a very big very sparse matrix, e.g.: ...
1
vote
0answers
49 views

How to apply PolynomialMod to a SparseArray?

I have a list bdrs of SparseArrays, whose entries are polynomials in the variable t with ...
9
votes
2answers
148 views

`Count` for `SparseArray`

Why does Count not work with SparseArrays, and is there any function I can use to replace it? What would be an efficient ...
6
votes
0answers
81 views

Memory usage for smallest eigenvalues

I have a bunch of hermitian matrices which are huge (of order 2^17 x 2^17) but extremely sparse so that, when I build the matrices, the usage of RAM is low (say of order 1 GB or similar). The ...
8
votes
1answer
255 views

Reusing PARDISO symbolic factorization

Is there a way to do this natively in Mathematica? I read in several places that this is possible with LibraryLink, but I don't have a lot of experience with C and so I'm having trouble implementing ...
1
vote
1answer
63 views

Arnoldi method misses eigenvalues degeneracies for very sparse matrices

I am here to signal a problem very very similar to the one already discussed here Wrong eigenvalues from a sparse matrix In particular, I have a very sparse matrix and I am asking just few dominant ...
3
votes
1answer
85 views

How to speed up my code using Sparse-array possibly?

I created two loops using Parallel table and a table. The outer loop causes a (nstates by nstates) dimension matrix and the inner loop calculates the matrix elements based on their position in the ...
5
votes
1answer
51 views

Create block matrix with secondary diagonals using Band function

Given matrices m1, ... , mk and matrices n1, ... , nk-1, I would like to define a block matrix $$ m=\left( \begin{array}{rrrr}m1 ...
2
votes
1answer
43 views

Fastest way to ArrayFlatten a 2D array of 2D arrays, both for Sparse and Dense

I am using Mathematica 12 and want to know what is the fastest way to transform a 2D array of 2D arrays both for dense and sparse arrays. I remember looking for this a few versions of Mathematica ago, ...
5
votes
1answer
92 views

How to speed up this SparseArray construction

This code is (obviously) getting slower with the growing number of trials, currently running over 5 hours. This stylised example takes 2.5 minutes. It is not entirel illustrative as the real situation ...
1
vote
0answers
68 views

Why did LinearSolve give me two different results?

Given that I have a sparse array as follows: ...
6
votes
1answer
54 views

NearestTo causes a kernel crash when applied to a list of SparseArrays [closed]

Nearest and NearestTo cause a kernel crash on Mathematica 12.0.0 for Mac OS X. (running on OS X 10.11.6) when operating on a list of SparseArray vectors. Here's my minimal breaking example: ...
0
votes
1answer
71 views

How does Indexed work in terms of sparse array

I want to use indexed given that s is an element [0,500] but I am unsure how to write that without getting a format error or a tensor error. ...
0
votes
0answers
104 views

Determining the price of an option using Black-Scholes

Code for randomly generated interest rates: ...
3
votes
1answer
33 views

Condition in SparseArray does not check the condition for some matrix elements

Consider the following MWE: SparseArray[{{i_, j_} /; (IntegerDigits[i - 1, 2, 2][[1]] == 0) :> 1}, {4, 4}] This should give a $4\times 4$ matrix in which the ...
2
votes
1answer
41 views

How can I create SparseArray by imposing conditions?

I have two lists: one for directed links list1 and another list2 for the values associated with the links. ...
7
votes
2answers
222 views

Fastest way to update rows of an array of SparseArrays

I have an array of sparse arrays (check update 3 below for a concrete example that has the same qualitative features as what I use in my real code) , say ...
0
votes
0answers
26 views

Filling Regions (Blocks) of a Binary Matrix with Different Probabilities

This is an extension of the following question: How to generate binary array whose elements with values 1 are randomly drawn, where the most elegant answer was posted in the comments: ...
7
votes
1answer
149 views

Efficiently calculating matrix exponential of a recursively generated matrix

Given a matrix $A$ of size $N \times N$, I want to evaluate the function \begin{equation} F(t) = |\operatorname{Tr}(\exp(-i A t))|^2 \end{equation} as a function of $t$. Here, $A[n]$ is constructed ...
2
votes
1answer
48 views

Efficient Linear Operators on SparseArrays

I have a rank four SparseArray of dimension {40, 40, 40, 40}. I wish to apply a linear operator to it, whose form I know abstractly. That is, I have a simple closed form for the action of the operator ...
2
votes
1answer
95 views

How to RowReduce a sparse matrix?

I want to compute the reduced row echelon form of a sparse matrix, but RowReduce returns a dense matrix. Is there a build-in function that computes the reduced form ...
4
votes
3answers
105 views

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 ...
8
votes
1answer
261 views

Finding the null space of a large sparse matrix

I am trying to compute the null space of a large $n\times n$ sparse matrix. No matter how large the dimension of the matrix is, it will always look like as below: The figure on the left shows the ...
4
votes
0answers
71 views

ParametricNDSolveValue with SparseArrays

I want to solve large, sparse matrix systems with ParametricNDSolveValue and was hoping to use SparseArray to speed up the computation. However I find that ParametricNDSolveValue doesn't seem to work ...
3
votes
1answer
80 views

Signed cell adjacency matrices (boundary operators)

In How to obtain the cell-adjacency graph of a mesh? there are some great answers that show how to obtain the cell adjacency matrix of a mesh. How would one generalize these to incorporate the ...
19
votes
1answer
398 views

Efficiently defining a SparseArray function

I've got a fairly large SparseMatrix (~60,000 entries in a ~10,000-by-10,000 matrix) that depends on two parameters (im1 and <...
6
votes
1answer
173 views

Speeding up tensor contractions and multiplication

Consider a tensor $T\in\mathbb{R}^{N\times N\times N\times M}$ and two vectors $x,y\in\mathbb{R}^N$. I want to compute the $N\times M$ vector defined by $X_{ij}=\operatorname{tr}(x^\top T_{:,:,i,j}y)=\...
1
vote
0answers
30 views

Computing the invariant factors of a sparse matrix

Let M be a sparse $n\times m$ integral matrix, where $nm$ is very large. The goal is to compute its invariant factors. The naïve approach ...
8
votes
1answer
101 views

SparseArray: how to efficiently extract/delete many entries?

This is a follow-up of this question. We have a large $m\!\times\!n$ matrix stored as a SparseArray a and a list of positions pos...
19
votes
3answers
430 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)=...
9
votes
3answers
558 views

Optimising subdiagonal shift matrix generation time

I want a square matrix $A$ such that $$A_{i,i-1} = 1 \ \forall i\\ A_{i,j} = 0 \text{ for } \ j \ne i-1$$ I have tried ...
5
votes
2answers
347 views

Lowest Magnitude Eigenvalues of Large Sparse Matrices

I am trying to find the first three lowest eigenvalues of large sparse matrices of size range $10^3 - 10^5$. The matrices depend on some parameter $x$, so I first construct the matrices and then use ...
0
votes
0answers
61 views

Inverse of Sparse Diagonal Array not Sparse

Kind of bummed that when I take the inverse of a matrix that is a diagonal sparse array, the result in not a sparse array. Further, the time to compute the inverse is the same as the time to compute ...
0
votes
1answer
64 views

Time used by Mathematica to calculate tridiagonal matrix

I have a question. I need to find the eigenvalues and eigenvectors of a tridiagonal matrix of size NxN. Can you tell me how much time does Mathematica need to do that in minutes? for Size N ...
4
votes
5answers
692 views

How to generate binary array whose elements with values 1 are randomly drawn

I am trying to generate a binary matrix (whose elements are 0 or 1) of dimension 20x20. To do this, I want to supply as input the number of matrix elements that will take value 1. After, I want to ...
13
votes
2answers
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 ...
10
votes
2answers
785 views

Quickly creating a sparse array

The motivation for my question is that I'm creating very large, very sparse symmetric matrices and finding the largest eigenvalue. (Think upwards of $10^5\times 10^5$ size matrices, with at most $10^...
0
votes
0answers
34 views

Selecting some vectors from a matrix

I am trying to write a code for band matrices that store only the nonzero vectors. For example if I have tridiagonal matrix of size 10, I want to store only the three diagonals as a vectors. n here ...
11
votes
4answers
1k views

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

I have the following code: ...
9
votes
0answers
175 views

Matrix-free Arnoldi method for eigensystems

I am solving a generalized eigenvalue problem $$\mathbf S\,\mathbf x = \lambda\,\mathbf M\,\mathbf x$$ w/ $\mathbf S := \mathbf B\,\mathbf A^{-1}\,\mathbf B^{T}$, and $\mathbf A$ is a sparse ...
2
votes
1answer
232 views

Bottom-most left-most entries in a sparse array

I have a list bdrs of SparseArrays, which represent the boundary matrices of a chain complex $R^{d_0}\overset{\partial_1}{\...
2
votes
1answer
128 views

Indices of nonzero rows in a sparse array

Given $A\!\in\!\mathbb{Z}^{m\times n}$ stored as a=SparseArray[{...},{m,n}], how can I obtain the list of all indices $i\!\in\!\{1,\ldots,m\}$ for which the $i$-th ...
0
votes
1answer
97 views

Read text files as sparse arrays and concatenate in a loop

I would like to read multiple text files and build a sparse array by concatenating row-wise one after the other. The text files are ordered in the following format: \begin{matrix} 0, & 0, &...

1
2 3 4 5 6