2from dataclasses
import dataclass
4from typing
import Optional
10 eigvals: numpy.ndarray
12 sqrt_eigvals: numpy.ndarray
16 self.
eigvals, self.
Q = numpy.linalg.eigh(cov)
20 self.
cov = numpy.ascontiguousarray(self.
cov, dtype=numpy.float64)
21 self.
Q = numpy.ascontiguousarray(self.
Q, dtype=numpy.float64)
22 self.
eigvals = numpy.ascontiguousarray(self.
eigvals, dtype=numpy.float64)
26 cdll: Optional[ctypes.CDLL] =
None
34 if ctype == ctypes.c_double:
36 elif ctype == ctypes.c_float:
38 elif ctype == ctypes.c_size_t:
41 raise TypeError(
"Unsupported ctype for mapping to numpy dtype")
44 self, arr: Optional[numpy.ndarray], L: int, N: Optional[int] =
None
45 ) -> Optional[numpy.ndarray]:
48 if not isinstance(arr, numpy.ndarray):
49 raise TypeError(
"Input must be a numpy array")
50 if not numpy.issubdtype(arr.dtype, numpy.floating):
52 f
"Input array must be of a floating type, but got {arr.dtype}."
54 if L <= 0
or (N
is not None and N <= 0):
55 raise ValueError(
"L and N must be positive integers.")
60 f
"Input array must have size [{L}] but got [{arrShape[0]}]"
62 if N
is not None and N != 1:
64 f
"Input array must have size [{L}x{N}] but got [{L}]"
67 if arrShape != (L, N):
70 f
"Input array must have size [{L}x{N}] but got [{row}x{cols}]"
73 raise ValueError(
"Input array must be 1D or 2D")
76 return numpy.ascontiguousarray(arr, dtype=numpy.float64)
79 if cov.shape != (N, N):
80 raise ValueError(f
"Covariance matrix must be of shape [{N}x{N}] but got {cov.shape}")
81 if not numpy.allclose(cov, cov.T, atol=tol):
82 raise ValueError(
"Covariance matrix must be symmetric")
83 if numpy.any(numpy.linalg.eigvalsh(cov) <= -tol):
84 raise ValueError(
"Covariance matrix must be positive definite")
89 def _check_weights(self, weight: Optional[numpy.ndarray], size: int, tol=1e-6) -> Optional[numpy.ndarray]:
93 if numpy.any(wChecked < 0):
94 raise ValueError(
"Weights cannot be negative")
95 if numpy.abs(1 - numpy.sum(wChecked)) > tol:
96 raise ValueError(f
"Sum of weights must be 1 within tolerance +/-{tol}, but got {numpy.sum(wChecked)}")
100 if N < 2
or N % 2 != 0:
101 raise ValueError(f
"Dimension N must be even and >= 2, but got {N}")
105 raise ValueError(f
"{name} must be positive, but got {value}")
109 BaseApproximation.cdll = cdll
Optional[numpy.ndarray] _check_weights(self, Optional[numpy.ndarray] weight, int size, tol=1e-6)
CovarianceData _check_covariance_matrix(self, numpy.ndarray cov, int N, tol=1e-6)
_check_positive(self, float value, str name)
_register_cdll(ctypes.CDLL cdll)
Optional[numpy.ndarray] _check_numpy_ndarray(self, Optional[numpy.ndarray] arr, int L, Optional[int] N=None)
_check_even_dimension(self, int N)
type _map_ctypes_numpy(self, ctype)
__init__(self, numpy.ndarray cov)