Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
LinearAlgebraExpr.h
Go to the documentation of this file.
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*!
8 * \file LinearAlgebraExpr.h
9 * \brief LinearAlgebraExpr.h
10 */
11#pragma once
12
13#include <alien/utils/Precomp.h>
14
15#include <memory>
16
17#include <alien/core/backend/BackEnd.h>
19
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Alien
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31class Space;
32class IVector;
33class IMatrix;
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38/*!
39 * \ingroup core
40 * \brief Linear algebra interface
41 *
42 * Interface for all linear algebra package
43 *
44 * \tparam Tag The tag of the type of matrix used
45 * \tparam TagV The tag of the type of vector used
46 */
47template <class Tag, class TagV = Tag>
49{
50 public:
51 /*!
52 * \brief Creates a linear algebra
53 *
54 * Creates a linear algebra using traits and the linear algebra factory
55 *
56 * \tparam T Variadics type of linear algebra
57 * \param[in] args Linear algebras
58 */
59 template <typename... T>
61 : m_algebra(AlgebraTraits<Tag>::algebra_expr_factory(args...))
62 {}
63
64 //! Free resources
66
67 /*!
68 * \brief Compute L0 norm of a vector
69 * \param[in] x The vector on which norm0 is computed
70 * \returns The norm0 of the vector
71 */
72 Real norm0(const IVector& x) const;
73
74 /*!
75 * \brief Compute L1 norm of a vector
76 * \param[in] x The vector on which norm0 is computed
77 * \returns The norm1 of the vector
78 */
79 Real norm1(const IVector& x) const;
80
81 /*!
82 * \brief Compute L2 norm of a vector
83 * \param[in] x The vector on which norm0 is computed
84 * \returns The norm2 of the vector
85 */
86 Real norm2(const IVector& x) const;
87
88 /*!
89 * \brief Compute LInf norm of a vector
90 * \param[in] x The vector on which norm0 is computed
91 * \returns The normInf of the vector
92 */
93 Real normInf(const IVector& x) const;
94
95 /*!
96 * \brief Compute L2 (Frobenous) norm of a matrix
97 * \param[in] x The matrix on which norm2 is computed
98 * \returns The norm2 of the matrix
99 */
100 Real norm2(const IMatrix& x) const;
101
102 /*!
103 * \brief Compute a matrix vector product
104 *
105 * Compute the matrix-vector product a by x and store it in r : r = a * x
106 *
107 * \param[in] a The matrix to be multiplied
108 * \param[in] x The vector to be multipled
109 * \param[in,out] r The resulting vector
110 */
111 void mult(const IMatrix& a, const IVector& x, IVector& r) const;
112
113 /*!
114 * \brief Scale a vector by a factor and adds the result to another vector
115 *
116 * Scale the vector x by the real value alpha and add the result to the vector y : y +=
117 * alpha * x
118 *
119 * \param[in] alpha The real value to scale with
120 * \param[in] x The vector to be scaled
121 * \param[in,out] y The resulting vector
122 */
123 void axpy(Real alpha, const IVector& x, IVector& y) const;
124
125 /*!
126 * \brief Scale a vector by a factor and adds the result to another vector
127 *
128 * Scale the vector y by the real value alpha and add the values of x : alpha * y += x
129 *
130 * \param[in] alpha The real value to scale with
131 * \param[in,out] y The vector to be scaled
132 * \param[in] x The vector to add
133 */
134 void aypx(Real alpha, IVector& y, const IVector& x) const;
135
136 /*!
137 * \brief Copy a vector in another one
138 *
139 * \param[in] x The vector to copy
140 * \param[in,out] r The copied vector
141 */
142 void copy(const IVector& x, IVector& r) const;
143
144 /*!
145 * \brief Copy a matrix in another one
146 *
147 * \param[in] x The matrix to copy
148 * \param[in,out] r The copied matrix
149 */
150 void copy(const IMatrix& x, IMatrix& r) const;
151
152 /*!
153 * \brief Add two matrices A and B
154 *
155 *
156 * \param[in] a The matrix A
157 * \param[in] b The matrix B
158 * \param[in,out] c The resulting matrix
159 */
160 void add(const IMatrix& a, IMatrix& b) const;
161
162 /*!
163 * \brief Compute the dot product of two vectors
164 * \param[in] x The first vector
165 * \param[in] y The second vector
166 * \returns The dot product of x * y
167 */
168 Real dot(const IVector& x, const IVector& y) const;
169
170 /*!
171 * \brief Scale a vector by a factor
172 * \param[in] alpha The real value to scale with
173 * \param[in,out] x The vector to be scaled
174 */
175 void scal(Real alpha, IVector& x) const;
176
177 /*!
178 * \brief Scale a matrix by a factor
179 * \param[in] alpha The real value to scale with
180 * \param[in,out] x The vector to be scaled
181 */
182 void scal(Real alpha, IMatrix& a) const;
183
184 /*!
185 * \brief Extract the diagonal of a matrix in a vector
186 * \param[in] a The matrix to extract the diagonal
187 * \param[in,out] x The diagonal elements of the matrix stored in a vector
188 */
189 void diagonal(const IMatrix& a, IVector& x) const;
190
191 /*!
192 * \brief Compute the reciprocal of a vector
193 * \param[in,out] x The vector to be processed
194 */
195 void reciprocal(IVector& x) const;
196
197 /*!
198 * \brief Compute the point wise multiplication of two vectors and store the result in
199 * another one
200 * \param[in] x The first vector
201 * \param[in] y The second vector
202 * \param[in,out] w The resulting vector
203 */
204 void pointwiseMult(const IVector& x, const IVector& y, IVector& w) const;
205
206 /*!
207 * \brief Compute a matrix vector product
208 *
209 * Compute the matrix-vector product a by x and store it in r : r = a * x
210 *
211 * \param[in] a The matrix to be multiplied
212 * \param[in] x The vector to be multipled
213 * \param[in,out] r The resulting vector
214 */
215 void mult(const IMatrix& a, const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
216
217 /*!
218 * \brief Scale a vector by a factor and adds the result to another vector
219 *
220 * Scale the vector x by the real value alpha and add the result to the vector y : y +=
221 * alpha * x
222 *
223 * \param[in] alpha The real value to scale with
224 * \param[in] x The vector to be scaled
225 * \param[in,out] y The resulting vector
226 */
227 void axpy(Real alpha, const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
228
229 /*!
230 * \brief Scale a vector by a factor and adds the result to another vector
231 *
232 * Scale the vector y by the real value alpha and add the values of x : alpha * y += x
233 *
234 * \param[in] alpha The real value to scale with
235 * \param[in,out] y The vector to be scaled
236 * \param[in] x The vector to add
237 */
238 void aypx(Real alpha, UniqueArray<Real>& y, const UniqueArray<Real>& x) const;
239
240 /*!
241 * \brief Copy a vector in another one
242 *
243 * \param[in] x The vector to copy
244 * \param[in,out] r The copied vector
245 */
246 void copy(const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
247
248 /*!
249 * \brief Compute the dot product of two vectors
250 * \param[in] local_size The size of the vectors
251 * \param[in] x The first vector
252 * \param[in] y The second vector
253 * \returns The dot product of x * y
254 */
255 Real dot(
256 Integer local_size, const UniqueArray<Real>& x, const UniqueArray<Real>& y) const;
257
258 /*!
259 * \brief Scale a vector by a factor
260 * \param[in] alpha The real value to scale with
261 * \param[in,out] x The vector to be scaled
262 */
263 void scal(Real alpha, UniqueArray<Real>& x) const;
264
265 /*!
266 * \brief Dumps a matrix to a file
267 * \param[in] a The matrix to dump
268 * \param[in] filename The name of the file
269 */
270 void dump(IMatrix const& a, std::string const& filename) const;
271
272 /*!
273 * \brief Dumps a vector to a file
274 * \param[in] x The vector to dump
275 * \param[in] filename The name of the file
276 */
277 void dump(IVector const& x, std::string const& filename) const;
278
279 private:
280 //! The type of the linear algebra
281 typedef typename AlgebraTraits<Tag>::algebra_type KernelAlgebra;
282 typedef typename AlgebraTraits<Tag>::algebra_expr_type KernelAlgebraExpr;
283 //! The linear algebra kernel
284 std::unique_ptr<KernelAlgebraExpr> m_algebra;
285};
286
287/*---------------------------------------------------------------------------*/
288/*---------------------------------------------------------------------------*/
289
290} // namespace Alien
291
292/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
IInternalLinearAlgebraExprT.h.
ILinearAlgebra.h.
Interface for all matrices.
Definition IMatrix.h:51
Interface for all vectors.
Definition IVector.h:51
Real norm0(const IVector &x) const
Compute L0 norm of a vector.
void aypx(Real alpha, IVector &y, const IVector &x) const
Scale a vector by a factor and adds the result to another vector.
void mult(const IMatrix &a, const IVector &x, IVector &r) const
Compute a matrix vector product.
Real norm2(const IMatrix &x) const
Compute L2 (Frobenous) norm of a matrix.
void copy(const IMatrix &x, IMatrix &r) const
Copy a matrix in another one.
Real normInf(const IVector &x) const
Compute LInf norm of a vector.
Real norm1(const IVector &x) const
Compute L1 norm of a vector.
void scal(Real alpha, IMatrix &a) const
Scale a matrix by a factor.
void add(const IMatrix &a, IMatrix &b) const
Add two matrices A and B.
Real dot(Integer local_size, const UniqueArray< Real > &x, const UniqueArray< Real > &y) const
Compute the dot product of two vectors.
LinearAlgebraExpr(T... args)
Creates a linear algebra.
void copy(const IVector &x, IVector &r) const
Copy a vector in another one.
Real norm2(const IVector &x) const
Compute L2 norm of a vector.
void axpy(Real alpha, const IVector &x, IVector &y) const
Scale a vector by a factor and adds the result to another vector.
void aypx(Real alpha, UniqueArray< Real > &y, const UniqueArray< Real > &x) const
Scale a vector by a factor and adds the result to another vector.
void copy(const UniqueArray< Real > &x, UniqueArray< Real > &r) const
Copy a vector in another one.
void dump(IMatrix const &a, std::string const &filename) const
Dumps a matrix to a file.
virtual ~LinearAlgebraExpr()
Free resources.
void mult(const IMatrix &a, const UniqueArray< Real > &x, UniqueArray< Real > &r) const
Compute a matrix vector product.
void axpy(Real alpha, const UniqueArray< Real > &x, UniqueArray< Real > &r) const
Scale a vector by a factor and adds the result to another vector.
void reciprocal(IVector &x) const
Compute the reciprocal of a vector.
void pointwiseMult(const IVector &x, const IVector &y, IVector &w) const
Compute the point wise multiplication of two vectors and store the result in another one.
void dump(IVector const &x, std::string const &filename) const
Dumps a vector to a file.
void scal(Real alpha, UniqueArray< Real > &x) const
Scale a vector by a factor.
void diagonal(const IMatrix &a, IVector &x) const
Extract the diagonal of a matrix in a vector.
void scal(Real alpha, IVector &x) const
Scale a vector by a factor.
Real dot(const IVector &x, const IVector &y) const
Compute the dot product of two vectors.
Implementation of an algebraic space.
Definition Space.h:47
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17