Tag Info

Hot answers tagged

125

Since Mathematica is a symbolic system, with symbolic evaluator much more general than in Matlab, it is not surprising that performance-tuning can be more tricky here. There are many techniques, but they can all be understood from a single main principle. It is: Avoid full Mathematica symbolic evaluation process as much as possible. All techniques seem ...


78

Preamble I spent some time and designed and implemented a tiny framework to deal with this problem, over the last two days. Here is what I've got. The main ideas will involve implementing a simple key-value store in Mathematica based on a file system, heavy use and automatic generation of UpValues, some OOP - inspired ideas, Compress, and a few other ...


74

How can this code be improved, for example, by including shadows, raytracing or the effects of gravity to make it more realistic? I felt that this question deserved an answer. The one I describe here is to create a set of confetti "agents" that respond in quasi-physical ways to external forces and "know" how they should be displayed. It is handy, and ...


69

As promised in the comments on my first answer, here is an implementation of an all-compiled-code Nelder-Mead minimizer, which hopefully represents a more useful response to the question. The algorithm used here corresponds to that given by Lagarias et al. in SIAM J. Optim. 9 (1), 112 (1998) (abridged .pdf). It is compatible with Mathematica versions 6, 7, ...


56

You can put your Mathematica session in debug mode by going to Evaluation->Debugger Then, make some definitions and wrap the profiled code in RuntimeTools`Profile For example, in debug mode, run f[x_] := x^2 Table[f[x], {100000}]; // RuntimeTools`Profile and you get a nice As @acl mentioned in the comments, clicking in the gray area in the output ...


52

This approach is based on a random walk of a shrinking disk. Several of these are combined and a Gaussian filter is used to smooth it out. Optionally the smoothed image can be multiplied by the original to restore the tiny "droplets" that are wiped out by the smoothing. There is a streakiness parameter which biases the random walk in a particular direction. ...


48

Use these 3 components: compile, C, parallel computing. Also to speed up coloring instead of ArrayPlot use Graphics[Raster[Rescale[...], ColorFunction -> "TemperatureMap"]] In such cases Compile is essential. Compile to C with parallelization will speed it up even more, but you need to have a C compiler installed. Note difference for usage of C and ...


46

I will answer a couple of your questions only. Space efficiency Packed arrays are significantly more space efficient. Example: Let's create an unpacked array, check its size, then do the same after packing it: f = Developer`FromPackedArray[RandomReal[{-1, 1}, 10000]]; ByteCount[f] ByteCount[Developer`ToPackedArray[f]] (* 320040 80168 *) Time efficiency ...


43

Preview and comparative results The implementation below may be not the most "minimal" one, because I don't use any of the built-in functionality (DictionaryLookup with patterns, Graph-related functions, etc), except the core language functions. However, it uses efficient data structures, such as Trie, linked lists, and hash tables, and arguably maximally ...


40

My solution is a recursive tree traversal algorithm which seeks and searches neighbouring vertices only if it will lead to a word (e.g., Something starting with ZQ is immediately disqualified), but it's faster than yours because I construct the adjacent vertices list from the adjacency matrix rather than calling NeighborhoodGraph each time. On my machine, ...


39

The difference Packed arrays give you pretty much an access to a direct C memory layout, where the arrays are stored. Unpacked arrays reference arrays of pointers to their elements. This explains most of the other differences, in particular: Space efficiency: if you look at how much space is required for packed arrays, you see that it is exactly the ...


38

