Tag Info

New answers tagged

0

I do not experience much difference in performance of StreamPlot between versions 7 and 10.1 with the example given. In fact 10.1 is faster than 7.0 at 0.22 seconds versus 0.28 seconds. (Times for generation and rendering combined.) However I can confirm the EvaluationMonitor steps reported, therefore I think this is a change to EvaluationMonitor rather ...


2

First let's take look at your version: pixelsize = 5.3; (flatimaData = Flatten[MapIndexed[{#2[[1]] pixelsize, #2[[2]] pixelsize, #1} &, imaData, {2}], 1];) // AbsoluteTiming (* 2.44 seconds *) Now I try it with Table instead of MapIndexed: dims = Dimensions@imaData; (flatimaData2 = Flatten[ Table[{i*pixelsize, j*pixelsize, ...


4

In Mathematica every inter-Kernel communication comes with significant overhead. Your simple Do loop with a shared Sow on every value is about the worst possible situation. Instead (for performance) you want to gather results within each Kernel and only pass them back to the master in a single call. (Or at least a limited number of calls.) Using linked ...


7

Never use pattern matching unless you absolutely have to. Using Cases instead of Select can make a huge difference. Vectorize operations. Use Range instead of Table if you can. Test several things at once. And[test1, test2, test3] will abort when it can for maximum efficiency ("short circuit evaluation"). Taking this into consideration your code looks ...


6

It is possible to Compile Position itself for machine types (e.g. Integer or Real): posmax = Compile[{{list, _Integer, 1}}, Position[list, Max@list] ]; Performance: x = RandomInteger[{1, 100}, 10^7]; Position[x, Max@x] // Timing // First posmax[x] // Timing // First 0.44754 0.0736 With a C compiler this should be faster still; I'll find out in a ...


5

My proposal: Nearest[list -> Automatic, Max[list], {All, 0.5}] Among non-C solutions, it's slightly faster than Pickett's, but slower than Simon Woods's. list = RandomInteger[{1, 100}, 10^7]; Needs["GeneralUtilities`"]; Nearest[list -> Automatic, Max[list], {All, 0.5}] // AccurateTiming SparseArray[Unitize[list - Max[list]] - ...


13

For a 1D list you can also use Pick[Range@Length@list, list, Max@list]


14

A fast uncompiled alternative without pattern matching is to use the NonzeroPositions property of SparseArray, as long as you're dealing with numerical data. list = RandomInteger[{1, 100}, 10^7]; Needs["GeneralUtilities`"] SparseArray[Unitize[list - Max[list]] - 1]["NonzeroPositions"] // AccurateTiming (* 0.120459 *) Position[list, Max[list]] // ...


2

I think this works Ordering[dat, -Count[dat, Max[dat]]] but it is actually slower than Position[dat,Max[dat]] This also works, but again, it's still slower pos1[list_, max_] := Block[{i = 1, l = Length[list]}, Last[Reap[While[i <= l, If[list[[i]] == max, CompoundExpression[Sow[i], i++], i++]]]]] ... unfortunately so is this more compact solution ...


9

For a one-dimensional list: compPos = Compile[{{list, _Integer, 1}, {max, _Integer}}, Block[{copy = list, i = 1}, Do[ If[ list[[j]] == max, copy[[i++]] = j], {j, Length[list]}]; copy[[1 ;; i - 1]] ], CompilationTarget -> "C" ]; Though I think Position is a good non-compiled alternative in this case, since the "pattern" ...


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


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


1

One way to improve the performance of your code is to remove definitions of auxiliary functions from the bodies of function definitions. Such auxiliary functions are redefined every time the function is called. This is demonstrated by f[] := Module[{g}, g[] := SymbolName[g]; g[]] Table[f[], {4}] {"g$11401", "g$11402", "g$11403", "g$11404"} In your ...


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


0

This method is based on the original article. First we need to convert list of spring connectivity (spring) into a graph: g = Graph[Apply[UndirectedEdge, spring, 1], VertexCoordinates -> mass]; Then Kirchhoff (Laplacian/admittance/discrete Laplacian) matrix should be found using KirchhoffMatrix: m = KirchhoffMatrix[g] // Normal; We also need to ...



Top 50 recent answers are included