# Tag Info

16

This happens because of unpacking when the numbers exceed $MaxMachineNumber: fast = Dot @@@ Partition[tab, Divisors[3960][[42]]]; DeveloperPackedArrayQ /@ fast (* {True, True, True, True, True, True, True, True} *) Max[fast] <=$MaxMachineNumber (* True *) slow = Dot @@@ Partition[tab, Divisors[3960][[43]]]; DeveloperPackedArrayQ /@ slow (* {False, ...

16

Here's an edited version of my answer to a related question (elsewhere). Since your central question was about speed (or time complexity), you might wish to know an important result from elementary theory of algorithms and computational complexity, which is that the time and space complexity of matrix multiplication depends upon the order of such ...

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 :) *) ...

13

For readers who didn't read all the comments, the slowdown is due to a lack of packing of tb, whereas RandomReal returns packed arrays when more than 250 elements are generated. The reason why packing tb fails is because some elements have different precision than others, and (I think?) ToPackedArray requires arrays to be of homogeneous type. To fix this, ...

13

It turns out ListSurfacePlot3D does a terribly poor job of approximating the surface in the OP, otherwise one will just apply DiscretizeGraphics to the output obtained from ListSurfacePlot3D and be done with it. But since that's not applicable here, we present an approach that uses alpha shapes to approximate the shape of the given point set by tuning a ...

12

