ug4
Loading...
Searching...
No Matches
operations_transform.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 * Author: Martin Rupp
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33
34#ifndef __H__UG__LIB_ALGEBRA__OPERATIONS_TRANSFORM__
35#define __H__UG__LIB_ALGEBRA__OPERATIONS_TRANSFORM__
36
37// here we transform a Template Expression x = X1 [+/- X2 [+/- X3]] into
38// a function like VecScaleAdd (in operations_vec.h) or MatMultAdd (in operations_mat.h),
39// where Xi can be Matrix*Vector, double*Matrix*vector, double*vector or vector.
40// x += X1 [+/- X2] and x -= X1 [+/- X2] also possible.
41// todo: be careful with x left and right. check.
43
44namespace ug
45{
46// 3 Operants
47//--------------
48
50template<typename vector_t, typename matrix_t, typename vector_t2>
51inline void VectorAssign(vector_t &dest, double alpha1, const MatVec_Expression<matrix_t, vector_t> &m1,
52 double alpha2, const MatVec_Expression<matrix_t, vector_t> &m2,
53 double alpha3, const vector_t2 &v3)
54{
55 MatMultAdd(dest, alpha1*m1.alpha, m1.l, m1.r, alpha2*m2.alpha, m2.l, m2.r, alpha3*getScaling(v3), getVector(v3));
56}
57
59template<typename vector_t, typename matrix_t, typename vector_t2, typename vector_t3>
60inline void VectorAssign(vector_t &dest, double alpha1, const MatVec_Expression<matrix_t, vector_t> &m1,
61 double alpha2, const vector_t2 &v2,
62 double alpha3, const vector_t3 &v3)
63{
64 MatMultAdd(dest, alpha1*m1.alpha, m1.l, m1.r, getScaling(v2)*alpha2, getVector(v2), getScaling(v3)*alpha3, getVector(v3));
65}
66
71template<typename vector_t, typename T0, typename matrix_t, typename vector_t2>
72inline void VectorAssign(vector_t &dest,
73 double alpha0, const T0 &t0,
74 double alpha1, const vector_t2 &v1,
75 double alpha2, const MatVec_Expression<matrix_t, vector_t> &m2)
76{
77 VectorAssign(dest, alpha0, t0, alpha2, m2, alpha1, v1);
78}
79
80
85template<typename vector_t, typename matrix_t, typename vector_t2, typename T3>
86inline void VectorAssign(vector_t &dest,
87 double alpha1, const vector_t2 &v1,
88 double alpha2, const MatVec_Expression<matrix_t, vector_t> &m2,
89 double alpha3, const T3 &t3)
90{
91 VectorAssign(dest, alpha2, m2, alpha1, v1, alpha3, t3);
92}
93
95template<typename vector_t, typename vector_t1, typename vector_t2, typename vector_t3>
96inline void VectorAssign(vector_t &dest,
97 double alpha1, const vector_t1 &v1,
98 double alpha2, const vector_t2 &v2,
99 double alpha3, const vector_t3 &v3)
100
101{
102 VecScaleAdd(dest, getScaling(v1)*alpha1, getVector(v1), getScaling(v2)*alpha2, getVector(v2), getScaling(v3)*alpha3, getVector(v3));
103}
104
105
106
107// 2 Operants
108//--------------
109
111template<typename vector_t, typename matrix_t>
112inline void VectorAssign(vector_t &dest, double alpha1, const MatVec_Expression<matrix_t, vector_t> &m1,
113 double alpha2, const MatVec_Expression<matrix_t, vector_t> &m2)
114{
115 MatMultAdd(dest, alpha1*m1.alpha, m1.l, m1.r, alpha2*m2.alpha, m2.l, m2.r);
116}
117
118
120template<typename vector_t, typename matrix_t, typename vector_t2>
121inline void VectorAssign(vector_t &dest, double alpha1, const MatVec_Expression<matrix_t, vector_t> &m1, double alpha2, const vector_t2 &v2)
122{
123 MatMultAdd(dest, alpha1*m1.alpha, m1.l, m1.r, getScaling(v2)*alpha2, getVector(v2));
124}
125
127template<typename vector_t, typename matrix_t>
128inline void VectorAssign(vector_t &dest, double alpha1, const vector_t &v1, double alpha2, const MatVec_Expression<matrix_t, vector_t> &m2)
129{
130 VectorAssign(dest, alpha2, m2, alpha1, v1);
131}
132
134template<typename vector_t, typename vector_t2, typename vector_t3>
135inline void VectorAssign(vector_t &dest, double alpha1, const vector_t2 &v1, double alpha2, const vector_t3 &v2)
136{
137 VecScaleAdd(dest, getScaling(v1)*alpha1, getVector(v1), getScaling(v2)*alpha2, getVector(v2));
138}
139
140
141
142
144// 1 Operant
145
147template<typename vector_t, typename matrix_t>
148inline void VectorAssign(vector_t &dest, const MatVec_Expression<matrix_t, vector_t> &m1)
149{
150 MatMult(dest, m1.alpha, m1.l, m1.r);
151}
152
153
155template<typename vector_t, typename vector_t2, typename vector_t3>
156inline void VectorAssign(vector_t &dest, const vector_t2 &v1)
157{
158 VecScaleAssign(dest, getScaling(v1), getVector(v1));
159}
160
161
163// transform all AlphaMatVec_X_Expression of the form a+b, (a+b)+c or a+(b+c)
164//
165template<typename vector_t, typename T1, typename operation, typename T2, typename T3>
166inline void VectorAssign(vector_t &dest, double alpha1, const AlphaMatVec_X_Expression<T1, operation, T2> &t1, double alpha2, const T3 &t2)
167{
168 VectorAssign(dest, alpha1, t1.cast().l, operation::is_add() ? alpha1 : -alpha1, t1.cast().r, alpha2, t2);
169}
170
171template<typename vector_t, typename T1, typename T2, typename operation, typename T3>
172inline void VectorAssign(vector_t &dest, double alpha1, const T1 &t1, double alpha2, const AlphaMatVec_X_Expression<T2, operation, T3> &t2)
173{
174 VectorAssign(dest, alpha1, t1, alpha2, t2.l, operation::is_add() ? alpha2 : -alpha2, t2.r);
175}
176
177template<typename vector_t, typename T1, typename operation, typename T2>
179{
180 VectorAssign(dest, 1.0, t.l, operation::is_add() ? 1.0 : -1.0, t.r);
181}
182
183
186template<typename vector_t, typename T1>
187inline void VectorAdd(vector_t &dest, const T1 &t1)
188{
189 VectorAssign<vector_t, T1, vector_t>(dest, 1.0, t1, 1.0, dest);
190}
191
193template<typename vector_t, typename T1>
194inline void VectorSub(vector_t &dest, const T1 &t1)
195{
196 VectorAssign(dest, -1.0, t1, 1.0, dest);
197}
198
199
200} // namespace ug
201
202#endif /* __H__UG__LIB_ALGEBRA__OPERATIONS_TRANSFORM__ */
Definition template_expressions.h:178
const R & r
Definition template_expressions.h:181
const L & l
Definition template_expressions.h:180
Definition template_expressions.h:152
const R & r
Definition template_expressions.h:156
const L & l
Definition template_expressions.h:155
double alpha
Definition template_expressions.h:154
const A & cast() const
cast this class down to original class A.
Definition template_expressions.h:94
void VecScaleAdd(vector_t &vOut, typename vector_t::value_type s1, const vector_t &v1, typename vector_t::value_type s2, const vector_t &v2)
Scales two Vectors, adds them and returns the sum in a third vector.
Definition math_vector_functions_common_impl.hpp:265
the ug namespace
bool MatMultAdd(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = alpha1*v1 + beta1 * A1 *w1;
Definition operations_mat.h:68
const T & getVector(const T &t)
Definition template_expressions.h:123
void VectorAdd(vector_t &dest, const T1 &t1)
transforms x += X1 into x = (1.0)*X1 + (1.0)*x
Definition operations_transform.h:187
bool MatMult(vector_t &dest, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = beta1 * A1;
Definition operations_mat.h:59
void VectorSub(vector_t &dest, const T1 &t1)
transforms x -= X1 into x = (-1.0)*X1 + (1.0)*x
Definition operations_transform.h:194
double getScaling(const T &t)
Definition template_expressions.h:117
void VectorAssign(vector_t &dest, double alpha1, const MatVec_Expression< matrix_t, vector_t > &m1, double alpha2, const MatVec_Expression< matrix_t, vector_t > &m2, double alpha3, const vector_t2 &v3)
v = Mv + Mv + v
Definition operations_transform.h:51
void VecScaleAssign(double &dest, double alpha1, const double &v1)
calculates dest = alpha1*v1. for doubles
Definition operations_vec.h:49