LinearOperator.cpp 14.8 KB
Newer Older
Tobias Lasser's avatar
Tobias Lasser committed
1
2
3
#include "LinearOperator.h"

#include <stdexcept>
Tobias Lasser's avatar
Tobias Lasser committed
4
#include <typeinfo>
Tobias Lasser's avatar
Tobias Lasser committed
5

6
7
#include "DescriptorUtils.h"

Tobias Lasser's avatar
Tobias Lasser committed
8
9
10
namespace elsa
{
    template <typename data_t>
Jens Petit's avatar
Jens Petit committed
11
12
    LinearOperator<data_t>::LinearOperator(const DataDescriptor& domainDescriptor,
                                           const DataDescriptor& rangeDescriptor)
Tobias Lasser's avatar
Tobias Lasser committed
13
        : _domainDescriptor{domainDescriptor.clone()}, _rangeDescriptor{rangeDescriptor.clone()}
Jens Petit's avatar
Jens Petit committed
14
15
    {
    }
Tobias Lasser's avatar
Tobias Lasser committed
16

Tobias Lasser's avatar
Tobias Lasser committed
17
18
    template <typename data_t>
    LinearOperator<data_t>::LinearOperator(const LinearOperator<data_t>& other)
19
20
        : Cloneable<LinearOperator<data_t>>(),
          _domainDescriptor{other._domainDescriptor->clone()},
Jens Petit's avatar
Jens Petit committed
21
          _rangeDescriptor{other._rangeDescriptor->clone()},
22
          _scalar{other._scalar},
Jens Petit's avatar
Jens Petit committed
23
24
25
26
          _isLeaf{other._isLeaf},
          _isAdjoint{other._isAdjoint},
          _isComposite{other._isComposite},
          _mode{other._mode}
Tobias Lasser's avatar
Tobias Lasser committed
27
28
29
30
31
    {
        if (_isLeaf)
            _lhs = other._lhs->clone();

        if (_isComposite) {
32
33
34
35
36
37
38
39
            if (_mode == CompositeMode::ADD || _mode == CompositeMode::MULT) {
                _lhs = other._lhs->clone();
                _rhs = other._rhs->clone();
            }

            if (_mode == CompositeMode::SCALAR_MULT) {
                _rhs = other._rhs->clone();
            }
Tobias Lasser's avatar
Tobias Lasser committed
40
41
42
43
44
45
46
47
48
        }
    }

    template <typename data_t>
    LinearOperator<data_t>& LinearOperator<data_t>::operator=(const LinearOperator<data_t>& other)
    {
        if (*this != other) {
            _domainDescriptor = other._domainDescriptor->clone();
            _rangeDescriptor = other._rangeDescriptor->clone();
49
            _scalar = other._scalar;
Tobias Lasser's avatar
Tobias Lasser committed
50
51
52
53
54
55
56
57
58
            _isLeaf = other._isLeaf;
            _isAdjoint = other._isAdjoint;
            _isComposite = other._isComposite;
            _mode = other._mode;

            if (_isLeaf)
                _lhs = other._lhs->clone();

            if (_isComposite) {
59
60
61
62
63
64
65
66
                if (_mode == CompositeMode::ADD || _mode == CompositeMode::MULT) {
                    _lhs = other._lhs->clone();
                    _rhs = other._rhs->clone();
                }

                if (_mode == CompositeMode::SCALAR_MULT) {
                    _rhs = other._rhs->clone();
                }
Tobias Lasser's avatar
Tobias Lasser committed
67
68
69
70
71
72
            }
        }

        return *this;
    }

Tobias Lasser's avatar
Tobias Lasser committed
73
74
75
76
77
78
79
80
81
82
83
84
85
    template <typename data_t>
    const DataDescriptor& LinearOperator<data_t>::getDomainDescriptor() const
    {
        return *_domainDescriptor;
    }

    template <typename data_t>
    const DataDescriptor& LinearOperator<data_t>::getRangeDescriptor() const
    {
        return *_rangeDescriptor;
    }

