computils 0.0.1__tar.gz → 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {computils-0.0.1 → computils-0.0.2}/PKG-INFO +2 -2
  2. {computils-0.0.1 → computils-0.0.2}/pyproject.toml +4 -2
  3. {computils-0.0.1 → computils-0.0.2}/src/computils/__init__.py +3 -3
  4. {computils-0.0.1 → computils-0.0.2}/src/computils/factory.py +8 -0
  5. {computils-0.0.1 → computils-0.0.2}/src/computils/fast_eval.py +17 -10
  6. computils-0.0.2/src/computils/globals.py +71 -0
  7. {computils-0.0.1 → computils-0.0.2}/src/computils/matrix_computations.py +6 -0
  8. computils-0.0.2/src/computils/parallelization.py +75 -0
  9. {computils-0.0.1 → computils-0.0.2}/src/computils/parameter_transformations.py +44 -0
  10. {computils-0.0.1 → computils-0.0.2}/src/computils/sorting_algorithms.py +5 -5
  11. computils-0.0.2/src/computils/type_utils.py +106 -0
  12. {computils-0.0.1 → computils-0.0.2}/src/computils.egg-info/PKG-INFO +2 -2
  13. {computils-0.0.1 → computils-0.0.2}/src/computils.egg-info/SOURCES.txt +1 -0
  14. computils-0.0.1/src/computils/globals.py +0 -64
  15. computils-0.0.1/src/computils/type_utils.py +0 -11
  16. {computils-0.0.1 → computils-0.0.2}/LICENSE +0 -0
  17. {computils-0.0.1 → computils-0.0.2}/README.md +0 -0
  18. {computils-0.0.1 → computils-0.0.2}/setup.cfg +0 -0
  19. {computils-0.0.1 → computils-0.0.2}/src/computils/finite_difference.py +0 -0
  20. {computils-0.0.1 → computils-0.0.2}/src/computils/gaussian_elim_spp.py +0 -0
  21. {computils-0.0.1 → computils-0.0.2}/src/computils/interpolation.py +0 -0
  22. {computils-0.0.1 → computils-0.0.2}/src/computils/performance_checking.py +0 -0
  23. {computils-0.0.1 → computils-0.0.2}/src/computils.egg-info/dependency_links.txt +0 -0
  24. {computils-0.0.1 → computils-0.0.2}/src/computils.egg-info/top_level.txt +0 -0
  25. {computils-0.0.1 → computils-0.0.2}/src/tests/matrix_computations_tests.py +0 -0
  26. {computils-0.0.1 → computils-0.0.2}/src/tests/numerical_derivatives_tests.py +0 -0
  27. {computils-0.0.1 → computils-0.0.2}/src/tests/parameter_transformation_tests.py +0 -0
  28. {computils-0.0.1 → computils-0.0.2}/src/tests/test_utils.py +0 -0
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: computils
3
- Version: 0.0.1
4
- Summary: A package collecting functionality for various numerical computations (numerical differentiation, interpolation, optimization, sorting, ).
3
+ Version: 0.0.2
4
+ Summary: A package collecting functionality for various numerical computations (numerical differentiation, interpolation, optimization, sorting, parallelization).
5
5
  Author-email: Karim Moussa <research@k-moussa.com>
6
6
  Project-URL: Homepage, https://github.com/k-moussa/computils
7
7
  Classifier: Programming Language :: Python :: 3
@@ -5,18 +5,20 @@ requires = [
5
5
  "scipy >= 1.13.0",
6
6
  "numba >= 0.59.1",
7
7
  "splines >= 0.3.1",
8
+ "psutil>=5.9.0",
8
9
  ]
9
10
  build-backend = "setuptools.build_meta"
10
11
 
11
12
  [project]
12
13
  name = "computils"
13
- version = "0.0.1"
14
+ version = "0.0.2"
14
15
  authors = [
15
16
  { name="Karim Moussa", email="research@k-moussa.com" },
16
17
  ]
17
- description = "A package collecting functionality for various numerical computations (numerical differentiation, interpolation, optimization, sorting, )."
18
+ description = "A package collecting functionality for various numerical computations (numerical differentiation, interpolation, optimization, sorting, parallelization)."
18
19
  readme = "README.md"
