Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
LinearAlgebraExprT.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//-----------------------------------------------------------------------------
11
12#pragma once
13
15
18#include <alien/data/IMatrix.h>
19#include <alien/data/IVector.h>
20#include <string>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Alien
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31template <class Tag, class TagV>
33
34/*---------------------------------------------------------------------------*/
35
36template <class Tag, class TagV>
37Arccore::Real
39{
40 const auto& vx = x.impl()->get<TagV>();
41 return m_algebra->norm0(vx);
42}
43
44/*---------------------------------------------------------------------------*/
45
46template <class Tag, class TagV>
47Arccore::Real
49{
50 const auto& vx = x.impl()->get<TagV>();
51 return m_algebra->norm1(vx);
52}
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57template <class Tag, class TagV>
58Arccore::Real
60{
61 const auto& vx = x.impl()->get<TagV>();
62 return m_algebra->norm2(vx);
63}
64
65/*---------------------------------------------------------------------------*/
66/*---------------------------------------------------------------------------*/
67
68template <class Tag, class TagV>
69Arccore::Real
71{
72 const auto& vx = x.impl()->get<Tag>();
73 return m_algebra->normInf(vx);
74}
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79template <class Tag, class TagV>
80Arccore::Real
82{
83 const auto& mx = x.impl()->get<Tag>();
84 return m_algebra->norm2(mx);
85}
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89template <class Tag, class TagV>
91{
92 const auto& ma = a.impl()->get<Tag>();
93 const auto& vx = x.impl()->get<TagV>();
94 auto& vr = r.impl()->get<TagV>(true);
95 ALIEN_ASSERT((ma.colSpace() == vx.space() && ma.rowSpace() == vr.space()),
96 ("Incompatible spaces"));
97 m_algebra->mult(ma, vx, vr);
98}
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
102
103template <class Tag, class TagV>
104void LinearAlgebraExpr<Tag, TagV>::axpy(Real alpha, const IVector& x, IVector& r) const
105{
106 const auto& vx = x.impl()->get<TagV>();
107 auto& vr = r.impl()->get<TagV>(true);
108 ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces"));
109 m_algebra->axpy(alpha, vx, vr);
110}
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115template <class Tag, class TagV>
116void LinearAlgebraExpr<Tag, TagV>::aypx(Real alpha, IVector& y, const IVector& x) const
117{
118 const auto& vx = x.impl()->get<TagV>();
119 auto& vy = y.impl()->get<TagV>(true);
120 ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible spaces"));
121 m_algebra->aypx(alpha, vy, vx);
122}
123
124/*---------------------------------------------------------------------------*/
125
126template <class Tag, class TagV>
128{
129 const auto& vx = x.impl()->get<TagV>();
130 auto& vr = r.impl()->get<TagV>(true);
131 ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces"));
132 m_algebra->copy(vx, vr);
133}
134
135template <class Tag, class TagV>
137{
138 const auto& mx = x.impl()->get<TagV>();
139 auto& mr = r.impl()->get<TagV>(true);
140 ALIEN_ASSERT((mx.rowSpace() == mr.rowSpace()), ("Incompatible spaces"));
141 m_algebra->copy(mx, mr);
142}
143
144template <class Tag, class TagV>
146{
147 const auto& ma = a.impl()->get<Tag>();
148 auto& mb = b.impl()->get<Tag>(true);
149 ALIEN_ASSERT((ma.rowSpace() == mb.rowSpace()), ("Incompatible spaces"));
150 m_algebra->add(ma, mb);
151}
152
153/*---------------------------------------------------------------------------*/
154
155template <class Tag, class TagV>
157{
158 const auto& vx = x.impl()->get<TagV>();
159 const auto& vy = y.impl()->get<TagV>();
160 ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible space"));
161 return m_algebra->dot(vx, vy);
162}
163
164/*---------------------------------------------------------------------------*/
165
166template <class Tag, class TagV>
168{
169 auto& vx = x.impl()->get<TagV>(true);
170 const auto& ma = a.impl()->get<Tag>();
171 ALIEN_ASSERT((ma.rowSpace() == ma.colSpace()), ("Matrix not square"));
172 ALIEN_ASSERT((ma.rowSpace() == vx.space()), ("Incompatible space"));
173 m_algebra->diagonal(ma, vx);
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179template <class Tag, class TagV>
181{
182 auto& vx = x.impl()->get<TagV>(true);
183 m_algebra->reciprocal(vx);
184}
185
186/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
189template <class Tag, class TagV>
191{
192 auto& vx = x.impl()->get<TagV>(true);
193 m_algebra->scal(alpha, vx);
194}
195
196template <class Tag, class TagV>
198{
199 auto& ma = A.impl()->get<Tag>(true);
200 m_algebra->scal(alpha, ma);
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206template <class Tag, class TagV>
208const IVector& x, const IVector& y, IVector& w) const
209{
210 auto& vw = w.impl()->get<TagV>(true);
211 const auto& vx = x.impl()->get<TagV>();
212 const auto& vy = y.impl()->get<TagV>();
213 ALIEN_ASSERT((vy.space() == vx.space()), ("Incompatible space"));
214 ALIEN_ASSERT((vw.space() == vx.space()), ("Incompatible space"));
215 m_algebra->pointwiseMult(vx, vy, vw);
216}
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221template <class Tag, class TagV>
223const IMatrix& a, const UniqueArray<Real>& x, UniqueArray<Real>& r) const
224{
225 auto const& ma = a.impl()->get<Tag>();
226 m_algebra->mult(ma, x, r);
227}
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
231
232template <class Tag, class TagV>
233void LinearAlgebraExpr<Tag, TagV>::axpy(Real alpha, const UniqueArray<Real>& x, UniqueArray<Real>& r) const
234{
235 m_algebra->axpy(alpha, x, r);
236}
237
238/*---------------------------------------------------------------------------*/
239/*---------------------------------------------------------------------------*/
240
241template <class Tag, class TagV>
242void LinearAlgebraExpr<Tag, TagV>::aypx(Real alpha, UniqueArray<Real>& y, UniqueArray<Real> const& x) const
243{
244 m_algebra->aypx(alpha, y, x);
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
249
250template <class Tag, class TagV>
252const Alien::UniqueArray<Real>& x, Alien::UniqueArray<Real>& r) const
253{
254 m_algebra->copy(x, r);
255}
256
257/*---------------------------------------------------------------------------*/
258/*---------------------------------------------------------------------------*/
259
260template <class Tag, class TagV>
262Integer local_size, const UniqueArray<Real>& x, const UniqueArray<Real>& y) const
263{
264 return m_algebra->dot(local_size, x, y);
265}
266
267/*---------------------------------------------------------------------------*/
268/*---------------------------------------------------------------------------*/
269
270template <class Tag, class TagV>
271void LinearAlgebraExpr<Tag, TagV>::scal(Real alpha, Alien::UniqueArray<Real>& x) const
272{
273 m_algebra->scal(alpha, x);
274}
275
276/*---------------------------------------------------------------------------*/
277/*---------------------------------------------------------------------------*/
278
279template <class Tag, class TagV>
280void LinearAlgebraExpr<Tag, TagV>::dump(const IMatrix& a, std::string const& filename) const
281{
282 auto const& ma = a.impl()->get<Tag>();
283 m_algebra->dump(ma, filename);
284}
285
286/*---------------------------------------------------------------------------*/
287/*---------------------------------------------------------------------------*/
288
289template <class Tag, class TagV>
290void LinearAlgebraExpr<Tag, TagV>::dump(const IVector& x, std::string const& filename) const
291{
292 auto const& vx = x.impl()->get<TagV>();
293 m_algebra->dump(vx, filename);
294}
295
296/*---------------------------------------------------------------------------*/
297/*---------------------------------------------------------------------------*/
298
299} // namespace Alien
300
301/*---------------------------------------------------------------------------*/
302/*---------------------------------------------------------------------------*/
IMatrix.h.
IVector.h.
LinearAlgebraExpr.h.
MultiMatrixImpl.h.
MultiVectorImpl.h.
Interface for all matrices.
Definition IMatrix.h:51
virtual MultiMatrixImpl * impl()=0
Get the multimatrix implementation.
Interface for all vectors.
Definition IVector.h:51
virtual MultiVectorImpl * impl()=0
Get the multivector implementation.
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.
std::unique_ptr< KernelAlgebraExpr > m_algebra
The linear algebra kernel.
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 add(const IMatrix &a, IMatrix &b) const
Add two matrices A and B.
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 dump(IMatrix const &a, std::string const &filename) const
Dumps a matrix to a file.
virtual ~LinearAlgebraExpr()
Free resources.
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 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.
const AlgebraTraits< tag >::matrix_type & get() const
Get a specific matrix implementation.
const AlgebraTraits< tag >::vector_type & get() const
Get a specific vector implementation.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17