Commit fcf8ce97 authored by Dr.李's avatar Dr.李

added abs sum function

parent 813e4f76
...@@ -10,6 +10,7 @@ from numpy import zeros ...@@ -10,6 +10,7 @@ from numpy import zeros
from numpy import asarray from numpy import asarray
cimport cython cimport cython
from libc.math cimport sqrt from libc.math cimport sqrt
from libc.math cimport fabs
from libc.stdlib cimport calloc from libc.stdlib cimport calloc
from libc.stdlib cimport free from libc.stdlib cimport free
...@@ -52,6 +53,27 @@ cdef double* agg_sum(long* groups, double* x, size_t length, size_t width) nogil ...@@ -52,6 +53,27 @@ cdef double* agg_sum(long* groups, double* x, size_t length, size_t width) nogil
return res_ptr return res_ptr
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
@cython.initializedcheck(False)
cdef double* agg_abssum(long* groups, double* x, size_t length, size_t width) nogil:
cdef long max_g = max_groups(groups, length)
cdef double* res_ptr = <double*>calloc((max_g+1)*width, sizeof(double))
cdef size_t i
cdef size_t j
cdef size_t loop_idx1
cdef size_t loop_idx2
cdef long curr
for i in range(length):
loop_idx1 = i*width
loop_idx2 = groups[i]*width
for j in range(width):
res_ptr[loop_idx2 + j] += fabs(x[loop_idx1 + j])
return res_ptr
@cython.boundscheck(False) @cython.boundscheck(False)
@cython.wraparound(False) @cython.wraparound(False)
@cython.cdivision(True) @cython.cdivision(True)
...@@ -144,6 +166,8 @@ cpdef np.ndarray[double, ndim=2] transform(long[:] groups, double[:, :] x, str f ...@@ -144,6 +166,8 @@ cpdef np.ndarray[double, ndim=2] transform(long[:] groups, double[:, :] x, str f
value_data_ptr = agg_std(&groups[0], &x[0, 0], length, width, ddof=1) value_data_ptr = agg_std(&groups[0], &x[0, 0], length, width, ddof=1)
elif func == 'sum': elif func == 'sum':
value_data_ptr = agg_sum(&groups[0], &x[0, 0], length, width) value_data_ptr = agg_sum(&groups[0], &x[0, 0], length, width)
elif func =='abssum':
value_data_ptr = agg_abssum(&groups[0], &x[0, 0], length, width)
with nogil: with nogil:
for i in range(length): for i in range(length):
...@@ -171,6 +195,8 @@ cpdef np.ndarray[double, ndim=2] aggregate(long[:] groups, double[:, :] x, str f ...@@ -171,6 +195,8 @@ cpdef np.ndarray[double, ndim=2] aggregate(long[:] groups, double[:, :] x, str f
value_data_ptr = agg_std(&groups[0], &x[0, 0], length, width, ddof=1) value_data_ptr = agg_std(&groups[0], &x[0, 0], length, width, ddof=1)
elif func == 'sum': elif func == 'sum':
value_data_ptr = agg_sum(&groups[0], &x[0, 0], length, width) value_data_ptr = agg_sum(&groups[0], &x[0, 0], length, width)
elif func =='abssum':
value_data_ptr = agg_abssum(&groups[0], &x[0, 0], length, width)
res = np.PyArray_SimpleNewFromData(2, [max_g+1, width], np.NPY_FLOAT64, value_data_ptr) res = np.PyArray_SimpleNewFromData(2, [max_g+1, width], np.NPY_FLOAT64, value_data_ptr)
return res return res
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment