Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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

0
votes
0answers
17 views

How to add the duplicated positions in sparse array? [duplicate]

When we use the SparseArray in such form: SparseArray[{pos1,pos2}->{val}] We know that we don't have duplication of positions in pos1 and pos2. I mean that we ...
3
votes
2answers
39 views

Using elements of a list as SparseArray patterns: “can't be used as part specification”

I don't have much experience with SparseArray, and the search here did not give me an answer to this. I'm trying to numerically compute a certain spectrum, and the ...
0
votes
0answers
23 views

SparseArray with width or height 0

I can store the zero matrix (=module morphism) $R^2\overset{a}{\leftarrow} R^3$ as a=SparseArray[{},{2,3}]. But $R^0\overset{a}{\leftarrow} R^3$ stored as ...
1
vote
1answer
70 views

Sparse matrices dot multiplication

I have two huge sparse matrices, and I am interested in dot multiplication them. But I have a memory issue so a notebook is a shutdown. I am using the 11.3 version. Any suggestion on how to avoid ...
1
vote
2answers
45 views

Filtering sparse matrix

I am interested in filtering a sparse matrix, where the values are between zero and one. I do it in the following manner. ...
4
votes
0answers
152 views

Loopy Belief Propagation

I look for an efficient way to implement a Belief Propagation in Pairwise Markov Random Field. Each node is a binary random variable, and the unary potential function is ...
2
votes
1answer
56 views

Save a list as a value in the sparse matrix

I am interested in save a list (the size of the list is two) of numbers in the sparse matrix, but this is not supported. Maybe you familiar with some workarounds or any suggestion on another data ...
5
votes
2answers
279 views

Replace values in sparse matrix

I am interested in updating the values of the sparse matrix. I have a list of matrix element which I am interested in updating and a list of new values. Currently, I do it in the following manner: <...
1
vote
1answer
83 views

SparseArray: accesing nonzero row and column entries

I have an enormous $m\!\times\!n$ sparse matrix $d$ with $l$ nonzero entries, stored as d=SparseArray[{{i1,j1}->w1,...,{il,jl}->wl},{m,n}];. How do I access ...
9
votes
4answers
301 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: ...
6
votes
1answer
306 views

How to find k largest element and their position in a matrix?

I have a sparse matrix, and I am interested in find k largest elements and their positions in the matrix. I do it in the following manner: ...
1
vote
1answer
72 views

Optimization of a slow function - reason to why it is slow

Edit: Okay, my question was viewed quite a few times but not answered so I assume I phrased/presented in a confusing way. I'll try to rephrase my problem to make it more accessible because luckily I ...
6
votes
1answer
82 views

Efficiently fill sparse matrix involving BSpline

Context In order to implement regularised fitting in 2 or 3 dimensions (as is done say here) using BSplineBasis one needs to evaluate a basis over a set of data. ...
5
votes
1answer
42 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"] ...
2
votes
1answer
48 views

How do I dynamically update a SparseArray?

Say I have a "seed" SparseArray e.g. m=SparseArray[{1->1}]. How do I add a rule at a position that has not yet been defined e....
1
vote
1answer
99 views

Find a few eigenvalues of a HUGE sparse Hermitian matrix

I know that methods such as Arnoldi and FEAST can be employed to only find a few eigenvalues. But now I need to add another ...
4
votes
1answer
89 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 ...
4
votes
1answer
84 views

Reorder matrix for nested dissection

I'm implementing a nested dissection linear solver outside of Mathematica and my original data is actually a graph, from which I have to construct an appropriate adjacency matrix to feed into the ...
2
votes
0answers
28 views

Odd behavior of ArrayReshape with SparseArrays

I am trying to understand why I am seeing inconsistent results when using ArrayReshape on a SparseArray vs. a normal nested <...
4
votes
2answers
272 views

Sparse block matrices all wrong

I need to represent $M-\lambda\cdot\textrm{Identity}$ where $M$ is an operator on $V_1\oplus\cdots\oplus V_n$ made from $$ V_1\xrightarrow{M_1}V_2\xrightarrow{M_2}\cdots\xrightarrow{M_{n-1}}V_n\...
4
votes
1answer
108 views

Generalized linear algebraic equation solver

$\newcommand{\d}{\vec{d}}$ $\newcommand{\S}{\vec{S}}$ In Mathematica one can easily solve a linear system given by $$A \vec{S} = \vec{d}$$ where $A$ is a matrix, simply by using ...
39
votes
2answers
960 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 ...
3
votes
1answer
47 views

Memory issue of SparseArray between Notebook window and Wolfram Documentation window

Recently I'm trying creating a 80000 x 80000 sparse array in Mathematica, and I discovered a strange thing when using SparseArray[]. Code: ...
5
votes
1answer
58 views

Fastest way to drop multiple rows and columns from SparseArrays

I would like to prune some unimportant rows and columns from a SparseArray to accelerate solving some eigenvalue problems. Given ...
5
votes
1answer
136 views

How to speed up working with Sparse Arrays

I make a SparseArray and then modify it by adding rows together. This seems to take a long time. Can it be speeded up? ...
4
votes
1answer
45 views

control symmetry property when exporting matrix as Matrix Market format .mtx

For example, I have h = SparseArray@{{0, I}, {-I, 0}} If I export h as matrix market format(.mtx) like ...
15
votes
0answers
296 views

Wrong eigenvalues from a sparse matrix

Bug introduced after 5.0, in or before 8.0. I notice in the following example that wrong smallest 2 eigenvalues are resulted if calculating from a sparse matrix. But it gives correct result if we ...
0
votes
1answer
53 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
24 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
217 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
32 views

NetTrain doesn't accept sparse input?

The following code fails to execute: ...
11
votes
3answers
262 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
77 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
121 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
60 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
105 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: ...
4
votes
0answers
80 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
446 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
398 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 ...
6
votes
0answers
52 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
22 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
63 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
55 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
142 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
152 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
94 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
206 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
105 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
465 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
102 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 ...