linearization.py 12.7 KB
 Martin Reinecke committed Jan 07, 2019 1 2 3 4 5 6 7 8 9 10 11 12 13 ``````# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # `````` Philipp Arras committed May 15, 2020 14 ``````# Copyright(C) 2013-2020 Max-Planck-Society `````` Martin Reinecke committed Jan 07, 2019 15 16 ``````# # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik. `````` Martin Reinecke committed Jul 26, 2018 17 18 19 20 `````` import numpy as np from .sugar import makeOp `````` Martin Reinecke committed Apr 06, 2020 21 ``````from .operators.operator import Operator `````` Martin Reinecke committed Jul 26, 2018 22 23 `````` `````` Martin Reinecke committed Apr 06, 2020 24 ``````class Linearization(Operator): `````` Martin Reinecke committed Jan 10, 2019 25 `````` """Let `A` be an operator and `x` a field. `Linearization` stores the value `````` Martin Reinecke committed Jan 08, 2019 26 27 28 29 30 `````` of the operator application (i.e. `A(x)`), the local Jacobian (i.e. `dA(x)/dx`) and, optionally, the local metric. Parameters ---------- `````` Philipp Arras committed Jan 13, 2019 31 32 `````` val : Field or MultiField The value of the operator application. `````` Martin Reinecke committed Jan 08, 2019 33 `````` jac : LinearOperator `````` Philipp Arras committed Jan 13, 2019 34 35 36 37 38 39 `````` The Jacobian. metric : LinearOperator or None The metric. Default: None. want_metric : bool If True, the metric will be computed for other Linearizations derived from this one. Default: False. `````` Martin Reinecke committed Jan 08, 2019 40 `````` """ `````` Martin Reinecke committed Aug 21, 2018 41 `````` def __init__(self, val, jac, metric=None, want_metric=False): `````` Martin Reinecke committed Jul 26, 2018 42 43 `````` self._val = val self._jac = jac `````` Martin Reinecke committed Aug 10, 2018 44 45 `````` if self._val.domain != self._jac.target: raise ValueError("domain mismatch") `````` Martin Reinecke committed Aug 21, 2018 46 `````` self._want_metric = want_metric `````` Martin Reinecke committed Jul 26, 2018 47 48 `````` self._metric = metric `````` Martin Reinecke committed Aug 21, 2018 49 `````` def new(self, val, jac, metric=None): `````` Martin Reinecke committed Jan 08, 2019 50 51 52 53 54 `````` """Create a new Linearization, taking the `want_metric` property from this one. Parameters ---------- `````` Philipp Arras committed Jan 13, 2019 55 `````` val : Field or MultiField `````` Martin Reinecke committed Jan 08, 2019 56 57 58 `````` the value of the operator application jac : LinearOperator the Jacobian `````` Philipp Arras committed Jan 13, 2019 59 60 `````` metric : LinearOperator or None The metric. Default: None. `````` Martin Reinecke committed Jan 08, 2019 61 `````` """ `````` Martin Reinecke committed Aug 21, 2018 62 63 `````` return Linearization(val, jac, metric, self._want_metric) `````` Philipp Arras committed Mar 11, 2020 64 `````` def trivial_jac(self): `````` Martin Reinecke committed Apr 06, 2020 65 `````` return self.make_var(self._val, self._want_metric) `````` Philipp Arras committed Mar 11, 2020 66 `````` `````` Philipp Arras committed Mar 11, 2020 67 68 69 70 71 72 73 `````` def prepend_jac(self, jac): metric = None if self._metric is not None: from .operators.sandwich_operator import SandwichOperator metric = None if self._metric is None else SandwichOperator.make(jac, self._metric) return self.new(self._val, self._jac @ jac, metric) `````` Martin Reinecke committed Jul 26, 2018 74 75 `````` @property def domain(self): `````` Philipp Arras committed Jan 13, 2019 76 `````` """DomainTuple or MultiDomain : the Jacobian's domain""" `````` Martin Reinecke committed Jul 26, 2018 77 78 79 80 `````` return self._jac.domain @property def target(self): `````` Philipp Arras committed Jan 13, 2019 81 `````` """DomainTuple or MultiDomain : the Jacobian's target (i.e. the value's domain)""" `````` Martin Reinecke committed Jul 26, 2018 82 83 84 85 `````` return self._jac.target @property def val(self): `````` Philipp Arras committed Jan 13, 2019 86 `````` """Field or MultiField : the value""" `````` Martin Reinecke committed Jul 26, 2018 87 88 89 90 `````` return self._val @property def jac(self): `````` Martin Reinecke committed Jan 08, 2019 91 `````` """LinearOperator : the Jacobian""" `````` Martin Reinecke committed Jul 26, 2018 92 93 `````` return self._jac `````` Martin Reinecke committed Jul 26, 2018 94 95 `````` @property def gradient(self): `````` Philipp Arras committed Jan 13, 2019 96 `````` """Field or MultiField : the gradient `````` Martin Reinecke committed Jan 08, 2019 97 98 99 100 101 `````` Notes ----- Only available if target is a scalar """ `````` Martin Reinecke committed Apr 06, 2020 102 `````` from .field import Field `````` Martin Reinecke committed Aug 09, 2018 103 `````` return self._jac.adjoint_times(Field.scalar(1.)) `````` Martin Reinecke committed Jul 26, 2018 104 `````` `````` Martin Reinecke committed Aug 21, 2018 105 106 `````` @property def want_metric(self): `````` Martin Reinecke committed Jan 11, 2019 107 `````` """bool : True iff the metric was requested in the constructor""" `````` Martin Reinecke committed Aug 21, 2018 108 109 `````` return self._want_metric `````` Martin Reinecke committed Jul 26, 2018 110 111 `````` @property def metric(self): `````` Martin Reinecke committed Jan 08, 2019 112 113 114 115 116 117 `````` """LinearOperator : the metric Notes ----- Only available if target is a scalar """ `````` Martin Reinecke committed Jul 26, 2018 118 119 `````` return self._metric `````` Martin Reinecke committed Jul 27, 2018 120 `````` def __getitem__(self, name): `````` Martin Reinecke committed Feb 05, 2019 121 `````` return self.new(self._val[name], self._jac.ducktape_left(name)) `````` Martin Reinecke committed Jul 27, 2018 122 `````` `````` Martin Reinecke committed Jul 26, 2018 123 `````` def __neg__(self): `````` Martin Reinecke committed Aug 21, 2018 124 125 `````` return self.new(-self._val, -self._jac, None if self._metric is None else -self._metric) `````` Martin Reinecke committed Jul 26, 2018 126 `````` `````` Martin Reinecke committed Aug 06, 2018 127 `````` def conjugate(self): `````` Martin Reinecke committed Aug 21, 2018 128 `````` return self.new( `````` Martin Reinecke committed Aug 06, 2018 129 130 131 132 133 `````` self._val.conjugate(), self._jac.conjugate(), None if self._metric is None else self._metric.conjugate()) @property def real(self): `````` Martin Reinecke committed Aug 21, 2018 134 `````` return self.new(self._val.real, self._jac.real) `````` Martin Reinecke committed Aug 06, 2018 135 `````` `````` Martin Reinecke committed Aug 12, 2018 136 `````` def _myadd(self, other, neg): `````` Martin Reinecke committed Apr 06, 2020 137 138 139 140 141 142 143 144 145 `````` if np.isscalar(other) or other.jac is None: return self.new(self._val-other if neg else self._val+other, self._jac, self._metric) met = None if self._metric is not None and other._metric is not None: met = self._metric._myadd(other._metric, neg) return self.new( self.val.flexible_addsub(other.val, neg), self.jac._myadd(other.jac, neg), met) `````` Martin Reinecke committed Aug 12, 2018 146 147 148 `````` def __add__(self, other): return self._myadd(other, False) `````` Martin Reinecke committed Jul 26, 2018 149 150 `````` def __radd__(self, other): `````` Martin Reinecke committed Aug 12, 2018 151 `````` return self._myadd(other, False) `````` Martin Reinecke committed Jul 26, 2018 152 153 `````` def __sub__(self, other): `````` Martin Reinecke committed Aug 12, 2018 154 `````` return self._myadd(other, True) `````` Martin Reinecke committed Jul 26, 2018 155 156 157 158 `````` def __rsub__(self, other): return (-self).__add__(other) `````` Martin Reinecke committed Sep 12, 2018 159 `````` def __truediv__(self, other): `````` Martin Reinecke committed Apr 06, 2020 160 161 162 `````` if np.isscalar(other): return self.__mul__(1/other) return self.__mul__(other.ptw("reciprocal")) `````` Martin Reinecke committed Sep 12, 2018 163 164 `````` def __rtruediv__(self, other): `````` Martin Reinecke committed Apr 06, 2020 165 `````` return self.ptw("reciprocal").__mul__(other) `````` Martin Reinecke committed Sep 12, 2018 166 `````` `````` Martin Reinecke committed Sep 27, 2018 167 `````` def __pow__(self, power): `````` Martin Reinecke committed Apr 06, 2020 168 `````` if not (np.isscalar(power) or power.jac is None): `````` Martin Reinecke committed Sep 27, 2018 169 `````` return NotImplemented `````` Martin Reinecke committed Apr 06, 2020 170 `````` return self.ptw("power", power) `````` Martin Reinecke committed Sep 27, 2018 171 `````` `````` Martin Reinecke committed Jul 26, 2018 172 `````` def __mul__(self, other): `````` Martin Reinecke committed Aug 06, 2018 173 174 175 176 `````` if np.isscalar(other): if other == 1: return self met = None if self._metric is None else self._metric.scale(other) `````` Martin Reinecke committed Aug 21, 2018 177 `````` return self.new(self._val*other, self._jac.scale(other), met) `````` Martin Reinecke committed Apr 06, 2020 178 179 `````` from .sugar import makeOp if other.jac is None: `````` Martin Reinecke committed Aug 10, 2018 180 181 `````` if self.target != other.domain: raise ValueError("domain mismatch") `````` Martin Reinecke committed Aug 21, 2018 182 `````` return self.new(self._val*other, makeOp(other)(self._jac)) `````` Martin Reinecke committed Apr 06, 2020 183 184 185 186 187 188 `````` if self.target != other.target: raise ValueError("domain mismatch") return self.new( self.val*other.val, (makeOp(other.val)(self.jac))._myadd( makeOp(self.val)(other.jac), False)) `````` Martin Reinecke committed Jul 26, 2018 189 190 `````` def __rmul__(self, other): `````` Martin Reinecke committed Aug 06, 2018 191 `````` return self.__mul__(other) `````` Martin Reinecke committed Jul 26, 2018 192 `````` `````` 193 `````` def outer(self, other): `````` Martin Reinecke committed Jan 16, 2019 194 195 196 197 198 199 200 201 202 203 204 205 `````` """Computes the outer product of this Linearization with a Field or another Linearization Parameters ---------- other : Field or MultiField or Linearization Returns ------- Linearization the outer product of self and other """ `````` 206 `````` if np.isscalar(other): `````` Martin Reinecke committed Sep 12, 2018 207 `````` return self.__mul__(other) `````` Martin Reinecke committed Apr 06, 2020 208 209 `````` from .operators.outer_product_operator import OuterProduct if other.jac is None: `````` Martin Reinecke committed May 19, 2020 210 211 212 `````` return self.new(OuterProduct(other.domain, self._val)(other), OuterProduct(other.domain, self._jac(self._val))) tmp_op = OuterProduct(other.target, self._val) `````` Martin Reinecke committed Apr 06, 2020 213 `````` return self.new( `````` Martin Reinecke committed May 19, 2020 214 215 216 `````` tmp_op(other._val), OuterProduct(other.target, self._jac(self._val))._myadd( tmp_op(other._jac), False)) `````` 217 `````` `````` Martin Reinecke committed Aug 03, 2018 218 `````` def vdot(self, other): `````` Martin Reinecke committed Jan 16, 2019 219 220 221 222 223 224 225 226 227 228 229 230 `````` """Computes the inner product of this Linearization with a Field or another Linearization Parameters ---------- other : Field or MultiField or Linearization Returns ------- Linearization the inner product of self and other """ `````` Martin Reinecke committed Aug 05, 2018 231 `````` from .operators.simple_linear_operators import VdotOperator `````` Martin Reinecke committed Apr 06, 2020 232 `````` if other.jac is None: `````` Martin Reinecke committed Aug 21, 2018 233 `````` return self.new( `````` Martin Reinecke committed Mar 11, 2020 234 `````` self._val.vdot(other), `````` Martin Reinecke committed Aug 05, 2018 235 `````` VdotOperator(other)(self._jac)) `````` Martin Reinecke committed Aug 21, 2018 236 `````` return self.new( `````` Martin Reinecke committed Mar 11, 2020 237 `````` self._val.vdot(other._val), `````` Martin Reinecke committed Aug 05, 2018 238 239 `````` VdotOperator(self._val)(other._jac) + VdotOperator(other._val)(self._jac)) `````` Martin Reinecke committed Aug 03, 2018 240 `````` `````` 241 `````` def sum(self, spaces=None): `````` Martin Reinecke committed Jan 16, 2019 242 243 244 245 246 247 248 249 250 251 252 253 254 `````` """Computes the (partial) sum over self Parameters ---------- spaces : None, int or list of int - if None, sum over the entire domain - else sum over the specified subspaces Returns ------- Linearization the (partial) sum """ `````` Martin Reinecke committed Sep 18, 2018 255 `````` from .operators.contraction_operator import ContractionOperator `````` Martin Reinecke committed Mar 11, 2020 256 257 258 `````` return self.new( self._val.sum(spaces), ContractionOperator(self._jac.target, spaces)(self._jac)) `````` 259 260 `````` def integrate(self, spaces=None): `````` Martin Reinecke committed Jan 16, 2019 261 262 263 264 265 266 267 268 269 270 271 272 273 `````` """Computes the (partial) integral over self Parameters ---------- spaces : None, int or list of int - if None, integrate over the entire domain - else integrate over the specified subspaces Returns ------- Linearization the (partial) integral """ `````` Philipp Arras committed May 15, 2020 274 275 `````` from .operators.contraction_operator import IntegrationOperator return IntegrationOperator(self._target, spaces)(self) `````` Martin Reinecke committed Jul 26, 2018 276 `````` `````` Martin Reinecke committed Apr 06, 2020 277 278 `````` def ptw(self, op, *args, **kwargs): t1, t2 = self._val.ptw_with_deriv(op, *args, **kwargs) `````` Martin Reinecke committed Apr 04, 2020 279 `````` return self.new(t1, makeOp(t2)(self._jac)) `````` Philipp Arras committed Oct 15, 2018 280 `````` `````` Martin Reinecke committed Jul 26, 2018 281 `````` def add_metric(self, metric): `````` Martin Reinecke committed Aug 21, 2018 282 `````` return self.new(self._val, self._jac, metric) `````` Martin Reinecke committed Jul 26, 2018 283 `````` `````` Martin Reinecke committed Aug 29, 2018 284 285 286 `````` def with_want_metric(self): return Linearization(self._val, self._jac, self._metric, True) `````` Martin Reinecke committed Jul 26, 2018 287 `````` @staticmethod `````` Martin Reinecke committed Aug 21, 2018 288 `````` def make_var(field, want_metric=False): `````` Martin Reinecke committed Jan 16, 2019 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 `````` """Converts a Field to a Linearization, with a unity Jacobian Parameters ---------- field : Field or Multifield the field to be converted want_metric : bool If True, the metric will be computed for other Linearizations derived from this one. Default: False. Returns ------- Linearization the requested Linearization """ `````` Martin Reinecke committed Jul 26, 2018 304 `````` from .operators.scaling_operator import ScalingOperator `````` Gordian Edenhofer committed Dec 05, 2019 305 `````` return Linearization(field, ScalingOperator(field.domain, 1.), `````` Martin Reinecke committed Aug 21, 2018 306 `````` want_metric=want_metric) `````` Martin Reinecke committed Jul 26, 2018 307 308 `````` @staticmethod `````` Martin Reinecke committed Aug 21, 2018 309 `````` def make_const(field, want_metric=False): `````` Martin Reinecke committed Jan 16, 2019 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 `````` """Converts a Field to a Linearization, with a zero Jacobian Parameters ---------- field : Field or Multifield the field to be converted want_metric : bool If True, the metric will be computed for other Linearizations derived from this one. Default: False. Returns ------- Linearization the requested Linearization Notes ----- The Jacobian is square and contains only zeroes. """ `````` Martin Reinecke committed Aug 05, 2018 329 `````` from .operators.simple_linear_operators import NullOperator `````` Martin Reinecke committed Aug 21, 2018 330 331 `````` return Linearization(field, NullOperator(field.domain, field.domain), want_metric=want_metric) `````` Martin Reinecke committed Aug 29, 2018 332 `````` `````` Martin Reinecke committed Sep 14, 2018 333 334 `````` @staticmethod def make_const_empty_input(field, want_metric=False): `````` Martin Reinecke committed Jan 16, 2019 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 `````` """Converts a Field to a Linearization, with a zero Jacobian Parameters ---------- field : Field or Multifield the field to be converted want_metric : bool If True, the metric will be computed for other Linearizations derived from this one. Default: False. Returns ------- Linearization the requested Linearization Notes ----- The Jacobian has an empty input domain, i.e. its matrix representation has 0 columns. """ `````` Martin Reinecke committed Sep 14, 2018 355 356 `````` from .operators.simple_linear_operators import NullOperator from .multi_domain import MultiDomain `````` Martin Reinecke committed Sep 18, 2018 357 358 359 `````` return Linearization( field, NullOperator(MultiDomain.make({}), field.domain), want_metric=want_metric) `````` Martin Reinecke committed Sep 14, 2018 360 `````` `````` Martin Reinecke committed Aug 29, 2018 361 362 `````` @staticmethod def make_partial_var(field, constants, want_metric=False): `````` Martin Reinecke committed Jan 16, 2019 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 `````` """Converts a MultiField to a Linearization, with a Jacobian that is unity for some MultiField components and a zero matrix for others. Parameters ---------- field : Multifield the field to be converted constants : list of string the MultiField components for which the Jacobian should be a zero matrix. want_metric : bool If True, the metric will be computed for other Linearizations derived from this one. Default: False. Returns ------- Linearization the requested Linearization Notes ----- The Jacobian is square. """ `````` Martin Reinecke committed Aug 29, 2018 386 `````` from .operators.scaling_operator import ScalingOperator `````` Philipp Arras committed Aug 31, 2018 387 `````` from .operators.block_diagonal_operator import BlockDiagonalOperator `````` Martin Reinecke committed Aug 29, 2018 388 389 390 `````` if len(constants) == 0: return Linearization.make_var(field, want_metric) else: `````` Gordian Edenhofer committed Dec 05, 2019 391 `````` ops = {key: ScalingOperator(dom, 0. if key in constants else 1.) `````` Martin Reinecke committed Jan 17, 2019 392 393 `````` for key, dom in field.domain.items()} bdop = BlockDiagonalOperator(field.domain, ops) `````` Martin Reinecke committed Aug 29, 2018 394 `` return Linearization(field, bdop, want_metric=want_metric)``