Tag Info

Hot answers tagged

20

Update 20/02/2015 I've now adapted my code to work with a binomial distribution, since that seemed to be the most problematic case for the OP. The relevant C++ code is at the bottom of this answer. Needs["CCompilerDriver`"] BinomialVariateLib = CreateLibrary[{ToString[NotebookDirectory[]] <> "binomialvariate.cpp"}, "BinomialVariateLib", ...


18

This is a poor use of the random functions in Mathematica. As clearly stated in the documentation, generation of variates one at a time has significant overhead, and generating them en masse has significant benefits, particularly with statistical distributions: For statistical distributions, the speed advantage of generating many numbers at once can be ...


12

You miss that many Mathematica functions are listable. It allows you to write a fast and clear code init2[distance_] := uniMass Total[liuQuartic[distance, h] UnitStep[2 h - distance], {2}] h = 0.1; uniMass = 1.0; liuQuartic[d_, h_] := d^2 - h^2; totalPos = RandomReal[1, {1119, 2}]; res1 = initializeDensity@computeDistance[totalPos]; // AbsoluteTiming res2 ...


10

Modify the calculation order a little to avoid ragged array and then make use of Listable and Compile: computeDistance[pos_] := DistanceMatrix[pos, DistanceFunction -> EuclideanDistance] liuQuartic = {r, h} \[Function] 15/(7 Pi*h^2) (2/3 - (9 r^2)/(8 h^2) + (19 r^3)/(24 h^3) - (5 r^4)/(32 h^4)); initializeDensity = With[{l = liuQuartic, m = ...


9

I believe the difference you are observing is attributable to packed arrays. What is a Mathematica packed array? The difference between slow and fast is due to the behavior of RandomChoice. Observe: << Developer` RandomChoice[{pr1, 1 - pr1} -> {1, 0}, 1350] // PackedArrayQ RandomInteger[1, 1350] // PackedArrayQ ...


7

We have a new Raspberry Pi with a 4-core ARMv7 processor and 1 GB of RAM. A recent version of Mathematica was just rolled out which apparently addresses a licensing issue that limited the number of cores that Mathematica was using. It's time to revise the benchmarking reports. Here's the results from Benchmark[] with a comparison to the ...


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


5

First of all, I think your mask calculation is wrong: That's because Mathematica array indices are row, column, 1-based, and data[[1,1]] is at the top-left corner of the image, while coordinates are x,y, 0-based and {0,0} is at the bottom-left corner. So the right way to build these masks would be: masks = Table[ Boole[InPolyQ[#, {j - 1, imgD - i}]], ...


5

Translated that roots() function from Matlab code to Mathematica, about 4 times faster than NRoots . Clear["`*"]; n=2000; m=RandomReal[1,{n,10}]; res1=x/.(ToRules@NRoots[FromDigits[#,x]==0.,x]& /@ m);//AbsoluteTiming (*-------------------------------*) roots[c_List]:=Block[{a}, a=DiagonalMatrix[ConstantArray[1.,Length@c-2],-1]; a[[1]]=-Rest@c/First@c; ...


4

Here is an actual solution that works. The first 3 lines of code were what I needed to make this work on my Mac. It is not in the documentation and you'd need something else on a PC. Needs["RLink`"] SetEnvironment["DYLD_LIBRARY_PATH" -> "/Library/Frameworks/R.framework/Resources/lib"]; InstallR["RHomeLocation" -> ...


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


3

Edit: At the end of this post, you will find an implementation with Nearest which is as fast as Nikies solution. The unfortunate thing is, that my first idea was to use Nearest but I somehow did not time it correctly. Since I wanted to answer to this comment and show that my implementation is faster, I timed it again - this time correctly - and I have to ...


3

Here's another way that's quite a bit faster on David Stork's example: #.Y.# &[A.X] SeedRandom[0]; A = Table[RandomReal[], {3000}]; X = Table[RandomReal[], {3000}, {8000}]; Y = Table[RandomReal[], {8000}, {8000}]; AbsoluteTiming[A.X.Y.X\[Transpose].A] AbsoluteTiming[(A.X).Y.(X\[Transpose].A)] AbsoluteTiming[A.(X.Y).(X\[Transpose].A)] ...


3

Change the method used by NIntegrate: pdf[s_?NumericQ] := combn NIntegrate[ N[q^k (1 - q)^(n - k), 100] (E^(4 n q s) (1 - q)^(-1 + 4 n \[Mu]) q^(-1 + 4 n \[Nu]))/ NIntegrate[ E^(4 n q s) (1 - q)^(-1 + 4 n \[Mu]) q^(-1 + 4 n \[Nu]), {q, 1/(2 n + 1), 1 - 1/(2 n + 1)}, MaxRecursion -> 12, Method -> ...


2

Since your code has a bottleneck in NotebookGet, the only way to speed it up directly would be to speed up the latter. If your notebook exists on disk, you may try using something like this: ClearAll[nbget]; nbget[nbfile_String?FileExistsQ] := FirstCase[_Notebook]@ToHeldExpression@Import[nbfile, "String"]; nbget[nb_NotebookObject] := With[{file = ...


2

The output of the Mathematica code is ConditionalExpression[ 1/2 (2 a - a Sqrt[1 - a^2] + Sqrt[3 + 2 a - a^2] - a Sqrt[3 + 2 a - a^2] - 2 b + b Sqrt[1 - b^2] - Sqrt[3 + 2 b - b^2]+ b Sqrt[3 + 2 b - b^2] + 4 ArcSin[(1 - a)/2] *Routine clean-up*-ArcSin[a] - 4 ArcSin[(1 - b)/2] + ArcSin[b]), -1 < b < 1 && ( -1 < a < b || b ...


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


2

X = RandomReal[1, {194, 32}]; With[{arr = ArrayFlatten[{{0.0, -Total[Outer[Plus, X, -X, 1]^2, {3}]}}]}, k[sigma_] := Exp[arr/(2 sigma^2)]] AbsoluteTiming[Do[k[s], {s, 1, 1000}]] (* {0.257026, Null} *)


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


1

Create the matrix once, substitute sigma... (* fake some data *) X = N@RandomInteger[{1, 10}, {194, 32}]; array = ArrayFlatten[{{1, E^(1/(2 sigma^2) Map[Tr, -(Outer[Subtract, X, X, 1]^2), {2}])}}]; //Timing (* do new for sigma 1 to 20 *) sigs = Table[Replace[array, sigma -> N@sig, {5}], {sig, 1, 20}]; // Timing (* op method *) (* ...


1

Far from what the OP asked... Crude table based linear interpolation approach to learning more about the PDF: n = 25000; k = 24991; \[Mu] = 10^-4; \[Nu] = 10^-4; q = k/n; combn = Binomial[n, k]; pdf[s_?NumericQ] := combn NIntegrate[N[q^k (1 - q)^(n - k), 100] (E^(4 n q s) (1 - q)^(-1 + 4 n \[Mu]) q^(-1 + 4 n \[Nu]))/ NIntegrate[E^(4 n q s) (1 - ...



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