    template <typename data_t>
86
    DataContainer<data_t> LinearOperator<data_t>::apply(const DataContainer<data_t>& x) const
Tobias Lasser's avatar
Tobias Lasser committed
87
    {
88
        DataContainer<data_t> result(*_rangeDescriptor, x.getDataHandlerType());
Tobias Lasser's avatar
Tobias Lasser committed
89
90
91
        apply(x, result);
        return result;
    }
Jens Petit's avatar
Jens Petit committed
92

Tobias Lasser's avatar
Tobias Lasser committed
93
    template <typename data_t>
Jens Petit's avatar
Jens Petit committed
94
95
    void LinearOperator<data_t>::apply(const DataContainer<data_t>& x,
                                       DataContainer<data_t>& Ax) const
Tobias Lasser's avatar
Tobias Lasser committed
96
    {
97
        applyImpl(x, Ax);
Tobias Lasser's avatar
Tobias Lasser committed
98
99
100
    }

    template <typename data_t>
101
102
    void LinearOperator<data_t>::applyImpl(const DataContainer<data_t>& x,
                                           DataContainer<data_t>& Ax) const
Tobias Lasser's avatar
Tobias Lasser committed
103
    {
Tobias Lasser's avatar
Tobias Lasser committed
104
105
106
        if (_isLeaf) {
            if (_isAdjoint) {
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
107
108
                if (_lhs->getRangeDescriptor().getNumberOfCoefficients() != x.getSize()
                    || _lhs->getDomainDescriptor().getNumberOfCoefficients() != Ax.getSize())
109
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
110
                        "LinearOperator::apply: incorrect input/output sizes for adjoint leaf");
Tobias Lasser's avatar
Tobias Lasser committed
111
112
113

                _lhs->applyAdjoint(x, Ax);
                return;
Jens Petit's avatar
Jens Petit committed
114
            } else {
Tobias Lasser's avatar
Tobias Lasser committed
115
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
116
117
                if (_lhs->getDomainDescriptor().getNumberOfCoefficients() != x.getSize()
                    || _lhs->getRangeDescriptor().getNumberOfCoefficients() != Ax.getSize())
118
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
119
                        "LinearOperator::apply: incorrect input/output sizes for leaf");
Tobias Lasser's avatar
Tobias Lasser committed
120
121
122
123

                _lhs->apply(x, Ax);
                return;
            }
Tobias Lasser's avatar
Tobias Lasser committed
124
125
126
        }

        if (_isComposite) {
127
            if (_mode == CompositeMode::ADD) {
Tobias Lasser's avatar
Tobias Lasser committed
128
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
129
130
131
132
                if (_rhs->getDomainDescriptor().getNumberOfCoefficients() != x.getSize()
                    || _rhs->getRangeDescriptor().getNumberOfCoefficients() != Ax.getSize()
                    || _lhs->getDomainDescriptor().getNumberOfCoefficients() != x.getSize()
                    || _lhs->getRangeDescriptor().getNumberOfCoefficients() != Ax.getSize())
133
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
134
                        "LinearOperator::apply: incorrect input/output sizes for add leaf");
Tobias Lasser's avatar
Tobias Lasser committed
135
136
137
138
139
140

                _rhs->apply(x, Ax);
                Ax += _lhs->apply(x);
                return;
            }

141
            if (_mode == CompositeMode::MULT) {
Tobias Lasser's avatar
Tobias Lasser committed
142
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
143
144
                if (_rhs->getDomainDescriptor().getNumberOfCoefficients() != x.getSize()
                    || _lhs->getRangeDescriptor().getNumberOfCoefficients() != Ax.getSize())
145
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
146
                        "LinearOperator::apply: incorrect input/output sizes for mult leaf");
Tobias Lasser's avatar
Tobias Lasser committed
147

148
                DataContainer<data_t> temp(_rhs->getRangeDescriptor(), x.getDataHandlerType());
