37 votes

How to check if a vector is an eigenvector of a matrix using mathematica?

You could use MatrixRank. Here is a function that does this: ...
Carl Woll's user avatar
  • 131k
28 votes
Accepted

How to speed up `RotationMatrix`?

Having this problem so often, I also generated some tools to handle it which I'd like to share. This is the code (along with a usage message which is basically a small modification of ...
Henrik Schumacher's user avatar
28 votes
Accepted

Speed up Do loops in MMA 13.0

Here's the fastest I've found: ...
Michael E2's user avatar
  • 236k
26 votes

How to roll a graph on the y-axis

We calculate the tangent vector of parametric curve {t^2,t} and the ArcLength from 0 to <...
cvgmt's user avatar
  • 72.9k
25 votes
Accepted

Find Elementary Matrices that produce RREF

Update April 29 2023 There is a newer version here Show steps in finding the reduced row echelon form of a symbolic matrix which now works on symbolic matrices also. edit May 12, 2022. Added frame ...
Nasser's user avatar
  • 144k
22 votes

Get the coefficient matrix from a quadratic form

Here is a very short solution: qf = a x^2 + b y^2 + c z^2 + 2 d x y + 2 e x z + 2 f y z; 1/2 D[qf, {{x, y, z}, 2}] (* ==> {{a, d, e}, {d, b, f}, {e, f, c}} *) ...
Jens's user avatar
  • 97.3k
22 votes
Accepted

How can I reproduce the result of PrincipalComponents

I vastly prefer the formulation in terms of the singular value decomposition (SVD). This question gets asked a lot by users of these functions, so here's a quick reference: ...
J. M.'s missing motivation's user avatar
22 votes

What are the counterparts in Wolfram to left and right division of a matrix in other programming language, e.g. Julia and MATLAB?

I just stumbled upon it! At least when storing the factorization in a LinearSolveFunction object, we can use it for the transposed solve by supplying a further (not ...
Henrik Schumacher's user avatar
22 votes
Accepted

Efficiently defining a SparseArray function

The way the code is written, you can neither exploit packed arrays nor any vectorization. There are two major reasons: Using Rule prevents using packed arrays ...
Henrik Schumacher's user avatar
22 votes
Accepted

Can someone boost even more my code?

Exploitation of low rank structure The ordering of summation/dot products is crucial here. As aooiiii pointed out, mat2 has a low-rank tensor product structure. So ...
Henrik Schumacher's user avatar
22 votes
Accepted

How can I construct this matrix?

a[n_Integer?Positive] := Array[Mod[#2 - #1, n] + 1 &, {n, n}] a[6] // MatrixForm $$ \left( \begin{array}{cccccc} 1 & 2 & 3 & 4 & 5 & 6 \\ ...
Roman's user avatar
  • 47.5k
21 votes

What are the counterparts in Wolfram to left and right division of a matrix in other programming language, e.g. Julia and MATLAB?

The documentation for MATLAB's mrdivide states that The operators / and \ are related to each other by the equation B/A = (A'\B')' In light of this we may ...
C. E.'s user avatar
  • 70.6k
21 votes

Speed up Do loops in MMA 13.0

Hmm = With[{s = s}, Compile[{}, Table[1/(r + c - 1), {r, 1, s}, {c, 1, s}]]][]; // AbsoluteTiming (* {0.707614, Null} *) If you insist on ...
xzczd's user avatar
  • 66.2k
20 votes

Is it possible to use the DGEEV and DSYEV LAPACK subroutines in Mathematica?

You can use LibraryLink to call LAPACK directly. It involves however quite a lot of boilerplate code. Here is an example: ...
Henrik Schumacher's user avatar
19 votes

Elegant operations on matrix rows and columns

There are some internal, undocumented functions for row and columns operations: ...
Michael E2's user avatar
  • 236k
19 votes

Why does MatrixFunction with Sinc return this error?

Two things come into play: Sinc'[0] yields Indeterminate, when it should be $0$. This value needs to be evaluated in the ...
J. M.'s missing motivation's user avatar
19 votes
Accepted

How to efficiently input matrices in Mathematica (compared to what MATLAB offers)

Quick-n-dirty. I dispense with open/close bracket, trivial to put in if it matters: ...
ciao's user avatar
  • 25.8k
19 votes
Accepted

Fast MATLAB, slow Mathematica: Export a matrix as TSV

After using them for several years, I have to admit that many Export routines in Mathematica are either broken or way too slow (a factor of 10 to 25 when compared ...
Henrik Schumacher's user avatar
19 votes

How to solve this matrix equation

mat = ({{1, x, 3}, {2, 4, 5}, {2, 4, x}}); Select[MatrixRank[mat /. #] == 2 &][Solve[Det[mat] == 0, x, Reals]] {{x -> 2}, {x -> 5}}
kglr's user avatar
  • 395k
19 votes
Accepted

Finding "chains" of numbers in a matrix

Maybe someone can build on this to actually get all such paths, but in the meantime, here's something to get you started! Here's how you can get a graph whose vertices are elements of the matrix at ...
thorimur's user avatar
  • 9,010
19 votes

Speed up Do loops in MMA 13.0

As other answers have suggested, a better approach here is to stick to high efficient functional way, and try to take advantage of "vectorizable" operation when possible. For OP's question, ...
18 votes
Accepted

How to plot the $\varepsilon$-pseudospectrum of a matrix

I still haven't gotten around to implementing the Arnoldi-based algorithm of Toh and Trefethen, so what follows is straightforward, but not very efficient code for visualizing $\varepsilon$-...
J. M.'s missing motivation's user avatar
17 votes

Matrix indexed by one number

Partition[ Array[a, 4], 2] will do it. In general, makeMat[n_, m_] := Partition[ Array[a, n*m], m]
Szabolcs's user avatar
  • 235k
17 votes
Accepted

Square root of a 2 by 2 matrix using MatrixPower[]

The nice thing about Mathematica is that one can (almost) always go back to the definitions if in doubt. For evaluating non-principal roots, one can just go back to the Jordan decomposition of a ...
J. M.'s missing motivation's user avatar
17 votes
Accepted

Are compound matrices implemented in mathematica?

I don't think that they are built-in, but they are easy enough to implement: ...
Henrik Schumacher's user avatar
16 votes
Accepted

Expressing the n-th power of a matrix

You need to understand that Mathematica prefers to write some numbers in their closed form because with numerical values, you would loose information and probably precision. It is kind of why it is ...
halirutan's user avatar
  • 113k
16 votes

Noise filtering in images with PCA

Start data First let us get some images. I am going to use the MNIST dataset for clarity. (And because I experimented with similar data some time ago.) ...
Anton Antonov's user avatar
16 votes
Accepted

Why doesn't Mathematica return the identity matrix for the 0-th power of a singular square matrix?

Consider a general $2\times 2$ matrix, and its general power $n$: ...
corey979's user avatar
  • 24k
16 votes
Accepted

qz like function in Mathematica

As already hinted, Mathematica is perfectly capable of computing the eigensystem of a pencil. However, none of the commenters gave the function that is the closest to the functionality of MATLAB's <...
J. M.'s missing motivation's user avatar
16 votes
Accepted

How to merge join two matrixes?

ma = Array[Subscript[a, #1, #2] &, {3, 3, 2}]; MatrixForm[ma] ...
Daniel Huber's user avatar
  • 51.8k

Only top scored, non community-wiki answers of a minimum length are eligible