Using Composition I can apply RotationTransform, TranslationTransform , ShearingTransform one after the other. Graphics3D[{ Opacity[1] , Red , Arrow[{{0, 0, 0}, {1, 0, 0}}] , Green , Arrow[{{0, 0, 0}, {0, 1, 0}}] , Blue , Arrow[{{0, 0, 0}, {0, 0, 1}}] , Opacity[0.2] , GeometricTransformation[Cuboid[-{1, 1, 1}/4, {1, 1, 1}/4], ...

12

Translated Cleve Moler's magic() function from Matlab code to Mathematica. Grid[Partition[MatrixForm@magic[#] & /@ {3, 4, 5, 6, 7, 8, 9, 10}, 4], Frame -> All, FrameStyle -> LightGray] code: magic[n_Integer /; (n > 0 && n != 2)] := Module[{m, j, k, p, i}, (*Translation of Cleve Moler's magic magic() function to ...

12

@Nasser's answer is nice, but slowly when Mod[n,4]==0. Here is a faster code, efficiency is close to Matlab : ClearAll[magic] magic[n_?OddQ] := oddOrderMagicSquare[n]; magic[n_ /; n~Mod~4 == 0] := Module[{J, K1, M}, J = Floor[(Range[n]~Mod~4)/2.0]; K1 = Abs@Outer[Plus, J, -J]~BitXor~1; M = Outer[Plus, Range[1, n^2, n], Range[0, n - 1]]; M ...

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.

11

A straightforward and clear solution: f[m_] := Flatten@Table[m[[j, i - j + 1]], {i, Length@m}, {j, i}] f@{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} (* {1, 2, 4, 3, 5, 7} *) A fast compiled version: fc = Compile[{{m, _Integer, 2}}, Module[{n = Length@m, res, k = 0}, res = Array[0 &, Quotient[n (n + 1), 2]]; Do[res[[++k]] = m[[j, i - j + 1]], {i, ...

11

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, ...

11

Now fixed in version 10.2. In[1]:= m = {{0, 1}, {-1, 0}}; In[2]:= {AntihermitianMatrixQ[m], HermitianMatrixQ[m], AntihermitianMatrixQ[m]} Out[2]= {True, False, True} As per the comments, yes, there is information stored in the internal representation of matrices (for example, a symmetry flag) and no, it is not accessible from top level code.

11

This is almost certainly an out-of-memory crash. The underlying issue is that the OS X front-end is a 32-bit program, so has a process memory limit of around 2 GB. It is normal for an attempted allocation beyond that limit to lead to a crash. A similar size ArrayPlot example I tried on my Windows machine (where the front-end is 64-bit) used more than 3 GB ...

11

I would simply do Dot[Conjugate[#1], #2] & @@@ Partition[mm, 2, 1] and do away with the sequential numbers, as those are easy enough to generate when needed any way (MapIndexed?) ListPlot does not need these indices. But instead of jumping straight to the solution, let's take your code and improve it step by step. Instead of arr[[i]][[j]] you can ...

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], ...

10

It's an issue of growth of term size in this example. If you do the straight iteration then at each step the matrix elements roughly quadruple in size (because each time you multiply every element by a variable, and sum four such products per matrix entry). We confirm this below on an example of half the size. tab = Partition[#, 4] & /@ ...

10

The definition of the scalar product in your question assumes that all your kets are orthogonal unit vectors. In that case, the most natural approach would be to use the built-in Bra and Ket as follows: Ket /: Dot[Bra[x__], Ket[y__]] := Times @@ MapThread[KroneckerDelta, {{x}, {y}}] BraKet[x_, y_] := Bra[x].Ket[y] Bra[2, 4].Ket[2, 4] (* ==> 1 *) ...

10

This is really a natural fit for Outer: t = Table[{i, j}, {i, 1, 2}, {j, 1, 2}]; Outer[Apply, {Plus, Subtract, Times, Divide}, t, 2] (* ==> {{{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}}} *)

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

The problem is that Dot does not evaluate if one of the arguments is not a list of some sort, and so when the matrix A0 is added, it does something you might not foresee. Consider: m = {{1, t}, {t, -1}}; A0 = {{1, 0}, {0, 1}}; m.A[t] + A0 (* { { 1 + {{1, t}, {t, -1}}.A[t], {{1, t}, {t, -1}}.A[t] } , { {{1, t}, {t, -1}}.A[t], 1 + {{1, t}, {t, -1}}.A[t] } ...

9

While the documentation does not specifically say that symbolic Hermitian matrices are not necessarily given orthonormal eigenbases, it does say For approximate numerical matrices m, the eigenvectors are normalized. For exact or symbolic matrices m, the eigenvectors are not normalized. From this, it's reasonable to guess that if Mathematica isn't ...

9

Note: the answer below referred to a previous version of the question You may have more success with SparseArray. For instance: SparseArray[ {{i_, i_} -> 1, {i_, j_} /; Abs[i - j] == 1 -> 2}, {20000, 20000}, 0 ]; // RepeatedTiming (* Out: {0.21, Null} *) You would use patterns to assign values to positions within the matrix determined ...

9

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 = ...

8

Avoiding exact calculation by using approximated numerical value before calculation speeds things up. I'm also calculating only the first Eigenvectors as pointed pot by @Öskå. Now it takes only 15 milliseconds time AbsoluteTiming[Chop@Eigenvectors[N[m], 1, Quartics -> True]] {0.015600, {{-0.0725514, -0.106358, -0.0986766, -0.110735 [...] }}}

8

Perhaps MatrixLog[ m ] / Log[2]

8

a = RandomReal[{0, 1}, {2, 8}]; b = RandomReal[{0, 1}, {2, 8, 2}]; a.# & /@ b {{{2.58906, 3.35618}, {2.5578, 3.12812}}, {{1.3762, 2.87723}, {1.56668, 3.04675}}}

8

I think you will find what you are looking for with MedianFilter mat = RandomReal[{0,1},{50,50}]; Animate[MatrixPlot[MedianFilter[mat,x]],{x,1,5,1}]

8

You can also use MapAt with Invisible or Style[#,White]&: f1 = MatrixForm[MapAt[Invisible, #, Position[#, Except[#2], {2}, Heads->False]]] &; (* or f1 = MatrixForm[MapAt[Style[#,White]&, #, Position[#, Except[#2], {2}, Heads->False]]] &; *) Example: m = RandomInteger[5, {5, 5}]; Row[Prepend[f1[m, #] & /@ {1, 2, 1 | 2}, ...

8

Update: here Table is faster and more user-friendly then Array. mat[n_] := LowerTriangularize@Table[2 (1 + Boole[j > 1]) (i - 1) Mod[i + j, 2], {i, n}, {j, n}]; mat[10] // MatrixForm It is fast and the result is packed array mat[1000] // Developer`PackedArrayQ // AbsoluteTiming (* {0.142522, True} *)

8

Is this what you wanted? expr = E^(-((I \[Beta])/2)) p (Cos[\[Alpha]/2] (Cos[\[Theta]]^2 + Sin[\[Theta]]^2 Sin[\[Phi]] (-I Cos[\[Phi]] + Sin[\[Phi]])) + E^(I \[Beta]) Sin[\[Alpha]/2] (Cos[\[Theta]]^2 + Sin[\[Theta]]^2 Sin[\[Phi]] (I Cos[\[Phi]] + Sin[\[Phi]]))) expr /. Complex[x_, y_] :> Complex[x, -y]

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