We can call C functions from Cython. In this example, we call the C `log()`

function. This function works on a single number only. Remember that the NumPy `log()`

function can also work with arrays. We will compute the so-called log returns of stock prices.

We start by writing some Cython code:

- First, import the C
`log()`

function from the`libc`

namespace. Then, apply this function to numbers in a`for`

loop. Finally, use the NumPy`diff()`

function to get the first-order difference between the log values in the second step:from libc.math cimport log import numpy as np def logrets(numbers): logs = [log(x) for x in numbers] return np.diff(logs)

Building has been covered in the previous recipes. We only need to change some values ...

Start Free Trial

No credit card required