linearization.py 11.6 KB
 Martin Reinecke committed Jan 07, 2019 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ``````# 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 . # # Copyright(C) 2013-2019 Max-Planck-Society # # 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 .field import Field `````` Martin Reinecke committed Aug 05, 2018 21 ``````from .multi_field import MultiField `````` Martin Reinecke committed Jul 26, 2018 22 23 24 25 ``````from .sugar import makeOp class Linearization(object): `````` Martin Reinecke committed Jan 10, 2019 26 `````` """Let `A` be an operator and `x` a field. `Linearization` stores the value `````` Martin Reinecke committed Jan 08, 2019 27 28 29 30 31 `````` 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 32 33 `````` val : Field or MultiField The value of the operator application. `````` Martin Reinecke committed Jan 08, 2019 34 `````` jac : LinearOperator `````` Philipp Arras committed Jan 13, 2019 35 36 37 38 39 40 `````` 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 41 `````` """ `````` Martin Reinecke committed Aug 21, 2018 42 `````` def __init__(self, val, jac, metric=None, want_metric=False): `````` Martin Reinecke committed Jul 26, 2018 43 44 `````` self._val = val self._jac = jac `````` Martin Reinecke committed Aug 10, 2018 45 46 `````` if self._val.domain != self._jac.target: raise ValueError("domain mismatch") `````` Martin Reinecke committed Aug 21, 2018 47 `````` self._want_metric = want_metric `````` Martin Reinecke committed Jul 26, 2018 48 49 `````` self._metric = metric `````` Martin Reinecke committed Aug 21, 2018 50 `````` def new(self, val, jac, metric=None): `````` Martin Reinecke committed Jan 08, 2019 51 52 53 54 55 `````` """Create a new Linearization, taking the `want_metric` property from this one. Parameters ---------- `````` Philipp Arras committed Jan 13, 2019 56 `````` val : Field or MultiField `````` Martin Reinecke committed Jan 08, 2019 57 58 59 `````` the value of the operator application jac : LinearOperator the Jacobian `````` Philipp Arras committed Jan 13, 2019 60 61 `````` metric : LinearOperator or None The metric. Default: None. `````` Martin Reinecke committed Jan 08, 2019 62 `````` """ `````` Martin Reinecke committed Aug 21, 2018 63 64 `````` return Linearization(val, jac, metric, self._want_metric) `````` Martin Reinecke committed Jul 26, 2018 65 66 `````` @property def domain(self): `````` Philipp Arras committed Jan 13, 2019 67 `````` """DomainTuple or MultiDomain : the Jacobian's domain""" `````` Martin Reinecke committed Jul 26, 2018 68 69 70 71 `````` return self._jac.domain @property def target(self): `````` Philipp Arras committed Jan 13, 2019 72 `````` """DomainTuple or MultiDomain : the Jacobian's target (i.e. the value's domain)""" `````` Martin Reinecke committed Jul 26, 2018 73 74 75 76 `````` return self._jac.target @property def val(self): `````` Philipp Arras committed Jan 13, 2019 77 `````` """Field or MultiField : the value""" `````` Martin Reinecke committed Jul 26, 2018 78 79 80 81 `````` return self._val @property def jac(self): `````` Martin Reinecke committed Jan 08, 2019 82 `````` """LinearOperator : the Jacobian""" `````` Martin Reinecke committed Jul 26, 2018 83 84 `````` return self._jac `````` Martin Reinecke committed Jul 26, 2018 85 86 `````` @property def gradient(self): `````` Philipp Arras committed Jan 13, 2019 87 `````` """Field or MultiField : the gradient `````` Martin Reinecke committed Jan 08, 2019 88 89 90 91 92 `````` Notes ----- Only available if target is a scalar """ `````` Martin Reinecke committed Aug 09, 2018 93 `````` return self._jac.adjoint_times(Field.scalar(1.)) `````` Martin Reinecke committed Jul 26, 2018 94 `````` `````` Martin Reinecke committed Aug 21, 2018 95 96 `````` @property def want_metric(self): `````` Martin Reinecke committed Jan 11, 2019 97 `````` """bool : True iff the metric was requested in the constructor""" `````` Martin Reinecke committed Aug 21, 2018 98 99 `````` return self._want_metric `````` Martin Reinecke committed Jul 26, 2018 100 101 `````` @property def metric(self): `````` Martin Reinecke committed Jan 08, 2019 102 103 104 105 106 107 `````` """LinearOperator : the metric Notes ----- Only available if target is a scalar """ `````` Martin Reinecke committed Jul 26, 2018 108 109 `````` return self._metric `````` Martin Reinecke committed Jul 27, 2018 110 `````` def __getitem__(self, name): `````` Martin Reinecke committed Nov 26, 2018 111 112 `````` from .operators.simple_linear_operators import ducktape return self.new(self._val[name], ducktape(None, self.domain, name)) `````` Martin Reinecke committed Jul 27, 2018 113 `````` `````` Martin Reinecke committed Jul 26, 2018 114 `````` def __neg__(self): `````` Martin Reinecke committed Aug 21, 2018 115 116 `````` return self.new(-self._val, -self._jac, None if self._metric is None else -self._metric) `````` Martin Reinecke committed Jul 26, 2018 117 `````` `````` Martin Reinecke committed Aug 06, 2018 118 `````` def conjugate(self): `````` Martin Reinecke committed Aug 21, 2018 119 `````` return self.new( `````` Martin Reinecke committed Aug 06, 2018 120 121 122 123 124 `````` 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 125 `````` return self.new(self._val.real, self._jac.real) `````` Martin Reinecke committed Aug 06, 2018 126 `````` `````` Martin Reinecke committed Aug 12, 2018 127 `````` def _myadd(self, other, neg): `````` Martin Reinecke committed Jul 26, 2018 128 129 130 `````` if isinstance(other, Linearization): met = None if self._metric is not None and other._metric is not None: `````` Martin Reinecke committed Aug 12, 2018 131 `````` met = self._metric._myadd(other._metric, neg) `````` Martin Reinecke committed Aug 21, 2018 132 `````` return self.new( `````` Martin Reinecke committed Aug 12, 2018 133 134 `````` self._val.flexible_addsub(other._val, neg), self._jac._myadd(other._jac, neg), met) `````` Martin Reinecke committed Jul 26, 2018 135 `````` if isinstance(other, (int, float, complex, Field, MultiField)): `````` Martin Reinecke committed Aug 12, 2018 136 `````` if neg: `````` Martin Reinecke committed Aug 21, 2018 137 `````` return self.new(self._val-other, self._jac, self._metric) `````` Martin Reinecke committed Aug 12, 2018 138 `````` else: `````` Martin Reinecke committed Aug 21, 2018 139 `````` return self.new(self._val+other, self._jac, self._metric) `````` Martin Reinecke committed Aug 12, 2018 140 141 142 `````` def __add__(self, other): return self._myadd(other, False) `````` Martin Reinecke committed Jul 26, 2018 143 144 `````` def __radd__(self, other): `````` Martin Reinecke committed Aug 12, 2018 145 `````` return self._myadd(other, False) `````` Martin Reinecke committed Jul 26, 2018 146 147 `````` def __sub__(self, other): `````` Martin Reinecke committed Aug 12, 2018 148 `````` return self._myadd(other, True) `````` Martin Reinecke committed Jul 26, 2018 149 150 151 152 `````` def __rsub__(self, other): return (-self).__add__(other) `````` Martin Reinecke committed Sep 12, 2018 153 154 `````` def __truediv__(self, other): if isinstance(other, Linearization): `````` Philipp Frank committed Jan 09, 2019 155 `````` return self.__mul__(other.one_over()) `````` Martin Reinecke committed Sep 12, 2018 156 157 158 `````` return self.__mul__(1./other) def __rtruediv__(self, other): `````` Philipp Frank committed Jan 09, 2019 159 `````` return self.one_over().__mul__(other) `````` Martin Reinecke committed Sep 12, 2018 160 `````` `````` Martin Reinecke committed Sep 27, 2018 161 162 163 `````` def __pow__(self, power): if not np.isscalar(power): return NotImplemented `````` Martin Reinecke committed Oct 09, 2018 164 165 `````` return self.new(self._val**power, makeOp(self._val**(power-1)).scale(power)(self._jac)) `````` Martin Reinecke committed Sep 27, 2018 166 `````` `````` Martin Reinecke committed Jul 26, 2018 167 168 169 `````` def __mul__(self, other): from .sugar import makeOp if isinstance(other, Linearization): `````` Martin Reinecke committed Aug 10, 2018 170 171 `````` if self.target != other.target: raise ValueError("domain mismatch") `````` Martin Reinecke committed Aug 21, 2018 172 `````` return self.new( `````` Martin Reinecke committed Jul 28, 2018 173 `````` self._val*other._val, `````` Martin Reinecke committed Aug 10, 2018 174 175 `````` (makeOp(other._val)(self._jac))._myadd( makeOp(self._val)(other._jac), False)) `````` Martin Reinecke committed Aug 06, 2018 176 177 178 179 `````` 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 180 `````` return self.new(self._val*other, self._jac.scale(other), met) `````` Martin Reinecke committed Jul 26, 2018 181 `````` if isinstance(other, (Field, MultiField)): `````` Martin Reinecke committed Aug 10, 2018 182 183 `````` if self.target != other.domain: raise ValueError("domain mismatch") `````` Martin Reinecke committed Aug 21, 2018 184 `````` return self.new(self._val*other, makeOp(other)(self._jac)) `````` Martin Reinecke committed Jul 26, 2018 185 186 `````` def __rmul__(self, other): `````` Martin Reinecke committed Aug 06, 2018 187 `````` return self.__mul__(other) `````` Martin Reinecke committed Jul 26, 2018 188 `````` `````` 189 190 191 192 `````` def outer(self, other): from .operators.outer_product_operator import OuterProduct if isinstance(other, Linearization): return self.new( `````` Sebastian Hutschenreuter committed Sep 19, 2018 193 194 195 `````` OuterProduct(self._val, other.target)(other._val), OuterProduct(self._jac(self._val), other.target)._myadd( OuterProduct(self._val, other.target)(other._jac), False)) `````` 196 `````` if np.isscalar(other): `````` Martin Reinecke committed Sep 12, 2018 197 `````` return self.__mul__(other) `````` 198 `````` if isinstance(other, (Field, MultiField)): `````` Sebastian Hutschenreuter committed Sep 19, 2018 199 200 `````` return self.new(OuterProduct(self._val, other.domain)(other), OuterProduct(self._jac(self._val), other.domain)) `````` 201 `````` `````` Martin Reinecke committed Aug 03, 2018 202 `````` def vdot(self, other): `````` Martin Reinecke committed Aug 05, 2018 203 `````` from .operators.simple_linear_operators import VdotOperator `````` Martin Reinecke committed Aug 03, 2018 204 `````` if isinstance(other, (Field, MultiField)): `````` Martin Reinecke committed Aug 21, 2018 205 `````` return self.new( `````` Martin Reinecke committed Aug 09, 2018 206 `````` Field.scalar(self._val.vdot(other)), `````` Martin Reinecke committed Aug 05, 2018 207 `````` VdotOperator(other)(self._jac)) `````` Martin Reinecke committed Aug 21, 2018 208 `````` return self.new( `````` Martin Reinecke committed Aug 09, 2018 209 `````` Field.scalar(self._val.vdot(other._val)), `````` Martin Reinecke committed Aug 05, 2018 210 211 `````` VdotOperator(self._val)(other._jac) + VdotOperator(other._val)(self._jac)) `````` Martin Reinecke committed Aug 03, 2018 212 `````` `````` 213 `````` def sum(self, spaces=None): `````` Martin Reinecke committed Sep 18, 2018 214 `````` from .operators.contraction_operator import ContractionOperator `````` 215 216 217 `````` if spaces is None: return self.new( Field.scalar(self._val.sum()), `````` Martin Reinecke committed Sep 18, 2018 218 `````` ContractionOperator(self._jac.target, None)(self._jac)) `````` 219 220 221 `````` else: return self.new( self._val.sum(spaces), `````` Martin Reinecke committed Sep 18, 2018 222 `````` ContractionOperator(self._jac.target, spaces)(self._jac)) `````` 223 224 `````` def integrate(self, spaces=None): `````` Martin Reinecke committed Sep 18, 2018 225 `````` from .operators.contraction_operator import ContractionOperator `````` 226 227 228 `````` if spaces is None: return self.new( Field.scalar(self._val.integrate()), `````` Martin Reinecke committed Sep 18, 2018 229 `````` ContractionOperator(self._jac.target, None, 1)(self._jac)) `````` 230 231 232 `````` else: return self.new( self._val.integrate(spaces), `````` Martin Reinecke committed Sep 18, 2018 233 `````` ContractionOperator(self._jac.target, spaces, 1)(self._jac)) `````` Martin Reinecke committed Jul 26, 2018 234 235 236 `````` def exp(self): tmp = self._val.exp() `````` Martin Reinecke committed Aug 21, 2018 237 `````` return self.new(tmp, makeOp(tmp)(self._jac)) `````` Philipp Arras committed Oct 15, 2018 238 `````` `````` Martin Reinecke committed Dec 21, 2018 239 240 `````` def clip(self, min=None, max=None): tmp = self._val.clip(min, max) `````` Jakob Knollmueller committed Dec 21, 2018 241 `````` if (min is None) and (max is None): `````` Martin Reinecke committed Dec 21, 2018 242 `````` return self `````` Jakob Knollmueller committed Dec 21, 2018 243 244 245 246 247 248 `````` elif max is None: tmp2 = makeOp(1. - (tmp == min)) elif min is None: tmp2 = makeOp(1. - (tmp == max)) else: tmp2 = makeOp(1. - (tmp == min) - (tmp == max)) `````` Jakob Knollmueller committed Dec 15, 2018 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 `````` return self.new(tmp, tmp2(self._jac)) def sin(self): tmp = self._val.sin() tmp2 = self._val.cos() return self.new(tmp, makeOp(tmp2)(self._jac)) def cos(self): tmp = self._val.cos() tmp2 = - self._val.sin() return self.new(tmp, makeOp(tmp2)(self._jac)) def tan(self): tmp = self._val.tan() tmp2 = 1./(self._val.cos()**2) return self.new(tmp, makeOp(tmp2)(self._jac)) def sinc(self): tmp = self._val.sinc() tmp2 = (self._val.cos()-tmp)/self._val return self.new(tmp, makeOp(tmp2)(self._jac)) `````` Martin Reinecke committed Jul 26, 2018 271 272 `````` def log(self): tmp = self._val.log() `````` Martin Reinecke committed Aug 21, 2018 273 `````` return self.new(tmp, makeOp(1./self._val)(self._jac)) `````` Martin Reinecke committed Jul 26, 2018 274 `````` `````` Jakob Knollmueller committed Dec 15, 2018 275 276 277 278 279 280 281 282 283 284 `````` def sinh(self): tmp = self._val.sinh() tmp2 = self._val.cosh() return self.new(tmp, makeOp(tmp2)(self._jac)) def cosh(self): tmp = self._val.cosh() tmp2 = self._val.sinh() return self.new(tmp, makeOp(tmp2)(self._jac)) `````` Martin Reinecke committed Jul 27, 2018 285 286 `````` def tanh(self): tmp = self._val.tanh() `````` Martin Reinecke committed Aug 21, 2018 287 `````` return self.new(tmp, makeOp(1.-tmp**2)(self._jac)) `````` Martin Reinecke committed Jul 27, 2018 288 `````` `````` Jakob Knollmueller committed Dec 15, 2018 289 `````` def sigmoid(self): `````` Martin Reinecke committed Jul 27, 2018 290 291 `````` tmp = self._val.tanh() tmp2 = 0.5*(1.+tmp) `````` Martin Reinecke committed Aug 21, 2018 292 `````` return self.new(tmp2, makeOp(0.5*(1.-tmp**2))(self._jac)) `````` Martin Reinecke committed Jul 27, 2018 293 `````` `````` Jakob Knollmueller committed Dec 15, 2018 294 295 296 297 298 299 300 301 302 303 `````` def absolute(self): tmp = self._val.absolute() tmp2 = self._val.sign() return self.new(tmp, makeOp(tmp2)(self._jac)) def one_over(self): tmp = 1./self._val tmp2 = - tmp/self._val return self.new(tmp, makeOp(tmp2)(self._jac)) `````` Martin Reinecke committed Jul 26, 2018 304 `````` def add_metric(self, metric): `````` Martin Reinecke committed Aug 21, 2018 305 `````` return self.new(self._val, self._jac, metric) `````` Martin Reinecke committed Jul 26, 2018 306 `````` `````` Martin Reinecke committed Aug 29, 2018 307 308 309 `````` def with_want_metric(self): return Linearization(self._val, self._jac, self._metric, True) `````` Martin Reinecke committed Jul 26, 2018 310 `````` @staticmethod `````` Martin Reinecke committed Aug 21, 2018 311 `````` def make_var(field, want_metric=False): `````` Martin Reinecke committed Jul 26, 2018 312 `````` from .operators.scaling_operator import ScalingOperator `````` Martin Reinecke committed Aug 21, 2018 313 314 `````` return Linearization(field, ScalingOperator(1., field.domain), want_metric=want_metric) `````` Martin Reinecke committed Jul 26, 2018 315 316 `````` @staticmethod `````` Martin Reinecke committed Aug 21, 2018 317 `````` def make_const(field, want_metric=False): `````` Martin Reinecke committed Aug 05, 2018 318 `````` from .operators.simple_linear_operators import NullOperator `````` Martin Reinecke committed Aug 21, 2018 319 320 `````` return Linearization(field, NullOperator(field.domain, field.domain), want_metric=want_metric) `````` Martin Reinecke committed Aug 29, 2018 321 `````` `````` Martin Reinecke committed Sep 14, 2018 322 323 324 325 `````` @staticmethod def make_const_empty_input(field, want_metric=False): from .operators.simple_linear_operators import NullOperator from .multi_domain import MultiDomain `````` Martin Reinecke committed Sep 18, 2018 326 327 328 `````` return Linearization( field, NullOperator(MultiDomain.make({}), field.domain), want_metric=want_metric) `````` Martin Reinecke committed Sep 14, 2018 329 `````` `````` Martin Reinecke committed Aug 29, 2018 330 331 332 `````` @staticmethod def make_partial_var(field, constants, want_metric=False): from .operators.scaling_operator import ScalingOperator `````` Philipp Arras committed Aug 31, 2018 333 `````` from .operators.block_diagonal_operator import BlockDiagonalOperator `````` Martin Reinecke committed Aug 29, 2018 334 335 336 337 338 `````` if len(constants) == 0: return Linearization.make_var(field, want_metric) else: ops = [ScalingOperator(0. if key in constants else 1., dom) for key, dom in field.domain.items()] `````` Philipp Arras committed Aug 31, 2018 339 `````` bdop = BlockDiagonalOperator(field.domain, tuple(ops)) `````` Martin Reinecke committed Aug 29, 2018 340 `` return Linearization(field, bdop, want_metric=want_metric)``