Tag Info

Hot answers tagged

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


15

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


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] & /@ ...


8

For a wide variety of applications, the cost of doing a scalar product is rarely linear in the complexity of the multiplicands. Furthermore, the complexity of a product is usually larger than the complexity of the inputs. This can range from the simple case of multiplying two $n$-bit integers to get a $2n$-bit sum, to the horrible case of multiplying two ...


8

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


7

Add this to your notebook or init file $PrePrint = If[MatrixQ[#], MatrixForm[#], #] &; Then all matrices will automatically display as MatrixForm and If you want to format lists as column vectors also, try $PrePrint = Which[MatrixQ[#], MatrixForm[#], VectorQ[#], ColumnForm[#], True, #] &; Now also


7

I've spotted three issues with your approach and posted code: Spectral clustering uses the eigenvectors associated with the $k$ smallest eigenvalues of the Laplacian, but your code is selecting those associated with the $k$ largest eigenvalues. You need to Transpose your Kvecs prior to passing them to ClusteringComponents. As currently written, you're ...


6

Based on the definition from the Wikipedia article, this should give you the resistance distance matrix of the graph g: With[{Γ = PseudoInverse[KirchhoffMatrix[g]]}, Outer[Plus, Diagonal[Γ], Diagonal[Γ]] - Γ - Transpose[Γ] ]


5

You could define $a_1,a_2$,.. as graphic primitives (Line) and use Translate: a1 = {Thickness[.01], Line[{{{0, 0}, {1, 0}}, {{1/2, -1/2}, {1/2, 1/2}}}]}; a2 = {Line[{{{0, 0}, {1, 0}}, {{1/2, -1/2}, {1/2, 1/2}}}]}; b1 = {Line[{{0, 0}, {1, 0}}], Thickness[.01], Line[{{1/2, -1/2}, {1/2, 1/2}}]}; b2 = {Line[{{1/2, -1/2}, {1/2, 1/2}}], Thickness[.01], Line[{{0, ...


5

The main reason for the slowness of the magicSquare is that your failed to insist on vecterization. (you're already aware of its importance, right? ) Making use of the internal functions owning Listable attribute and treating lists as a whole as much as possible, it's not hard to come to the following: magicSquare2[n_] /; Mod[n, 4] == 0 := Module[{mat, ...


4

MapIndexed can be used to apply styling to elements conditionally based upon the their indices within the matrix. For example: format[v_, {_, 1}] := framed[v, White, Blue] format[v_, {_, 2}] := MatrixForm[v, TableSpacing -> {None, None}] format[v_, {_, 2, 1, _} | {_, 2, _, 4}] := framed[v, Black, Darker[Yellow, 0.01]] format[v_, _] := v framed[v_, f_, ...


4

mfnested = MapAt[MatrixForm, nested, {{}, {;; , ;;}}]; colF = MapAt[Function[{i}, Item[i, Background -> #2[[1]]]], #, #2[[2]]] &; Fold[colF, mfnested, {{Yellow, {{1, All, 2, 1, 1, All}, {1, All, 2, 1, All, -1}}}, {Lighter@Blue, {{All, All, 1}}}}] Grid[MapAt[Grid[#, Background -> {{-1 -> Yellow}, {1 -> Yellow}}] &, ...


4

One way of doing that is create an image for each element and then use GraphicsGrid With the definition about line of @halmir a1 = {Thickness[.03], Line[{{{0, 0}, {1, 0}}, {{1/2, -1/2}, {1/2, 1/2}}}]}; a2 = {Line[{{{0, 0}, {1, 0}}, {{1/2, -1/2}, {1/2, 1/2}}}]}; b1 = {Line[{{0, 0}, {1, 0}}], Thickness[.03], Line[{{1/2, -1/2}, {1/2, 1/2}}]}; b2 = ...


4

This gives a slight speed up (~40%) for me: stepcp = Compile[{{m, _Complex, 2}, {u, _Complex, 2}, {n, _Integer}, {l, _Real}}, Block[{ m2 = ConjugateTranspose@m, x = u, ut = Flatten@Transpose@u}, Join[{1.0}, Table[x = m.x.m2; Flatten[x].ut, {i, n - 1}]/(2 l)]]] I replaced the NestList with a more procedural approach, calculating the trace of the ...


4

The relevant topics here are "Operators without built-in meanings" and solving your problem is a matter of assigning meanings to them. Important to note here: the left and right angle brackets are typed as :esc: < :esc: and :esc: > :esc: and are not the same as the Greater and Less signs, but for brevity I will be typing them simply as < and > ...


3

Just sample your function over a 2D window with a Table. f[x_,y_]:= 1-2*Sinc[2(x^2 + y^2)] step = .2; kern = Table[f[x,y], {x, -3, 3, step}, {y, -3, 3, step}]; The dimensions of the kernel can be returned with Dimensions[kern]. Experiment with values of step and window sizes. Now just do an ImageConvolve[img,kern] and optionally use ImageAdjust to ...


3

I like the existing answers but I cannot resist posting my own formulation. I shall make use of the new-in-10.1 CirclePoints though I shall also provide an alternative without it. First Rules that specify the thickness of each radial line, counterclockwise from 3 o'clock: rls = {"a1" -> {3, 3, 3, 3}, "b1" -> {1, 3, 1, 3}, "c1" -> {1, 1, 3, 3}, ...


3

One simple solution to this is to a combination of Simplify and Chop, which will get rid of items like (0. * I). Chop[Simplify[M1.Z1.M2 /. α -> (-0.5 + I*Sqrt[3]/2)]] {{Z + 3 Zg, 0, 0}, {0, 1. Z, 0}, {0, 0, 1. Z}} There are still some (1. Z) terms in there, which, if you look at the FullForm are actually just very close to 1, according to the ...


3

Another approach combining @SquareOne's observation with ArrayComponents: counts = Max /@ ArrayComponents[data[[All, All, 1]]] (* {4, 6, 7, 8} *) times = First /@ data[[All, All, -1]]; Transpose[{times, counts}] (* {{20.5, 4}, {20.4, 6}, {20.3, 7}, {20.2, 8}} *) Note: Assumed that 0 is not used as a particleID.


3

E.g.: SparseArray[Band[{1, 1}] -> Infinity, {3, 3}, 2] // Normal Creates a 3×3 with 2 everywhere but the diagonal of infinity. Drop the // Normal if you want to keep it sparse...


2

Code The case when $n$ is odd magicSquare[n_?OddQ] := Module[{mat1, mat2}, mat1 = Table[Range[n], {i, n}]; mat2 = Transpose@mat1; With[ {matA = Mod[mat2 + mat1 + (n - 3)/2, n], matB = Mod[mat2 + 2 mat1 - 2, n]}, n matA + matB + 1] ] The case when $n$ is doubly-even magicSquare[n_] /; Mod[n, 4] == 0 := Module[{mat1, mat2, square, ...


2

What follows is my latest edit. I have moved my previous attempts to solve this to the bottom of the answer. EDIT 04.05.15 I have given this problem some more thought. It's peculiar, that Mathematica cannot solve it straight away, but it should in fact be quite easy. Using your notation, we are dealing with the following differential equation: $$ ...


2

Slightly different approach. This Max[#[[All, 1]]] & /@ data {26, 28, 29, 29, 30} gives you the maximum particle number ID at each successive time (notice as @Halirutan said that you have a double entry (29), but never mind) Theoretically, by calculating the successive differences between theses elements: Max[#[[All, 1]]] & /@ data // ...


2

When you take the IDs of each time-step and accumulate them through the time (but deleting duplicates), then you get the information you want. Therefore, the basic idea is to extract all IDs from each time and then you use Union over and over again trough all time-steps FoldList[Union, data[[All, All, 1]]] (* {{23, 24, 25, 26}, {23, 24, 25, 26, 27, ...


2

I think this does what you need: With[{a = Merge[Apply[#5 -> #1 &, data, {2}], Union]}, Thread[{Keys@a, Length /@ FoldList[Union, Values@a]}]] (* {{20.5, 4}, {20.4, 6}, {20.3, 7}, {20.2, 8}} *)


2

A couple of approaches: f[x_] := {x[[1]], Sequence @@ (x[[-1]] # & /@ x[[2 ;; -2]]), x[[-1]]}; g = {#1, #2 #5, #3 #5, #4 #5, #5} &; Map[f, data, {2}] g @@@ # & /@ data


2

You were one character away from the solution: data[[All, All, {2, 3, 4}]] *= data[[All, All, 5]]; data {{{23, 61.5, 82., 102.5, 20.5}, {24, 61.5, 20.5, 0., 20.5}, . . . See documentation for TimesBy and Elegant operations on matrix rows and columns.


2

A simple solution would be using Coefficient p = Sum[c[i, j] x^i Exp[I j x], {i, 0, 3}, {j, 0, 3}]; Table[a[i, j] = Coefficient[Coefficient[p, x, i], Exp[I x], j] , {j, 0, 3}, {i, 0, 3}]; MatrixForm[%] $ \left( \begin{array}{cccc} c(0,0) & c(0,1) & c(0,2) & c(0,3) \\ c(1,0) & c(1,1) & c(1,2) & c(1,3) \\ c(2,0) & ...


2

I'll illustrate with a simple example. mat = {{2, 1}, {1, 3.}}; ch = CholeskyDecomposition[mat] (* Out[145]= {{1.41421356237, 0.707106781187}, {0., 1.58113883008}} *) Pull out the diagonal. Use it to modify and get a triangular matrix with ones on the diagonal. diag = Diagonal[ch] (* Out[148]= {1.41421356237, 1.58113883008} *) modch = ch*1/diag (* ...


2

As @kattern pointed out, MatrixForm will pretty print your lists to look like matrices. {{0}, {1}, {0}, {-1}} // MatrixForm A word of caution, however: MatrixForm can get in the way of your calculations if you are not careful. See this question and the related answers: Why does MatrixForm affect calculations?. For instance, you could get bitten by ...



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