You can use GatherBy for this. You can map List onto Range[...] first if you wish to have exactly the same output you showed. positionDuplicates[list_] := GatherBy[Range@Length[list], list[[#]] &] list = {3, 3, 6, 11, 13, 13, 11, 1, 2, 3, 12, 8, 9, 9, 4, 15, 5, 6, 9, 12} positionDuplicates[list] (* ==> {{1, 2, 10}, {3, 18}, {4, 7}, {5, 6}, {8}, ...


36

Mathematica is every bit as fast as Matlab for these types of computations. The source of the discrepancy arises from the fact that Timing keeps track of total time used by all processors when Mathematica distributes the computation across them. We can examine a fair comparison using AbsoluteTiming, which is more comparable to Matlab's tic and toc. ...


35

Here's a slow and concave version: blot[smoothness_: 20, points_Integer: 10] := With[ {fun = Exp[-smoothness #.#] &, pts = RandomReal[1, {points, 2}]}, RegionPlot[ Total[fun[# - {x, y}] & /@ pts] > .5, {x, -.5, 1.5}, {y, -.5, 1.5}, Frame -> False, PlotStyle -> Black, BoundaryStyle -> Black] ] Grid@Table[blot[], {3}, {3}] ...


35

NMinimize is implemented entirely in Mathematica code, so its behaviour is open to examination by investigation of symbols defined in the Optimization`NMinimizeDump` context (after calling NMinimize at least once to pre-load the necessary package). In particular, the ability to give diagnostic output is present, and can be switched on using the internal ...


34

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


33

A bit of image processing: Table[ Blur[ Dilation[ Graphics[ Table[ Rotate[ Disk[RandomReal[{-10, 10}, {2}], {RandomReal[{1, 5}],RandomReal[{1, 5}]}], RandomReal[{0, 3.14}] ], {40} ] ], DiskMatrix[20] ], 20 ]// Binarize, {3}, {3} ] // Grid Lots of parameters to ...


32

You may use the profiler included in the Wolfram Workbench


31

There are two performance problems here. The first is relatively minor: MultinormalDistribution[μ, Σ] is evaluated in each slave kernel, returned to the master kernel, and sent back to the slave kernels as part of the RandomVariate call. In your example, this is a packed array of about 80KB in size: not large, yet not small either, and this behaviour may ...


31

No fluid dynamics I'm afraid, but here's what I came up with Preliminaries n = {200, 200, 200}; dim = 2; edges = {.015, .018, .024}; speed = {{0, -1}, {0, -1.5}, {0., -2}}; basePoly = {{0, -1}, {1/2, 0}, {0, 1}, {-1/2, 0}}; period = 3; Initial position colour and orientation angularVelocity = N[RandomChoice[Range[-8, 8], #] period Pi] & /@ n; ...


30

I second @Verbeia's suggestion: compute the function on a mesh of points and use ListContourPlot. The disadvantage is that ListContourPlot has no adaptive sampling, so it'd be preferable if we could do our own adaptive sampling somehow. Adaptive sampling can give you a much better result while needing to compute the function in far less points---and the ...


30

UPDATE Since version 10, we have Associations. Here is the modified code for trie building and querying, based on Associations. It is almost the same as the old code (which is below): ClearAll[makeTreeAssoc]; makeTreeAssoc[wrds : {__String}] := Association@makeTreeAssoc[Characters[wrds]]; makeTreeAssoc[wrds_ /; MemberQ[wrds, {}]] := ...


29

An inkblot used to look like this, in the days when I used fountain pens and indian ink, rather than Mathematica: blot = Image[BubbleChart[RandomReal[1, {20, 3}] , Axes -> None, Frame -> None, ColorFunction -> Function[Black], BubbleSizes -> {.001, .3}, Background -> LightGray, ChartElementFunction -> "NoiseBubble", ImageSize ...


29

Simple solution Why not just dropWhile[list_, test_] := Drop[list, LengthWhile[list, test]] ? Fast JIT-based solution with automatic type identification / dispatch Here I will show a solution that is potentially much faster on packed arrays. The code is directly modeled after this answer, so I refer to some additional details there. JIT version with ...


28

For general large integers n, I don't know if there's a better method than Min[IntegerExponent[n, 5], IntegerExponent[n, 2]]. Or more compactly, IntegerExponent[n, 10] or IntegerExponent[n].


28

I will try to list some cases I can recall. The unpacking will happen when: The result, or any intermediate step, is a ragged (irregular) array. For example Range /@ Range[4] To avoid this, you can try to use regular structures, perhaps padding your arrays with zeros appropriately The result (or any intermediate step) contains numbers of different ...


28

Using Outer is here one of the worst methods, and not just because it computes the distance twice, but because you can't leverage vectorization in this approach. This is actually a common issue and an important point to stress: Outer works pairwise and is unable to utilize the possible vectorized nature of the operation it is performing on an ...


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


28

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


27

You could do something like this mean = Reap[Sow @@@ Flatten[cogAK6, 1];, _, {Mean[#2], #} &][[2]]; This will be a lot faster than your approach because by using Sow and Reap this code only iterates through the list of data once. In your code, you reiterate through all elements of the data list for every value of gazeNo (so 3000 times instead of only ...



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