# Questions tagged [sparse-arrays]

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

272 questions
Filter by
Sorted by
Tagged with
70 views

### Fast definition of sparse tensor

I'm defining a sparse array through ...
68 views

### Lowest Eigenvalue of a large sparse matrix without choking up the RAM (Corrected) [closed]

I am trying to find the lowest eigenvalue of a large sparse matrix of dimension N (=$3^n$, where n is number of particles in system ) using ...
68 views

### Memory leak when creating a SparseArray

Problem: I have a list e={u,v,w} and wish to construct a sparse matrix with entries {u[i],v[i]}->w[i]. Note that the same ...
54 views

### What is the fastest way to get the lowest eigenvalue and corresponding eigenstate in Mathematica?

I tried to use the Arnoldi method to get the smallest eigenvalue and corresponding eigenstate for large matrix. However, Arnoldi did not give me the desired result: ...
81 views

### Efficient construction of a SparseArray from LIL (list of lists of column entries)

In Python scipy.sparse, there are methods to convert between CSR, CSC, LIL, DOK, etc. implementations of a sparse matrix. What is the most efficient way in Mathematica to construct a ...
95 views

### Insufficient memory problem in Mathematica

I am trying to initialize a 48600 x 48600 packed array from a sparse matrix and a diagonal matrix in Mathematica 8. If S1 is a sparsearray of that size then and S2 is list of 48600 length, then I am ...
68 views

### How to efficiently upsample a greyscale image to generate a representative binary image

I want to create binary images by upsampling a series of greyscale images (192 x 144 resolution) by a factor of 10 with the new pixel values being a pseudorandom distribution of 0's & 1's based on ...
93 views

### Creating a block matrix from arrays of blocks

I am trying to generate a matrix from square blocks. Effectively, I have a $n×n$ matrix polynomial $P(l)$, the $qth$ derivative of $P(l)$ with respect to $l$, which is denoted by $P^{(q)} (l)$, and a ...
175 views

### Permute association key and generate a sparse array

Imagine having an Association Av = <|{1, 4, 2, 3} -> a, {2, 4, 2, 4} -> b|> It can be much longer. I am searching ...
54 views

### How can I define a function using ArrayFlatten without using SetDelayed?

I created a matrix F2 using SparseArray and ArrayFlatten as follows: ...
59 views

### Seeming inconsistencies with “Eigenfunction” [duplicate]

I am working on a problem which involves calculating the spectrum of a family of 2970x2970 Hermitian matrices. I have done similar things with Mathematica in the past, but this time there is an odd, ...
59 views

43 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 ...
72 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 ...
47 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 ...
172 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 ...
231 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 ...
155 views

### SparseArray to list of lists (column entries) efficiently?

Suppose we have a very big very sparse matrix, e.g.: ...
54 views

### How to apply PolynomialMod to a SparseArray?

I have a list bdrs of SparseArrays, whose entries are polynomials in the variable t with ...
203 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 ...
104 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 ...
305 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 ...
98 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 ...
94 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 ...
62 views

Given matrices m1, ... , mk and matrices n1, ... , nk-1, I would like to define a block matrix $$m=\left( \begin{array}{rrrr}m1 ... 1answer 49 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, ... 1answer 102 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 ... 0answers 71 views ### Why did LinearSolve give me two different results? Given that I have a sparse array as follows: ... 1answer 58 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: ... 1answer 84 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. ... 0answers 110 views ### Determining the price of an option using Black-Scholes Code for randomly generated interest rates: ... 1answer 37 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][] == 0) :> 1}, {4, 4}] This should give a 4\times 4 matrix in which the ... 1answer 43 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. ... 2answers 238 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 ... 0answers 34 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: ... 1answer 183 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 ... 1answer 52 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 ... 1answer 137 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 ... 3answers 108 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 ... 1answer 360 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 ... 0answers 73 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 ... 1answer 100 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 ... 1answer 417 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 <... 1answer 216 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)=\... 0answers 33 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 ... 1answer 128 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... 3answers 477 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)=...
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 ...
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 ...