First of all, the post is about non-linear phenomena only. Perturbation theory is the tool of choice if you are interested for an approximation in a small interval compared to the overall time dependent development of the system. HAM doesn’t have this limit. Moreover, HAM gives you a great freedom in choosing the base functions to construct your ‘solution’.

]]>a perturbation approach. ]]>

Thanks a lot for your answer and suggestions. Yes, you are absolutely right. Your suggestions make things much better. After incorporating your comments, I end up with the following code:

Timing[Module[{}, m = Transpose[m]; dims = Dimensions[m]; dinv = SparseArray[ Band[{1, 1}] -> 1/Total[Sign[m]], {dims[[2]], dims[[2]]}]; mnorm = m/5; w = Total[mnorm]/Total[Sign[m]]; W = SparseArray[Band[{1, 1}] -> w, {dims[[2]], dims[[2]]}]; W = N[W]; B = Dot[Sign[m], Dot[W, Dot[dinv, Transpose[Sign[m]]]]]; B = B*SparseArray[{i_, i_} -> 0, Dimensions[B], 1]; kinv = SparseArray[ Band[{1, 1}] -> 1/Total[B], {dims[[1]], dims[[1]]}]; P = Dot[kinv, B];]]

and

Timing[Module[{}, m = Transpose[m]; in = Position[Normal[m], _?(# >= 3 &)]; H = SparseArray[in -> Table[1, {Length[in]}]]; P = SparseArray[P]; fp = Dot[Transpose[P], Transpose[H]]; bp = Dot[P, Transpose[H]]; rk = fp^(alpha) * bp ^(1 - alpha);]]

This makes things indeed faster.

Mathematica: **4.7 s **

Python : **1.2 s **

Now, I am happy!

]]>B = ReplacePart[Normal[B], {i_, i_} :> 0];

Here Normal[B] converts B from a sparse array into a full dense array, which is slow (and presumably not is done in the Python code). Instead you could compute this in a sparse way:

B *= SparseArray[{i_, i_} -> 0, Dimensions[B], 1]

I find this is about 30 to 40 times faster.

]]>However your timing comparison between Python and Mathematica isn’t at all fair. This is because your Mathematica code is using exact numbers (integers and fractions) at a couple of places where Python would not.

In particular after your line 9 defining W, here is one of the elements of W:

In[174]:= Max[W]

Out[174]= 112/115

Actually all of the elements of W are infinite precision integers or fractions and Mathematica will work with them in this form as long as possible, whereas in Python you would be using fast inexact (machine precision) numbers at this stage.

If you insert this line after line 9:

W = N[W];

then W becomes a sparse array of fast inexact numbers and you get a much fairer comparison against Python.

]]>I used twitter’s API library. Now there are wrappers for several languages (python, R, java, and others). For that post I used R’s twitteR and python’s tweepy. With these libraries it is quite straight forward to access tweets from twitter. Depending on what you do you have to use an authentication mechanism to fetch data. In python this looks like:

`# -*- coding: utf-8 -*-`

from pylab import *

from networkx import *

import tweepy
consumer_key = 'your_consumer_key'

consumer_secret = 'your_consumer_secret'

access_key = 'your_access_key'

access_secret = 'your_secret_key'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_key, access_secret)

api = tweepy.API(auth_handler=auth)

`friends = api.friends()`

....do whatever you want to do...

Putting that side, I also love the Twitter info and makes me ask what that graph looked it a week before the study tour began.

]]>