Python CAMB python convergence power spectrum code

Click For Summary
The discussion focuses on understanding the CAMB Python code, specifically the integration process over comoving distance without using scipy.integrate.quad. The key point is that the integration occurs through a dot product of two arrays in a for-loop, where one array represents Delta-Chi and the other serves as the integrand. The user shares a script that demonstrates the effectiveness of this method by comparing results from the dot product with those from scipy's quad function, showing good agreement. The method used in the script is a numerical integration technique that approximates the integral by summing the products of small intervals and function values. This approach is effective for numerical integration, particularly when dealing with discrete data points.
sunrah
Messages
191
Reaction score
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
 
Technology news on Phys.org
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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
4K
Replies
5
Views
47K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K