# Tag Info

1

I ran your code on my computer, a 4-year old i7 iMac running V10.0.2 on OS X 10.6.8 (Snow Leopard). The first set of timings I got were 0.105747 0.105747 16.865200 The above timings are slower than yours. I attribute that to age of the iMac. For the ratios I got 48.9902 159.486 which are a little better than yours.

9

Unless both lists given to Equal are packed arrays Equal will first unpack. Unfortunately for this case {} is not a packable expression, therefore list == {} will always unpack list, assuming it starts packed. That unpacking takes time: test = RandomInteger[100000000, 10000000]; Developer`FromPackedArray[test]; // AbsoluteTiming {0.207012, Null} ...

7

Here's my functional variant of your code: findSeam2[e_List] := Module[{f = FoldList[MinFilter[#1, 1] + #2 &, First[e], Rest[e]]}, Reverse@ FoldList[#1 + First@Ordering[#2[[Max[1, #1 - 1] ;; Min[Length[#2], #1 + 1]]]] - 1 - If[#1 == 1, 0, 1] &, First@Ordering[Last[f], 1], Reverse@Most[f]]]; And my test case ...

1

I failed to think of anything with competitive performance, however in simply working through the problem I produced a function that is somewhat different from the rest. Despite being slower than the original perhaps it will inspire something better. f1 = Module[{a = Range @ Length @ #}, Scan[(a[[# ;;]] = RotateLeft @ a[[# ;;]]) &, #]; a ] ...

10

The slowness is due to the fact that several steps in your code were not compilable because they invoked MainEvaluate. I localized all the variables by adding Module. Then, several variables were mis-recognized as integer when they should be reals. To fix this I added decimal points to some numbers like 1. The externally defined functions at the beginning ...

10

My answer is based on a modification of a binary heap. Basically the construction looks something like this. We start with a binary tree: Notice that if we label the nodes breadth-first, the labels have an interesting property. Each parent node $n$ has two children, $2n$ and $2n+1$. This also works in reverse: the parent of node $n$ is node ...

7

I have a tree-based method that has the right asymptotics but a very high coefficient. The upshot being, it will not compete with other methods until we get past 10^6 or so in list size. With considerable work that tree structure could be flattened so that Compile might be brought into play. The basic tree layout is {left subtree, node, right subtree} where ...

2

l = {1, 2, 3, 4, 5}; t = {1, 1, 2, 1, 1}; l2 = t; Fold[(Delete[l2[[#2]] = #1[[t[[#2]]]]; #1, t[[#2]]]) &, l, Range[5]]; l2 (*{1, 2, 4, 3, 5}*)

5

Module[{p = Range[Length@#]}, Reap@Fold[(Sow[#[[#2]]]; Drop[#, {#2}]) &, p, #]] &@{1, 1, 2, 1, 1} (* {{}, {{1, 2, 4, 3, 5}}} *)

11

Preface Below, you will find two different solutions. For understanding the problem itself, the first, iterative solution is better suited since it gives insight in how the solution can be found without directly executing the instructions given as input. Iterative Solution Detailed explanation To explain the idea behind this approach let us work with a ...

2

If I've understood correctly you appear to be wanting a nested delete in which you keep the deleted items in their preserved order of deletion. This solution is probably not any easier than yours but here goes: Rest@FoldList[{Join[#1[[1]], {#1[[2, #2]]}], Delete[#1[[2]], #2]} &, {{}, Range[5]}, {1, 1, 2, 1, 1}] (* {{{1}, {2, 3, 4, 5}}, {{1, 2}, ...

6

Starting with an extension to the point raised in your edit. rL = RandomReal[{0.5, 1.5}, 10000]; timings = Table[{10^n, AbsoluteTiming[Exp[-(rL*10^n)]][[1]]}, {n, 0, 5, .01}]; ListLogLinearPlot[{timings, {{746, 0}, {746, 0.05}}}, AxesLabel -> {"Scaling", "Time"}, Joined -> {False, True}, PlotStyle -> {Black, Red}] The red vertical line is ...

3

Yes, there is! One way to do such things more quickly: multinomialRangedIP[n_, ps_, min_, max_] := Total[PDF[MultinomialDistribution[n, ps], Permutations@#] & /@ IntegerPartitions[n, {Length@ps}, Range[min, max]], 2] Using the OP example, multinomialRangedIP[50, N@{1/2, 3/8, 1/16, 1/16}, 5, 20] is over 200X faster. That advantage grows as ...

1

You don't even need to numerically integrate. Each of your intended integrals is simply: $$\int_0^\infty e^{-k x^2}dx={1\over 2}\sqrt{\pi\over k}$$ Also you don't need to evaluate a bunch of Bessel functions, since BesselJZero[1/2,n] is $n\pi$. As noted by @belisarius, your first term would diverge if you integrate to $\infty$, since the integrand is 1. ...

1

Slider does not allow play button but Manipulator does. This is an example and I don't know if it fits you need. Grid[{{Manipulator[Dynamic[x]], Dynamic[x]}, {Manipulator[Dynamic[y]], Dynamic[y]}, {Show[{Graphics[{EdgeForm[Black], Dynamic@Circle[{x, y}, 1]}, PlotRange -> {{-2, 2}, {-2, 2}}], Graphics[ Translate[{Opacity[0.3], ...

3

First of all, we should be aware that given loop has number of operations proportional to $k^6$, so the time grows quite fast with $k$. I think the best way to implement $i_{21} + i_{31} + i_{41} + i_{32} + i_{42} + i_{43} \le k$ is to sum over (you can insert it in your code instead of your arguments of Do) {idx21, 0, k - idx43 - idx42 - idx32 - idx41 ...

15

It seems that there is a significant overhead every time a color scheme is switched. Once a scheme is loaded each use is fast, but changing color schemes apparently unloads and reloads the mechanism. The result is that the speed of application is directly related to the frequency of switching. With sorted values there is only one switch and application is ...

2

Adapting listMaxArg from linked topic seems to be the fastest. list = RandomReal[1, {10^6, 2}]; list[[Ordering[list[[All, 2]], 1]]] // AbsoluteTiming {0.010000, {{0.817248, 6.71112*10^-7}}}

2

The reason why the second method is failing is because you are looking for a pair whose second element is the minimum over the whole list (including all the first elements). This of course will fail whenever the global minimum is in the first position. The proper syntax is Cases[list, {_, Min[list[[All, 2]]]}] The speed improvement, on the other hand, is ...

2

The fastest I can come up with is (by separating finding the minimum second value in each pair): AbsoluteTiming@With[{min = Min@list[[All, 2]]}, Cases[list, {_, min}]] (* {1.288129, {{0.555911, 1.05947*10^-6}}} *) while MinimalBy takes much longer: AbsoluteTiming@MinimalBy[list, #[[2]]&] (* {2.074207, {{0.555911, 1.05947*10^-6}}} *) Your second ...

0

project euler 28 This is a pad version, padMatrix is written a bit complex. f[x_] := 1/4 (-1)^x (1 + 3 (-1)^x + 2 (-1)^x x) padMatrix[x_, pad_, type_: 0] := Which[type == 1, Append[x\[Transpose], pad]\[Transpose], type == 2, Append[x, Reverse@pad]\[Transpose], type == 3, Prepend[x, Reverse@pad], type == 4, Prepend[x\[Transpose], pad]] ...

2

GatherBy does exactly what you want. l = {{a, obj1}, {c, obj2}, {a, obj3}, {b, obj4}}; GatherBy[l, First] {{{a, obj1}, {a, obj3}}, {{c, obj2}}, {{b, obj4}}}

1

You can't do this. If correct behaviour is f[{{1,2},{3,4}}] -> {f[{1,2}],f[{3,4}]} then what is correct behaviour for f[{1,2}] ??? Clearly the second expression has no idea that it came from a previous application of f unless you find some way to tell it. Map is the simple and correct way of achieving what you want; I don't believe that it is ...

6

Odd that this question had no answers until today. Anyway the performance of various method unsurprisingly depends on the details of their applications. Generally speaking there are several layers of possible performance in Mathematica including optimized C libraries, sometimes with automatic parallelism, functions compiled within Mathematica either ...

3

The problem isn't choosing whether Map, Apply, etc., are faster, but how to decide how much work you're asking Mathematica to do. I like the following method: Trace shows you every detail of every step of a calculation; if you apply Trace to each of your calculations, you'll see that there are simply more steps required for some methods than the others. ...

2

"% of CPU" represents the entire capacity of your system. If you have N cores and a process uses only one core, then it will use 100/N % of the capacity of your system --- you obviously have 8 cores. Unless a problem is explicitly parallelised, it won't run on more than one core. You should be able to parallelise Simplify / FullSimplify: ...

Top 50 recent answers are included