Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python CAMB python convergence power spectrum code

  1. Jun 19, 2016 #1
    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
  2. jcsd
  3. Jun 19, 2016 #2
    I'v been messing around with this and found something interesting:

    Code (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)

    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
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted