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