|
| 1 | +from __future__ import annotations |
| 2 | +from typing import TYPE_CHECKING, List |
| 3 | +if TYPE_CHECKING: |
| 4 | + from .kernels import Kernel |
| 5 | + from .constraints import LinearEquality |
| 6 | + |
| 7 | +from .operators import Integral, Identity, ConstMatrix, Derivative |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +class TwoSided: |
| 12 | + """ |
| 13 | + Construct the symmetric block matrix of covariances of all linear equality |
| 14 | + constraints by sandwiching the kernel between all combinations of the |
| 15 | + associated operators. |
| 16 | +
|
| 17 | + Rules for combining operators are defined by functions named with the |
| 18 | + appropriate types, e.g. _Integral_Identity() to sandwich the kernel between |
| 19 | + an integral operator and the identity operator. |
| 20 | +
|
| 21 | + New rules for user-defined operators can be defined by creating a class that |
| 22 | + inherits from this class, adding the appropriate functions following the |
| 23 | + naming scheme, and passing an instance to the model constructor. |
| 24 | + """ |
| 25 | + def __init__(self): |
| 26 | + pass |
| 27 | + |
| 28 | + def __call__( |
| 29 | + self, |
| 30 | + kernel: Kernel, |
| 31 | + constraints: List[LinearEquality] |
| 32 | + ) -> np.ndarray: |
| 33 | + rows = [] |
| 34 | + for c1 in constraints: |
| 35 | + columns = [] |
| 36 | + for c2 in constraints: |
| 37 | + try: |
| 38 | + entry = getattr(self, f"_{type(c1.op).__name__}_{type(c2.op).__name__}")(c1, kernel, c2) |
| 39 | + except AttributeError: |
| 40 | + try: |
| 41 | + entry = getattr(self, f"_{type(c2.op).__name__}_{type(c1.op).__name__}")(c2, kernel, c1).T |
| 42 | + except AttributeError: |
| 43 | + raise NotImplementedError( |
| 44 | + f"No rule found to combine operators of types {type(c1.op).__name__} and {type(c2.op).__name__} with kernel.") |
| 45 | + columns.append(entry) |
| 46 | + rows.append(np.concatenate(columns, axis=1)) |
| 47 | + return np.concatenate(rows) |
| 48 | + |
| 49 | + def _Integral_Integral(self, c1, k, c2): |
| 50 | + if c1 == c2: |
| 51 | + return c1.op.integrator.doubleIntegrationSymmetric(c1, k) |
| 52 | + else: |
| 53 | + return c1.op.integrator.doubleIntegration(c1, k, c2) |
| 54 | + |
| 55 | + def _Identity_Identity(self, c1, k, c2): |
| 56 | + return k(c1.x, c2.x) |
| 57 | + |
| 58 | + def _Derivative_Derivative(self, c1, k, c2): |
| 59 | + return k.d2K_dxdy(c1.x, c2.x) |
| 60 | + |
| 61 | + def _Integral_Identity(self, c1, k, c2): |
| 62 | + return c1.op.integrator.singleIntegration(c1, k, c2.x) |
| 63 | + |
| 64 | + def _Integral_Derivative(self, c1, k, c2): |
| 65 | + return c1.op.integrator.singleIntegration(c1, k.dK_dy, c2.x) |
| 66 | + |
| 67 | + def _Identity_Derivative(self, c1, k, c2): |
| 68 | + return k.dK_dy(c1.x, c2.x) |
| 69 | + |
| 70 | + def _ConstMatrix_ConstMatrix(self, c1, k, c2): |
| 71 | + return c1.op() @ k(c1.x, c2.x) @ c2.op().T |
| 72 | + |
| 73 | + |
| 74 | +class OneSided: |
| 75 | + """ |
| 76 | + Construct the asymmetric block matrix of covariances of the prediction |
| 77 | + points and all linear equality constraints by left-application of the |
| 78 | + associated operators to the kernel. |
| 79 | +
|
| 80 | + Rules for combining operators with the kernel are defined by functions |
| 81 | + named with the appropriate type, e.g. _Integral() to apply an integral |
| 82 | + operator. |
| 83 | +
|
| 84 | + New rules for user-defined operators can be defined by creating a class that |
| 85 | + inherits from this class, adding the appropriate functions following the |
| 86 | + naming scheme, and passing an instance to the model constructor. |
| 87 | + """ |
| 88 | + def __init__(self): |
| 89 | + pass |
| 90 | + |
| 91 | + def __call__( |
| 92 | + self, |
| 93 | + kernel: Kernel, |
| 94 | + constraints: List[LinearEquality], |
| 95 | + w: np.ndarray |
| 96 | + ) -> np.ndarray: |
| 97 | + entries = [] |
| 98 | + for c in constraints: |
| 99 | + try: |
| 100 | + entry = getattr(self, f"_{type(c.op).__name__}")(c, kernel, w) |
| 101 | + except AttributeError: |
| 102 | + raise NotImplementedError( |
| 103 | + f"No rule found to combine operator of type {type(c.op).__name__} with kernel.") |
| 104 | + entries.append(entry) |
| 105 | + return np.concatenate(entries) |
| 106 | + |
| 107 | + def _Integral(self, c, k, w): |
| 108 | + return c.op.integrator.singleIntegration(c, k, w) |
| 109 | + |
| 110 | + def _Identity(self, c, k, w): |
| 111 | + return k(c.x, w) |
| 112 | + |
| 113 | + def _Derivative(self, c, k, w): |
| 114 | + return k.dK_dx(c.x, w) |
| 115 | + |
| 116 | + def _ConstMatrix(self, c, k, w): |
| 117 | + return c.op() |
0 commit comments