# Tag Info

33

New version update The July 29, 2014 version of Mathematica brings to the RPi the ability to evaluate expressions on the cloud, in other words, to cheat at benchmarking! As an added bonus, we have a default retro color scheme for BarCharts that is sure to please fans of the 70s. I have a sudden urge to dust off my Algol and Cobol handbooks. Ahem. The ...

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

25

Well, you can use the undocumented RegionDistance which does exactly this as follows: (This answer, as written, only works for V9 as noted by Oska, for V10 see update below) 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 ...

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

17

@Simon Woods points out in a comment that: In fact the delay on the initial run is caused by compiling code to provide the Poisson distribution :-) You can look at ImageColorOperationsDumpiImageEffectPoissonNoise to see how it works internally. Now, although PoissonDistribution can't be compiled, there's nothing stopping the use of my own C++ ...

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

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

14

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

Let's start by taking a look at the compiled form of one of our queries: DatasetCompileQuery[Query @ First @ spans] (* DatasetWithOverrides@*Checked[Slice[205 ;; 313], Identity] *) We can see that the operation is not implemented directly in terms of part. Indeed, there are three components: DatasetWithOverrides, GeneralUtilitiesChecked and ...

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

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

To create ExperimentalNumericalFunction, one needs to evaluate ExperimentalCreateNumericalFunction[vars, expr, dims] where vars is a list of arguments, expr - the expression from which the numerical function will be created, dims - the dimensions of the output matrix produced by this expression. If the output is scalar, then dims should be set to {}. It ...

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

12

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

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

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

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

9

I think this may be a duplicate of: How to avoid unpacking from Language`ExtendedFullDefinition In Mathematica parallelism is only useful when processing takes longer than data transfer, otherwise the overhead of that transfer will make the parallel operation slower than the plain one. It should be somewhat faster than your original use of ParallelMap, but ...

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