We’re rewarding the question askers & reputations are being recalculated! Read more.
74

High-level languages, like Mathematica, have a high overhead for executing each command/instruction. However, they also typically include commands/instructions that solve a larger and more complex task than those in low-level languages. To take a concrete example, in C, we can add two numbers. In Mathematica, we can add two arrays directly. If we want to do ...


8

The first thing I noticed in the author code is that this is not an exact reproduction of the Verlet algorithm. Second, there is no vector implementation, which is the main advantage of the Verlet algorithm. If we correct both inaccuracies, we get a code that is not inferior in speed to the standard solver with an option similar to Verlet's algorithm. We ...


7

I don't understand why you think there will be a difference in timing for your example. In both cases the head is evaluated, the data variable is evaluated, the Function is evaluated, and then the Part is evaluated. It's just a matter of order. Let's take a smaller example so that we can follow the trace: data = RandomReal[1, 3]; f1 = Function[x, x[[1]]]; ...


6

The following did the first trick 40 times faster (on my machine): sparse[[All, All, 1 ;; -n - 1 ;; n]] *= 0. One of the major problems here is that ConstantArray[0., Dimensions[b[[All, All, 1 ;; -n - 1 ;; n]]]] is a dense array and that in conveys basically no information.


6

Yes, putting the function call in twice will cause it to be evaluated twice. You can visualize this by putting a Print inside the function, for example: f[x_] := (Print["In f"]; x) g[x_] := f[x]^2 + 2*f[x] + 1 g[2] (* In f *) (* In f *) (* 2 *) You can see that it went to f twice. To avoid that, you can assign a temp variable: g2[x_] := Module[{t =...


5

It is possible to speed up your existing code even without compilation. The idea is to count not all configurations, but rather the topologically different ones. There are 3 topologically distinct possibilities to start a path Corner: 1, 3, 7, 9 Edge: 2, 4, 6, 8 Center: 5 Thus, it is sufficient to count the number of paths starting from 1 (n[1]), ...


5

Let's generate sample data: array = ConstantArray[ RandomInteger[{0, 10}, {100, 100}], {100, 100}]; replace = ConstantArray[RandomInteger[{0, 10}, {100, 100}], 100]; array1 = array2 = array3 = array4 = array; and apply different methods: RepeatedTiming[ Table[array1[[i, i]] = replace[[i]], {i, Length@replace}];] {0.0064, Null} RepeatedTiming[(...


4

We can also use SparseArrays. m1 = {{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}, {{{9, 10}, {11, 12}}, {{13, 14}, {15, 16}}}}; v1 = {{{11, 3}, {7, 5}}, {{13, 97}, {3, 16}}}; res = {{{{11, 3}, {7, 5}}, {{5, 6}, {7, 8}}}, {{{9, 10}, {11, 12}}, {{13, 97}, {3, 16}}}}; blockmatrixreplace[mat_, rep_] := Block[ { diagonalmatrixindices = Flatten[Table[{i, i, j, k}, ##]...


4

You could do something like G[x_] := a^2 + 2 a + 1 /. a -> F[x] or G[x_] := #^2 + 2 # + 1 &@F[x]


4

In general, as Henrik and I note in the comments, Mathematica makes sure only to copy data when it has undergone some sort of change internally. An easy way to see this is to set a flag like Valid and see when it disappears: myBigData = RandomReal[{}, {500, 800}]; myBigData // System`Private`SetValid; amIDifferentNowHeld[Hold[data_]] := ! System`Private`...


3

One idea is to use ImplicitRegion and RegionMeasure. First, convert your variable ranges to an ImplicitRegion: intervals = { {x1, 0, 2}, {x2, x1, 4}, {x3, x1, 6}, {x4, x1, 8}, {x5, x1, 10}, {x6, x1, 12}, {x7, x1, 14}, {x8, x1, 16}, {x9, x1, 18}, {x10, x1, 20}, {x11, x1, 22}, {x12, x1, 24}, {x13, x1, 26}, {x14, x1, 28}, {x15, x1, 30}, {x16, ...


3

Mathematica is written in C/C++ itself. That means it cannot run faster than C/C++. (Assuming of course the faster implementations are used for each algorithm).


2

An alternative to Henrik solution: Generate data: dim = 50; sparse = Table[ KroneckerProduct[RandomReal[{-10, 10}, {dim, dim}], IdentityMatrix[dim, SparseArray]], {ii, 1, 5}, {jj, 1, 5}]; My method: n = 3; AbsoluteTiming[ sp = SparseArray[ Drop[ArrayRules[IdentityMatrix[dim^2, SparseArray]], {1, -n, n}], {dim^2, dim^2}]; sparse1 = sp.# ...


2

In the same manner as yarchik's answer, but leveraging more symmetries. Also using ps as defined in the question. t = {2, 6}; u = {3, 6, 9}; eightfoldStarts = Thread /@ {{1, t}, {1, 5, {2, 3, 6}}, {1, 5, 9, t}, {2, u}, {2, 5, u}, {2, 5, 8, u}, {5, 1, t}, {5, 1, 9, t}, {5, 2, u}, {5, 2, 8, u}} // Catenate; finish[s : {__Integer}] := Join[...


2

The extra Null and {} is just Reap/Sow working as expected, there is nothing to fix. The Null is the value of the expression. The extra {} that wraps the result is there because if you use tags then the values will be grouped by those tags: Reap[ Sow[a, "key1"]; Sow[b, "key1"]; Sow[c, "key2"]; value ] {value, {{a, b}, {c}}} So then clearly, if you ...


2

One potential bottleneck is incidv = Flatten[Position[edges, (v \[UndirectedEdge] _ | _ \[UndirectedEdge] v)]] as it involves (i) a search in the rather long list of edges and (ii) pattern matching, which both tend to be rather slow. A quicker way will be to compute all these lists at once via vertexedgeincidences = IncidenceMatrix[G]["AdjacencyLists"]; ...


2

To make comparable timings, the definition of the ImplicitRegion should be included in the timing of the RegionMeasure approach. Clear["Global`*"]; $HistoryLength = 0; res1 = N[Integrate[1, Sequence @@ Table[{x[k], x[1] (1 - KroneckerDelta[k, 1]), 2 k}, {k, 50}]]] // AbsoluteTiming (* {2.89516, 9.12478*10^78} *) res2 = Module[{reg = ...


1

I'm not seeing how subscripts or multiple criteria change anything. xs = Array[Subscript[x, #] &, 5] pairs = Tuples[xs, 2] multcrit[pr_] := OddQ@Last@First@pr && EvenQ@Last@Last@pr Select[pairs, multcrit] (* the selected elements *) Replace this Select with the approach from the referenced question and then use ReplaceAll to substitute ...


1

There is a more concise and slightly faster pattern matching solution: impossible = {{1, 3}, {3, 1}, {4, 6}, {6, 4}, {7, 9}, {9, 7}, {1, 7}, {7, 1}, {2, 8}, {8, 2}, {3, 9}, {9, 3}, {1, 9}, {9, 1}, {3, 7}, {7, 3}}; possible = Permutations[Range[9], {4, 9}]; Count[possible, {___, PatternSequence @@@ Alternatives @@ impossible, ___}]; This takes ...


1

Currently I have come to a much more complicated solution than I wished, which completely depends on the automatic grouping behavior (which I temporarily switch on): SetOptions[EvaluationNotebook[], {CellProlog :> AbortProtect[ Module[{nb = EvaluationNotebook[], evalCell = EvaluationCell[], cells, pos, grouping, groupingRules}, If[...


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