Coverage for gamdpy/misc/make_function.py: 40%
25 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
1import numpy as np
2import numba
3from numba import cuda
4import math
6def make_function_constant(value):
7 """ Return a function that returns a constant value """
8 value = np.float32(value)
10 def function(x):
11 return value
13 return function
16def make_function_ramp(value0, x0, value1, x1):
17 """ Return a function that ramps linearly between two values
19 Return a function that returns a constant value for x<x0,
20 linearly ramps from value0 to value1 for x0<=x<=x1,
21 and returns value1 for x>x1.
23 """
24 value0, x0, value1, x1 = np.float32(value0), np.float32(x0), np.float32(value1), np.float32(x1)
25 alpha = (value1 - value0) / (x1 - x0)
27 def function(x):
28 if x < x0:
29 return value0
30 if x < x1:
31 return value0 + (x - x0) * alpha
32 return value1
34 return function
37def make_function_sin(period, amplitude, offset):
38 """ Return a function that returns a sin function with given period, amplitude and offset """
40 from math import sin, pi
41 period, amplitude, offset = np.float32(period), np.float32(amplitude), np.float32(offset)
43 def function(x):
44 return offset + amplitude * sin(2 * pi * x / period)
46 return function