Hot answers tagged

18

The comments by both Michael E2 and J. M. ♦ are already an excellent answer, so this is just my attempt at summarizing. Undocumented means just what it says: there need not be any reference pages or usage messages, or any other kind of documentation. There are many undocumented functions and if you follow MSE regularly, you will encounter them often. Using ...


17

LinearSolve[] actually computes a permuted Cholesky decomposition; that is, it performs the decomposition $\mathbf P^\top\mathbf A\mathbf P=\mathbf G^\top\mathbf G$. To extract $\mathbf P$ and $\mathbf G$, we need to use some undocumented properties. Here's a demo: mat = SparseArray[{Band[{2, 1}] -> -1., Band[{1, 1}] -> 2., Band[{1, ...


16

Using the properties of Block matrices: $$\det\begin{pmatrix}\mathbf A&\mathbf B\\\mathbf C&\mathbf D\end{pmatrix}=\det(\mathbf A)\det\left(\mathbf D-\mathbf C\mathbf A^{-1}\mathbf B\right)$$ To visualize your matrix: mat1 = mat; {mat1[[;; 10, ;; 10]], mat1[[;; 10, 11 ;;]], mat1[[11 ;;, ;; 10]], mat1[[11 ;;, 11 ;;]]} = Range@4; (* cool :) *) ...


12

Mathematica actually has a function purpose-built for the operation you're looking for. MatrixFunction[f, m] gives the matrix generated by the scalar function f at the matrix argument m. In your case, MatrixFunction[p, A] will return the 3-by-3 zero matrix, as desired.


12