Tobias Lasser's avatar
Tobias Lasser committed
149
150
151
152
                _rhs->apply(x, temp);
                _lhs->apply(temp, Ax);
                return;
            }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

            if (_mode == CompositeMode::SCALAR_MULT) {
                // sanity check the arguments for the intended evaluation tree leaf operation
                if (_rhs->getDomainDescriptor().getNumberOfCoefficients() != x.getSize())
                    throw InvalidArgumentError("LinearOperator::apply: incorrect input/output "
                                               "sizes for scalar mult. leaf");
                // sanity check the scalar in the optional
                if (!_scalar.has_value())
                    throw InvalidArgumentError(
                        "LinearOperator::apply: no value found in the scalar optional");

                _rhs->apply(x, Ax);
                Ax *= _scalar.value();
                return;
            }
Tobias Lasser's avatar
Tobias Lasser committed
168
169
        }

170
        throw LogicError("LinearOperator: apply called on ill-formed object");
Tobias Lasser's avatar
Tobias Lasser committed
171
172
173
    }

    template <typename data_t>
174
    DataContainer<data_t> LinearOperator<data_t>::applyAdjoint(const DataContainer<data_t>& y) const
Tobias Lasser's avatar
Tobias Lasser committed
175
    {
176
        DataContainer<data_t> result(*_domainDescriptor, y.getDataHandlerType());
Tobias Lasser's avatar
Tobias Lasser committed
177
178
179
        applyAdjoint(y, result);
        return result;
    }
Jens Petit's avatar
Jens Petit committed
180

Tobias Lasser's avatar
Tobias Lasser committed
181
    template <typename data_t>
Jens Petit's avatar
Jens Petit committed
182
183
    void LinearOperator<data_t>::applyAdjoint(const DataContainer<data_t>& y,
                                              DataContainer<data_t>& Aty) const
Tobias Lasser's avatar
Tobias Lasser committed
184
    {
185
        applyAdjointImpl(y, Aty);
Tobias Lasser's avatar
Tobias Lasser committed
186
187
188
    }

    template <typename data_t>
189
190
    void LinearOperator<data_t>::applyAdjointImpl(const DataContainer<data_t>& y,
                                                  DataContainer<data_t>& Aty) const
Tobias Lasser's avatar
Tobias Lasser committed
191
    {
Tobias Lasser's avatar
Tobias Lasser committed
192
193
194
        if (_isLeaf) {
            if (_isAdjoint) {
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
195
196
                if (_lhs->getDomainDescriptor().getNumberOfCoefficients() != y.getSize()
                    || _lhs->getRangeDescriptor().getNumberOfCoefficients() != Aty.getSize())
197
198
                    throw InvalidArgumentError("LinearOperator::applyAdjoint: incorrect "
                                               "input/output sizes for adjoint leaf");
Tobias Lasser's avatar
Tobias Lasser committed
199
200
201

                _lhs->apply(y, Aty);
                return;
Jens Petit's avatar
Jens Petit committed
202
            } else {
Tobias Lasser's avatar
Tobias Lasser committed
203
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
204
205
                if (_lhs->getRangeDescriptor().getNumberOfCoefficients() != y.getSize()
                    || _lhs->getDomainDescriptor().getNumberOfCoefficients() != Aty.getSize())
206
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
207
                        "LinearOperator::applyAdjoint: incorrect input/output sizes for leaf");
Tobias Lasser's avatar
Tobias Lasser committed
208
209
210
211

                _lhs->applyAdjoint(y, Aty);
                return;
            }
Tobias Lasser's avatar
Tobias Lasser committed
212
213
214
        }

        if (_isComposite) {
215
            if (_mode == CompositeMode::ADD) {
Tobias Lasser's avatar
Tobias Lasser committed
216
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
217
218
219
220
                if (_rhs->getRangeDescriptor().getNumberOfCoefficients() != y.getSize()
                    || _rhs->getDomainDescriptor().getNumberOfCoefficients() != Aty.getSize()
                    || _lhs->getRangeDescriptor().getNumberOfCoefficients() != y.getSize()
                    || _lhs->getDomainDescriptor().getNumberOfCoefficients() != Aty.getSize())
221
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
222
                        "LinearOperator::applyAdjoint: incorrect input/output sizes for add leaf");
Tobias Lasser's avatar
Tobias Lasser committed
223
224
225
226
227
228

                _rhs->applyAdjoint(y, Aty);
                Aty += _lhs->applyAdjoint(y);
                return;
            }

