Tag Info

Hot answers tagged

28

RPi configuration options The RPi has two easily accessible features that may influence the performance of M/RPi. The first is overclocking. The default is 700 MHz but the configuration software allows for 4 levels of overclocking: modest, medium, fast and turbo. The second option is the memory split. The Model B RPi comes with 512 Mb of RAM which is ...


24

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


18

Maybe two advises for the start: Use the fact that Sin is Listable and you can call Sin[{1,2,3,4,..}] to get a list of results. Don't calculate the sum twice. Calculate the sine part only once and make the multiplication with i in the first sum as vectorized multiplication. Taking this into account give in a first try something like fHal = Compile[{{n, ...


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


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

edit: this doesn't really answer the question but merely provides some other alternatives, you should probably up-vote other more useful answers. There are also faster ways to do this using Pick or by compiling Select. Timing comparison done on a Macbook Air OS X 10.8.3 w/ 1.7 GHz Intel Core i5 with Mathematica 9.0.0.0. t = RandomInteger[100, 10^7]; ...


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

Obviously, for large negative inputs, Exp will produce very small numbers. While this isn't intrinsically problematic, it so happens that, by default, Mathematica deals with machine underflow by converting the affected values to an arbitrary precision representation in order to avoid catastrophic loss of precision. However, sometimes one would rather ...


13

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


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


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

(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

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

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


10

In the first example, the list r1 is getting shorter each iteration, resulting in much fewer iterations overall: max = 10^5; r1 = Range[max]; c1 = 0; Timing[ For[i = 2, i <= Length[r1], i++, c1++; r1 = del[r1, i]]; c1] (* ==> {0.012426, 356} *) r2 = Range[max]; c2 = 0; Timing[Do[c2++; r2 = del[r2, i], {i, 2, Length@r2}]; c2] (* ==> ...


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

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


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


9

Mathematica does not do well with code that relies on mutable state (i.e. an explicit variable whose value is changing during the run of the program). Let's look at your For code: For[i=0, i < Length[t], i++, If[ t[[i]] > 50, r=Append[r, t[[i]]]] ] Notice that for every iteration, it needs to evaluate the following by interpreting high level ...


9

You can get about 100 times faster by using Java, without any particular tuning, but you will have to provide the date format explicitly. Here is the solution based on Java reloader. Implementation Load the Java reloader Compile the following class: JCompileLoad@ " import java.text.ParseException; import java.text.SimpleDateFormat; import ...


9

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


9

Straightforward method without interpolation, takes 0.1s but it feels like there is lots of room for improvement. I used the definition with dst_pixel so the orientation is different from result in question. I don't know of a quick way to have Extract or Part return a default value for indices that are out of range so I ended up using Compile instead: ...


9

Here are a few possibilities: MapThread[Count, {Take[target, #] & /@ Range@Length@target, target}] {1, 1, 1, 2, 2, 1, 3, 2, 1, 1, 2, 1, 1} MapThread[Count, {Reverse@NestList[Most, target, Length@target - 1], target}] {1, 1, 1, 2, 2, 1, 3, 2, 1, 1, 2, 1, 1} And my favorite: MapThread[Coefficient, {Accumulate[target], target}] {1, 1, ...


9

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


9

A more functional approach: a = With[{f = Subtract @@@ Subsets[Reverse@#, {2}] &}, f[v2]/f[v1]] For a bit more speed you could do this: ii = Join @@ Table[ConstantArray[i, i - 1], {i, n, 2, -1}]; jj = Join @@ Table[Range[j, 1, -1], {j, n - 1, 1, -1}]; a = Divide[Subtract[v2[[ii]], v2[[jj]]], Subtract[v1[[ii]], v1[[jj]]]];


9

Here is a simple method that seems to be somewhat faster than yours on unpackable data: colDrop[array_, drop_] := Module[{m = array}, m[[All, drop]] = Sequence[]; m] Test: data = Range /@ RandomInteger[{15, 50}, 500000]; data = Map[FromCharacterCode, data + 37, {2}]; colDropper[data, {1, 3, 5, 8, 10, 11}] // Timing // First colDrop[data, {1, 3, 5, 8, ...



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