19
20
  requires-python = ">=3.7"
21
+
20
22
  classifiers = [
21
23
  "Programming Language :: Python :: 3",
22
24
  "License :: OSI Approved :: MIT License",
@@ -11,12 +11,12 @@ from .globals import *
11
11
  from .factory import create_interpolator
12
12
  from .sorting_algorithms import index_eq, find_le, find_ge, find_gt, find_lt
13
13
  from .finite_difference import compute_derivative, compute_gradient, compute_jacobian, compute_hessian
14
- from .type_utils import size
15
- from .fast_eval import fast_matrix_inversion, fast_determinant, fast_quadratic_form, fast_diag_mult_AD, \
16
- fast_diag_mult_DA, fast_columnwise_bilinear_form
14
+ from .type_utils import size, to_1d_array, to_float_if_singleton, has_nan, compute_hash, dicts_to_array, dict_to_array
15
+ from .fast_eval import fast_matrix_inversion, fast_determinant, fast_quadratic_form, fast_columnwise_bilinear_form
17
16
  from .gaussian_elim_spp import compute_inverse_using_gauss_elim_spp
18
17
  from .matrix_computations import robust_inverse, is_diagonal
19
18
  from .parameter_transformations import impose_lower_bound, inverse_impose_lower_bound, impose_upper_bound, \
20
19
  inverse_impose_upper_bound, impose_bounds, inverse_impose_bounds, impose_upper_bound_sum, \
21
20
  inverse_impose_upper_bound_sum
22
21
  from .performance_checking import compute_average_running_time
22
+ from .parallelization import BatchAllocation, get_batch_allocations, get_n_physical_cores
@@ -8,5 +8,13 @@ def create_interpolator(x: np.ndarray,
8
8
  y: np.ndarray,
9
9
  inter_type: InterpolationType,
10
10
  extra_type: ExtrapolationType = ExtrapolationType.nan) -> Interpolator:
11
+ """ Creates an instance of the Interpolator class with the given inter- and extrapolation types.
12
+
13
+ :param x:
14
+ :param y:
15
+ :param inter_type:
16
+ :param extra_type:
17
+ :return: an instance of the Interpolator class with the given inter- and extrapolation types.
18
+ """
11
19
 
12
20
  return InternalInterpolator(x=x, y=y, inter_type=inter_type, extra_type=extra_type)
@@ -7,6 +7,12 @@ from scipy.linalg import inv, det
7
7
 
8
8
 
9
9
  def fast_matrix_inversion(M: np.ndarray) -> np.ndarray:
10
+ """ Performs fast matrix inversion.
11
+
12
+ :param M:
13
+ :return: The inverse of M.
14
+ """
15
+
10
16
  if M.size == 1:
11
17
  return 1.0 / M
12
18
  elif issparse(M):
@@ -16,6 +22,11 @@ def fast_matrix_inversion(M: np.ndarray) -> np.ndarray:
16
22
 
17
23
 
18
24
  def fast_determinant(M: np.ndarray) -> float:
25
+ """ Computes the determinant of M.
26
+
27
+ :param M:
28
+ :return: det(M).
29
+ """
19
30
  if M.size == 1:
20
31
  return np.abs(M[0, 0])
21
32
  else:
@@ -24,17 +35,13 @@ def fast_determinant(M: np.ndarray) -> float:
24
35
 
25
36
  def fast_quadratic_form(A: np.ndarray,
26
37
  x: np.ndarray) -> float:
27
- return (np.dot(x.T, np.dot(A, x)))[0, 0]
38
+ """ Computes the quadratic form x'Ax.
28
39
 
29
-
30
- def fast_diag_mult_AD(A: np.ndarray,
31
- D: np.ndarray) -> np.ndarray:
32
- return np.multiply(A, np.diag(D))
33
-
34
-
35
- def fast_diag_mult_DA(A: np.ndarray,
36
- D: np.ndarray) -> np.ndarray:
37
- return np.multiply(np.diag(D)[:, None], A)
40
+ :param A: An (n,n) array.
41
+ :param x: An (n,1) array.
42
+ :return: the quadratic form x'Ax.
43
+ """
44
+ return (np.dot(x.T, np.dot(A, x)))[0, 0]
38
45
 
39
46
 
40
47
  def fast_columnwise_bilinear_form(A: np.ndarray,
@@ -0,0 +1,71 @@
1
+ """ This module collects all exposed types."""
2
+
3
+ import numpy as np
4
+ from enum import Enum
5
+ from abc import ABC, abstractmethod
6
+ from typing import final, Union
7
+
8
+ MACHINE_EPS: final = float(2 ** (-53)) #: max relative error corresponding to 1/2 ULP
9
+ SQUARE_ROOT_MACHINE_EPS: final = np.sqrt(MACHINE_EPS) #:
10
+ CUBE_ROOT_MACHINE_EPS: final = np.cbrt(MACHINE_EPS) #:
11
+ FOURTH_ROOT_MACHINE_EPS: final = np.power(MACHINE_EPS, 1/4) #:
12
+
13
+ Integer: final = Union[int, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64] #:
14
+ Float: final = Union[float, np.float16, np.float32, np.float64, np.float128] #:
15
+ Complex: final = Union[complex, np.complex64, np.complex128, np.complex256] #:
16
+ Scalar: final = Union[Integer, Float, Complex] #:
17
+ Bool: final = Union[bool, np.bool_]
18
+
19
+ IntOrArray: final = Union[Integer, np.ndarray] #:
20
+ FloatOrArray: final = Union[Float, np.ndarray] #:
21
+ ComplexOrArray: final = Union[Complex, np.ndarray] #:
22
+ ScalarOrArray: final = Union[Scalar, np.ndarray] #:
23
+ BoolOrArray = Union[Bool, np.ndarray] #:
24
+
25
+
26
+ # 128-bit integers generated using entropy gathered from the OS, for fixing the seed of NumPy generators.
27
+ SEED128_1: final = 137088887599416403785824428186011523708
28
+ SEED128_2: final = 230453062773196406322953725600125856954
29
+ SEED128_3: final = 334883455271001690768699206953238296696
30
+ SEED128_4: final = 69805789651723487751993596682833892465
31
+ SEED128_5: final = 107997836754270478432192384195731366045
32
+ SEED128_6: final = 61384398454038591198002507585631369346
33
+ SEED128_7: final = 165303562401480433055547884045914282112
34
+ SEED128_8: final = 215418768511176275020128984871223831370
35
+ SEED128_9: final = 247520618467519227348322182729719821192
36
+ SEED128_10: final = 260473913589332506348144405693894964574
37
+
38
+
39
+ class InterpolationType(Enum):
40
+ linear = 0 #: linear interpolation
41
+ ncs = 1 #: natural cubic spline
42
+ ccs = 2 #: clamped cubic spline
43
+ pmc = 3 #: piecewise monotone cubic [FC80]
44
+ pchip = 4 #: piecewise cubic Hermite interpolation polynomial [FB84]
45
+
46
+
47
+ class ExtrapolationType(Enum):
48
+ nan = 0 #: set extrapolated values to nan
49
+ flat = 1 #: set all extrapolated values equal to the nearest interpolated value (constant or flat extrapolation).
50
+
51
+
52
+ class Interpolator(ABC):
53
+ def __init__(self,
54
+ inter_type: InterpolationType,
55
+ extra_type: ExtrapolationType):
56
+ """ Class to perform inter- and extrapolation.
57
+
58
+ :param inter_type: interpolation type.
59
+ :param extra_type: extrapolation type.
60
+ """
61
+
62
+ self.inter_type: InterpolationType = inter_type #: the interpolation type
63
+ self.extra_type: ExtrapolationType = extra_type #: the extrapolation type
64
+
65
+ @abstractmethod
66
+ def __call__(self, x: ScalarOrArray) -> ScalarOrArray:
67
+ """ Returns the inter- or extrapolated value(s) at the given domain value(s) 'x'.
68
+
69
+ :param x: domain value(s) at which to compute the inter- or extrapolated value.
70
+ :return: the inter- or extrapolated value(s).
71
+ """
@@ -20,4 +20,10 @@ def robust_inverse(square_matrix: np.ndarray) -> np.ndarray:
20
20
 
21
21
 
22
22
  def is_diagonal(matrix: np.ndarray) -> bool:
23
+ """ Determines if the given matrix is diagonal.
24
+
25
+ :param matrix:
26
+ :return: True if matrix is diagonal, False otherwise.
27
+ """
28
+
23
29
  return np.count_nonzero(matrix - np.diag(np.diagonal(matrix))) == 0
@@ -0,0 +1,75 @@
1
+ """ This module contains functionality for parallelization. """
2
+
3
+ from math import floor
4
+ from psutil import cpu_count
5
+ from typing import List
6
+
7
+
8
+ class BatchAllocation:
9
+ def __init__(self,
10
+ batch_id: int,
11
+ n_cores: int,
12
+ indices: List[int]):
13
+ """ Initializes a BatchAllocation object.
14
+
15
+ :param batch_id: unique number to identify batch.
16
+ :param n_cores: >= 1, number of physical cores allocated to this batch.
17
+ :param indices: contains the indices of the elements allocated to this batch.
18
+ """
19
+
20
+ self.batch_id: int = batch_id #: id number to identify a batch
21
+ self.n_cores: int = n_cores #: the number of cores allocated to the batch
22
+ self.indices: List[int] = indices #: the indices allocated to the batch.
23
+
24
+ def batch_size(self) -> int:
25
+ """ Returns the number of indices allocated to the batch.
26
+
27
+ :return: the number of indices allocated to the batch.
28
+ """
29
+ return len(self.indices)
30
+
31
+
32
+ def get_batch_allocations(n_elements: int,
33
+ n_cores: int = None) -> List[BatchAllocation]:
34
+ """ Returns an allocation of elements over batches such that the maximum number of elements per core is minimized.
35
+
36
+ :param n_elements: the number of elements to be allocated over the cores.
37
+ :param n_cores: optional, number of physical cores to be used for the batch allocations. If None, all physical
38
+ cores are used.
39
+ :return: a list with batch allocations.
40
+ """
41
+
42
+ if n_cores is None:
43
+ n_cores = get_n_physical_cores()
44
+
45
+ n_batches = min(n_elements, n_cores)
46
+ indices = list(range(n_elements))
47
+
48
+ lower_bound_cores_per_batch = floor(n_cores / n_batches)
49
+ lower_bound_elements_per_batch = floor(n_elements / n_batches)
50
+ batch_allocations = []
51
+ for i in range(n_batches):
52
+ indices_for_batch = [indices.pop() for j in range(lower_bound_elements_per_batch)]
53
+ allocation = BatchAllocation(batch_id=i, n_cores=lower_bound_cores_per_batch,
54
+ indices=indices_for_batch)
55
+ batch_allocations.append(allocation)
56
+
57
+ n_remaining_cores = n_cores - n_batches * lower_bound_cores_per_batch
58
+ while n_remaining_cores > 0:
59
+ batch_index = n_batches - n_remaining_cores
60
+ batch_allocations[batch_index].n_cores += 1
61
+ n_remaining_cores -= 1
62
+
63
+ while len(indices) > 0:
64
+ batch_index = n_batches - len(indices)
65
+ batch_allocations[batch_index].indices.append(indices.pop())
66
+
67
+ return batch_allocations
68
+
69
+
70
+ def get_n_physical_cores() -> int:
71
+ """ Returns the number of physical cores on your machine.
72
+
73
+ :return: the number of physical cores on your machine.
74
+ """
75
+ return cpu_count(logical=False)
@@ -4,26 +4,70 @@ import numpy as np
4
4
 
5
5
 
6
6
  def impose_lower_bound(transformed_parameter: float, lower_bound: float) -> float:
7
+ """ Imposes a lower bound on transformed_parameter.
8
+
9
+ :param transformed_parameter: any real number.
10
+ :param lower_bound: any real number.
11
+ :return: the corresponding parameter value with the lower bound imposed.
12
+ """
13
+
7
14
  return np.exp(transformed_parameter) + lower_bound
8
15
 
9
16
 
10
17
  def inverse_impose_lower_bound(parameter: float, lower_bound: float) -> float:
18
+ """ Applies the inverse lower-bound transformation to parameter.
19
+
20
+ :param parameter: a real number which adheres to the given lower bound.
21
+ :param lower_bound: any real number.
22
+ :return: the transformed parameter, which can take on any real number.
23
+ """
24
+
11
25
  return np.log(parameter - lower_bound)
12
26
 
13
27
 
14
28
  def impose_upper_bound(transformed_parameter: float, upper_bound: float) -> float:
29
+ """ Imposes an upper bound on transformed_parameter.
30
+
31
+ :param transformed_parameter: any real number.
32
+ :param upper_bound: any real number.
33
+ :return: the corresponding parameter value with the upper bound imposed.
34
+ """
35
+
15
36
  return -np.exp(transformed_parameter) + upper_bound
16
37
 
17
38
 
18
39
  def inverse_impose_upper_bound(parameter: float, upper_bound: float) -> float:
40
+ """ Applies the inverse upper-bound transformation to parameter.
41
+
42
+ :param parameter: a real number which adheres to the given upper bound.
43
+ :param upper_bound: any real number.
44
+ :return: the transformed parameter, which can take on any real number.
45
+ """
46
+
19
47
  return np.log(upper_bound - parameter)
20
48
 
21
49
 
22
50
  def impose_bounds(transformed_parameter: float, lower_bound: float, upper_bound: float) -> float:
51
+ """ Imposes a lower and an upper bound on transformed_parameter.
52
+
53
+ :param transformed_parameter: any real number.
54
+ :param lower_bound: any real number.
55
+ :param upper_bound: any real number strictly larger than lower bound.
56
+ :return: a real number which adheres to the given bounds.
57
+ """
58
+
23
59
  return lower_bound + (upper_bound - lower_bound)/(1.0 + np.exp(-transformed_parameter))
24
60
 
25
61
 
26
62
  def inverse_impose_bounds(parameter: float, lower_bound: float, upper_bound: float) -> float:
63
+ """ Applies the inverse bounds transformation to parameter.
64
+
65
+ :param parameter: a real number which adheres to the given bounds.
66
+ :param lower_bound: any real number.
67
+ :param upper_bound: any real number strictly larger than lower bound.
68
+ :return: the transformed parameter, which can take on any real number.
69
+ """
70
+
27
71
  z = (parameter - lower_bound)/(upper_bound - lower_bound)
28
72
  return np.log(z) - np.log(1.0 - z)
29
73
 
@@ -38,7 +38,7 @@ def find_lt(a: Sequence,
38
38
 
39
39
  :param a:
40
40
  :param x: an object with '<' and '==' operators.
41
- :return:
41
+ :return: the rightmost value in a that is less than x.
42
42
  """
43
43
 
44
44
  _check_args(a=a, x=x)
@@ -55,7 +55,7 @@ def find_le(a: Sequence,
55
55
 
56
56
  :param a:
57
57
  :param x: an object with '<' and '==' operators.
58
- :return:
58
+ :return: the rightmost value in a that is less than or equal to x.
59
59
  """
60
60
 
61
61
  _check_args(a=a, x=x)
@@ -72,7 +72,7 @@ def find_gt(a: Sequence,
72
72
 
73
73
  :param a:
74
74
  :param x: an object with '<' and '==' operators.
75
- :return:
75
+ :return: the leftmost value in a that is greater than x.
76
76
  """
77
77
 
78
78
  _check_args(a=a, x=x)
@@ -87,11 +87,11 @@ def find_ge(a: Sequence,
87
87
  x: Any) -> Any:
88
88
  """ Returns the leftmost value in a that is greater than or equal to x.
89
89
 
90
- Remark: raises a value error if
90
+ Remark: raises a value error if
91
91
 
92
92
  :param a:
93
93
  :param x: an object with '<' and '==' operators.
94
- :return:
94
+ :return: the leftmost value in a that is greater than or equal to x.
95
95
  """
96
96
 
97
97
  _check_args(a=a, x=x)
@@ -0,0 +1,106 @@
1
+ """ This module implements helping functions for the introduced types. """
2
+
3
+ import hashlib
4
+ import numpy as np
5
+ from typing import Union, Iterable, List, Dict
6
+ from .globals import ScalarOrArray, FloatOrArray
7
+
8
+
9
+ def size(x: ScalarOrArray) -> int:
10
+ """ Returns the number of elements in x.
11
+
12
+ :param x:
13
+ :return: the number of elements in x.
14
+ """
15
+
16
+ if isinstance(x, np.ndarray):
17
+ return x.size
18
+ else:
19
+ return 1
20
+
21
+
22
+ def to_1d_array(x: Union[float, int, Iterable, np.ndarray]) -> np.ndarray:
23
+ """ Transforms x into a 1d array.
24
+
25
+ :param x:
26
+ :return: a 1-dimensional array corresponding to x.
27
+ """
28
+
29
+ return np.asarray(x).reshape(1, -1)[0, :]
30
+
31
+
32
+ def to_float_if_singleton(x: FloatOrArray) -> FloatOrArray:
33
+ """ Transforms x into a Float if it has a single element.
34
+
35
+ :param x:
36
+ :return: x if it is an array, or x as a float if it has only a single element.
37
+ """
38
+
39
+ if x.size == 1:
40
+ return x.flatten()[0]
41
+ else:
42
+ return x
43
+
44
+
45
+ def has_nan(x: np.ndarray) -> bool:
46
+ """ Checks whether any element in x is NaN.
47
+
48
+ :param x:
49
+ :return: True if any element in x is NaN, False otherwise.
50
+ """
51
+ return np.isnan(np.sum(x))
52
+
53
+
54
+ def compute_hash(a: np.ndarray,
55
+ n_chars: int = -1,
56
+ include_dtype: bool = False,
57
+ include_shape: bool = False) -> str:
58
+ """ Returns a hash for the given array a. The hashing algorithm for np arrays is based on
59
+ https://stackoverflow.com/questions/64753916/unique-identifier-for-numpy-array
60
+
61
+ :param a:
62
+ :param n_chars: the number of characters in the hashing string.
63
+ :param include_dtype: if True also incorporate the dtype in the hash.
64
+ :param include_shape: if True also incorporate the shape in the hash (so two arrays with identical elements but a different shape receive a different hash string).
65
+ :return: the hash string corresponding to the given array.
66
+ """
67
+
68
+ data = bytes()
69
+ if include_dtype:
70
+ data += str(a.dtype).encode('ascii')
71
+ data += b','
72
+ if include_shape:
73
+ data += str(a.shape).encode('ascii')
74
+ data += b','
75
+ data += a.tobytes()
76
+ hash_values = hashlib.sha256(data).hexdigest()
77
+ return hash_values[:n_chars]
78
+
79
+
80
+ def dicts_to_array(dicts: List[dict]) -> np.ndarray:
81
+ """ Converts a list of dicts with indices as keys to an array.
82
+
83
+ :param dicts:
84
+ :return: array corresponding to the list.
85
+ """
86
+
87
+ result_dict = {}
88
+ for d in dicts:
89
+ result_dict.update(d)
90
+
91
+ return dict_to_array(result_dict)
92
+
93
+
94
+ def dict_to_array(d: Dict[int, float]) -> np.ndarray:
95
+ """ Convert a dict with indices as keys to an array.
96
+
97
+ :param d:
98
+ :return: array corresponding to the dict.
99
+ """
100
+
101
+ idx = np.array(list(d.keys()))
102
+ val = np.array(list(d.values()))
103
+
104
+ out = np.zeros(shape=val.shape)
105
+ out[idx[:]] = val
106
+ return out
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: computils
3
- Version: 0.0.1
4
- Summary: A package collecting functionality for various numerical computations (numerical differentiation, interpolation, optimization, sorting, ).
3
+ Version: 0.0.2
4
+ Summary: A package collecting functionality for various numerical computations (numerical differentiation, interpolation, optimization, sorting, parallelization).
5
5
  Author-email: Karim Moussa <research@k-moussa.com>
6
6
  Project-URL: Homepage, https://github.com/k-moussa/computils
7
7
  Classifier: Programming Language :: Python :: 3
@@ -9,6 +9,7 @@ src/computils/gaussian_elim_spp.py
9
9
  src/computils/globals.py
10
10
  src/computils/interpolation.py
11
11
  src/computils/matrix_computations.py
12
+ src/computils/parallelization.py
12
13
  src/computils/parameter_transformations.py
13
14
  src/computils/performance_checking.py
14
15
  src/computils/sorting_algorithms.py
@@ -1,64 +0,0 @@
1
- """ This module collects all exposed types."""
2
-
3
- import numpy as np
4
- from enum import Enum
5
- from abc import ABC, abstractmethod
6
- from typing import final, Union
7
-
8
- MACHINE_EPS: final = float(2 ** (-53)) # max relative error corresponding to 1/2 ULP
9
- SQUARE_ROOT_MACHINE_EPS: final = np.sqrt(MACHINE_EPS)
10
- CUBE_ROOT_MACHINE_EPS: final = np.cbrt(MACHINE_EPS)
11
- FOURTH_ROOT_MACHINE_EPS: final = np.power(MACHINE_EPS, 1/4)
12
-
13
- Integer: final = Union[int, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64]
14
- Float: final = Union[float, np.float16, np.float32, np.float64, np.float128]
15
- Complex: final = Union[complex, np.complex64, np.complex128, np.complex256]
16
- Scalar: final = Union[Integer, Float, Complex]
17
-
18
- IntOrArray: final = Union[Integer, np.ndarray]
19
- FloatOrArray: final = Union[Float, np.ndarray]
20
- ComplexOrArray: final = Union[Complex, np.ndarray]
21
- ScalarOrArray: final = Union[Scalar, np.ndarray]
22
-
23
-
24
- # 128-bit integers generated using entropy gathered from the OS, for fixing the seed of NumPy generators.
25
- SEED128_1: final = 137088887599416403785824428186011523708
26
- SEED128_2: final = 230453062773196406322953725600125856954
27
- SEED128_3: final = 334883455271001690768699206953238296696
28
- SEED128_4: final = 69805789651723487751993596682833892465
29
- SEED128_5: final = 107997836754270478432192384195731366045
30
- SEED128_6: final = 61384398454038591198002507585631369346
31
- SEED128_7: final = 165303562401480433055547884045914282112
32
- SEED128_8: final = 215418768511176275020128984871223831370
33
- SEED128_9: final = 247520618467519227348322182729719821192
34
- SEED128_10: final = 260473913589332506348144405693894964574
35
-
36
-
37
- class InterpolationType(Enum):
38
- linear = 0
39
- ncs = 1 # natural cubic spline
40
- ccs = 2 # clamped cubic spline
41
- pmc = 3 # piecewise monotone cubic [FC80]
42
- pchip = 4 # [FB84]
43
-
44
-
45
- class ExtrapolationType(Enum):
46
- nan = 0
47
- flat = 1
48
-
49
-
50
- class Interpolator(ABC):
51
- def __init__(self,
52
- inter_type: InterpolationType,
53
- extra_type: ExtrapolationType):
54
-
55
- self.inter_type: InterpolationType = inter_type
56
- self.extra_type: ExtrapolationType = extra_type
57
-
58
- @abstractmethod
59
- def __call__(self, x: ScalarOrArray) -> ScalarOrArray:
60
- """
61
-
62
- :param x:
63
- :return:
64
- """
@@ -1,11 +0,0 @@
1
- """ Implements helping functions for the introduced types. """
2
-
3
- import numpy as np
4
- from .globals import ScalarOrArray
5
-
6
-
7
- def size(x: ScalarOrArray) -> int:
8
- if isinstance(x, np.ndarray):
9
- return x.size
10
- else:
11
- return 1
File without changes
File without changes
File without changes