229
            if (_mode == CompositeMode::MULT) {
Tobias Lasser's avatar
Tobias Lasser committed
230
                // sanity check the arguments for the intended evaluation tree leaf operation
Jens Petit's avatar
Jens Petit committed
231
232
                if (_lhs->getRangeDescriptor().getNumberOfCoefficients() != y.getSize()
                    || _rhs->getDomainDescriptor().getNumberOfCoefficients() != Aty.getSize())
233
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
234
                        "LinearOperator::applyAdjoint: incorrect input/output sizes for mult leaf");
Tobias Lasser's avatar
Tobias Lasser committed
235

236
                DataContainer<data_t> temp(_lhs->getDomainDescriptor(), y.getDataHandlerType());
Tobias Lasser's avatar
Tobias Lasser committed
237
238
239
240
                _lhs->applyAdjoint(y, temp);
                _rhs->applyAdjoint(temp, Aty);
                return;
            }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

            if (_mode == CompositeMode::SCALAR_MULT) {
                // sanity check the arguments for the intended evaluation tree leaf operation
                if (_rhs->getRangeDescriptor().getNumberOfCoefficients() != y.getSize())
                    throw InvalidArgumentError("LinearOperator::apply: incorrect input/output "
                                               "sizes for scalar mult. leaf");
                // sanity check the scalar in the optional
                if (!_scalar.has_value())
                    throw InvalidArgumentError(
                        "LinearOperator::apply: no value found in the scalar optional");

                _rhs->applyAdjoint(y, Aty);
                Aty *= _scalar.value();
                return;
            }
Tobias Lasser's avatar
Tobias Lasser committed
256
257
        }

258
        throw LogicError("LinearOperator: applyAdjoint called on ill-formed object");
Tobias Lasser's avatar
Tobias Lasser committed
259
260
261
262
263
    }

    template <typename data_t>
    LinearOperator<data_t>* LinearOperator<data_t>::cloneImpl() const
    {
Tobias Lasser's avatar
Tobias Lasser committed
264
265
        if (_isLeaf)
            return new LinearOperator<data_t>(*_lhs, _isAdjoint);
Tobias Lasser's avatar
Tobias Lasser committed
266

267
268
269
270
271
272
        if (_isComposite) {
            if (_mode == CompositeMode::ADD || _mode == CompositeMode::MULT) {
                return new LinearOperator<data_t>(*_lhs, *_rhs, _mode);
            }

            if (_mode == CompositeMode::SCALAR_MULT) {
273
                return new LinearOperator<data_t>(*this);
274
275
            }
        }
Tobias Lasser's avatar
Tobias Lasser committed
276
277
278
279
280
281
282

        return new LinearOperator<data_t>(*_domainDescriptor, *_rangeDescriptor);
    }

    template <typename data_t>
    bool LinearOperator<data_t>::isEqual(const LinearOperator<data_t>& other) const
    {
283
284
285
        if (typeid(other) != typeid(*this))
            return false;

Jens Petit's avatar
Jens Petit committed
286
287
        if (*_domainDescriptor != *other._domainDescriptor
            || *_rangeDescriptor != *other._rangeDescriptor)
Tobias Lasser's avatar
Tobias Lasser committed
288
289
            return false;

290
291
292
        if (_isLeaf ^ other._isLeaf || _isComposite ^ other._isComposite)
            return false;

Tobias Lasser's avatar
Tobias Lasser committed
293
        if (_isLeaf)
294
            return (_isAdjoint == other._isAdjoint) && (*_lhs == *other._lhs);
Tobias Lasser's avatar
Tobias Lasser committed
295

296
297
298
299
300
301
302
303
304
        if (_isComposite) {
            if (_mode == CompositeMode::ADD || _mode == CompositeMode::MULT) {
                return _mode == other._mode && (*_lhs == *other._lhs) && (*_rhs == *other._rhs);
            }

            if (_mode == CompositeMode::SCALAR_MULT) {
                return (_isAdjoint == other._isAdjoint) && (*_rhs == *other._rhs);
            }
        }
Tobias Lasser's avatar
Tobias Lasser committed
305
306
307
308
309

        return true;
    }

    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
