summaryrefslogtreecommitdiff
path: root/pyspike/function.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyspike/function.py')
-rw-r--r--pyspike/function.py127
1 files changed, 20 insertions, 107 deletions
diff --git a/pyspike/function.py b/pyspike/function.py
index 26ca4b2..5444c36 100644
--- a/pyspike/function.py
+++ b/pyspike/function.py
@@ -82,46 +82,15 @@ class PieceWiseConstFunc:
"""
assert self.x[0] == f.x[0], "The functions have different intervals"
assert self.x[-1] == f.x[-1], "The functions have different intervals"
- x_new = np.empty(len(self.x) + len(f.x))
- y_new = np.empty(len(x_new)-1)
- x_new[0] = self.x[0]
- y_new[0] = self.y[0] + f.y[0]
- index1 = 0
- index2 = 0
- index = 0
- while (index1+1 < len(self.y)) and (index2+1 < len(f.y)):
- index += 1
- # print(index1+1, self.x[index1+1], self.y[index1+1], x_new[index])
- if self.x[index1+1] < f.x[index2+1]:
- index1 += 1
- x_new[index] = self.x[index1]
- elif self.x[index1+1] > f.x[index2+1]:
- index2 += 1
- x_new[index] = f.x[index2]
- else: # self.x[index1+1] == f.x[index2+1]:
- index1 += 1
- index2 += 1
- x_new[index] = self.x[index1]
- y_new[index] = self.y[index1] + f.y[index2]
- # one array reached the end -> copy the contents of the other to the end
- if index1+1 < len(self.y):
- x_new[index+1:index+1+len(self.x)-index1-1] = self.x[index1+1:]
- y_new[index+1:index+1+len(self.y)-index1-1] = self.y[index1+1:] + \
- f.y[-1]
- index += len(self.x)-index1-2
- elif index2+1 < len(f.y):
- x_new[index+1:index+1+len(f.x)-index2-1] = f.x[index2+1:]
- y_new[index+1:index+1+len(f.y)-index2-1] = f.y[index2+1:] + \
- self.y[-1]
- index += len(f.x)-index2-2
- else: # both arrays reached the end simultaneously
- # only the last x-value missing
- x_new[index+1] = self.x[-1]
- # the last value is again the end of the interval
- # x_new[index+1] = self.x[-1]
- # only use the data that was actually filled
- self.x = x_new[:index+2]
- self.y = y_new[:index+1]
+
+ # python implementation
+ # from python_backend import add_piece_wise_const_python
+ # self.x, self.y = add_piece_wise_const_python(self.x, self.y, f.x, f.y)
+
+ # cython version
+ from cython_add import add_piece_wise_const_cython
+ self.x, self.y = add_piece_wise_const_cython(self.x, self.y, f.x, f.y)
+
def mul_scalar(self, fac):
""" Multiplies the function with a scalar value
@@ -204,73 +173,17 @@ class PieceWiseLinFunc:
"""
assert self.x[0] == f.x[0], "The functions have different intervals"
assert self.x[-1] == f.x[-1], "The functions have different intervals"
- x_new = np.empty(len(self.x) + len(f.x))
- y1_new = np.empty(len(x_new)-1)
- y2_new = np.empty_like(y1_new)
- x_new[0] = self.x[0]
- y1_new[0] = self.y1[0] + f.y1[0]
- index1 = 0 # index for self
- index2 = 0 # index for f
- index = 0 # index for new
- while (index1+1 < len(self.y1)) and (index2+1 < len(f.y1)):
- # print(index1+1, self.x[index1+1], self.y[index1+1], x_new[index])
- if self.x[index1+1] < f.x[index2+1]:
- # first compute the end value of the previous interval
- # linear interpolation of the interval
- y = f.y1[index2] + (f.y2[index2]-f.y1[index2]) * \
- (self.x[index1+1]-f.x[index2]) / (f.x[index2+1]-f.x[index2])
- y2_new[index] = self.y2[index1] + y
- index1 += 1
- index += 1
- x_new[index] = self.x[index1]
- # and the starting value for the next interval
- y1_new[index] = self.y1[index1] + y
- elif self.x[index1+1] > f.x[index2+1]:
- # first compute the end value of the previous interval
- # linear interpolation of the interval
- y = self.y1[index1] + (self.y2[index1]-self.y1[index1]) * \
- (f.x[index2+1]-self.x[index1]) / \
- (self.x[index1+1]-self.x[index1])
- y2_new[index] = f.y2[index2] + y
- index2 += 1
- index += 1
- x_new[index] = f.x[index2]
- # and the starting value for the next interval
- y1_new[index] = f.y1[index2] + y
- else: # self.x[index1+1] == f.x[index2+1]:
- y2_new[index] = self.y2[index1] + f.y2[index2]
- index1 += 1
- index2 += 1
- index += 1
- x_new[index] = self.x[index1]
- y1_new[index] = self.y1[index1] + f.y1[index2]
- # one array reached the end -> copy the contents of the other to the end
- if index1+1 < len(self.y1):
- # compute the linear interpolations values
- y = f.y1[index2] + (f.y2[index2]-f.y1[index2]) * \
- (self.x[index1+1:-1]-f.x[index2]) / (f.x[index2+1]-f.x[index2])
- x_new[index+1:index+1+len(self.x)-index1-1] = self.x[index1+1:]
- y1_new[index+1:index+1+len(self.y1)-index1-1] = self.y1[index1+1:]+y
- y2_new[index:index+len(self.y2)-index1-1] = self.y2[index1:-1] + y
- index += len(self.x)-index1-2
- elif index2+1 < len(f.y1):
- # compute the linear interpolations values
- y = self.y1[index1] + (self.y2[index1]-self.y1[index1]) * \
- (f.x[index2+1:-1]-self.x[index1]) / \
- (self.x[index1+1]-self.x[index1])
- x_new[index+1:index+1+len(f.x)-index2-1] = f.x[index2+1:]
- y1_new[index+1:index+1+len(f.y1)-index2-1] = f.y1[index2+1:] + y
- y2_new[index:index+len(f.y2)-index2-1] = f.y2[index2:-1] + y
- index += len(f.x)-index2-2
- else: # both arrays reached the end simultaneously
- # only the last x-value missing
- x_new[index+1] = self.x[-1]
- # finally, the end value for the last interval
- y2_new[index] = self.y2[-1]+f.y2[-1]
- # only use the data that was actually filled
- self.x = x_new[:index+2]
- self.y1 = y1_new[:index+1]
- self.y2 = y2_new[:index+1]
+
+ # python implementation
+ # from python_backend import add_piece_wise_lin_python
+ # self.x, self.y1, self.y2 = add_piece_wise_lin_python(
+ # self.x, self.y1, self.y2, f.x, f.y1, f.y2)
+
+ # cython version
+ from cython_add import add_piece_wise_lin_cython
+ self.x, self.y1, self.y2 = add_piece_wise_lin_cython(
+ self.x, self.y1, self.y2, f.x, f.y1, f.y2)
+
def mul_scalar(self, fac):
""" Multiplies the function with a scalar value