To verify that matrix is a zero of its characteristic polynomial, The Characteristic polynomial of the matrix is found, then evaluated for the matrix. The result should be the zero matrix. Clear[x] a = {{-1, -4, -2}, {0, 1, 1}, {-6, -12, 2}}; n = Length[a]; p = CharacteristicPolynomial[a, x]; (Sum[ Coefficient[p, x, i] MatrixPower[a, i], {i, 0, Exponent[p, ...


12

Setting the Method option to "CofactorExpansion" results in the correct output. mat = {{2, 2.161209223472559` + 1.682941969615793` I}, {2.161209223472559` - 1.682941969615793` I, 2}} Inverse[mat, Method -> "CofactorExpansion"] $\ $ {{-0.57092 + 0. I, 0.616939 + 0.480412 I}, {0.616939 - 0.480412 I, -0.57092 + 0. I}} As you want to perform ...


11

It's pretty clear that the complexity of this function is $\text{O}(n^3)$, since we're iterating over each element of a matrix (a factor of $n^2$) and taking a sum at each one (an additional factor of $n$). As a former USACO competitor, I don't like $\text{O}(n^3)$ algorithms. However, we can't really do anything to reduce the complexity; LU decomposition ...


11

The Pitsianis-Van Loan algorithm turns out to be surprisingly easy to implement in Mathematica: nearestKroneckerProduct[mat_?MatrixQ, dim1_?VectorQ, dim2_?VectorQ] /; TrueQ[Dimensions[mat] == dim1 dim2] := Module[{bv, cv, sig}, {bv, sig, cv} = SingularValueDecomposition[Flatten[ Map[Composition[Flatten, Transpose], ...


11

I needed this decomposition to answer another question, so I broke down and implemented it myself. The code is more or less a straightforward translation of the pseudocode in Golub/Van Loan: LDLT[mat_?SymmetricMatrixQ] := Module[{n = Length[mat], mt = mat, v, w}, Do[ If[j > 1, w = mt[[j, ;; j - 1]]; v = ...


10

The Susyno and LieART Mathematica packages can do this. I know best the first one (I wrote it), so let me use it as an example in this answer. Assuming that you have installed and loaded the package in a Mathematica session, your example ($\mathbf{10}\times\mathbf{27}$ in $SU(3)$) is computed as follows: ReduceRepProduct[SU3, {{3, 0}, {2, 2}}] This ...


10

As indicated in the comments, machine-precision linear algebra operations in Mathematica use the Intel MKL library optimized implementation of BLAS/LAPACK. That is the case for all platforms where MKL is available: Windows, Linux and Mac OS X (there will be no obvious MKL library files present in the layout on OS X in 10.1 or later due to static linking). ...


10

There is another option, using the relatively new tensor capabilities of Mathematica. This is pretty much copied from another answer by jose, but I don't need any assumptions here: TensorExpand[KroneckerProduct[X, X] + KroneckerProduct[-X, X]] (* ==> 0 *) TensorExpand[KroneckerProduct[2 X, 3 Y]] (* ==> 6 KroneckerProduct[X, Y] *) There is a ...


10

Maybe m1 = {{1, 2}, {3, 4}}; m2 = m1*10; m3 = m1/10; (plus = Inactivate[m1 + m2 + m3, Plus]) /. a_List :> MatrixForm@a Activate@plus


9

One possibility is to temporarily inactivate the arithmetic operators, like so: Block[{Times = Inactive[Times], Plus = Inactive[Plus]}, Det[{{a, b}, {-a, -b}}]] a*(-1*b)+-1*b*(-1*a)


9

It seems like RandomReal[1., 2] is automatically a packed array, whereas {1., 2.} is not. Notice; (Outer[Times, list1, Developer`ToPackedArray@{1., 2.}];) // AbsoluteTiming // First (* 0.032425 *) whereas (Outer[Times, list1, {1., 2.}];) // AbsoluteTiming // First (* 0.542086 *) Also: list1 = Developer`FromPackedArray@RandomReal[1., 1000000]; ...


8

You are apparently looking for a way to reliably compare two numerical matrices by using their eigensystems. This can always be done for normal matrices by using the eigenvectors to construct their spectral decomposition. To do that, you shape the eigenvectors into the equivalent system of projectors. Then you can compare the projectors instead. The good ...


8

You are correct to say that this is a problem with the precision of the numbers involved. You can set the precision of those numbers explicitly: SetPrecision[{{3.9999999999998025*^14 + 0.001*I, 3.141592653589793 - 3.1405926535897932*I}, {3.141592653589793 - 3.1405926535897932*I, 3.9999999999998025*^14 + 0.001*I}}, 20]; Eigenvalues[%] (* ...


8

m = {{1, 2, 3}, {2, 4, 1}, {2, 5, 7}}; {lu, p, c} = LUDecomposition[m]; l = lu SparseArray[{i_, j_} /; j < i -> 1, {3, 3}] + IdentityMatrix[3]; u = lu SparseArray[{i_, j_} /; j >= i -> 1, {3, 3}]; l.u == m[[p]] (* True *) l.u is equal to a permutation of the rows of m MatrixForm /@ {l, u}


8

As I have previously noted, QRDecomposition[] is by default set to return the so-called "thin QR" or "economy QR" decomposition; this is often the form desired in applications, since the triangular factor does not have the unneeded zero rows. MATLAB's qr(), by contrast, returns the full QR decomposition by default, and the economy QR through an option ...


8

This is not a complete solution but should provide a start. In the special case of a square matrix we can determine conditions on rank by determining where specific numbers of eigenvalues vanish. We show how to set this up using the example from the post. mat = Normal@SparseArray[{i_, i_} -> a, 5, b] (* Out[17]= {{a, b, b, b, b}, {b, a, b, b, b}, {b, b, ...


8

In this comment, it is noted that LatticeReduce[] is now using the Nguyen-Stehle variant of LLL, so any results you might see from LatticeReduce[] can be different from a "classical" implementation of LLL. Having said this, LatticeReduce[] does take options, but through a not too transparent interface: SetSystemOptions["LatticeReduceOptions" -> ...


8

How should I know the correct order of arguments without trying several times? You can't, usually. A lot of the undocumented usage that you see on this site will have been worked out by trial and error. Sometimes it is fruitless - I have explored plenty of interesting-sounding internal functions and got nowhere. Are there detailed usage information of ...


8

After some investigation I can mostly explain the (correct) behavior. First off, we set this up using machine doubles. After computing the eigensystems we reorder by eigenvalues. While there is the possibility of messing up conjugate pairs (wherein right and left eigenvalues get ordered differently due to small numeric discrepancies), I checked and this ...


7

Any square matrix can be put into Jordan normal form via a similarity transformation; and for a matrix in Jordan normal form, the geometric and algebraic multiplicities of the eigenvalues are determined by its block structure. So you could take the following approach: generate a Jordan matrix with the appropriate multiplicities, and then perform a random ...


7

Here is an approach that might work out. Use GroebnerBasis to set up all polynomial relations, then reduce to see where there may be linear dependencies. We use surrogate variables to define algebraic relations for roots and reciprocals, so sx stands in for sqrt(x) below, and similar for the reciprocal variables xr1 and sxr2. We also use new variables to ...


7

Experimentation based on the documentation you quoted led to two valid Method options: MatrixExp[{{1.2, 5.6}, {3, 4}}, Method -> "Pade"] {{346.557, 661.735}, {354.501, 677.425}} MatrixExp[{{1.2, 5.6}, {3, 4}}, {1, 2}, Method -> "Krylov"] {1670.03, 1709.35} If "Krylov" is used for the single parameter syntax it complains: MatrixExp[{{1.2, ...


7

Matlab is the fertile soil of bad Mathematica programming... try baseGenerator2[m_Integer, n_Integer] := Reverse@Sort[Join @@ Permutations /@ IntegerPartitions[n, {m}, Range[n, 0, -1]]] And for your own sanity, don't use uppercase initials on symbols - you may very well clash with built-ins and/or create debugging nightmares (e.g. N is a built-in, by ...


7

Update: it seems this only works in 10.1 and later, but not in 10.0. This works: Assuming[(a | b | c) ∈ Matrices[{d, d}], TensorExpand@Transpose[a.(b + c)]] (* Transpose[b, {2, 1}].Transpose[a, {2, 1}] + Transpose[c, {2, 1}].Transpose[a, {2, 1}] *) We needed to tell Mathematica that a, b and c are square matrices and use TensorExpand. Check here: ...


7

b = {{1, 1, 0, 0, 0, 0}, {1, -1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {-1, -2, -3, -3, -3, -3/2}}; h = NullSpace@b (* {{0, 0, -1, 0, 1, 0}, {0, 0, -1, 1, 0, 0}} * MatrixRank@Join[b, h] (* 6 *)


7

The relationship between Q and R as computed by QRDecomposition and the "full QR" results (as described by Guesswhoitis} can be found in, for instance, Wikipedia. The following illustrates how to go from the Mathematica to the Wikipedia formulation. With a as defined in the question, {q, r} = QRDecomposition[a] (* {{{1/Sqrt[5], 0, 2/Sqrt[5]}, ...



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