# Tag Info

30

Minor update There was an update to the wolfram engine on the Raspberry Pi in late January. I presume there were some bug fixes and re-ran some of the benchmarks. Here, the results show that there was a significant performance boost in the way M/RPi handles linear systems: These results were obtained with a fresh install of the Raspbian image ...

28

This is a general guide on debugging issues with parallelization performance. 1. Measuring performance The proper way to measure the timing of parallelized calculations is AbsoluteTiming, which measures wall time. Timing measures CPU time on the main kernel only and won't give a correct result when used with parallel calculations. 2. How to parallelize ...

21

Well, you can use the undocumented RegionDistance which does exactly this as follows: (This only works for V9 as noticed by Oska) here is a triangle in 3D region = Polygon[{{0, 0, 0}, {1, 0, 0}, {0, 1, 1}}]; Graphics3D[region] Now suppose you want to find the shortest distance from the point {1, 1, 1} in 3D to this triangle just do the following: ...

21

As @RahulNarain says, forming the image point by point saves significant memory because the number of image pixels is typically much smaller than the hundreds of millions of iterations that compose it. Therefore, iterate the attractor equations, and for each point generated, find its location within the image matrix. Colour coding of the number of hits in ...

17

Regarding your 1. question A density plot is clearly not recommended for your problem. Firstly, a density is AFAIK per definition not complex, but let's ignore this for a moment. The real neck-breaker here is, that ListDensisityPlot interpolates the values if you don't turn it explicitly off. And even if you turn it off, a ListDensityPlot will create a ...

17

Here's a faster way: Clear[f] Timing[MapIndexed[If[Not@IntegerQ@f[#1], f[#1] = First[#2]] &, list];] Now f[elem] will tell you the position of the first occurrence of elem. On my machine this is approximately 8-10 times faster than your approach for a list of 10000 elements. The timing for a=5000, b=100 is 1.3 s on my machine. In general I expect ...

16

