CAMB python convergence power spectrum code

  • Python
  • Thread starter sunrah
  • Start date
  • #1
198
22
I'm trying to understand this python CAMB code: http://camb.readthedocs.io/en/latest/CAMBdemo.html
Scroll down to In[29] and In[30] to see it.

It's an integration over chi (comoving distance), yet scipy.integrate.quad is not called. It seems that the fun stuff happens in the last for-loop in In[30], where they use numpy.dot to take dot product of two arrays. What's going on here? The first array is Delta-Chi (see In[29]) and the second is simply the integrand. So how is this integration? Thanks
 

Answers and Replies

  • #2
198
22
I'v been messing around with this and found something interesting:

Python:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad

xmax = 10
N = 1000
xx = np.linspace(0,xmax,N)

dx = (xx[2:] - xx[:-2])/2
xx = xx[1:-1]
Nx = len(xx)
yy1 = np.arange(0,Nx, dtype=np.float64)
yy2 = []

f = lambda z: z**3

for ix, x in enumerate(xx):
    foo = np.linspace(min(xx),x,N-2)**3
    yy1[ix] = np.dot(dx, foo)
    yy2.append(quad(f,0,x)[0])

plt.plot(xx,yy1,label='dot')
plt.plot(xx,yy2,label='quad')
If you run this script you'll find pretty good agreement between the integration via quad and integration using the dot product of dx and integrand. The value of N controls accuracy, with N>1000 not producing much noticeable difference. But why does this work? What is this method called thanks
 

Related Threads on CAMB python convergence power spectrum code

  • Last Post
Replies
11
Views
1K
Replies
4
Views
1K
Replies
6
Views
4K
Replies
1
Views
2K
Replies
6
Views
1K
Replies
2
Views
6K
Replies
18
Views
1K
Replies
13
Views
1K
Replies
14
Views
2K
Replies
2
Views
3K
Top