Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
symmetricmatrix.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_FUFEM_SYMMETRICMATRIX_HH
5#define DUNE_FUFEM_SYMMETRICMATRIX_HH
6
8
9namespace Dune {
10namespace Fufem {
11
19template <class T, int N>
21{
22public:
23
26 using field_type = T;
27
28 using Data = Dune::FieldVector<T,N*(N+1)/2>;
29
34
35
37 template <class Iterator>
38 SymmetricMatrix(Iterator it)
39 {
40 for (size_t i=0; i<data_.size(); ++i, ++it)
41 data_[i] = *it;
42 }
43
46 {
48 for( size_t i=0; i<N; ++i)
49 id.setEntry(i,i,1);
50
51 return id;
52 }
53
58 static constexpr Data traceMap()
59 {
60 // trace(A) = <Id,A>, therefore return data of Id
61 // use isometry!
62 return identityMatrix().data();
63 }
64
66 {
67 data_ = s;
68 return *this;
69 }
70
72 {
73 data_ *= s;
74 return *this;
75 }
76
85 void setEntry(int i, int j, const T& entry)
86 {
87 if( i > j )
88 data_[i*(i+1)/2+j] = std::sqrt(2.) * entry;
89 else if ( i < j )
90 data_[j*(j+1)/2+i] = std::sqrt(2.) * entry;
91 else // ( i == j )
92 data_[i*(i+1)/2+i] = entry;
93 }
94
100 T operator() (int i, int j) const
101 {
102 if( i > j )
103 return 1./std::sqrt(2.) * data_[i*(i+1)/2+j];
104 else if ( i < j )
105 return 1./std::sqrt(2.) * data_[j*(j+1)/2+i];
106 else // ( i == j )
107 return data_[i*(i+1)/2+i];
108 }
109
111 {
112 T result = 0;
113 for (size_t i=0; i<N; i++)
114 for (size_t j=0; j<=i; j++)
115 result += (1-0.5*(i==j)) * operator()(i,j) * (v1[i] * v2[j] + v1[j] * v2[i]);
116 return result;
117 }
118
119 void axpy(const T& a, const SymmetricMatrix<T,N>& other)
120 {
121 data_.axpy(a,other.data_);
122 }
123
130 {
131 return data_.two_norm2();
132 }
133
136 {
137 return std::sqrt(frobenius_norm2());
138 }
139
140 const Data& data() const
141 {
142 return data_;
143 }
144
146 {
147 return data_;
148 }
149
152 static constexpr size_t dataSize()
153 {
154 return Data::size();
155 }
156
157private:
158 Data data_;
159};
160
161} // namespace Fufem
162} // namespace Dune
163
164#endif
165
int id()
static constexpr size_type N()
virtual void operator()()=0
constexpr K * data() noexcept
constexpr derived_type & axpy(const field_type &a, const DenseVector< Other > &x)
constexpr FieldTraits< value_type >::real_type two_norm2() const
constexpr size_type size() const
A class implementing a symmetric matrix with compile-time size.
Definition symmetricmatrix.hh:21
Dune::FieldVector< T, N *(N+1)/2 > Data
Definition symmetricmatrix.hh:28
const Data & data() const
Definition symmetricmatrix.hh:140
T energyScalarProduct(const FieldVector< T, N > &v1, const FieldVector< T, N > &v2) const
Definition symmetricmatrix.hh:110
SymmetricMatrix< T, N > & operator*=(const T &s)
Definition symmetricmatrix.hh:71
T frobenius_norm2() const
Compute the Frobenius norm of the matrix.
Definition symmetricmatrix.hh:129
void axpy(const T &a, const SymmetricMatrix< T, N > &other)
Definition symmetricmatrix.hh:119
SymmetricMatrix(Iterator it)
Construct from raw memory.
Definition symmetricmatrix.hh:38
SymmetricMatrix< T, N > & operator=(const T &s)
Definition symmetricmatrix.hh:65
static constexpr SymmetricMatrix< T, N > identityMatrix()
Construct identityMatrix.
Definition symmetricmatrix.hh:45
SymmetricMatrix()
Default constructor, creates uninitialized matrix.
Definition symmetricmatrix.hh:32
T field_type
The type used for scalars.
Definition symmetricmatrix.hh:26
void setEntry(int i, int j, const T &entry)
Random write access to components.
Definition symmetricmatrix.hh:85
static constexpr Data traceMap()
return the trace map in vector representation the returned vector is such that <data,...
Definition symmetricmatrix.hh:58
static constexpr size_t dataSize()
Number of scalars needed for compressed vector storage of the symmetric matrix.
Definition symmetricmatrix.hh:152
T frobenius_norm() const
Compute the Frobenius norm of the matrix.
Definition symmetricmatrix.hh:135
Data & data()
Definition symmetricmatrix.hh:145
T sqrt(T... args)