Well, you can trade memory for speed and use Compile, as follows: accumC = Compile[{{l, _Integer, 1}, {max, _Integer}}, Module[{accum = Table[0, {max}], res = Table[0, {Length[l]}]}, Do[res[[i]] = ++accum[[l[[i]]]], {i, Length[l]}]; res ] ] ClearAll[occurrences]; occurrences[lst_List] := With[{rules = Thread[# -> ...

15

This is an incomplete answer; I will continue it tomorrow. Work In Progress: errors may abound. Preamble hat-tip to Leonid For the variations with custom test or ordering functions we can snoop on applications of that function to deduce the algorithm that is used. In the case of the default methods we must rely on observed complexity and guesswork ...

15

I have found that my approach with textures has different applications: How to plot contours in the faces of a cube? How to plot ternary density plots? Now I want to use it for the enhancement of the DensityPlot: Options[fastDensityPlot] = Append[Options[DensityPlot], Subpoints -> 30]; SyntaxInformation[fastDensityPlot] = ...

14

Another problem is that the Table[..., {x, -L1, L1, δ}, {y, -L2, L2, δ}] produces unpacked array. f = With[{fun = Evaluate[ Sum[Exp[ω I Sqrt[(#1 - 1.0 Cos[θ])^2 + (#2 - 1.0 Sin[θ])^2]], {θ, 2 Pi/n, 2 Pi, 2 Pi/n}]] &}, Compile[{{x, _Real}, {y, _Real}}, {Mod[Arg[#]/(2.0 Pi), 1], 1.0, Abs[#/2]} &@fun[x, y], ...

13

Nasser gives good standard ControlActive approach. But that by definition looses quality during motion. I just would like to share a trick that avoids that. Most of the time is spent on rendering your bell shape. But it is static. Plane moves but it is simple, so it should not all the time trigger recomputing of static bell shape. You can separate motion of ...

13

From Oleksandr's comments: From my limited experience of Geekbench, the odd choice of weightings for the different components makes the result rather meaningless as a practical performance metric. This is a general problem: any real workload emphasizes particular aspects of performance more than others, and unless you can define the workload very ...

13

Here is a solution based on binary search (compiled). Implementation First, this is a version of a binary search, which would return the position of a maximal number in a list, smaller or equal to yours, and -1 if no such is found: ClearAll[bsminComp]; bsminComp= Compile[ {{lst,_Integer,1},{elem,_Integer}}, ...

13

One of the main bottlenecks in your code are the texture you apply on each surface. Try to use texture = {}; in your drawTile function and the graphics should be faster by magnitudes. Additionally, as Yves mentioned, Tubes are a performance killer too. Therefore, a workaround for your problem might be to Rasterize the texture graphics by yourself and use ...

13

UPDATE I thought it would be neat to try and animate the thing, so I let the $a$ parameter run between $-\pi$ and $\pi$. I generated 600 images and put them together using ffmpeg. Check it out on youtube. It might not be in the spirit of Mathematica Stack Exchange, but allow me an objection - stuff that is slow in Mathematica should be kept out of it. To ...

12

This is not a question of functional style per se. Functional style is, AFAICT, largely about avoiding mutable state (where possible), using functions as fundamental building blocks, using certain abstractions such as higher-order functions, closures, function composition, etc., and avoiding mixing state and behavior. I think that the value of pre ...

12

For unknown reasons, TensorProduct produces unpacked array (see packed arrays here). You can use Outer[Times, a, b] instead: \$HistoryLength = 0; L = 2000; a = RandomReal[{-10^6, 10^6}, {L, L}]; b = RandomReal[{-10^6, 10^6}, 3]; c = TensorProduct[a, b]; c2 = Outer[Times, a, b]; d = RandomReal[{-10^6, 10^6}, {L, L, 3}]; ByteCount /@ {a, b, c, c2, d} // Column ...

11

Try this: n = 1000; coeffs = RandomVariate[NormalDistribution[], n]; f[x_] := Sum[coeffs[[k]] Sin[k x]/k, {k, 1, n}]; Plot[Evaluate@f[x], {x, 0, 2. Pi}, PlotPoints -> n, MaxRecursion -> 0, Mesh -> All] // Timing With[{n = 1000}, First@Timing[Table[Evaluate@f[x], {x, 0, 2. Pi, 2. Pi/n}]] ] 2 times as fast as plot. I remembered my own ...

11

(Update, added more points, and more timings) Using MATLAB's help standard example for meshgrid: Mathematica implementation meshgrid[x_List, y_List]:={ConstantArray[x,Length[x]],Transpose@ConstantArray[y,Length[y]]} {xx, yy} = meshgrid[Range[-2, 2, .1], Range[-4, 4, .2]]; c = xx*Exp[-xx^2 - yy^2]; pts = Flatten[{xx, yy, c}, {2, 3}]; ListPlot3D[pts, ...

11

As halirutan comments Dispatch will speed the application of long lists of rules: SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}] n = 1500; big = Sum[Expand[(RandomInteger[99] + a[i])^RandomInteger[9]], {i, n}]; vals = RandomInteger[9999, n]; rules = Thread[Array[a, n] -> ...

11

You can use Mod to create a periodic distance function, with a period of, say, d0 (in each coordinate direction). This approach could be altered to have different periods in different directions. Then Nearest will create a NearestFunction that will return the nearest points modulo the period. In the animation below, the square on the left shows the points ...

11

The only improvement I can think of: comm[a_, n_] := #[[ Ordering[#[[All, 2]], -n] ]] & @ Tally[a] Or if you don't want the counts: comm2[a_, n_] := #[[ Ordering[#2, -n] ]] & @@ (Tally[a]\[Transpose]) Test: MyCommonest2[a, 15] // Timing // First comm[a, 15] // Timing // First comm2[a, 15] // Timing // First 0.3276 0.3056 ...

11

This seems fast(er): Extract[a, Transpose[{v, Range@Length@v}]] Addendum Mr.Wizard's clean method Diagonal @ a[[v]] has a surprising property for those of us who think that packed arrays rank just below the wheel in the list of inventions for the sake of efficiency. For unpacked arrays a, it uses virtually no extra memory. Example Initialization. ...

10

A print statement shows that this will overflow on platforms where Mathematica machine integers are 32 bits. pe14 = Compile[{}, Module[{n1, len, maxLen = 0, res = 0, print = 0}, Do[n1 = n; len = 1; While[n1 != 1, n1 = If[EvenQ@n1, n1~Quotient~2, 3 n1 + 1]; If[n1 > 10^4*n && print < 10, print++; Print[{n, n1}]]; ...

10

This is what I do. I have been using this method for long time. The idea is simple. Use the second argument of Dynamics. In there, make any changes to the state of the program you want, that only relates to the change of the current control variable being changed. In your case, in the second argument of a and b, you can make your heavy computations. In t, ...

10

I think that c[_] = 0; ++c[#] & /@ list is a perfectly good method and one that I make use of myself. There is an advantage in the fact that it can be interrupted or continued at any time, and it keeps a running count of the elements in the DownValues of c. (If not using Module that is.) Therefore I think it is a nice general method. One may ...

10

Notes: I completely overlooked the fact that rasher specified arbitrary precision input when crafting my functions. Because of this the numeric methods I used will not be as fast. However, to my surprise it seems that at least fn2 is still faster than the FoldList method on arbitrary precision data. Timings with resets on average at about 1 in 40: big = ...

10

I believe that this is an intentional and beneficial change in v10. Mathematica 9 was not able to correctly detect the number of physical cores, and it launched as many kernels as the number of virtual cores (which is double when using HyperThreading). Mathematica 10 can now detect the number of physical cores correctly and will launch only as many kernels ...

9

Map is autocompiling. If you turn autocompilation off, i.e., if you set SetSystemOptions[ "CompileOptions" -> "MapCompileLength" -> Infinity]; then the timings are the same.

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