Community RSS Feed
https://community.wolfram.com
RSS Feed for Wolfram Community showing any discussions in tag Tuning and Debugging sorted by activeWhy is my implementation of a random network slower than the built-in?
https://community.wolfram.com/groups/-/m/t/1528378
Hello everybody,
I wanted for accademic purposes to implement the algorithm that generate a Random Graph of the Barabasi-Albert kind.
I know that there exsist already a built-in function that do exactly that, but for better understanding it I challenged myself to reproduce it.
I came up with this (relatively simple, but effective) code:
m0 = 2;(*How many edges to connect at each time step*)
n = 1000; \
(*How many nodes*)
g = CompleteGraph[m0, VertexLabels -> Automatic];
addBarabAlbert[g_Graph, m_Integer] :=
EdgeAdd[g,
Rule[m, #] & /@ RandomSample[VertexDegree@g -> VertexList@g, m0]]
Do[g = addBarabAlbert[g, i], {i, m0, n}]
Which I think it gives me a correct solution, in fact you can see from the code below that the degree of the vertexes follow a power-law of the same nature as the one generated with the built-in wolfram function.
The only problem is that my implementation is kind of slow (compared to the built-in) and I was wondering why.
Maybe knowing how I could set it up to make it faster will help me in the future with different problems.
Any ideas?
(In the attachment you find the brief code)Ektor Mariotti2018-10-22T23:32:50ZPerformance of recursion code with radial polynomials
https://community.wolfram.com/groups/-/m/t/1549861
Consider the following code:
Clear[r, re, p, pmax, delta, imagesize, delta]
ClearSystemCache[]
re[0, r_] := Sqrt[8/Pi]*((1 - r)/r)^(1/4)*1;
re[1, r_] := Sqrt[8/Pi]*((1 - r)/r)^(1/4)*-1*2*(1 - 2*r);
re[p_, r_] := re[p, r] = Sqrt[8/Pi]*((1 - r)/r)^(1/4)*(-1)^p*(re[1, r]*re[p - 1, r] - re[p - 2, r]);
imagesize = 32;
pmax = 10;
delta = 2/imagesize;
Table[r = Sqrt[x^2 + y^2]; re[pmax, r], {x, -1 + delta/2, 1 - delta/2, delta}, {y, 1 - delta/2, -1 + delta/2, -delta}];
this code is to calculate the distance r from each pixel to point(0,0), then evaluate the radial polynomial as below:
![enter image description here][1]
for accuracy, I will use the recursion version:
![enter image description here][2]
when the imagesize and pmax increase, the time will become unacceptable. So, I would ask if we can use compile of other methods to speed up,like: for imagesize is 256 and pmax is 120, the time will be about 10 seconds. In my code, I also use the memoization to store the value during the evaluation which I will use in the future.
[1]: http://community.wolfram.com//c/portal/getImageAttachment?filename=1.png&userId=1399878
[2]: http://community.wolfram.com//c/portal/getImageAttachment?filename=2.png&userId=1399878look xu2018-11-14T00:34:36Z