-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolve_registrations.py
214 lines (182 loc) · 8.4 KB
/
solve_registrations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Registrations for LinearOperator.solve."""
from tensorflow.python.ops.linalg import linear_operator
from tensorflow.python.ops.linalg import linear_operator_algebra
from tensorflow.python.ops.linalg import linear_operator_block_diag
from tensorflow.python.ops.linalg import linear_operator_circulant
from tensorflow.python.ops.linalg import linear_operator_composition
from tensorflow.python.ops.linalg import linear_operator_diag
from tensorflow.python.ops.linalg import linear_operator_identity
from tensorflow.python.ops.linalg import linear_operator_inversion
from tensorflow.python.ops.linalg import linear_operator_lower_triangular
from tensorflow.python.ops.linalg import registrations_util
# By default, use a LinearOperatorComposition to delay the computation.
@linear_operator_algebra.RegisterSolve(
linear_operator.LinearOperator, linear_operator.LinearOperator)
def _solve_linear_operator(linop_a, linop_b):
"""Generic solve of two `LinearOperator`s."""
is_square = registrations_util.is_square(linop_a, linop_b)
is_non_singular = None
is_self_adjoint = None
is_positive_definite = None
if is_square:
is_non_singular = registrations_util.combined_non_singular_hint(
linop_a, linop_b)
elif is_square is False: # pylint:disable=g-bool-id-comparison
is_non_singular = False
is_self_adjoint = False
is_positive_definite = False
return linear_operator_composition.LinearOperatorComposition(
operators=[
linear_operator_inversion.LinearOperatorInversion(linop_a),
linop_b
],
is_non_singular=is_non_singular,
is_self_adjoint=is_self_adjoint,
is_positive_definite=is_positive_definite,
is_square=is_square,
)
@linear_operator_algebra.RegisterSolve(
linear_operator_inversion.LinearOperatorInversion,
linear_operator.LinearOperator)
def _solve_inverse_linear_operator(linop_a, linop_b):
"""Solve inverse of generic `LinearOperator`s."""
return linop_a.operator.matmul(linop_b)
# Identity
@linear_operator_algebra.RegisterSolve(
linear_operator_identity.LinearOperatorIdentity,
linear_operator.LinearOperator)
def _solve_linear_operator_identity_left(identity, linop):
del identity
return linop
@linear_operator_algebra.RegisterSolve(
linear_operator.LinearOperator,
linear_operator_identity.LinearOperatorIdentity)
def _solve_linear_operator_identity_right(linop, identity):
del identity
return linop.inverse()
@linear_operator_algebra.RegisterSolve(
linear_operator_identity.LinearOperatorScaledIdentity,
linear_operator_identity.LinearOperatorScaledIdentity)
def _solve_linear_operator_scaled_identity(linop_a, linop_b):
"""Solve of two ScaledIdentity `LinearOperators`."""
return linear_operator_identity.LinearOperatorScaledIdentity(
num_rows=linop_a.domain_dimension_tensor(),
multiplier=linop_b.multiplier / linop_a.multiplier,
is_non_singular=registrations_util.combined_non_singular_hint(
linop_a, linop_b),
is_self_adjoint=registrations_util.combined_commuting_self_adjoint_hint(
linop_a, linop_b),
is_positive_definite=(
registrations_util.combined_commuting_positive_definite_hint(
linop_a, linop_b)),
is_square=True)
# Diag.
@linear_operator_algebra.RegisterSolve(
linear_operator_diag.LinearOperatorDiag,
linear_operator_diag.LinearOperatorDiag)
def _solve_linear_operator_diag(linop_a, linop_b):
return linear_operator_diag.LinearOperatorDiag(
diag=linop_b.diag / linop_a.diag,
is_non_singular=registrations_util.combined_non_singular_hint(
linop_a, linop_b),
is_self_adjoint=registrations_util.combined_commuting_self_adjoint_hint(
linop_a, linop_b),
is_positive_definite=(
registrations_util.combined_commuting_positive_definite_hint(
linop_a, linop_b)),
is_square=True)
@linear_operator_algebra.RegisterSolve(
linear_operator_diag.LinearOperatorDiag,
linear_operator_identity.LinearOperatorScaledIdentity)
def _solve_linear_operator_diag_scaled_identity_right(
linop_diag, linop_scaled_identity):
return linear_operator_diag.LinearOperatorDiag(
diag=linop_scaled_identity.multiplier / linop_diag.diag,
is_non_singular=registrations_util.combined_non_singular_hint(
linop_diag, linop_scaled_identity),
is_self_adjoint=registrations_util.combined_commuting_self_adjoint_hint(
linop_diag, linop_scaled_identity),
is_positive_definite=(
registrations_util.combined_commuting_positive_definite_hint(
linop_diag, linop_scaled_identity)),
is_square=True)
@linear_operator_algebra.RegisterSolve(
linear_operator_identity.LinearOperatorScaledIdentity,
linear_operator_diag.LinearOperatorDiag)
def _solve_linear_operator_diag_scaled_identity_left(
linop_scaled_identity, linop_diag):
return linear_operator_diag.LinearOperatorDiag(
diag=linop_diag.diag / linop_scaled_identity.multiplier,
is_non_singular=registrations_util.combined_non_singular_hint(
linop_diag, linop_scaled_identity),
is_self_adjoint=registrations_util.combined_commuting_self_adjoint_hint(
linop_diag, linop_scaled_identity),
is_positive_definite=(
registrations_util.combined_commuting_positive_definite_hint(
linop_diag, linop_scaled_identity)),
is_square=True)
@linear_operator_algebra.RegisterSolve(
linear_operator_diag.LinearOperatorDiag,
linear_operator_lower_triangular.LinearOperatorLowerTriangular)
def _solve_linear_operator_diag_tril(linop_diag, linop_triangular):
return linear_operator_lower_triangular.LinearOperatorLowerTriangular(
tril=linop_triangular.to_dense() / linop_diag.diag[..., None],
is_non_singular=registrations_util.combined_non_singular_hint(
linop_diag, linop_triangular),
# This is safe to do since the Triangular matrix is only self-adjoint
# when it is a diagonal matrix, and hence commutes.
is_self_adjoint=registrations_util.combined_commuting_self_adjoint_hint(
linop_diag, linop_triangular),
is_positive_definite=None,
is_square=True)
# Circulant.
# pylint: disable=protected-access
@linear_operator_algebra.RegisterSolve(
linear_operator_circulant._BaseLinearOperatorCirculant,
linear_operator_circulant._BaseLinearOperatorCirculant)
def _solve_linear_operator_circulant_circulant(linop_a, linop_b):
if not isinstance(linop_a, linop_b.__class__):
return _solve_linear_operator(linop_a, linop_b)
return linop_a.__class__(
spectrum=linop_b.spectrum / linop_a.spectrum,
is_non_singular=registrations_util.combined_non_singular_hint(
linop_a, linop_b),
is_self_adjoint=registrations_util.combined_commuting_self_adjoint_hint(
linop_a, linop_b),
is_positive_definite=(
registrations_util.combined_commuting_positive_definite_hint(
linop_a, linop_b)),
is_square=True)
# pylint: enable=protected-access
# Block Diag
@linear_operator_algebra.RegisterSolve(
linear_operator_block_diag.LinearOperatorBlockDiag,
linear_operator_block_diag.LinearOperatorBlockDiag)
def _solve_linear_operator_block_diag_block_diag(linop_a, linop_b):
return linear_operator_block_diag.LinearOperatorBlockDiag(
operators=[
o1.solve(o2) for o1, o2 in zip(
linop_a.operators, linop_b.operators)],
is_non_singular=registrations_util.combined_non_singular_hint(
linop_a, linop_b),
# In general, a solve of self-adjoint positive-definite block diagonal
# matrices is not self-=adjoint.
is_self_adjoint=None,
# In general, a solve of positive-definite block diagonal matrices is
# not positive-definite.
is_positive_definite=None,
is_square=True)