Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
LinearAlgebraT.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
14#include <string>
15
17
20#include <alien/data/IMatrix.h>
21#include <alien/data/IVector.h>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Alien
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32template <class Tag, class TagV>
34
35/*---------------------------------------------------------------------------*/
36
37template <class Tag, class TagV>
38Arccore::Real
40{
41 const auto& vx = x.impl()->get<TagV>();
42 return m_algebra->norm0(vx);
43}
44
45/*---------------------------------------------------------------------------*/
46
47template <class Tag, class TagV>
48Arccore::Real
50{
51 const auto& vx = x.impl()->get<TagV>();
52 return m_algebra->norm1(vx);
53}
54
55/*---------------------------------------------------------------------------*/
56/*---------------------------------------------------------------------------*/
57
58template <class Tag, class TagV>
59Arccore::Real
61{
62 const auto& vx = x.impl()->get<TagV>();
63 return m_algebra->norm2(vx);
64}
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
69template <class Tag, class TagV>
70Arccore::Real
72{
73 const auto& vx = x.impl()->get<TagV>();
74 return m_algebra->normInf(vx);
75}
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79template <class Tag, class TagV>
80void LinearAlgebra<Tag, TagV>::mult(const IMatrix& a, const IVector& x, IVector& r) const
81{
82 const auto& ma = a.impl()->get<Tag>();
83 const auto& vx = x.impl()->get<TagV>();
84 auto& vr = r.impl()->get<TagV>(true);
85 ALIEN_ASSERT((ma.colSpace() == vx.space() && ma.rowSpace() == vr.space()),
86 ("Incompatible spaces"));
87 m_algebra->mult(ma, vx, vr);
88}
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93template <class Tag, class TagV>
94void LinearAlgebra<Tag, TagV>::axpy(Real alpha, const IVector& x, IVector& r) const
95{
96 const auto& vx = x.impl()->get<TagV>();
97 auto& vr = r.impl()->get<TagV>(true);
98 ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces"));
99 m_algebra->axpy(alpha, vx, vr);
100}
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
105template <class Tag, class TagV>
106void LinearAlgebra<Tag, TagV>::aypx(Real alpha, IVector& y, const IVector& x) const
107{
108 const auto& vx = x.impl()->get<TagV>();
109 auto& vy = y.impl()->get<TagV>(true);
110 ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible spaces"));
111 m_algebra->aypx(alpha, vy, vx);
112}
113
114/*---------------------------------------------------------------------------*/
115
116template <class Tag, class TagV>
118{
119 const auto& vx = x.impl()->get<TagV>();
120 auto& vr = r.impl()->get<TagV>(true);
121 ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces"));
122 m_algebra->copy(vx, vr);
123}
124
125/*---------------------------------------------------------------------------*/
126
127template <class Tag, class TagV>
128Real LinearAlgebra<Tag, TagV>::dot(const IVector& x, const IVector& y) const
129{
130 const auto& vx = x.impl()->get<TagV>();
131 const auto& vy = y.impl()->get<TagV>();
132 ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible space"));
133 return m_algebra->dot(vx, vy);
134}
135
136/*---------------------------------------------------------------------------*/
137
138template <class Tag, class TagV>
140{
141 auto& vx = x.impl()->get<TagV>(true);
142 const auto& ma = a.impl()->get<Tag>();
143 ALIEN_ASSERT((ma.rowSpace() == ma.colSpace()), ("Matrix not square"));
144 ALIEN_ASSERT((ma.rowSpace() == vx.space()), ("Incompatible space"));
145 m_algebra->diagonal(ma, vx);
146}
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151template <class Tag, class TagV>
153{
154 auto& vx = x.impl()->get<TagV>(true);
155 m_algebra->reciprocal(vx);
156}
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
160
161template <class Tag, class TagV>
162void LinearAlgebra<Tag, TagV>::scal(Real alpha, IVector& x) const
163{
164 auto& vx = x.impl()->get<TagV>(true);
165 m_algebra->scal(alpha, vx);
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171template <class Tag, class TagV>
173const IVector& x, const IVector& y, IVector& w) const
174{
175 auto& vw = w.impl()->get<TagV>(true);
176 const auto& vx = x.impl()->get<TagV>();
177 const auto& vy = y.impl()->get<TagV>();
178 ALIEN_ASSERT((vy.space() == vx.space()), ("Incompatible space"));
179 ALIEN_ASSERT((vw.space() == vx.space()), ("Incompatible space"));
180 m_algebra->pointwiseMult(vx, vy, vw);
181}
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188template <class Tag, class TagV>
189void LinearAlgebra<Tag, TagV>::dump(const IMatrix& a, std::string const& filename) const
190{
191 auto const& ma = a.impl()->get<Tag>();
192 m_algebra->dump(ma, filename);
193}
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
197
198template <class Tag, class TagV>
199void LinearAlgebra<Tag, TagV>::dump(const IVector& x, std::string const& filename) const
200{
201 auto const& vx = x.impl()->get<TagV>();
202 m_algebra->dump(vx, filename);
203}
204
205/*---------------------------------------------------------------------------*/
206/*---------------------------------------------------------------------------*/
207
208} // namespace Alien
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
IMatrix.h.
IVector.h.
LinearAlgebra.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.
virtual ~LinearAlgebra()
Free resources.
Real normInf(const IVector &x) const
Compute LInf norm of a vector.
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 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 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.
std::unique_ptr< KernelAlgebra > m_algebra
The linear algebra kernel.
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.
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