walberla::math::MathTrait< T1, T2 > Class Template Reference

Detailed Description

template<typename T1, typename T2>
class walberla::math::MathTrait< T1, T2 >

Base template for the MathTrait class.

General

The MathTrait class template offers the possibility to select the resulting data type of a generic mathematical operation. In case of operations between built-in data types, the MathTrait class defines the more significant data type as the resulting data type. For this selection, signed data types are given a higher significance. In case of operations involving user-defined data types, the MathTrait template specifies the resulting data type of this operation.
Specifying the resulting data type for a specific operation is done by specializing the MathTrait template for this particular type combination. In case a certain type combination is not defined in a MathTrait specialization, the base template is selected, which defines no resulting types and therefore stops the compilation process. Each specialization defines the data types HighType that represents the high-order data type of the two given data types and LowType that represents the low-order data type. Additionally, each specialization defines the types AddType, SubType, MultType and DivType, that represent the type of the resulting data type of the corresponding mathematical operation. The following example shows the specialization for operations between the double and the integer type:

template<>
struct MathTrait< double, int >
{
typedef double HighType;
typedef int LowType;
typedef double AddType;
typedef double SubType;
typedef double MultType;
typedef double DivType;
};

Per default, the MathTrait template provides specializations for the following built-in data types:

  • integers
    • unsigned char, signed char, char, wchar_t
    • unsigned short, short
    • unsigned int, int
    • unsigned long, long
    • std::size_t, std::ptrdiff_t (for certain 64-bit compilers)
  • floating points
    • float
    • double
    • long double


Creating custom specializations

It is possible to specialize the MathTrait template for additional user-defined data types. However, it is possible that a specific mathematical operation is invalid for the particular type combination. In this case, the INVALID_NUMERICAL_TYPE can be used to fill the missing type definition. The INVALID_NUMERICAL_TYPE represents the resulting data type of an invalid numerical operation. It is left undefined to stop the compilation process in case it is instantiated. The following example shows the specialization of the MathTrait template for Matrix3 and Vector3. In this case, only the multiplication between the matrix and the vector is a valid numerical operation. Therefore for all other types the INVALID_NUMERICAL_TYPE is used.

template< typename T1, typename T2 >
struct MathTrait< Matrix3<T1>, Vector3<T2> >
{
typedef INVALID_NUMERICAL_TYPE HighType; // Invalid, no common high data type
typedef INVALID_NUMERICAL_TYPE LowType; // Invalid, no common low data type
typedef INVALID_NUMERICAL_TYPE AddType; // Invalid, cannot add a matrix and a vector
typedef INVALID_NUMERICAL_TYPE SubType; // Invalid, cannot subtract a vector from a matrix
typedef Vector3< typename MathTrait<T1,T2>::MultType > MultType; // Multiplication between a matrix and a vector
typedef INVALID_NUMERICAL_TYPE DivType; // Invalid, cannot divide a matrix by a vector
};


Examples

The following example demonstrates the use of the MathTrait template, where depending on the two given data types the resulting data type is selected:

template< typename T1, typename T2 > // The two generic types
typename MathTrait<T1,T2>::HighType // The resulting generic return type
add( T1 t1, T2 t2 ) //
{ // The function 'add' returns the sum
return t1 + t2; // of the two given values
} //

Additionally, the specializations of the MathTrait template enable arithmetic operations between any combination of the supported data types:

typedef Vector3< Matrix3< float > > VectorOfMatrices; // Vector of single-precision matrices
typedef Vector3< Vector3 < double > > VectorOfVectors; // Vector of double-precision vectors
typedef Vector3< double > VectorOfScalars; // Vector of double-precision scalars
VectorOfMatrices vm; // Setup of a vector of matrices
VectorOfVectors vv; // Setup of a vector of vectors
// Calculation of the scalar product between the two vectors. The resulting data type
// is a plain 3-dimensional vector of scalar values of type double.
VectorOfScalars res = vm * vv;

#include <MathTrait.h>

Public Types

using HighType = T1
 
using LowType = T2
 
using High = T1
 
using Low = T2
 

Member Typedef Documentation

◆ High

template<typename T1 , typename T2 >
using walberla::math::MathTrait< T1, T2 >::High = T1

◆ HighType

template<typename T1 , typename T2 >
using walberla::math::MathTrait< T1, T2 >::HighType = T1

◆ Low

template<typename T1 , typename T2 >
using walberla::math::MathTrait< T1, T2 >::Low = T2

◆ LowType

template<typename T1 , typename T2 >
using walberla::math::MathTrait< T1, T2 >::LowType = T2

The documentation for this class was generated from the following file:
T1 HighType
Definition: MathTrait.h:198
T2 LowType
Definition: MathTrait.h:199