# Tag Info

22

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 by changing the order of summation/dotting operations, we can make sure that this beast is never assembled explicitly. A good rule of thumb is to sum intermediate results as early as possible. ...

19

mat = ({{1, x, 3}, {2, 4, 5}, {2, 4, x}}); Select[MatrixRank[mat /. #] == 2 &][Solve[Det[mat] == 0, x, Reals]] {{x -> 2}, {x -> 5}}

13

f[x_, y_] = x + y; g[x_, y_] = x^3 + y^2 + 2 x; functions = {f, g}; Using Map #[x, y] & /@ functions (* {x + y, 2 x + x^3 + y^2} *) Using Through Through[functions[x, y]] *) (* {x + y, 2 x + x^3 + y^2}

13

That's an eigenvalue problem! How to see that? Well, let's define some example data: d = 3; H = N@DiagonalMatrix[Range[d]]; A = # + #\[Transpose] &@RandomReal[{-1, 1}, {d, d}]; Both numerator and denominator can be written as bilinear forms of the vector $\operatorname{vec}(A)$: Avec = Flatten[A]; To this end, we need the following matrices and ...

13

Not necessarily optimal, but it works: a = {{A11, A12}, {A21, A22}}; b = {{B11, B12}, {B21, B22}}; c = {{C11, C12}, {C21, C22}}; d = {{D11, D12}, {D21, D22}}; Flatten[{{a, b}, {c, d}}, {{3, 1}, {4, 2}}] {{A11, B11, A12, B12}, {C11, D11, C12, D12}, {A21, B21, A22, B22}, {C21, D21, C22, D22}}

13

Solutions force exactly one eigenvalue to be zero. So we solve for the condition that an eigenvalue vanish, and check that rank is two. mat = {{1, x, 3}, {2, 4, 5}, {2, 4, x}}; candidateSols = Flatten[Map[Solve[# == 0, x] &, Eigenvalues[(mat)]]] (* Out= {x -> 2, x -> 5} *) Both pass the test: Map[MatrixRank[mat /. #] &, candidateSols] ...

12

I managed to achieve a 20-fold performance boost with the following ideas. First, the elements of your 6-dimensional intermediate array A[m, n, p, q, x, y] can be decomposed into pairwise products of X[m, p, x] and Y[n, q, y] - a square root reduction in trigonometric computations. Then, X and Y can be combined into A via heavily optimized functions Outer ...

11

Use Array vMatrix[n_Integer?Positive] := Array[x[#1]^(#2 - 1) &, {n, n}] Format[x[n_]] := Subscript[x, n] (mat = vMatrix) // MatrixForm Det[mat] // Simplify EDIT: To enable the argument to also be a vector, add to the definition vMatrix[v_?VectorQ] := #^Range[0, Length[v] - 1] & /@ v mat == vMatrix[Array[x, 5]] (* True *)

11

This is precisely what SparseArray is good for: SparseArray[data[[All, 1 ;; 2]] -> data[[All, 3]]]

11

The easiest way to apply different functions to different columns is with Query. This has the added advantage that columns you don't want to do anything with do not have to be specified explicitly. For example, to apply functions to the 1st and 3rd columns: Query[All, {1 -> f, 3 -> g}] @ RandomInteger[10, {5, 4}] // TableForm This also works very well ...

10

See this book on page 53 (section 3.4.1): SpinQ[S_] := IntegerQ[2S] && S>=0 op[S_?SpinQ, n_Integer, k_Integer, a_?MatrixQ] /; 1<=k<=n && Dimensions[a] == {2S+1,2S+1} := KroneckerProduct[IdentityMatrix[(2S+1)^(k-1), SparseArray], a, IdentityMatrix[(2S+1)^(n-k), SparseArray]] ...

10

a = {{A11, A12}, {A21, A22}}; b = {{B11, B12}, {B21, B22}}; c = {{C11, C12}, {C21, C22}}; d = {{D11, D12}, {D21, D22}}; ArrayFlatten[Transpose[{{a, b}, {c, d}}, {3, 4, 1, 2}]] (* {{A11, B11, A12, B12}, {C11, D11, C12, D12}, {A21, B21, A22, B22}, {C21, D21, C22, D22}} *)

10

SparseArray is especially versatile with how you specify the elements. E.g. SparseArray[{{i_, i_} -> f[i], {i_, j_} /; Abs[i - j] == 1 -> g[i]}, {5, 5}] // MatrixForm will produce the following matrix:  \left( \begin{array}{ccccc} f(1) & g(1) & 0 & 0 & 0 \\ g(2) & f(2) & g(2) & 0 & 0 \\ 0 & g(3) & f(3) &...

10

mat = {TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]/Sqrt}; FixedPoint[ArrayFlatten, mat] // MatrixForm $\left( \begin{array}{cccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & \... 9 Really this should have a concrete example in Mathematica copy-pastable format. Anyway, I'll show a few ways using a made-up example. We'll eventually get it down to straight linear algebra. To keep it simple I'll assume the matrix has distinct eigenvalues. This restriction can be lifted of course. Also as has been noted in comments, without loss of ... 9 SeedRandom n = 5; A = RandomInteger[{1, 9}, {1, 1}]; B = RandomInteger[{1, 9}, {1, n - 1}]; F = RandomInteger[{1, 9}, {n - 1, n - 1}]; {A, B, F} {{{2}}, {{5, 1, 8, 1}}, {{1, 9, 7, 1}, {5, 2, 9, 6}, {2, 2, 2, 4}, {3, 2, 7, 1}} ArrayFlatten You can use Transpose@B in the second block and ArrayFlatten: c = ArrayFlatten[{{A, B}, {Transpose @ B, F}}] ... 9 Something like this? ArrayReshape[{{A, B}, {B, F}}, {n, n}] MatrixForm@% {{9, 6, 9, 3, 6}, {6, 9, 3, 6, 7}, {3, 8, 2, 8, 2}, {5, 5, 2, 8, 8}, {3, 1, 9, 2, 1}}$\left( \begin{array}{ccccc} 9 & 6 & 9 & 3 & 6 \\ 6 & 9 & 3 & 6 & 7 \\ 3 & 8 & 2 & 8 & 2 \\ 5 & 5 & 2 & 8 & 8 \\ 3 &...

9

A (probably) very slow but very explicit solution: a = {{A11, A12}, {A21, A22}}; b = {{B11, B12}, {B21, B22}}; c = {{C11, C12}, {C21, C22}}; d = {{D11, D12}, {D21, D22}}; Riffle[ MapThread[Riffle, {a, b}], MapThread[Riffle, {c, d}] ] { {A11, B11, A12, B12}, {C11, D11, C12, D12}, {A21, B21, A22, B22}, {C21, D21, C22, D22} }

9

ClearAll[meshF, edgesF, verticesF, coordsF, meshGraph] meshF = DiscretizeGraphics @ Show[CantorMesh[#, 2], RegionBoundary[RegionDifference[Rectangle[], CantorMesh[#, 2]]] & /@ Range[#]] &; verticesF = MeshCells[meshF[#], 0][[All, 1]] &; edgesF = UndirectedEdge @@@ MeshCells[meshF[#], 1][[All, 1]] &; coordsF = ...

9

A not so good answer Cases[Table[{x, MatrixRank[({{1, x, 3}, {2, 4, 5}, {2, 4, x}})]}, {x, -10, 10, 1/10}], {_, 2}] gives out {{2, 2}, {5, 2}} So answer is 2 or 5.

9

You can use Subsets: mat = Array[Subscript[a, ##]&, {6, 6}]; TeXForm @ MatrixForm @ mat $\left( \begin{array}{cccccc} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} & a_{1,5} & a_{1,6} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} & a_{2,5} & a_{2,6} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} & a_{3,5} &... 9 First of all, I observed that your matrix can be represented as KroneckerProduct of smaller matrixes which are themselves KroneckerProducts (or TensorProducts) of vectors: Ux = Table[m^2 Sin[m x], {m, 1, k}]; Vx = Table[p^2 Sin[p x], {p, 1, k}]; Uy = Table[Sin[n y], {n, 1, k}]; Vy = Table[Sin[q y], {q, 1, k}]; Ax = KroneckerProduct[Ux, Vx]; Ay = ... 9 First a CompiledFunction; it expects the T-matrices and the M matrices is separate lists. cf = Compile[{{T, _Complex, 3}, {M, _Complex, 3}, {B0, _Complex}}, Block[{u, n}, n = Min[Length[T], Length[M]]; u = {1. + 0. I, B0}; Join[ {u}, Table[ u = CompileGetElement[M, i].(CompileGetElement[T, i].u), {i, 1, n}] ] ]... 9 There are a few ways to get what you want. One way to get close would be: ArrayPlot[ PadRight[Table[i + j, {i, n}, {j, i}], {n, n}], ColorFunction -> "BlueGreenYellow", FrameStyle -> Black, FrameTicks -> {{True, False}, {True, False}}, PlotRangePadding -> 0 ] If you delve deeper into the various options, it's not too difficult to ... 9 SeedRandom outtabletoprint = RandomInteger[{0, 5}, {20, 20}]; 1. You can replace 0s in the input matrix with Black: MatrixPlot[outtabletoprint /. 0 -> Black, ColorFunction -> "TemperatureMap", PlotLegends -> Automatic] 2. You can use the option ColorRules: MatrixPlot[outtabletoprint, ColorRules -> {0 -> Black}, ColorFunction -> "... 9 Plot[Evaluate[Eigenvalues[M1]], {t, 0, 10}, PlotLegends -> "Expressions"] sumofnegativeeigenvalues = Total @ - Ramp @ - Eigenvalues[M1] -2 Ramp[30 - t] - Ramp[1/2 (-1 - 7 t - Sqrt[1 - 6 t + 9 t^2 + 4 t^4])] - Ramp[1/2 (-1 - 7 t + Sqrt[1 - 6 t + 9 t^2 + 4 t^4])] Plot[sumofnegativeeigenvalues, {t, 0, 10}, AxesOrigin -> {0, 0}] Alternatively, ... 9 tt = {{(ω1 + ω4)/Sqrt}, {(μ ω2 + ω3)/Sqrt}, {(-μ ω2 + ω3)/Sqrt}, {(-ω1 + ω4)/Sqrt}} // Flatten mat =Last@ CoefficientArrays[tt, {ω1, ω2, ω3, ω4}] // Normal mat. {ω1, ω2, ω3, ω4} - tt // Simplify {0,0,0,0} 9 First we run the original with an example (which should have been in the post). Mmax = 400; W = 1024; deltaX = .1; SeedRandom; lambdaMatrix = RandomReal[1, {W, W}]; lPoly = Developer`ToPackedArray[RandomReal[{-10, 10}, {Mmax + 1, W}]]; AbsoluteTiming[ res = MomentComputing[Mmax, 5, lambdaMatrix, lPoly, lPoly];] (* Out= {12.1522, Null} *) What I ... 9 Here is a definition that is insensitive to density matrix normalization: F[ρ_, σ_] := Tr[MatrixPower[MatrixPower[ρ, 1/2].σ.MatrixPower[ρ, 1/2], 1/2]]^2/(Tr[ρ]*Tr[σ]) Tests with positive semi-definite$3\times3\$ matrices: testρ = {{1/5, 1/5, 0}, {1/5, 3/5, 1/5}, {0, 1/5, 1/5}}; testσ = {{1/3, -1/6, 1/6}, {-1/6, 1/3, -1/6}, {1/6, -1/6, 1/3}}; F[testρ, testρ]...

8

Matrix construction First of all, it might be a good idea to use memoization for building the matrices because this avoids recursive calls to A. By using SparseArrays, we can generate the matrices for much higher n: (*define matrices required for recursion relation*) Id[n_] := IdentityMatrix[Fibonacci[n - 2], SparseArray]; Zm[n_] := SparseArray[{}, {...

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