-
Notifications
You must be signed in to change notification settings - Fork 707
/
linear_operator.h
1582 lines (1378 loc) · 49.6 KB
/
linear_operator.h
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2015 - 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
#ifndef dealii_linear_operator_h
#define dealii_linear_operator_h
#include <deal.II/base/config.h>
#include <deal.II/base/exceptions.h>
#include <deal.II/lac/vector_memory.h>
#include <array>
#include <functional>
#include <type_traits>
DEAL_II_NAMESPACE_OPEN
// Forward declarations:
#ifndef DOXYGEN
namespace internal
{
namespace LinearOperatorImplementation
{
class EmptyPayload;
}
} // namespace internal
template <typename Number>
class Vector;
class PreconditionIdentity;
template <typename Range = Vector<double>,
typename Domain = Range,
typename Payload =
internal::LinearOperatorImplementation::EmptyPayload>
class LinearOperator;
#endif
template <
typename Range = Vector<double>,
typename Domain = Range,
typename Payload = internal::LinearOperatorImplementation::EmptyPayload,
typename OperatorExemplar,
typename Matrix>
LinearOperator<Range, Domain, Payload>
linear_operator(const OperatorExemplar &, const Matrix &);
template <
typename Range = Vector<double>,
typename Domain = Range,
typename Payload = internal::LinearOperatorImplementation::EmptyPayload,
typename Matrix>
LinearOperator<Range, Domain, Payload>
linear_operator(const Matrix &);
template <
typename Range = Vector<double>,
typename Domain = Range,
typename Payload = internal::LinearOperatorImplementation::EmptyPayload>
LinearOperator<Range, Domain, Payload>
null_operator(const LinearOperator<Range, Domain, Payload> &);
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
identity_operator(const LinearOperator<Range, Domain, Payload> &);
/**
* A class to store the abstract concept of a linear operator.
*
* The class essentially consists of <code>std::function</code> objects that
* store the knowledge of how to apply the linear operator by implementing the
* abstract @p Matrix interface:
* @code
* std::function<void(Range &, const Domain &)> vmult;
* std::function<void(Range &, const Domain &)> vmult_add;
* std::function<void(Domain &, const Range &)> Tvmult;
* std::function<void(Domain &, const Range &)> Tvmult_add;
* @endcode
*
* But, in contrast to a usual matrix object, the domain and range of the
* linear operator are also bound to the LinearOperator class on the type
* level. Because of this, `LinearOperator<Range, Domain>` has two
* additional function objects
* @code
* std::function<void(Range &, bool)> reinit_range_vector;
* std::function<void(Domain &, bool)> reinit_domain_vector;
* @endcode
* that store the knowledge how to initialize (resize + internal data
* structures) an arbitrary vector of the @p Range and @p Domain space.
*
* The primary purpose of this class is to provide syntactic sugar for complex
* matrix-vector operations and free the user from having to create, set up
* and handle intermediate storage locations by hand.
*
* As an example consider the operation $(A+k\,B)\,C$, where $A$, $B$ and $C$
* denote (possible different) matrices. In order to construct a
* LinearOperator <code>op</code> that stores the knowledge of this operation,
* one can write:
*
* @code
* #include <deal.II/lac/linear_operator_tools.h>
*
* dealii::SparseMatrix<double> A, B, C;
* const double k = ...;
*
* // Setup and assembly of matrices
*
* const auto op_a = linear_operator(A);
* const auto op_b = linear_operator(B);
* const auto op_c = linear_operator(C);
*
* const auto op = (op_a + k * op_b) * op_c;
* @endcode
*
* @note This class makes heavy use of <code>std::function</code> objects and
* lambda functions. This flexibility comes with a run-time penalty. Only use
* this object to encapsulate matrix object of medium to large size (as a rule
* of thumb, sparse matrices with a size $1000\times1000$, or larger).
*
* @note In order to use Trilinos or PETSc sparse matrices and preconditioners
* in conjunction with the LinearOperator class, it is necessary to extend the
* functionality of the LinearOperator class by means of an additional Payload.
*
* For example: LinearOperator instances representing matrix inverses usually
* require calling some linear solver. These solvers may not have interfaces
* to the LinearOperator (which, for example, may represent a composite
* operation). The
* TrilinosWrappers::internal::LinearOperatorImplementation::TrilinosPayload
* therefore provides an interface extension to the LinearOperator so that it
* can be passed to the solver and used by the solver as if it were a Trilinos
* operator. This implies that all of the necessary functionality of the
* specific Trilinos operator has been overloaded within the Payload class.
* This includes operator-vector multiplication and inverse operator-vector
* multiplication, where the operator can be either a
* TrilinosWrappers::SparseMatrix or a TrilinosWrappers::PreconditionBase
* and the vector is a native Trilinos vector.
*
* Another case where payloads provide a crucial supplement to the
* LinearOperator class are when composite operations are constructed (via
* operator overloading). In this instance, it is again necessary to provide
* an interface that produces the result of this composite operation that is
* compatible with Trilinos operator used by Trilinos solvers.
*
* @note Many use cases of LinearOperator lead to intermediate expressions
* requiring a PackagedOperation. In order to include all necessary header
* files in one go consider using
* @code
* #include <deal.II/lac/linear_operator_tools.h>
* @endcode
*
* In order to use the full LinearOperator and PackagedOperation
*
* @note To ensure that the correct payload is provided, wrapper functions
* for linear operators have been provided within the respective
* TrilinosWrappers (and, in the future, PETScWrappers) namespaces.
*
* <h3> Examples of use </h3>
* The step-20 tutorial program has a detailed usage example of the
* LinearOperator class.
*
* <h3> Instrumenting operations </h3>
* It is sometimes useful to know when functions are called, or to inject
* additional operations. In such cases, what one wants is to replace, for
* example, the `vmult` object of this class with one that does the additional
* operations and then calls what was originally supposed to happen. This
* can be done with commands such as the following:
* @code
* auto A_inv = inverse_operator(A, solver_A, preconditioner_A);
* A_inv.vmult = [base_vmult = A_inv.vmult](Vector<double> &dst,
* const Vector<double> &src) {
* std::cout << "Calling A_inv.vmult()" << std::endl;
* base_vmult(dst, src);
* };
* @endcode
* Here, we replace `A_inv.vmult` with a lambda function that first captures
* the previous value of `A_inv.vmult` and stores it in the `base_vmult`
* object. The newly installed `A_inv.vmult` function then first outputs some
* status information, and then calls the original functionality.
*
* This approach works for all of the other function objects mentioned above
* as well.
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
class LinearOperator : public Payload
{
public:
/**
* Create an empty LinearOperator object.
* When a payload is passed to this constructor, the resulting operator is
* constructed with a functional payload.
* In either case, this constructor yields an object that can not actually
* be used for any linear operator operations, and will throw an exception
* upon invocation.
*/
LinearOperator(const Payload &payload = Payload())
: Payload(payload)
, is_null_operator(false)
{
vmult = [](Range &, const Domain &) {
Assert(false,
ExcMessage("Uninitialized LinearOperator<Range, "
"Domain>::vmult called"));
};
vmult_add = [](Range &, const Domain &) {
Assert(false,
ExcMessage("Uninitialized LinearOperator<Range, "
"Domain>::vmult_add called"));
};
Tvmult = [](Domain &, const Range &) {
Assert(false,
ExcMessage("Uninitialized LinearOperator<Range, "
"Domain>::Tvmult called"));
};
Tvmult_add = [](Domain &, const Range &) {
Assert(false,
ExcMessage("Uninitialized LinearOperator<Range, "
"Domain>::Tvmult_add called"));
};
reinit_range_vector = [](Range &, bool) {
Assert(false,
ExcMessage("Uninitialized LinearOperator<Range, "
"Domain>::reinit_range_vector method called"));
};
reinit_domain_vector = [](Domain &, bool) {
Assert(false,
ExcMessage("Uninitialized LinearOperator<Range, "
"Domain>::reinit_domain_vector method called"));
};
}
/**
* Default copy constructor.
*/
LinearOperator(const LinearOperator<Range, Domain, Payload> &) = default;
/**
* Templated copy constructor that creates a LinearOperator object from an
* object @p op for which the conversion function
* <code>linear_operator</code> is defined.
*/
template <typename Op,
typename = std::enable_if_t<
!std::is_base_of_v<LinearOperator<Range, Domain, Payload>, Op>>>
LinearOperator(const Op &op)
{
*this = linear_operator<Range, Domain, Payload, Op>(op);
}
/**
* Default copy assignment operator.
*/
LinearOperator<Range, Domain, Payload> &
operator=(const LinearOperator<Range, Domain, Payload> &) = default;
/**
* Templated copy assignment operator for an object @p op for which the
* conversion function <code>linear_operator</code> is defined.
*/
template <typename Op,
typename = std::enable_if_t<
!std::is_base_of_v<LinearOperator<Range, Domain, Payload>, Op>>>
LinearOperator<Range, Domain, Payload> &
operator=(const Op &op)
{
*this = linear_operator<Range, Domain, Payload, Op>(op);
return *this;
}
/**
* Application of the LinearOperator object to a vector u of the @p Domain
* space giving a vector v of the @p Range space.
*/
std::function<void(Range &v, const Domain &u)> vmult;
/**
* Application of the LinearOperator object to a vector u of the @p Domain
* space. The result is added to the vector v.
*/
std::function<void(Range &v, const Domain &u)> vmult_add;
/**
* Application of the transpose LinearOperator object to a vector u of the
* @p Range space giving a vector v of the @p Domain space.
*/
std::function<void(Domain &v, const Range &u)> Tvmult;
/**
* Application of the transpose LinearOperator object to a vector @p u of
* the @p Range space.The result is added to the vector @p v.
*/
std::function<void(Domain &v, const Range &u)> Tvmult_add;
/**
* Initializes a vector v of the Range space to be directly usable as the
* destination parameter in an application of vmult. Similar to the reinit
* functions of the vector classes, the boolean determines whether a fast
* initialization is done, i.e., if it is set to false the content of the
* vector is set to 0.
*/
std::function<void(Range &v, bool omit_zeroing_entries)> reinit_range_vector;
/**
* Initializes a vector of the Domain space to be directly usable as the
* source parameter in an application of vmult. Similar to the reinit
* functions of the vector classes, the boolean determines whether a fast
* initialization is done, i.e., if it is set to false the content of the
* vector is set to 0.
*/
std::function<void(Domain &v, bool omit_zeroing_entries)>
reinit_domain_vector;
/**
* @name In-place vector space operations
*/
/** @{ */
/**
* Addition with a LinearOperator @p second_op with the same @p Domain and
* @p Range.
*/
LinearOperator<Range, Domain, Payload> &
operator+=(const LinearOperator<Range, Domain, Payload> &second_op)
{
*this = *this + second_op;
return *this;
}
/**
* Subtraction with a LinearOperator @p second_op with the same @p Domain
* and @p Range.
*/
LinearOperator<Range, Domain, Payload> &
operator-=(const LinearOperator<Range, Domain, Payload> &second_op)
{
*this = *this - second_op;
return *this;
}
/**
* Composition of the LinearOperator with an endomorphism @p second_op of
* the @p Domain space.
*/
LinearOperator<Range, Domain, Payload> &
operator*=(const LinearOperator<Domain, Domain, Payload> &second_op)
{
*this = *this * second_op;
return *this;
}
/**
* Scalar multiplication of the LinearOperator with @p number from the
* right.
*/
LinearOperator<Range, Domain, Payload>
operator*=(typename Domain::value_type number)
{
*this = *this * number;
return *this;
}
/**
* This bool is used to determine whether a linear operator is a null
* operator. In this case the class is able to optimize some operations like
* multiplication or addition.
*/
bool is_null_operator;
/** @} */
};
/**
* @name Vector space operations
*/
/** @{ */
/**
* @relatesalso LinearOperator
*
* Addition of two linear operators @p first_op and @p second_op given by
* $(\mathrm{first\_op}+\mathrm{second\_op})x \dealcoloneq \mathrm{first\_op}(x)
* + \mathrm{second\_op}(x)$
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
operator+(const LinearOperator<Range, Domain, Payload> &first_op,
const LinearOperator<Range, Domain, Payload> &second_op)
{
if (first_op.is_null_operator)
{
return second_op;
}
else if (second_op.is_null_operator)
{
return first_op;
}
else
{
LinearOperator<Range, Domain, Payload> return_op{
static_cast<const Payload &>(first_op) +
static_cast<const Payload &>(second_op)};
return_op.reinit_range_vector = first_op.reinit_range_vector;
return_op.reinit_domain_vector = first_op.reinit_domain_vector;
// ensure to have valid computation objects by catching first_op and
// second_op by value
return_op.vmult = [first_op, second_op](Range &v, const Domain &u) {
first_op.vmult(v, u);
second_op.vmult_add(v, u);
};
return_op.vmult_add = [first_op, second_op](Range &v, const Domain &u) {
first_op.vmult_add(v, u);
second_op.vmult_add(v, u);
};
return_op.Tvmult = [first_op, second_op](Domain &v, const Range &u) {
second_op.Tvmult(v, u);
first_op.Tvmult_add(v, u);
};
return_op.Tvmult_add = [first_op, second_op](Domain &v, const Range &u) {
second_op.Tvmult_add(v, u);
first_op.Tvmult_add(v, u);
};
return return_op;
}
}
/**
* @relatesalso LinearOperator
*
* Subtraction of two linear operators @p first_op and @p second_op given by
* $(\mathrm{first\_op}-\mathrm{second\_op})x \dealcoloneq \mathrm{first\_op}(x)
* - \mathrm{second\_op}(x)$
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
operator-(const LinearOperator<Range, Domain, Payload> &first_op,
const LinearOperator<Range, Domain, Payload> &second_op)
{
if (first_op.is_null_operator)
{
return -1. * second_op;
}
else if (second_op.is_null_operator)
{
return first_op;
}
else
{
// implement with addition and scalar multiplication
return first_op + (-1. * second_op);
}
}
/**
* @relatesalso LinearOperator
*
* Scalar multiplication of a ScalarOperator object @p op with @p number from
* the left.
*
* The @p Domain and @p Range types must implement the following
* <code>operator*=</code> member functions accepting the appropriate scalar
* Number type for rescaling:
*
* @code
* Domain & operator *=(Domain::value_type);
* Range & operator *=(Range::value_type);
* @endcode
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
operator*(typename Range::value_type number,
const LinearOperator<Range, Domain, Payload> &op)
{
static_assert(
std::is_convertible<typename Range::value_type,
typename Domain::value_type>::value,
"Range and Domain must have implicitly convertible 'value_type's");
if (op.is_null_operator)
{
return op;
}
else if (number == 0.)
{
return null_operator(op);
}
else
{
LinearOperator<Range, Domain, Payload> return_op = op;
// ensure to have valid computation objects by catching number and op by
// value
return_op.vmult = [number, op](Range &v, const Domain &u) {
op.vmult(v, u);
v *= number;
};
return_op.vmult_add = [number, op](Range &v, const Domain &u) {
v /= number;
op.vmult_add(v, u);
v *= number;
};
return_op.Tvmult = [number, op](Domain &v, const Range &u) {
op.Tvmult(v, u);
v *= number;
};
return_op.Tvmult_add = [number, op](Domain &v, const Range &u) {
v /= number;
op.Tvmult_add(v, u);
v *= number;
};
return return_op;
}
}
/**
* @relatesalso LinearOperator
*
* Scalar multiplication of a ScalarOperator object from the right.
*
* The @p Domain and @p Range types must implement the following
* <code>operator*=</code> member functions for rescaling:
*
* @code
* Domain & operator *=(Domain::value_type);
* Range & operator *=(Range::value_type);
* @endcode
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
operator*(const LinearOperator<Range, Domain, Payload> &op,
typename Domain::value_type number)
{
static_assert(
std::is_convertible<typename Range::value_type,
typename Domain::value_type>::value,
"Range and Domain must have implicitly convertible 'value_type's");
return number * op;
}
/** @} */
/**
* @name Composition and manipulation of a LinearOperator
*/
/** @{ */
/**
* @relatesalso LinearOperator
*
* Composition of two linear operators @p first_op and @p second_op given by
* $(\mathrm{first\_op}*\mathrm{second\_op})x \dealcoloneq
* \mathrm{first\_op}(\mathrm{second\_op}(x))$
*
* @ingroup LAOperators
*/
template <typename Range,
typename Intermediate,
typename Domain,
typename Payload>
LinearOperator<Range, Domain, Payload>
operator*(const LinearOperator<Range, Intermediate, Payload> &first_op,
const LinearOperator<Intermediate, Domain, Payload> &second_op)
{
if (first_op.is_null_operator || second_op.is_null_operator)
{
LinearOperator<Range, Domain, Payload> return_op;
return_op.reinit_domain_vector = second_op.reinit_domain_vector;
return_op.reinit_range_vector = first_op.reinit_range_vector;
return null_operator(return_op);
}
else
{
LinearOperator<Range, Domain, Payload> return_op{
static_cast<const Payload &>(first_op) *
static_cast<const Payload &>(second_op)};
return_op.reinit_domain_vector = second_op.reinit_domain_vector;
return_op.reinit_range_vector = first_op.reinit_range_vector;
// ensure to have valid computation objects by catching first_op and
// second_op by value
return_op.vmult = [first_op, second_op](Range &v, const Domain &u) {
GrowingVectorMemory<Intermediate> vector_memory;
typename VectorMemory<Intermediate>::Pointer i(vector_memory);
second_op.reinit_range_vector(*i, /*bool omit_zeroing_entries =*/true);
second_op.vmult(*i, u);
first_op.vmult(v, *i);
};
return_op.vmult_add = [first_op, second_op](Range &v, const Domain &u) {
GrowingVectorMemory<Intermediate> vector_memory;
typename VectorMemory<Intermediate>::Pointer i(vector_memory);
second_op.reinit_range_vector(*i, /*bool omit_zeroing_entries =*/true);
second_op.vmult(*i, u);
first_op.vmult_add(v, *i);
};
return_op.Tvmult = [first_op, second_op](Domain &v, const Range &u) {
GrowingVectorMemory<Intermediate> vector_memory;
typename VectorMemory<Intermediate>::Pointer i(vector_memory);
first_op.reinit_domain_vector(*i, /*bool omit_zeroing_entries =*/true);
first_op.Tvmult(*i, u);
second_op.Tvmult(v, *i);
};
return_op.Tvmult_add = [first_op, second_op](Domain &v, const Range &u) {
GrowingVectorMemory<Intermediate> vector_memory;
typename VectorMemory<Intermediate>::Pointer i(vector_memory);
first_op.reinit_domain_vector(*i, /*bool omit_zeroing_entries =*/true);
first_op.Tvmult(*i, u);
second_op.Tvmult_add(v, *i);
};
return return_op;
}
}
/**
* @relatesalso LinearOperator
*
* Return the transpose linear operations of @p op.
*
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Domain, Range, Payload>
transpose_operator(const LinearOperator<Range, Domain, Payload> &op)
{
LinearOperator<Domain, Range, Payload> return_op{op.transpose_payload()};
return_op.reinit_range_vector = op.reinit_domain_vector;
return_op.reinit_domain_vector = op.reinit_range_vector;
return_op.vmult = op.Tvmult;
return_op.vmult_add = op.Tvmult_add;
return_op.Tvmult = op.vmult;
return_op.Tvmult_add = op.vmult_add;
return return_op;
}
/**
* @relatesalso LinearOperator
*
* Return an object representing the inverse of the LinearOperator @p op.
*
* The function takes references @p solver and @p preconditioner to an
* iterative solver and a preconditioner that are used in the
* <code>vmult</code> and <code>Tvmult</code> implementations of the
* LinearOperator object.
*
* The LinearOperator object that is created stores a reference to @p solver
* and @p preconditioner. Thus, both objects must remain a valid reference for
* the whole lifetime of the LinearOperator object. Internal data structures
* of the @p solver object will be modified upon invocation of
* <code>vmult</code> or <code>Tvmult</code>.
*
*
* @ingroup LAOperators
*/
template <typename Payload,
typename Solver,
typename Preconditioner,
typename Range = typename Solver::vector_type,
typename Domain = Range>
LinearOperator<Domain, Range, Payload>
inverse_operator(const LinearOperator<Range, Domain, Payload> &op,
Solver &solver,
const Preconditioner &preconditioner)
{
LinearOperator<Domain, Range, Payload> return_op{
op.inverse_payload(solver, preconditioner)};
return_op.reinit_range_vector = op.reinit_domain_vector;
return_op.reinit_domain_vector = op.reinit_range_vector;
return_op.vmult = [op, &solver, &preconditioner](Range &v, const Domain &u) {
op.reinit_range_vector(v, /*bool omit_zeroing_entries =*/false);
solver.solve(op, v, u, preconditioner);
};
return_op.vmult_add = [op, &solver, &preconditioner](Range &v,
const Domain &u) {
GrowingVectorMemory<Range> vector_memory;
typename VectorMemory<Range>::Pointer v2(vector_memory);
op.reinit_range_vector(*v2, /*bool omit_zeroing_entries =*/false);
solver.solve(op, *v2, u, preconditioner);
v += *v2;
};
return_op.Tvmult = [op, &solver, &preconditioner](Range &v, const Domain &u) {
op.reinit_range_vector(v, /*bool omit_zeroing_entries =*/false);
solver.solve(transpose_operator(op), v, u, preconditioner);
};
return_op.Tvmult_add = [op, &solver, &preconditioner](Range &v,
const Domain &u) {
GrowingVectorMemory<Range> vector_memory;
typename VectorMemory<Range>::Pointer v2(vector_memory);
op.reinit_range_vector(*v2, /*bool omit_zeroing_entries =*/false);
solver.solve(transpose_operator(op), *v2, u, preconditioner);
v += *v2;
};
return return_op;
}
/**
* @relatesalso LinearOperator
*
* Variant of above function that takes a LinearOperator @p preconditioner
* as preconditioner argument.
*
* @ingroup LAOperators
*/
template <typename Payload,
typename Solver,
typename Range = typename Solver::vector_type,
typename Domain = Range>
LinearOperator<Domain, Range, Payload>
inverse_operator(const LinearOperator<Range, Domain, Payload> &op,
Solver &solver,
const LinearOperator<Range, Domain, Payload> &preconditioner)
{
LinearOperator<Domain, Range, Payload> return_op{
op.inverse_payload(solver, preconditioner)};
return_op.reinit_range_vector = op.reinit_domain_vector;
return_op.reinit_domain_vector = op.reinit_range_vector;
return_op.vmult = [op, &solver, preconditioner](Range &v, const Domain &u) {
op.reinit_range_vector(v, /*bool omit_zeroing_entries =*/false);
solver.solve(op, v, u, preconditioner);
};
return_op.vmult_add = [op, &solver, preconditioner](Range &v,
const Domain &u) {
GrowingVectorMemory<Range> vector_memory;
typename VectorMemory<Range>::Pointer v2(vector_memory);
op.reinit_range_vector(*v2, /*bool omit_zeroing_entries =*/false);
solver.solve(op, *v2, u, preconditioner);
v += *v2;
};
return_op.Tvmult = [op, &solver, preconditioner](Range &v, const Domain &u) {
op.reinit_range_vector(v, /*bool omit_zeroing_entries =*/false);
solver.solve(transpose_operator(op), v, u, preconditioner);
};
return_op.Tvmult_add = [op, &solver, preconditioner](Range &v,
const Domain &u) {
GrowingVectorMemory<Range> vector_memory;
typename VectorMemory<Range>::Pointer v2(vector_memory);
op.reinit_range_vector(*v2, /*bool omit_zeroing_entries =*/false);
solver.solve(transpose_operator(op), *v2, u, preconditioner);
v += *v2;
};
return return_op;
}
/**
* @relatesalso LinearOperator
*
* Variant of above function without a preconditioner argument. In this
* case the identity_operator() of the @p op argument is used as a
* preconditioner. This is equivalent to using PreconditionIdentity.
*
* @ingroup LAOperators
*/
template <typename Payload,
typename Solver,
typename Range = typename Solver::vector_type,
typename Domain = Range>
LinearOperator<Domain, Range, Payload>
inverse_operator(const LinearOperator<Range, Domain, Payload> &op,
Solver &solver)
{
return inverse_operator(op, solver, identity_operator(op));
}
/**
* @relatesalso LinearOperator
*
* Special overload of above function that takes a PreconditionIdentity
* argument.
*
* @ingroup LAOperators
*/
template <typename Payload,
typename Solver,
typename Range = typename Solver::vector_type,
typename Domain = Range>
LinearOperator<Domain, Range, Payload>
inverse_operator(const LinearOperator<Range, Domain, Payload> &op,
Solver &solver,
const PreconditionIdentity &)
{
return inverse_operator(op, solver);
}
/** @} */
/**
* @name Creation of a LinearOperator
*/
/** @{ */
/**
* @relatesalso LinearOperator
*
* Return a LinearOperator that is the identity of the vector space @p Range.
*
* The function takes an <code>std::function</code> object @p reinit_vector as
* an argument to initialize the <code>reinit_range_vector</code> and
* <code>reinit_domain_vector</code> objects of the LinearOperator object.
*
* @ingroup LAOperators
*/
template <
typename Range,
typename Payload = internal::LinearOperatorImplementation::EmptyPayload>
LinearOperator<Range, Range, Payload>
identity_operator(const std::function<void(Range &, bool)> &reinit_vector)
{
LinearOperator<Range, Range, Payload> return_op{Payload()};
return_op.reinit_range_vector = reinit_vector;
return_op.reinit_domain_vector = reinit_vector;
return_op.vmult = [](Range &v, const Range &u) { v = u; };
return_op.vmult_add = [](Range &v, const Range &u) { v += u; };
return_op.Tvmult = [](Range &v, const Range &u) { v = u; };
return_op.Tvmult_add = [](Range &v, const Range &u) { v += u; };
return return_op;
}
/**
* @relatesalso LinearOperator
*
* Return a LinearOperator that is the identity of the vector space @p Range.
*
* The function takes a LinearOperator @p op and uses its range initializer
* to create an identity operator. In contrast to the function above, this
* function also ensures that the underlying Payload matches that of the
* input @p op.
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
identity_operator(const LinearOperator<Range, Domain, Payload> &op)
{
auto return_op = identity_operator<Range, Payload>(op.reinit_range_vector);
static_cast<Payload &>(return_op) = op.identity_payload();
return return_op;
}
/**
* @relatesalso LinearOperator
*
* Return a nulled variant of the LinearOperator @p op, i.e. with optimized
* LinearOperator::vmult, LinearOperator::vmult_add, etc. functions and with
* LinearOperator::is_null_operator set to true.
*
* @ingroup LAOperators
*/
template <typename Range, typename Domain, typename Payload>
LinearOperator<Range, Domain, Payload>
null_operator(const LinearOperator<Range, Domain, Payload> &op)
{
LinearOperator<Range, Domain, Payload> return_op{op.null_payload()};
return_op.is_null_operator = true;
return_op.reinit_range_vector = op.reinit_range_vector;
return_op.reinit_domain_vector = op.reinit_domain_vector;
return_op.vmult = [](Range &v, const Domain &) { v = 0.; };
return_op.vmult_add = [](Range &, const Domain &) {};
return_op.Tvmult = [](Domain &v, const Range &) { v = 0.; };
return_op.Tvmult_add = [](Domain &, const Range &) {};
return return_op;
}
/**
* @relatesalso LinearOperator
*
* Return a LinearOperator that acts as a mean value filter. The vmult()
* functions of this matrix subtract the mean values of the vector.
*
* The function takes an <code>std::function</code> object @p reinit_vector as
* an argument to initialize the <code>reinit_range_vector</code> and
* <code>reinit_domain_vector</code> objects of the LinearOperator object.
*
* @ingroup LAOperators
*/
template <
typename Range,
typename Payload = internal::LinearOperatorImplementation::EmptyPayload>
LinearOperator<Range, Range, Payload>
mean_value_filter(const std::function<void(Range &, bool)> &reinit_vector)
{
LinearOperator<Range, Range, Payload> return_op{Payload()};
return_op.reinit_range_vector = reinit_vector;
return_op.reinit_domain_vector = reinit_vector;
return_op.vmult = [](Range &v, const Range &u) {
const auto mean = u.mean_value();
v = u;
v.add(-mean);
};
return_op.vmult_add = [](Range &v, const Range &u) {
const auto mean = u.mean_value();
v += u;
v.add(-mean);
};
return_op.Tvmult = return_op.vmult_add;
return_op.Tvmult_add = return_op.vmult_add;
return return_op;
}