Coverage for gamdpy/misc/make_function.py: 56%

25 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1import numpy as np 

2import numba 

3from numba import cuda 

4import math 

5 

6def make_function_constant(value): 

7 """ Return a function that returns a constant value """ 

8 value = np.float32(value) 

9 

10 def function(x): 

11 return value 

12 

13 return function 

14 

15 

16def make_function_ramp(value0, x0, value1, x1): 

17 """ Return a function that ramps linearly between two values 

18 

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. 

22 

23 """ 

24 value0, x0, value1, x1 = np.float32(value0), np.float32(x0), np.float32(value1), np.float32(x1) 

25 alpha = (value1 - value0) / (x1 - x0) 

26 

27 def function(x): 

28 if x < x0: 

29 return value0 

30 if x < x1: 

31 return value0 + (x - x0) * alpha 

32 return value1 

33 

34 return function 

35 

36 

37def make_function_sin(period, amplitude, offset): 

38 """ Return a function that returns a sin function with given period, amplitude and offset """ 

39 

40 from math import sin, pi 

41 period, amplitude, offset = np.float32(period), np.float32(amplitude), np.float32(offset) 

42 

43 def function(x): 

44 return offset + amplitude * sin(2 * pi * x / period) 

45 

46 return function 

47 

48