KASKADE 7 development version
Loading...
Searching...
No Matches
scalar.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2012-2023 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef SCALAR_HH_
14#define SCALAR_HH_
15
16#include <complex>
17#include <type_traits>
18
20// forward declarations
21namespace Dune
22{
23 template <class Scalar, int n> class FieldVector;
24 template <class Scalar, int n, int m> class FieldMatrix;
25}
27
28namespace Kaskade
29{
30
34 template <class Scalar>
36 {
37 public:
41 static bool const isBLAStype = std::is_same_v<Scalar,float> || std::is_same_v<Scalar,double>;
42
49 typedef Scalar Real;
50
54 static Real real(Scalar const& s) { return s; }
55 };
56
57 // Specialization for standard complex types
58 template <class T>
59 class ScalarTraits<std::complex<T>>
60 {
61 public:
62 static bool const isBLAStype = std::is_floating_point_v<T> && ScalarTraits<T>::isBLAStype;
63
64 using Real = T;
65
66 static Real real(std::complex<T> const& z) { return z.real(); }
67 };
68
69 //-----------------------------------------------------------------------------------------------
70
74 template <class Entry> // Default implementation for scalars (float, double, complex<float>,...)
76 {
77 static int const rows = 1; // number of scalar rows
78 static int const cols = 1; // number of scalar columns
79 using transpose_type = Entry;
80 using field_type = Entry;
82 static int const lapackLayout = std::is_floating_point<Entry>::value;
83
84 static real_type frobenius_norm2(Entry const& x) { return x*x; } // TODO: needs specialization for complex
85 };
86
87 template <class Entry, int n, int m>
88 struct EntryTraits<Dune::FieldMatrix<Entry,n,m>>
89 {
90 static int const rows = n*EntryTraits<Entry>::rows;
91 static int const cols = m*EntryTraits<Entry>::cols;
95 static int const lapackLayout = m==1 && EntryTraits<Entry>::lapackLayout; // FieldMatrices are stored row by row
96
97 static real_type frobenius_norm2(Dune::FieldMatrix<Entry,n,m> const& x) { return x.frobenius_norm2(); }
98 };
99
100 template <class Entry, int n>
112
113 template <class Entry>
115 {
117 }
118
119 // ----------------------------------------------------------------------------------------------
120
124 namespace ScalarDetail
125 {
126 template <class K>
127 struct Rank
128 {
129 static constexpr int value = 0;
130 };
131
132 template <class K, int n>
133 struct Rank<Dune::FieldVector<K,n>>
134 {
135 static constexpr int value = 1;
136 };
137
138 template <class K, int n, int m>
139 struct Rank<Dune::FieldMatrix<K,n,m>>
140 {
141 static constexpr int value = 2;
142 };
143 }
147
152 template <class T>
153 constexpr int rank = ScalarDetail::Rank<T>::value;
154
155
156 // ----------------------------------------------------------------------------------------------
157
162 template <class T, class Real>
163 struct ConvertTo
164 {
165 using type = std::conditional_t<std::is_convertible<T,Real>::value,Real,T>;
166 };
167
168 template <class T, int n, int m, class Real>
169 struct ConvertTo<Dune::FieldMatrix<T,n,m>,Real>
170 {
172 };
173}
174
175#endif
static Real real(std::complex< T > const &z)
Definition scalar.hh:66
Helper class for working with (real or complex) scalar field types.
Definition scalar.hh:36
static Real real(Scalar const &s)
Conversion to the real type, ignoring the imaginary part if nonzero.
Definition scalar.hh:54
typenameGetScalar< X >::type Real
Definition scalar.hh:49
static bool const isBLAStype
Definition scalar.hh:41
constexpr int rank
Reports the rank of vector, matrix, and tensor types of static size.
Definition scalar.hh:153
EntryTraits< Entry >::real_type frobenius_norm2(Entry const &x)
Definition scalar.hh:114
Dune::FieldMatrix< typename ConvertTo< T, Real >::type, n, m > type
Definition scalar.hh:171
Reports the converted type.
Definition scalar.hh:164
std::conditional_t< std::is_convertible< T, Real >::value, Real, T > type
Definition scalar.hh:165
static int const lapackLayout
Definition scalar.hh:95
typename ScalarTraits< field_type >::Real real_type
Definition scalar.hh:94
static int const rows
Definition scalar.hh:90
typename EntryTraits< Entry >::field_type field_type
Definition scalar.hh:93
Dune::FieldMatrix< typename EntryTraits< Entry >::transpose_type, m, n > transpose_type
Definition scalar.hh:92
static int const cols
Definition scalar.hh:91
static real_type frobenius_norm2(Dune::FieldMatrix< Entry, n, m > const &x)
Definition scalar.hh:97
static int const rows
Definition scalar.hh:103
Dune::FieldMatrix< typename EntryTraits< Entry >::transpose_type, 1, n > transpose_type
Definition scalar.hh:105
typename ScalarTraits< field_type >::Real real_type
Definition scalar.hh:107
static int const cols
Definition scalar.hh:104
static int const lapackLayout
Definition scalar.hh:108
typename EntryTraits< Entry >::field_type field_type
Definition scalar.hh:106
static real_type frobenius_norm2(Dune::FieldVector< Entry, n > const &x)
Definition scalar.hh:110
A traits class for getting static information about block matrix entries.
Definition scalar.hh:76
K field_type
Definition scalar.hh:80
static real_type frobenius_norm2(Entry const &x)
Definition scalar.hh:84
typename ScalarTraits< field_type >::Real real_type
Definition scalar.hh:81
static int const rows
Definition scalar.hh:77
static int const lapackLayout
Definition scalar.hh:82
static int const cols
Definition scalar.hh:78
K transpose_type
Definition scalar.hh:79