# Tag Info

11

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

7

Try to use lowercase names at least for the initial letter of your functions. My timings are different than yours. Probably your computer is much faster than mine. In your code pro is calculated in 144 seconds and GCD[func[160001],pro] needs another 34 seconds so a total of 178 seconds. Of course pro is calculated only once but the same happens in the ...

6

Mathematica is full of these idiosyncrasies and honestly I have no idea how one is to figure out why. Still I think it's an interesting question. One possibility is that Part was optimized at some point and Reverse got left behind. In my experience Part is highly optimized for packed arrays. At least in v7 Reverse is faster on unpacked data: ...

5

I found a good solution. The new version of Mathematica is using the symbolic processing, so we can just turn it off. b[i_] := NIntegrate[(Sin[x] - 1) x^i, {x,-5,5}, Method->{Automatic,"SymbolicProcessing"->0}] source: Techniques for Accelerating NIntegrate Evaluations http://support.wolfram.com/kb/3442

4

One can proof that QRDecomposition[m][[2]] returns the upper triangular matrix with the same singular values e = {1, 2, 3}; n = Length[e]; q := Orthogonalize[RandomReal[NormalDistribution[], {n, n}] + I RandomReal[NormalDistribution[], {n, n}]]; r = QRDecomposition[q.DiagonalMatrix[e].q][[2]]; SingularValueDecomposition[r][[2]] // Diagonal (* {3., ...

3

I post here my latest and final code. I am adding an additional post to expose a clear code. At first we define func and we put in prp list the candidate exponents. prp = {}; func[q_] := 77^q + 2; Do[If[TimeConstrained[PrimeQ[func[i]], 1, True], AppendTo[prp, i]], {i, 160000, 161000}] Then the following function returns the exponents from the list ...

3

Not a full answer but perhaps something you can work from. Thinking of a matrix as the action it does on the unit sphere $\{Ax ,\|x\| == 1\}$ which is an ellipsoid centered at 0, as it is the image of a linear transformation. The singular values represent the length of the semiaxes, the only freedom that remains is picking the orthonormal basis representing ...

3

A certain speedup is achieved when computing a with a NestList and Compiling . MainEvaluate is called only for the IdentityMatrix but i think this is ok because it is not the time consuming part of the code. I also used the "InlineExternalDefinitions" -> True option but the gain was very small. nnx = 200; pp = RandomReal[{-1, 1}, 2 {nnx, nnx}]; {n, q, ...

2

I did a few tests and arrived to the conclusion, that fastest and safest is to produce regular data that can be quickly read in. So this answer is more like a memo to remember which is the fastest solution. Accordingly, data should consist of: identically typed entries (e.g. only integers or reals) NaN-s are represented as out-of-scope values of same type ...

2

An approach using Reap and Sow: f[list_]:=Reap[MapIndexed[Sow[First@#2, #1] &, list], _, #1 -> First@#2 &][[2]] For: list=With[{a = 5000, b = 100}, list = RandomInteger[a, b a]]; then f[list]//Timing takes 0.9375 seconds.

1

This answer deals only with f, not IdentityMatrix (which is sadly not compilable). Compile is much better at extracting expressions form pure functions. If you are going to use InlineExternalDefinitions, consider making the external functions pure Functions. In[10]:= Clear[f] f = Function[t, If[t <= 1., Cos[t]*Sin[t], 0.]]; CompilePrint@ ...

1

In what IdentityMatrix[2] - f[t] evaluates to, there is pretty much four times the same code. Clear[t]; IdentityMatrix[2] - f[t] {{1 - If[t <= 1., Cos[t] Sin[t], 0.], -If[t <= 1., Cos[t] Sin[t], 0.]}, {-If[t <= 1., Cos[t] Sin[t], 0.], 1 - If[t <= 1., Cos[t] Sin[t], 0.]}} I will inline f using DownValues. Throughout this ...

1

Of the non-GatherBy methods, the fastest are those that use the same approach as Leonid Shifrin's positionOfDuplicates. In v5.2 I use posi[list_] := Sort@Part[Range[Length@list][[#]], Most@FoldList[Plus,1,Length/@Split@list[[#]]]]& @ Ordering@list which gives the position of the initial instance of each value in list, in the order in which they ...

1

I've made a number of changes to your code which may speed things up, but I honestly can't say with certainty. Rather than enumerating the changes, I'll just list the code here: ClearAll["Global`*"]; integrand[k_?NumericQ, P0_?NumericQ, P1_?NumericQ, rho_?NumericQ, l_?NumericQ] := Module[{x, h}, x = P1*l + P0*(1. - l) - rho; h = HankelH2[0, ...

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