310
    LinearOperator<data_t>::LinearOperator(const LinearOperator<data_t>& op, bool isAdjoint)
Jens Petit's avatar
Jens Petit committed
311
312
313
314
315
        : _domainDescriptor{(isAdjoint) ? op.getRangeDescriptor().clone()
                                        : op.getDomainDescriptor().clone()},
          _rangeDescriptor{(isAdjoint) ? op.getDomainDescriptor().clone()
                                       : op.getRangeDescriptor().clone()},
          _lhs{op.clone()},
316
          _scalar{op._scalar},
Jens Petit's avatar
Jens Petit committed
317
318
          _isLeaf{true},
          _isAdjoint{isAdjoint}
Tobias Lasser's avatar
Tobias Lasser committed
319
320
    {
    }
Tobias Lasser's avatar
Tobias Lasser committed
321
322
323

    template <typename data_t>
    LinearOperator<data_t>::LinearOperator(const LinearOperator<data_t>& lhs,
324
                                           const LinearOperator<data_t>& rhs, CompositeMode mode)
325
326
        : _domainDescriptor{mode == CompositeMode::MULT
                                ? rhs.getDomainDescriptor().clone()
327
328
329
330
                                : bestCommon(*lhs._domainDescriptor, *rhs._domainDescriptor)},
          _rangeDescriptor{mode == CompositeMode::MULT
                               ? lhs.getRangeDescriptor().clone()
                               : bestCommon(*lhs._rangeDescriptor, *rhs._rangeDescriptor)},
Jens Petit's avatar
Jens Petit committed
331
332
333
334
          _lhs{lhs.clone()},
          _rhs{rhs.clone()},
          _isComposite{true},
          _mode{mode}
Tobias Lasser's avatar
Tobias Lasser committed
335
336
337
    {
        // sanity check the descriptors
        switch (_mode) {
338
            case CompositeMode::ADD:
339
                /// feasibility checked by bestCommon()
Tobias Lasser's avatar
Tobias Lasser committed
340
341
                break;

342
            case CompositeMode::MULT:
Tobias Lasser's avatar
Tobias Lasser committed
343
                // for multiplication, domain of _lhs should match range of _rhs
344
345
                if (_lhs->getDomainDescriptor().getNumberOfCoefficients()
                    != _rhs->getRangeDescriptor().getNumberOfCoefficients())
346
                    throw InvalidArgumentError(
Jens Petit's avatar
Jens Petit committed
347
                        "LinearOperator: composite mult domain/range mismatch");
Tobias Lasser's avatar
Tobias Lasser committed
348
349
350
                break;

            default:
351
                throw LogicError("LinearOperator: unknown composition mode");
Tobias Lasser's avatar
Tobias Lasser committed
352
353
354
        }
    }

355
356
357
358
359
360
361
362
363
364
365
    template <typename data_t>
    LinearOperator<data_t>::LinearOperator(data_t scalar, const LinearOperator<data_t>& rhs)
        : _domainDescriptor{rhs.getDomainDescriptor().clone()},
          _rangeDescriptor{rhs.getRangeDescriptor().clone()},
          _rhs{rhs.clone()},
          _scalar{scalar},
          _isComposite{true},
          _mode{CompositeMode::SCALAR_MULT}
    {
    }

Tobias Lasser's avatar
Tobias Lasser committed
366
367
368
    // ------------------------------------------
    // explicit template instantiation
    template class LinearOperator<float>;
369
    template class LinearOperator<complex<float>>;
Tobias Lasser's avatar
Tobias Lasser committed
370
    template class LinearOperator<double>;
371
    template class LinearOperator<complex<double>>;
Tobias Lasser's avatar
Tobias Lasser committed
372
373

} // namespace elsa