# Tag Info

21

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

17

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

11

I feel this answer is as sensible as the question. First, binarise the image: m = Import["maz1.jpg"] bin = Opening[ColorNegate@Binarize[m], 6] Find the biggest white space, and thin it into a skeleton thin = Thinning[SelectComponents[bin, "Count", -1], Method -> "MedialAxis"] I used "MedialAxis" here to make it squarer, at the cost of extra bits ...

11

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

10

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

7

Mathematica is "aware" of linearity of Dot in all its arguments, but to exploit it you need to tell Mathematica which expressions represent Matrices, and explicitly ask to expand expression using TensorExpand function: ClearAll[α, β, γ, δ, a, b, c, d, k, l, m, n] TensorExpand[ Dot[α a, β b + γ c, δ d], Assumptions -> a ∈ Matrices[{k, l}] &...

6

f[k_] := Table[Tr[M @ k]^(k - i) Array[M @ k &, i, 1, Tr @ Dot @ ## &], {i, k}] f /@ Range[5] // Column // TeXForm $\tiny\begin{array}{l} \{\text{Tr}[M(1)]\} \\ \left\{\text{Tr}[M(2)]^2,\text{Tr}[M(2).M(2)]\right\} \\ \left\{\text{Tr}[M(3)]^3,\text{Tr}[M(3).M(3)] \text{Tr}[M(3)],\text{Tr}[M(3).M(3).M(3)]\right\} \\ \left\{\text{Tr}[M(4)]^4,\... 6 In raw symbolic form, Mathematica, simply doesn't know what a and b in a.b + a.-bare, so it doesn't evaluate the expression. However, given a hint ... With[{a = {x, y}, b = {u, y}}, a.b + a.-b]] 0 6 GridLines and Ticks should prove useful. Graphics[{PointSize[Large], Point[{10, 20}]}, GridLines -> {Range[0, 25], Range[0, 25]}, Ticks -> {{{5, "A"}, {10, "B"}, {15, "C"}, {20, "D"}}, Automatic}, PlotRange -> { {0, 25}, {0, 25}}, Axes -> True] 6 There is a bug in your code which causes one of the factors in the Dot product to have excessively large imaginary parts (of the form 0. +1.32133*10^246 I). This means the multiplication can't be done in machine precision arithmetic, and as a result the calculation slows down considerably. The mistake is that the exponentiation of the eigenvalues must read:... 5 To factor out numeric factors in any argument of Dot: (2 yy.(3 zz)).(4 zz) //. Dot[a___, d_?NumericQ b_, c___] :> d Dot[a, b, c] 24 yy.zz.zz Edit: If you want this to happen automatically, you can add the rule as a new definition for Dot: Unprotect[Dot]; Dot[a___, d_?NumericQ b_, c___] := d Dot[a, b, c] Protect[Dot]; Now the factoring happens by ... 4 There are small number of rules that make a function bilinear, so why not just implement them directly? ClearAll@blDot; Attributes[blDot] = {OneIdentity, Flat, Orderless}; blDot[Plus[a_, b_], c_] := blDot[a, c] + blDot[b, c]; blDot[Times[a_, b_], c_] := Times[a, blDot[b, c]]; blDot[a_, b_] := Dot[a, b] It works on these examples, blDot[a, (b + c)] blDot[(... 3 comp[i_, j_] := Array[m, 1 + j - i, {j - 1, i}, Dot] comp[3, 8] m[8].m[7].m[6].m[5].m[4].m[3] 3 You can improve timings by giving dodo an argument restriction, so that it doesn't Activate too early: Clear[dodo]; dodo[m1_, m2_List, pos_] := TensorTranspose[ Activate[TensorContract[Inactive[TensorProduct][m1,m2],pos]], {1,3,2} ]; AbsoluteTiming[ sol2 = NDSolve[ {f'[t]==Normal[dodo[a,f[t],{{2,5},{4,7}}]],f[0]==Normal[ini]}, f, ... 3 Conceptually, I think the following is the clearest: ip[a_, b_, k_] := With[{rank = TensorRank[a]}, TensorContract[ TensorProduct[a, b], Table[{i, rank+i}, {i, k}] ] ] For example: A = RandomReal[1, {2, 3, 4, 5}]; B = RandomReal[1, {2, 3, 4, 6, 7}]; ip[A, B, 3] //Dimensions {5, 6, 7} For large tensors, this approach is ... 3 Does this function do what you are looking for? InnerProduct[T1_, T2_, k_] := Dot[ Transpose[Flatten[T1, k - 1],RotateRight[Range[TensorRank[T1] + 1 - k]]], Flatten[T2, k - 1] ] It flattens the first k slots together, rotates the flattened slot of the first tensor to the right and uses Dot to compute the summation efficiently. Here is a test example ... 3 Is the following sufficiently general? t[e_] := e /. Dot[Times[z1_ /;!ArrayQ[z1], Dot[z2__]], z3__] :> z1 Dot[z2, z3] Simplify[a, TransformationFunctions -> {Automatic, t}] (* yy.zz.zz *) 3 Defining the vectors: v1 = Array[v, 3] v2 = Array[w, 3] (* {v[1], v[2], v[3]} *) (* {w[1], w[2], w[3]} *) Then: Cross[v1, v2].Cross[v1, v2] - (v1.v1 v2.v2 - (v1.v2)^2) // Expand (* 0 *) Note that in general, when doing algebraic manipulations, it is better to use the explicit form of the squared-norm of a vector, because Norm is interpreted in terms of ... 2 You could use my TensorSimplify package to do this. Install the paclet with: PacletInstall[ "TensorSimplify", "Site" -> "http://raw.githubusercontent.com/carlwoll/TensorSimplify/master" ] Once installed, you can load the package with: <<TensorSimplify` The basic issue is that TensorReduce does not handle a mixture of Dot and ... 2 Here's a redefinition of the LM[] function in the OP: LM[M_] := Total[DiagonalMatrix[Λ].Outer[Dot, ω.M, ConjugateTranspose[ω, {1, 3, 2}], 1], 2] On my limited set of tests with much smaller matrices and smaller lists of matrices, this version runs slightly faster than the Sum[]-based solution. You will have to do your own tests on your larger matrices. 2 For such tasks you need to use the appropriate functions, which are optimized for such purposes. In particular, these are Dot[], Flatten[], and Total[]. Never use looping constructs such as Do or Sum for such purposes if performance is an issue. So, I would create the the N complex valued matrices$\omega\$ as well as the other objects as appropriately ...

2

A variant of the method from the other question: z = Sin[φ]*v; Dot[z, Conjugate[z]] (v Sin[φ]).Conjugate[v Sin[φ]] Simplify[%, φ ∈ Reals] //. Dot[a___, d_ b_ /; FreeQ[d, v], c___] :> d Dot[a, b, c] v.Conjugate[v] Sin[φ]^2 Integrate[%, {φ, 0, 2 π}] π v.Conjugate[v] Some explanation of the rule: //. -- "repeatedly apply the following rule ...

2

As conveyed in the comments, the problem seems to have been with version 9 of Mathematica. I upgraded to 10.4.1 this morning, and it is working as expected.

2

not pretty but there is always this: Table[Sum[aa[[i, j]] bb[[j, k, l ]],{j,2}],{i,2},{k,2},{l,2}]

2

How about this: listVonMat[M_] := Module[{n = Length[M], inter1, inter2}, inter1 = TakeDrop[Table[M, n], #] & /@ Range[n - 1, 0, -1]; inter2 = {Times @@ Tr /@ #1, Tr @ (Dot @@ #2)} & @@@ inter1; Times @@@ inter2 ] Maybe numerical results are easier to be checked with: listVonMat[Partition[Range[#^2], #]] & /@ ...

2

You can add Graphoptions during Importing. That is, if example.dot is the file to be imported Export["example.dot", Import["ExampleData/sample.gv"], "DOT"] "example.dot" you can import it as follows Import["example.dot", ImagePadding -> 20, ImageSize -> 400, VertexLabelStyle -> 16, EdgeStyle -> Directive[Red, Thick]] to get This ...

1

It'd be great if you posted/would show your code and matrices to see exactly what you're doing. It's a good habit to post your attempt (and your data) for us to play with. Unless you're doing something considerably more complicated than just having a dot product of integers or Reals, It should be super fast to calculate, matA = Table[RandomReal[], {i, 1, ...

1

Use c : Shortest[___ ~~ "fillcolor="] instead of c : "*fillcolor=": txt = Import["https://wolfr.am/zp54P15j", "String"]; colors = {"black", "yellow", "red"}; txtNew = StringReplace[txt, a : (StartOfLine ~~ Whitespace ~~ "\"cep_s" ~~ DigitCharacter ~~ "_") ~~ b : DigitCharacter .. ~~ c : Shortest[___ ~~ "fillcolor="] ~~ d : "gold" ~~ e : "];"...

1

What about Dot @@ m /@ Range[13, 6, -1] ?

1

I think you are looking for this: L[x_, y_, z_, w_] := {4.0 x - 2 y + 3 z - 5 w, 3 x + 3 y + 3 z - 8 w, -6 x - y + 4 z + 3 w, -4 x + 2 y + 3 z + 5 w} i.e. remove the multiple curly brackets. So that you have output In[71]:= Dot[L[x, y, z, w], L[x, y, z, w]] Out[71]= (-5 w + 4. x - 2 y + 3 z)^2 + (5 w - 4 x + 2 y + 3 z)^2 + (-8 w + 3 x + 3 y + 3 ...

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