Home > Programming, Python > Use numpy.fromfunction to avoid for-loop

Use numpy.fromfunction to avoid for-loop

Using numpy.fromfunction to avoid for-loop over a matrix. Does it look like the ‘map’ function 🙂

An example of computing the quantized perturbed cat map:

from numpy import *
def quantum_cat_numeric(N,kappa):
    alpha = 2.0*pi/N
    mat_fn = lambda k,l: alpha*(k*k-k*l+l*l)
    phi = fromfunction(mat_fn,(N,N))+(kappa/alpha)*sin(alpha*arange(N))
    return (1.0/sqrt(N))*exp(1j*phi)

Another way to optimize the speed could be to use the Scipy.weave inline c-code, and code this loop using c/c++.

Advertisements
Categories: Programming, Python
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: