linearization.py 5.28 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
from __future__ import absolute_import, division, print_function

import numpy as np

from .compat import *
from .field import Field
Martin Reinecke's avatar
Martin Reinecke committed
7
from .multi_field import MultiField
Martin Reinecke's avatar
Martin Reinecke committed
8
9
10
11
from .sugar import makeOp


class Linearization(object):
12
    def __init__(self, val, jac, metric=None, want_metric=False):
Martin Reinecke's avatar
Martin Reinecke committed
13
14
        self._val = val
        self._jac = jac
Martin Reinecke's avatar
Martin Reinecke committed
15
16
        if self._val.domain != self._jac.target:
            raise ValueError("domain mismatch")
17
        self._want_metric = want_metric
Martin Reinecke's avatar
Martin Reinecke committed
18
19
        self._metric = metric

20
21
22
    def new(self, val, jac, metric=None):
        return Linearization(val, jac, metric, self._want_metric)

Martin Reinecke's avatar
Martin Reinecke committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    @property
    def domain(self):
        return self._jac.domain

    @property
    def target(self):
        return self._jac.target

    @property
    def val(self):
        return self._val

    @property
    def jac(self):
        return self._jac

Martin Reinecke's avatar
Martin Reinecke committed
39
40
41
    @property
    def gradient(self):
        """Only available if target is a scalar"""
Martin Reinecke's avatar
Martin Reinecke committed
42
        return self._jac.adjoint_times(Field.scalar(1.))
Martin Reinecke's avatar
Martin Reinecke committed
43

44
45
46
47
    @property
    def want_metric(self):
        return self._want_metric

Martin Reinecke's avatar
Martin Reinecke committed
48
49
    @property
    def metric(self):
Martin Reinecke's avatar
Martin Reinecke committed
50
        """Only available if target is a scalar"""
Martin Reinecke's avatar
Martin Reinecke committed
51
52
        return self._metric

Martin Reinecke's avatar
Martin Reinecke committed
53
    def __getitem__(self, name):
Martin Reinecke's avatar
Martin Reinecke committed
54
        from .operators.simple_linear_operators import FieldAdapter
55
        return self.new(self._val[name], FieldAdapter(self.domain, name))
Martin Reinecke's avatar
Martin Reinecke committed
56

Martin Reinecke's avatar
Martin Reinecke committed
57
    def __neg__(self):
58
59
        return self.new(-self._val, -self._jac,
                        None if self._metric is None else -self._metric)
Martin Reinecke's avatar
Martin Reinecke committed
60

Martin Reinecke's avatar
Martin Reinecke committed
61
    def conjugate(self):
62
        return self.new(
Martin Reinecke's avatar
Martin Reinecke committed
63
64
65
66
67
            self._val.conjugate(), self._jac.conjugate(),
            None if self._metric is None else self._metric.conjugate())

    @property
    def real(self):
68
        return self.new(self._val.real, self._jac.real)
Martin Reinecke's avatar
Martin Reinecke committed
69

Martin Reinecke's avatar
Martin Reinecke committed
70
    def _myadd(self, other, neg):
Martin Reinecke's avatar
Martin Reinecke committed
71
72
73
        if isinstance(other, Linearization):
            met = None
            if self._metric is not None and other._metric is not None:
Martin Reinecke's avatar
Martin Reinecke committed
74
                met = self._metric._myadd(other._metric, neg)
75
            return self.new(
Martin Reinecke's avatar
Martin Reinecke committed
76
77
                self._val.flexible_addsub(other._val, neg),
                self._jac._myadd(other._jac, neg), met)
Martin Reinecke's avatar
Martin Reinecke committed
78
        if isinstance(other, (int, float, complex, Field, MultiField)):
Martin Reinecke's avatar
Martin Reinecke committed
79
            if neg:
80
                return self.new(self._val-other, self._jac, self._metric)
Martin Reinecke's avatar
Martin Reinecke committed
81
            else:
82
                return self.new(self._val+other, self._jac, self._metric)
Martin Reinecke's avatar
Martin Reinecke committed
83
84
85

    def __add__(self, other):
        return self._myadd(other, False)
Martin Reinecke's avatar
Martin Reinecke committed
86
87

    def __radd__(self, other):
Martin Reinecke's avatar
Martin Reinecke committed
88
        return self._myadd(other, False)
Martin Reinecke's avatar
Martin Reinecke committed
89
90

    def __sub__(self, other):
