0

I have math_helper.pyx that is written in cython with nogil. I have another cython script named feature.pyx that uses m_mean function, however, its output (z_mean) is always zero. I don't know where the problem is.

this is math_helper.pyx

cimport cython
from libc.math cimport fabs, pow, log, sqrt
from cython cimport view
#from libcpp.algorithm cimport sort


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.cdivision(True)
cdef double m_sum(double [:] a) nogil:
    cdef:
        int i
        double total = 0.0
    for i in range(a.shape[0]):
        total += a[i]
    return total

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.cdivision(True)
cdef inline double m_mean(double [:] a) nogil:
    cdef:
        int i
        double n = a.shape[0]
    return m_sum(a) / n

this is part of feature.pyx that calls m_mean function

cimport math_helper

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
@cython.initializedcheck(False)
cdef inline void compute_features_from_neighbors(
    double[:] center_point,
    double[::1, :] neighbor_points,
    double[::1, :] neighbor_points2d,
    double radius,
    float [:] out,
    int [:] feature_map_list,
) nogil:
    cdef:
        double z_mean

    z_mean = math_helper.m_mean(neighbor_points[2, :])

The problem is somewhere in m_sum function in math_helper.pyx because total is zero even after loop!

  • It isn't just that you're assigning to `z_mean` instead of `x_mean`? – DavidW Dec 02 '22 at 08:32
  • Hi @DavidW, sorry it was a wording typo when I wrote my question. I corrected the question now. BTW, it does not resolve the issue. tnx – Emad Ghalenoei Dec 02 '22 at 20:03
  • I think it's going to be hard to tell without a real [mre]. The code looks like it should work. But you've turned off boundschecking, so my best guess is that `neighbor_points[2, :]` is reading outside valid memory. – DavidW Dec 03 '22 at 10:09

0 Answers0