| 1 |
#cython: language_level=3 |
| 2 |
#cython: boundscheck=False |
| 3 |
#cython: wraparound=False |
| 4 |
#cython: cdivision=True |
| 5 |
|
| 6 |
""" |
| 7 |
cython_add.pyx |
| 8 |
|
| 9 |
cython implementation of the add function for piece-wise const and |
| 10 |
piece-wise linear functions |
| 11 |
|
| 12 |
Note: using cython memoryviews (e.g. double[:]) instead of ndarray objects |
| 13 |
improves the performance of spike_distance by a factor of 10! |
| 14 |
|
| 15 |
Copyright 2014, Mario Mulansky <[email protected]> |
| 16 |
|
| 17 |
Distributed under the BSD License |
| 18 |
|
| 19 |
""" |
| 20 |
|
| 21 |
""" |
| 22 |
To test whether things can be optimized: remove all yellow stuff |
| 23 |
in the html output:: |
| 24 |
|
| 25 |
cython -a cython_add.pyx |
| 26 |
|
| 27 |
which gives:: |
| 28 |
|
| 29 |
cython_add.html |
| 30 |
|
| 31 |
""" |
| 32 |
|
| 33 |
import numpy as np |
| 34 |
cimport numpy as np |
| 35 |
|
| 36 |
from libc.math cimport fabs |
| 37 |
|
| 38 |
#DTYPE = float |
| 39 |
#ctypedef np.float_t DTYPE_t |
| 40 |
|
| 41 |
############################################################ |
| 42 |
# add_piece_wise_const_cython |
| 43 |
############################################################ |
| 44 |
def add_piece_wise_const_cython(double[:] x1, double[:] y1, |
| 45 |
double[:] x2, double[:] y2): |
| 46 |
|
| 47 |
cdef int N1 = len(x1) |
| 48 |
cdef int N2 = len(x2) |
| 49 |
cdef double[:] x_new = np.empty(N1+N2) |
| 50 |
cdef double[:] y_new = np.empty(N1+N2-1) |
| 51 |
cdef int index1 = 0 |
| 52 |
cdef int index2 = 0 |
| 53 |
cdef int index = 0 |
| 54 |
cdef int i |
| 55 |
with nogil: # release the interpreter lock to allow multi-threading |
| 56 |
x_new[0] = x1[0] |
| 57 |
y_new[0] = y1[0] + y2[0] |
| 58 |
while (index1+1 < N1-1) and (index2+1 < N2-1): |
| 59 |
index += 1 |
| 60 |
# print(index1+1, x1[index1+1], y1[index1+1], x_new[index]) |
| 61 |
if x1[index1+1] < x2[index2+1]: |
| 62 |
index1 += 1 |
| 63 |
x_new[index] = x1[index1] |
| 64 |
elif x1[index1+1] > x2[index2+1]: |
| 65 |
index2 += 1 |
| 66 |
x_new[index] = x2[index2] |
| 67 |
else: # x1[index1+1] == x2[index2+1]: |
| 68 |
index1 += 1 |
| 69 |
index2 += 1 |
| 70 |
x_new[index] = x1[index1] |
| 71 |
y_new[index] = y1[index1] + y2[index2] |
| 72 |
# one array reached the end -> copy the contents of the other to the end |
| 73 |
if index1+1 < N1-1: |
| 74 |
x_new[index+1:index+1+N1-index1-1] = x1[index1+1:] |
| 75 |
for i in xrange(N1-index1-2): |
| 76 |
y_new[index+1+i] = y1[index1+1+i] + y2[N2-2] |
| 77 |
index += N1-index1-2 |
| 78 |
elif index2+1 < N2-1: |
| 79 |
x_new[index+1:index+1+N2-index2-1] = x2[index2+1:] |
| 80 |
for i in xrange(N2-index2-2): |
| 81 |
y_new[index+1+i] = y2[index2+1+i] + y1[N1-2] |
| 82 |
index += N2-index2-2 |
| 83 |
else: # both arrays reached the end simultaneously |
| 84 |
# only the last x-value missing |
| 85 |
x_new[index+1] = x1[N1-1] |
| 86 |
# end nogil |
| 87 |
# return np.asarray(x_new[:index+2]), np.asarray(y_new[:index+1]) |
| 88 |
return np.asarray(x_new[:index+2]), np.asarray(y_new[:index+1]) |
| 89 |
|
| 90 |
|
| 91 |
############################################################ |
| 92 |
# add_piece_wise_lin_cython |
| 93 |
############################################################ |
| 94 |
def add_piece_wise_lin_cython(double[:] x1, double[:] y11, double[:] y12, |
| 95 |
double[:] x2, double[:] y21, double[:] y22): |
| 96 |
cdef int N1 = len(x1) |
| 97 |
cdef int N2 = len(x2) |
| 98 |
cdef double[:] x_new = np.empty(N1+N2) |
| 99 |
cdef double[:] y1_new = np.empty(N1+N2-1) |
| 100 |
cdef double[:] y2_new = np.empty_like(y1_new) |
| 101 |
cdef int index1 = 0 # index for self |
| 102 |
cdef int index2 = 0 # index for f |
| 103 |
cdef int index = 0 # index for new |
| 104 |
cdef int i |
| 105 |
cdef double y |
| 106 |
with nogil: # release the interpreter lock to allow multi-threading |
| 107 |
x_new[0] = x1[0] |
| 108 |
y1_new[0] = y11[0] + y21[0] |
| 109 |
while (index1+1 < N1-1) and (index2+1 < N2-1): |
| 110 |
# print(index1+1, x1[index1+1], self.y[index1+1], x_new[index]) |
| 111 |
if x1[index1+1] < x2[index2+1]: |
| 112 |
# first compute the end value of the previous interval |
| 113 |
# linear interpolation of the interval |
| 114 |
y = y21[index2] + (y22[index2]-y21[index2]) * \ |
| 115 |
(x1[index1+1]-x2[index2]) / (x2[index2+1]-x2[index2]) |
| 116 |
y2_new[index] = y12[index1] + y |
| 117 |
index1 += 1 |
| 118 |
index += 1 |
| 119 |
x_new[index] = x1[index1] |
| 120 |
# and the starting value for the next interval |
| 121 |
y1_new[index] = y11[index1] + y |
| 122 |
elif x1[index1+1] > x2[index2+1]: |
| 123 |
# first compute the end value of the previous interval |
| 124 |
# linear interpolation of the interval |
| 125 |
y = y11[index1] + (y12[index1]-y11[index1]) * \ |
| 126 |
(x2[index2+1]-x1[index1]) / \ |
| 127 |
(x1[index1+1]-x1[index1]) |
| 128 |
y2_new[index] = y22[index2] + y |
| 129 |
index2 += 1 |
| 130 |
index += 1 |
| 131 |
x_new[index] = x2[index2] |
| 132 |
# and the starting value for the next interval |
| 133 |
y1_new[index] = y21[index2] + y |
| 134 |
else: # x1[index1+1] == x2[index2+1]: |
| 135 |
y2_new[index] = y12[index1] + y22[index2] |
| 136 |
index1 += 1 |
| 137 |
index2 += 1 |
| 138 |
index += 1 |
| 139 |
x_new[index] = x1[index1] |
| 140 |
y1_new[index] = y11[index1] + y21[index2] |
| 141 |
# one array reached the end -> copy the contents of the other to the end |
| 142 |
if index1+1 < N1-1: |
| 143 |
x_new[index+1:index+1+N1-index1-1] = x1[index1+1:] |
| 144 |
for i in xrange(N1-index1-2): |
| 145 |
# compute the linear interpolations value |
| 146 |
y = y21[index2] + (y22[index2]-y21[index2]) * \ |
| 147 |
(x1[index1+1+i]-x2[index2]) / (x2[index2+1]-x2[index2]) |
| 148 |
y1_new[index+1+i] = y11[index1+1+i] + y |
| 149 |
y2_new[index+i] = y12[index1+i] + y |
| 150 |
index += N1-index1-2 |
| 151 |
elif index2+1 < N2-1: |
| 152 |
x_new[index+1:index+1+N2-index2-1] = x2[index2+1:] |
| 153 |
# compute the linear interpolations values |
| 154 |
for i in xrange(N2-index2-2): |
| 155 |
y = y11[index1] + (y12[index1]-y11[index1]) * \ |
| 156 |
(x2[index2+1+i]-x1[index1]) / \ |
| 157 |
(x1[index1+1]-x1[index1]) |
| 158 |
y1_new[index+1+i] = y21[index2+1+i] + y |
| 159 |
y2_new[index+i] = y22[index2+i] + y |
| 160 |
index += N2-index2-2 |
| 161 |
else: # both arrays reached the end simultaneously |
| 162 |
# only the last x-value missing |
| 163 |
x_new[index+1] = x1[N1-1] |
| 164 |
# finally, the end value for the last interval |
| 165 |
y2_new[index] = y12[N1-2]+y22[N2-2] |
| 166 |
# only use the data that was actually filled |
| 167 |
# end nogil |
| 168 |
return (np.asarray(x_new[:index+2]), |
| 169 |
np.asarray(y1_new[:index+1]), |
| 170 |
np.asarray(y2_new[:index+1])) |
| 171 |
|
| 172 |
|
| 173 |
############################################################ |
| 174 |
# add_discrete_function_cython |
| 175 |
############################################################ |
| 176 |
def add_discrete_function_cython(double[:] x1, double[:] y1, double[:] mp1, |
| 177 |
double[:] x2, double[:] y2, double[:] mp2): |
| 178 |
|
| 179 |
cdef double[:] x_new = np.empty(len(x1) + len(x2)) |
| 180 |
cdef double[:] y_new = np.empty_like(x_new) |
| 181 |
cdef double[:] mp_new = np.empty_like(x_new) |
| 182 |
cdef int index1 = 0 |
| 183 |
cdef int index2 = 0 |
| 184 |
cdef int index = 0 |
| 185 |
cdef int N1 = len(y1)-1 |
| 186 |
cdef int N2 = len(y2)-1 |
| 187 |
x_new[0] = x1[0] |
| 188 |
while (index1+1 < N1) and (index2+1 < N2): |
| 189 |
if x1[index1+1] < x2[index2+1]: |
| 190 |
index1 += 1 |
| 191 |
index += 1 |
| 192 |
x_new[index] = x1[index1] |
| 193 |
y_new[index] = y1[index1] |
| 194 |
mp_new[index] = mp1[index1] |
| 195 |
elif x1[index1+1] > x2[index2+1]: |
| 196 |
index2 += 1 |
| 197 |
index += 1 |
| 198 |
x_new[index] = x2[index2] |
| 199 |
y_new[index] = y2[index2] |
| 200 |
mp_new[index] = mp2[index2] |
| 201 |
else: # x1[index1+1] == x2[index2+1] |
| 202 |
index1 += 1 |
| 203 |
index2 += 1 |
| 204 |
index += 1 |
| 205 |
x_new[index] = x1[index1] |
| 206 |
y_new[index] = y1[index1] + y2[index2] |
| 207 |
mp_new[index] = mp1[index1] + mp2[index2] |
| 208 |
# one array reached the end -> copy the contents of the other to the end |
| 209 |
if index1+1 < N1: |
| 210 |
x_new[index+1:index+1+N1-index1] = x1[index1+1:] |
| 211 |
y_new[index+1:index+1+N1-index1] = y1[index1+1:] |
| 212 |
mp_new[index+1:index+1+N1-index1] = mp1[index1+1:] |
| 213 |
index += N1-index1 |
| 214 |
elif index2+1 < N2: |
| 215 |
x_new[index+1:index+1+N2-index2] = x2[index2+1:] |
| 216 |
y_new[index+1:index+1+N2-index2] = y2[index2+1:] |
| 217 |
mp_new[index+1:index+1+N2-index2] = mp2[index2+1:] |
| 218 |
index += N2-index2 |
| 219 |
else: # both arrays reached the end simultaneously |
| 220 |
x_new[index+1] = x1[index1+1] |
| 221 |
y_new[index+1] = y1[index1+1] + y2[index2+1] |
| 222 |
mp_new[index+1] = mp1[index1+1] + mp2[index2+1] |
| 223 |
index += 1 |
| 224 |
|
| 225 |
y_new[0] = y_new[1] |
| 226 |
mp_new[0] = mp_new[1] |
| 227 |
|
| 228 |
# the last value is again the end of the interval |
| 229 |
# only use the data that was actually filled |
| 230 |
return (np.asarray(x_new[:index+1]), |
| 231 |
np.asarray(y_new[:index+1]), |
| 232 |
np.asarray(mp_new[:index+1])) |