human
I am working on the file "05_cython/05_cython.ipynb".
The first task is:
Pure C functions
With the cdef keywork we can realy define C function, as we shown below. In such functions all variable types should be defined and should have a return type, and can't be called directly in Python, i.e, only can be called by functions defined in the same module.
There is a midpoint between def and cdef which automatically creates a Python function with the same name, so the function can be called directly.
Can you write Python code for it?
 ... What is a the Python code you requested?
assistant
#Here is the Python code you requested.

%%cython -a -c=-fPIC -c=-fwrapv -c=-O3 -c=-fno-strict-aliasing
#!python
#cython: cdivision=True, boundscheck=False, nonecheck=False, wraparound=False, initializedcheck=False

import numpy as np
cimport numpy as cnp

ctypedef cnp.float64_t float64_t
from libc.math cimport sqrt

cdef float64_t dist(float64_t[::1] a, float64_t[::1] b):
    cdef:
        int i = 0
        int n = a.shape[0]
        float ret = 0
    for i in range(n):
        ret += (a[i]-b[i])**2
    return sqrt(ret)

def distance_matrix_cython6(float64_t[:,::1] A, float64_t[:,::1] B):
    cdef:
        int m = A.shape[0]
        int n = B.shape[0]
        int i,j
        float64_t[:,::1] D = np.empty((m,n))
    for i in range(m):
        for j in range(n):
            D[i,j] = dist(A[i,:], B[j,:])
    return D

%timeit -n 10 distance_matrix_cython6(A,B)