KASKADE 7 development version
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
71 template <class Entry> // Default implementation for scalars (float, double, complex<float>,...)
73 {
74 static int const rows = 1; // number of scalar rows
75 static int const cols = 1; // number of scalar columns
76 using transpose_type = Entry;
77 using field_type = Entry;
79 static int const lapackLayout = std::is_floating_point<Entry>::value;
80
81 static real_type frobenius_norm2(Entry const& x) { return x*x; } // TODO: needs specialization for complex
82 };
83
84 template <class Entry, int n, int m>
85 struct EntryTraits<Dune::FieldMatrix<Entry,n,m>>
86 {
87 static int const rows = n*EntryTraits<Entry>::rows;
88 static int const cols = m*EntryTraits<Entry>::cols;
92 static int const lapackLayout = m==1 && EntryTraits<Entry>::lapackLayout; // FieldMatrices are stored row by row
93
94 static real_type frobenius_norm2(Dune::FieldMatrix<Entry,n,m> const& x) { return x.frobenius_norm2(); }
95 };
96
97 template <class Entry, int n>
98 struct EntryTraits<Dune::FieldVector<Entry,n>>
99 {
100 static int const rows = n*EntryTraits<Entry>::rows;
101 static int const cols = EntryTraits<Entry>::cols;
106
107 static real_type frobenius_norm2(Dune::FieldVector<Entry,n> const& x) { return x.two_norm2(); }
108 };
109
110 template <class Entry>
112 {
114 }
115
116 // ----------------------------------------------------------------------------------------------
117
121 namespace ScalarDetail
122 {
123 template <class K>
124 struct Rank
125 {
126 static constexpr int value = 0;
127 };
128
129 template <class K, int n>
130 struct Rank<Dune::FieldVector<K,n>>
131 {
132 static constexpr int value = 1;
133 };
134
135 template <class K, int n, int m>
136 struct Rank<Dune::FieldMatrix<K,n,m>>
137 {
138 static constexpr int value = 2;
139 };
140 }
149 template <class T>
150 constexpr int rank = ScalarDetail::Rank<T>::value;
151
152
153 // ----------------------------------------------------------------------------------------------
154
159 template <class T, class Real>
160 struct ConvertTo
161 {
162 using type = std::conditional_t<std::is_convertible<T,Real>::value,Real,T>;
163 };
164
165 template <class T, int n, int m, class Real>
166 struct ConvertTo<Dune::FieldMatrix<T,n,m>,Real>
167 {
169 };
170}
171
172#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
Scalar Real
The real type on which the scalar field is based.
Definition: scalar.hh:49
static bool const isBLAStype
Whether the scalar is BLAS/LAPACK compatible or not.
Definition: scalar.hh:41
constexpr int rank
Reports the rank of vector, matrix, and tensor types of static size.
Definition: scalar.hh:150
EntryTraits< Entry >::real_type frobenius_norm2(Entry const &x)
Definition: scalar.hh:111
Reports the converted type.
Definition: scalar.hh:161
std::conditional_t< std::is_convertible< T, Real >::value, Real, T > type
Definition: scalar.hh:162
typename ScalarTraits< field_type >::Real real_type
Definition: scalar.hh:91
typename EntryTraits< Entry >::field_type field_type
Definition: scalar.hh:90
static real_type frobenius_norm2(Dune::FieldMatrix< Entry, n, m > const &x)
Definition: scalar.hh:94
typename ScalarTraits< field_type >::Real real_type
Definition: scalar.hh:104
typename EntryTraits< Entry >::field_type field_type
Definition: scalar.hh:103
static real_type frobenius_norm2(Dune::FieldVector< Entry, n > const &x)
Definition: scalar.hh:107
Definition: scalar.hh:73
Entry field_type
Definition: scalar.hh:77
static real_type frobenius_norm2(Entry const &x)
Definition: scalar.hh:81
typename ScalarTraits< field_type >::Real real_type
Definition: scalar.hh:78
static int const rows
Definition: scalar.hh:74
static int const lapackLayout
Definition: scalar.hh:79
static int const cols
Definition: scalar.hh:75
Entry transpose_type
Definition: scalar.hh:76