Hot answers tagged

13

Here's a million points processed in half a second: SeedRandom[0]; (* updated for reproducibility *) xyCoordinateCentreCircle = RandomReal[1, {1*^6, 3}]; Map[ Mean[#[[All, -1]]] &, BinLists[xyCoordinateCentreCircle, {0, 1, 1/10}, {0, 1, 1/10}, {0, 1, 1}], {3}] // AbsoluteTiming (* {0.513721, {{{0.506174}, {0.497757},..., {0.50284}}, ...


11

You can rewrite your idea using If as follows: If[MemberQ[elList, #], 1., 1.13] & /@ els {1.13, 1., 1., 1.13, 1.} However you may find on larger problems that repetitive use of MemberQ is not as fast as you would like, so consider a hash table in the form of an Association, or if using an older version of Mathematica a Dispatch table. Create a ...


10

The following is not particularly fast and could be more accurate but does make progress toward the goals set in the Question. To begin, consider the analytical properties of f, as defined in the Question. By observation, it has branch points at {I Sqrt[6/5] ξ, I Sqrt[2/5] ξ} and their conjugates. Poles are obtained by p /. ...


10

Slightly expanding my comment. It is a partial answer, explaining most but not all of the observed timing increase. First I show some representative timings. Note that I use 2^16-1 and 2^21-1 below, for reasons that will be explained. AbsoluteTiming[RandomInteger[2^16 - 1, 100000000];] AbsoluteTiming[RandomInteger[2^21 - 1, 100000000];] (* Out[37]= ...


9

Mr.Wizard proposed that the slowness is due to the copying of the data. It seems that this intuition is correct. We can use the two different arguments passing mechanism in LibraryLink to test this conjecture. In LibraryLink, there are the "copied" passing and the "shared" passing. The copied passing copies the data from Mathematica to the library function ...


8

Dirichelet random variates are constructed from gamma random variates. Let $$ v_j \stackrel{\text{iid}}{\sim} \textsf{Gamma}(\alpha_j,1) $$ for $j = 1, \ldots, n$ and set $$ x_j = \frac{v_j}{\sum_{j=1}^n v_j}. $$ Then $x \sim \textsf{Dirichlet}(\alpha)$, where $x = (x_1,\ldots,x_n)$ and $\alpha = (\alpha_1,\ldots,\alpha_n)$. Gamma random variates ...


7

You can use Interpolate to interpolate between the polygon vertices: First make sure the polygon is closed, by appending the first vertex: cyclic = Append[manualPolyPoints, First[manualPolyPoints]]; then accumulate the distance from one vertex to the next: accumulatedDistance = Rescale@Prepend[Accumulate[Norm /@ Differences[cyclic]], 0.]; then ...


7

Sure, try for example: pr[14709321003111578837870501266345370175409, 2, 2] There are many things in MMA where small cases/edge cases can be done much more quickly with user code, this is one of them. The advantage here is that PowersRepresentation can handle huge cases...


7

Solution for Update1 The ellipses described by matThetaList can be plotted by Show[ParametricPlot[#[[1]].{Sin[θ], Cos[θ], 1}, {θ, 0, 2 Pi}] & /@ matThetaList, PlotRange -> All] To describe each of these four curves as an ImplicitRegion, first eliminate θ from the parametric equations given in the question, h = Total[#^2 & /@ ({Sin[θ], ...


7

Here, I applied the discrete strategy(sampling $400$ points in a period $2\pi$) to calculate the boundary of ellipses $E_1,\cdots,E_n$. The algorithm mainly consists of four steps as follows : Using the ellipses $E_2,\cdots,E_n$ to trim the black segment of the first ellipse $E_1$; Using the ellipses $E_1,\cdots,E_{n-1}$ to trim the red segment of the last ...


6

Verily, this is a headache, since Table wants its iterators as Sequence rather than nested list of lists. Here is a method I used quite recently to get sufficiently fast code for this. c1c[n_] := With[{itvals = RandomReal[{0, 1}, {n, 5}]}, With[{iters = Apply[Sequence, Table[{x[j], itvals[[j]]}, {j, n}]]}, c1[n] = cCompile[{}, tTable[1, ...


6

If the date format is sufficiently rigid, you might try string patterns or regular expressions. AbsoluteTime[{"05-Mar-2004 10:15:00", {"Day", "-", "MonthNameShort", "-", "Year", " ", "Hour24", ":", "Minute", ":", "Second"}}] // RepeatedTiming (* {0.00043, 3287470500} *) This is about 10 times faster: months = <|"Jan" -> 1, "Feb" -> 2, ...


5

The answer is as you suspect - when you evaluate Dot[m1, m2, m3, m4, m5, ......m1000] the process is something like this: Look at the input: Dot[m1, m2, m3, m4, m5, ......m1000] Evaluate the first matrix product, m12=m1.m2 Look at the input: Dot[m12, m3, m4, m5, ......m1000] Evaluate the first matrix product, m123=m12.m3 Look at the input: Dot[m123, m4, ...


5

Your cube file had a very large grid ( 117*117*130 = 1779570), and 2 million points is just far too many for testing a function. So I created cube files for the electron density and electrostatic potential for the molecule furan, using a much sparser grid (around 8000 grid points instead). Here they are: Density cube file Potential cube file Now that ...


5

Update: You can go even faster with Compile, and exploit the Listable and Parallelization attributes to great effect, if you have a multi-core machine: SeedRandom[0]; cluster = RandomReal[{0, 1}, {5000, 14}]; myDistMatrix = Compile[{{point, _Real, 1}, {tr, _Real, 2}}, Total[(point - tr)^2], RuntimeOptions -> "Speed", ...


4

You just need to fully compile your function: fullycompiledBSplineSurf = Hold@Compile[{{ctrlnets, _Real, 3}, {deg1, _Integer}, {deg2, _Integer}, {knots1, _Real, 1}, {knots2, _Real, 1}, {u, _Real}, {v, _Real}}, Module[{i, j, validnets, row, col}, i = searchSpan[{deg1, knots1}, u]; j = searchSpan[{deg2, knots2}, v]; validnets ...


4

I believe that on packed arrays both Dot and Times are performed by external libraries, e.g. Intel MKL, and that following Mathematica's paradigm of immutability the library does not act directly upon the original array but rather a copy. I conjecture that this copying or transport is the cause of the slow-down that you observe and that within Mathematica ...


4

SetAttributes[hmsAbsTiming, HoldAllComplete]; hmsAbsTiming[calculation_] := MapAt[IntegerDigits[IntegerPart[#], MixedRadix[{24, 60, 60}]] &, AbsoluteTiming[ calculation ], 1] If you prefer a Quantity object: SetAttributes[hmsAbsTiming2, HoldAllComplete]; hmsAbsTiming2[calculation_] := MapAt[UnitConvert[Quantity[#, "Seconds"], ...


4

Not an answer, but just a collection of results on my computer (Mac OS X 11.4) The timings are in seconds as reported by AbsoluteTiming. They are in the same order as the test cases provided by OP On Mathematica 8: {1.44, 1.33, 271.7, 0.000066} On Mathematica 9: {0.62, 0.61, 9.00, 0.00012} On Mathematica 10.3: {8.82, 8.78, 8.62, 0.00006} On ...


4

If you're on 10.3+, this should be faster (it's two orders of magnitude faster than your first example on my loungebook): medioid=With[{m = #, d = Tr /@ DistanceMatrix[#, DistanceFunction ->SquaredEuclideanDistance]}, {First@m[[#]], First@#} &@Pick[Range@Length@d, d, Min@d]]&;


4

This is much faster: distMatCompiled = Compile[{{cluster, _Real, 2}}, Outer[Function[diff, diff.diff][#1 - #2] &, cluster, cluster, 1, 1] , CompilationTarget -> "C" ] medoidCompiled[cluster_] := Block[{distances, indexOfMin}, distances = Total@distMatCompiled[cluster]; indexOfMin = First[Ordering[distances, 1]]; {cluster[[indexOfMin]], ...


3

Here goes the benchmark for a high-end surface book with i7-6600U (2.6 up to 3.4 GHz, 4 MB cache, 15 W), which might be not quite different from surface pro 4 with intel i7-6650U (2.2 up to 3.4 GHz, 4 MB cache, 15 W). Plugged On battery


3

This is your definition: m = 1; u[x_, t_] = (t^\[Alpha]*x*(2*t^2 + (1 + \[Alpha])*(2 + \[Alpha])))/ Gamma[3 + \[Alpha]]; Your code makes 3.19 seconds on Mma10.4.1 DUt = FullSimplify[(1/Gamma[m - \[Alpha]])*Integrate[(t - \[Tau])^(m - \[Alpha] - 1)*D[u[x, \[Tau]], {\[Tau], m}], {\[Tau], 0, t}], Assumptions -> {m - 1 < \[Alpha] < m, t ...


3

Perhaps something like: f = 6 + # Sin[# + Pi] - Cos[Pi #/4] &; g = # Cos[# + Pi/3]/2 - 1/12 Sin[Pi #] &; Plot[{f[t], g[t]}, {t, 0, 3 Pi}, PlotStyle -> {Red, Blue}, Mesh -> {{0}, {0}}, MeshFunctions -> {ConditionalExpression[f'[#], f''[#] < 0 && g[#] > 0] &, ConditionalExpression[g'[#], g''[#] < 0 && ...


3

This is a very quick-and-dirty, but gives 4X speed-up (7X with tweak for symmetry) on my crappy netbook for the n=8 case, don't have time or patience to test bigger cases to see scaling differences. Perhaps a description of what you're trying to calculate? It appears to be some combinatorial problem, there may well be a much more efficient scheme to do ...


3

As example functions I'll use x[t_] := (0.5 (1 - t^0.8)^0.625 + 0.5 t^0.5)^2 y[t_] := (0.7 (1 - t^0.8)^0.125 + 0.3 t^0.1)^10 To get the points that were calculated while plotting, one can use Reap and Sow. {plot, {tValues, xValues, yValues}} = Reap@ParametricPlot[{Sow[x[Sow[t, "t"]], "x"], Sow[y[t], "y"]}, {t, 0, 1}]; Because ParametricPlot does ...


3

So why the matrix number multiplication and addition so much slower than the matrix vector multiplication? Are there ways to speed them up? It seems that if we use SparseArray for this computation we can get ~3 times speed-up: n = 2000; tres = Table[ (Print[lth]; mtx = RandomReal[{0, 1}, {lth, lth}]; {lth, (AbsoluteTiming[ ...


2

Here are couple of quick tips. Lets say this is your matrix mat = SparseArray[{{i_, j_} /; Abs[i - j] == 3 -> 1, {i_, i_} -> 1}, {200, 200}]; This is the conventional way to find EigenSystem Eigensystem[SparseArray[{{i_, j_} /; Abs[i - j] == 3 -> 1, {i_, i_} -> 1}, {200, 200}]]; // AbsoluteTiming {53.6551, Null} Now just change the ...


1

So I found a method that might be of interest, but i'm not sure it's optimal. rawPlotData = ParametricPlot[{x[t], y[t]}, {t, 0, 1}][[1]]; (* get plot data *) plotData = Apply[List, rawPlotData[[1, 3, 2]]][[1]]; Index (1,3,2) of rawPlotData has a Line[-data-] object where -data- contains all the points. I don't know if this is the address for all ...


1

If the digit and month characters always appear in the exact same place you can use StringTake to extract them and a lookup table for all the conversions: a = Association@Join[ StringPadLeft[ToString[#], 2, "0"] -> # & /@ Range[0, 99], {"Jan" -> 1, "Feb" -> 2, "Mar" -> 3, "Apr" -> 4, "May" -> 5, "Jun" -> 6, "Jul" -> ...



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