Martin Reinecke's avatar
Martin Reinecke committed
91
        return self._myadd(other, True)
Martin Reinecke's avatar
Martin Reinecke committed
92
93
94
95
96
97
98

    def __rsub__(self, other):
        return (-self).__add__(other)

    def __mul__(self, other):
        from .sugar import makeOp
        if isinstance(other, Linearization):
Martin Reinecke's avatar
Martin Reinecke committed
99
100
            if self.target != other.target:
                raise ValueError("domain mismatch")
101
            return self.new(
Martin Reinecke's avatar
Martin Reinecke committed
102
                self._val*other._val,
Martin Reinecke's avatar
Martin Reinecke committed
103
104
                (makeOp(other._val)(self._jac))._myadd(
                 makeOp(self._val)(other._jac), False))
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
105
106
107
108
        if np.isscalar(other):
            if other == 1:
                return self
            met = None if self._metric is None else self._metric.scale(other)
109
            return self.new(self._val*other, self._jac.scale(other), met)
Martin Reinecke's avatar
Martin Reinecke committed
110
        if isinstance(other, (Field, MultiField)):
Martin Reinecke's avatar
Martin Reinecke committed
111
112
            if self.target != other.domain:
                raise ValueError("domain mismatch")
113
            return self.new(self._val*other, makeOp(other)(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
114
115

    def __rmul__(self, other):
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
116
        return self.__mul__(other)
Martin Reinecke's avatar
Martin Reinecke committed
117

Martin Reinecke's avatar
Martin Reinecke committed
118
    def vdot(self, other):
Martin Reinecke's avatar
Martin Reinecke committed
119
        from .operators.simple_linear_operators import VdotOperator
Martin Reinecke's avatar
Martin Reinecke committed
120
        if isinstance(other, (Field, MultiField)):
121
            return self.new(
Martin Reinecke's avatar
Martin Reinecke committed
122
                Field.scalar(self._val.vdot(other)),
Martin Reinecke's avatar
Martin Reinecke committed
123
                VdotOperator(other)(self._jac))
124
        return self.new(
Martin Reinecke's avatar
Martin Reinecke committed
125
            Field.scalar(self._val.vdot(other._val)),
Martin Reinecke's avatar
Martin Reinecke committed
126
127
            VdotOperator(self._val)(other._jac) +
            VdotOperator(other._val)(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
128

Martin Reinecke's avatar
Martin Reinecke committed
129
    def sum(self):
Martin Reinecke's avatar
Martin Reinecke committed
130
        from .operators.simple_linear_operators import SumReductionOperator
131
        return self.new(
Martin Reinecke's avatar
Martin Reinecke committed
132
            Field.scalar(self._val.sum()),
Martin Reinecke's avatar
Martin Reinecke committed
133
            SumReductionOperator(self._jac.target)(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
134
135
136

    def exp(self):
        tmp = self._val.exp()
137
        return self.new(tmp, makeOp(tmp)(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
138
139
140

    def log(self):
        tmp = self._val.log()
141
        return self.new(tmp, makeOp(1./self._val)(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
142

Martin Reinecke's avatar
Martin Reinecke committed
143
144
    def tanh(self):
        tmp = self._val.tanh()
145
        return self.new(tmp, makeOp(1.-tmp**2)(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
146
147
148
149

    def positive_tanh(self):
        tmp = self._val.tanh()
        tmp2 = 0.5*(1.+tmp)
150
        return self.new(tmp2, makeOp(0.5*(1.-tmp**2))(self._jac))
Martin Reinecke's avatar
Martin Reinecke committed
151

Martin Reinecke's avatar
Martin Reinecke committed
152
    def add_metric(self, metric):
153
        return self.new(self._val, self._jac, metric)
Martin Reinecke's avatar
Martin Reinecke committed
154
155

    @staticmethod
156
    def make_var(field, want_metric=False):
Martin Reinecke's avatar
Martin Reinecke committed
157
        from .operators.scaling_operator import ScalingOperator
158
159
        return Linearization(field, ScalingOperator(1., field.domain),
                             want_metric=want_metric)
Martin Reinecke's avatar
Martin Reinecke committed
160
161

    @staticmethod
162
    def make_const(field, want_metric=False):
Martin Reinecke's avatar
Martin Reinecke committed
163
        from .operators.simple_linear_operators import NullOperator
164
165
        return Linearization(field, NullOperator(field.domain, field.domain),
                             want_metric=want_metric)