20#ifndef LIBMESH_TYPE_VECTOR_H
21#define LIBMESH_TYPE_VECTOR_H
24#include "libmesh/libmesh_common.h"
25#include "libmesh/libmesh_device.h"
26#include "libmesh/compare_types.h"
27#include "libmesh/tensor_tools.h"
28#include "libmesh/int_range.h"
29#include "libmesh/fuzzy_equals.h"
41template <
typename T>
class TypeTensor;
42template <
typename T>
class VectorValue;
43template <
typename T>
class TensorValue;
65 template <
typename T2>
90 template <
typename Scalar1,
typename Scalar2,
typename Scalar3>
93 const Scalar1>::type & x,
96 const Scalar2>::type & y=0,
99 const Scalar3>::type & z=0);
107 template <
typename Scalar>
111 const Scalar>::type * sfinae =
nullptr);
128 template <
typename T2>
144 template <
typename T2>
145 LIBMESH_DEVICE_INLINE
151 template <
typename Scalar>
152 LIBMESH_DEVICE_INLINE
153 typename std::enable_if<
157 { libmesh_assert_equal_to (p, Scalar(0)); this->
zero();
return *
this; }
163 LIBMESH_DEVICE_INLINE
164 const T &
slice (
const unsigned int i)
const {
return (*
this)(i); }
170 LIBMESH_DEVICE_INLINE
171 T &
slice (
const unsigned int i) {
return (*
this)(i); }
178 template <
typename T2>
187 template <
typename T2>
193 template <
typename T2>
199 template <
typename T2>
200 LIBMESH_DEVICE_INLINE
208 template <
typename T2>
217 template <
typename T2>
223 template <
typename T2>
230 template <
typename T2>
231 LIBMESH_DEVICE_INLINE
244 template <
typename Scalar>
245 typename std::enable_if<
262 template <
typename Scalar>
263 typename std::enable_if<
281 template <
typename T2>
288 template <
typename T2>
289 LIBMESH_DEVICE_INLINE
296 template <
typename T2>
303 LIBMESH_DEVICE_INLINE
321 LIBMESH_DEVICE_INLINE
327 LIBMESH_DEVICE_INLINE
333 LIBMESH_DEVICE_INLINE
357 LIBMESH_DEVICE_INLINE
363 LIBMESH_DEVICE_INLINE
424 const bool newline =
true)
const;
470 libmesh_assert_equal_to (y, 0);
477 libmesh_assert_equal_to (z, 0);
483template <
typename Scalar1,
typename Scalar2,
typename Scalar3>
487 const Scalar1>::type & x,
490 const Scalar2>::type & y,
493 const Scalar3>::type & z)
500 libmesh_assert_equal_to (y, 0);
506 libmesh_assert_equal_to (z, 0);
513template <
typename Scalar>
518 const Scalar>::type * )
534template <
typename T2>
539 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
546template <
typename T2>
550 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
560 libmesh_assert_less (i, LIBMESH_DIM);
571 libmesh_assert_less (i, LIBMESH_DIM);
579template <
typename T2>
605template <
typename T2>
617template <
typename T2>
641template <
typename T2>
646 _coords[0] += factor*p(0);
650 _coords[0] += factor*p(0);
651 _coords[1] += factor*p(1);
655 _coords[0] += factor*p(0);
656 _coords[1] += factor*p(1);
657 _coords[2] += factor*p(2);
665template <
typename T2>
692template <
typename T2>
704template <
typename T2>
708 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
715template <
typename T2>
719 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
720 _coords[i] -= factor*p(i);
750template <
typename Scalar>
752typename std::enable_if<
777template <
typename T,
typename Scalar>
779typename std::enable_if<
795 _coords[0] *= factor;
799 _coords[0] *= factor;
800 _coords[1] *= factor;
804 _coords[0] *= factor;
805 _coords[1] *= factor;
806 _coords[2] *= factor;
815template <
typename Scalar>
817typename std::enable_if<
824 libmesh_assert_not_equal_to (
static_cast<TS
>(factor),
825 static_cast<TS
>(0.));
852 libmesh_assert_not_equal_to (factor,
static_cast<T
>(0.));
854 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
855 _coords[i] /= factor;
864template <
typename T2>
870 return _coords[0]*p.
_coords[0];
874 return (_coords[0]*p.
_coords[0] +
879 return (_coords[0]*p(0) +
886template <
typename T2>
897template <
typename T2>
903 libmesh_assert_equal_to (LIBMESH_DIM, 3);
935 return decltype(sqrt(this->norm_sq())){};
937 return sqrt(this->norm_sq());
946 for (
unsigned int i=0; i<LIBMESH_DIM; i++)
981 return sqrt(this->norm_sq());
989 return sqrt(this->norm_sq());
997 return sqrt(this->norm_sq());
1000#ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION
1009 return sqrt(this->norm_sq());
1014template <
typename T>
1015LIBMESH_DEVICE_INLINE
1018 for (
const auto & val : _coords)
1028template <
typename T>
1029LIBMESH_DEVICE_INLINE
1034 decltype(abs(T())) ret{};
1036 ret += abs(_coords[i]);
1041template <
typename T>
1050template <
typename T>
1059template <
typename T>
1060LIBMESH_DEVICE_INLINE
1064 return (_coords[0] == rhs.
_coords[0]);
1068 return (_coords[0] == rhs.
_coords[0] &&
1069 _coords[1] == rhs.
_coords[1]);
1073 return (_coords[0] == rhs.
_coords[0] &&
1074 _coords[1] == rhs.
_coords[1] &&
1075 _coords[2] == rhs.
_coords[2]);
1081template <
typename T>
1082LIBMESH_DEVICE_INLINE
1085 return (!(*
this == rhs));
1098template <
typename T>
1099LIBMESH_DEVICE_INLINE
1106 a(0)*(
b(1)*c(2) -
b(2)*c(1)) -
1107 a(1)*(
b(0)*c(2) -
b(2)*c(0)) +
1108 a(2)*(
b(0)*c(1) -
b(1)*c(0));
1120template <
typename T>
1121LIBMESH_DEVICE_INLINE
1129 norm02 = v02.
norm(),
1130 norm03 = v03.
norm();
1131 const T tan_half_angle =
1133 ((v01*v02)*norm03 + (v01*v03)*norm02 + (v02*v03)*norm01 +
1134 norm01*norm02*norm03);
1136 return Real(2)*atan(tan_half_angle);
1146template <
typename T>
1159 (e02.
cross(e21)).cross(scaled_vec);
1161 const T e02_cross_e21_z = e02(0)*e21(1)-e02(1)*e21(0);
1162 const TypeVector<T> numerator {-e02_cross_e21_z*scaled_vec(1),
1163 e02_cross_e21_z*scaled_vec(0)};
1177template <
typename T>
1178LIBMESH_DEVICE_INLINE
1182 T z =
b(0)*c(1) -
b(1)*c(0);
1185 T x =
b(1)*c(2) -
b(2)*c(1),
1186 y =
b(0)*c(2) -
b(2)*c(0);
1187 return x*x + y*y + z*z;
1198template <
typename T>
1199LIBMESH_DEVICE_INLINE
1207template <
typename T>
1208LIBMESH_DEVICE_INLINE
1212 auto && length = norm();
1214 libmesh_assert_not_equal_to (length,
static_cast<Real>(0.));
1233template <
typename T>
1238 os <<
"x=" << (*this)(0);
1244 << std::setw(8) << (*this)(0) <<
", "
1245 << std::setw(8) << (*this)(1) <<
")";
1251 << std::setw(8) << (*this)(0) <<
", "
1252 << std::setw(8) << (*this)(1) <<
", "
1253 << std::setw(8) << (*this)(2) <<
")";
1259template <
typename T>
1262 using std::isfinite;
1278template <
typename T>
1285 bool has_inf =
false;
1291 has_inf = has_inf ||
isinf(var(i));
1303template <
typename T>
1315template <
typename T>
1321template <
typename T,
typename T2>
1327template <typename T, typename T2, typename std::enable_if<ScalarTraits<T>::value,
int>::type = 0>
1328LIBMESH_DEVICE_INLINE
1333 for (
unsigned int i = 0; i < LIBMESH_DIM; i++)
1339template <typename T, typename T2, typename std::enable_if<ScalarTraits<T2>::value,
int>::type = 0>
1340LIBMESH_DEVICE_INLINE
1341TypeVector<typename CompareTypes<T, T2>::supertype>
1346 for (
unsigned int i = 0; i < LIBMESH_DIM; i++)
1347 ret(i) = a(i) * conj_b;
1352template <
typename T>
1359template <
typename T,
typename T2>
1370template <
typename T>
1371LIBMESH_DEVICE_INLINE
1375 return vector.norm_sq();
1379#ifdef LIBMESH_HAVE_METAPHYSICL
1382template <
typename T>
1390 for (
unsigned int i = 0; i < LIBMESH_DIM; ++i)
1391 ret(i) = raw_value(in(i));
1397template <
typename T,
typename U>
1398struct ReplaceAlgebraicType<
libMesh::TypeVector<T>, U>
This class defines a tensor in LIBMESH_DIM dimensional space of type T.
This class defines a vector in LIBMESH_DIM dimensional space of type T.
LIBMESH_DEVICE_INLINE auto l1_norm() const
bool operator>(const TypeVector< T > &rhs) const
LIBMESH_DEVICE_INLINE void assign(const TypeVector< T2 > &)
Assign to this vector without creating a temporary.
T _coords[LIBMESH_DIM]
The coordinates of the TypeVector.
LIBMESH_DEVICE_INLINE const T & slice(const unsigned int i) const
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
bool operator>=(const TypeVector< T > &rhs) const
TypeVector(const Scalar &x, typename std::enable_if< ScalarTraits< Scalar >::value, const Scalar >::type *sfinae=nullptr)
Constructor-from-scalar.
const T & operator()(const unsigned int i) const
TypeVector< typename CompareTypes< T, T2 >::supertype > cross(const TypeVector< T2 > &v) const
LIBMESH_DEVICE_INLINE TypeVector< T > unit() const
TypeVector(const TypeVector &p)=default
Copy-constructor for the trivial case.
TypeVector< typename CompareTypes< T, T2 >::supertype > operator+(const TypeVector< T2 > &) const
Add two vectors.
TypeVector< T > operator-() const
LIBMESH_DEVICE_INLINE void subtract_scaled(const TypeVector< T2 > &, const T &)
Subtract a scaled value from this vector without creating a temporary.
const TypeVector< T > & operator-=(const TypeVector< T2 > &)
Subtract from this vector.
~TypeVector()=default
Destructor.
bool operator<=(const TypeVector< T > &rhs) const
void write_unformatted(std::ostream &out_stream, const bool newline=true) const
Unformatted print to the stream out.
TypeVector()
Empty constructor.
LIBMESH_DEVICE_INLINE void zero()
Set all entries of the vector to 0.
T value_type
Helper typedef for C++98 generic programming.
const TypeVector< T > & operator*=(const T &)
Multiply this vector by a scalar value.
TypeVector(const T &x, const T &y=0, const T &z=0)
Constructor-from-T.
LIBMESH_DEVICE_INLINE std::enable_if< ScalarTraits< Scalar >::value, TypeVector & >::type operator=(const Scalar &libmesh_dbg_var(p))
Assignment-from-scalar operator.
LIBMESH_DEVICE_INLINE CompareTypes< T, T2 >::supertype contract(const TypeVector< T2 > &) const
unsigned int index_type
Helper typedef for generic index programming.
bool relative_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
const TypeVector< T > & operator/=(const T &)
Divide each entry of this vector by scalar value.
friend std::ostream & operator<<(std::ostream &os, const TypeVector< T > &t)
Formatted print as above but supports the syntax:
std::enable_if< ScalarTraits< Scalar >::value, TypeVector< typenameCompareTypes< T, Scalar >::supertype > >::type operator*(const Scalar &) const
Multiply this vector by a scalar value.
TypeVector(typename std::enable_if< ScalarTraits< Scalar1 >::value, const Scalar1 >::type &x, typename std::enable_if< ScalarTraits< Scalar2 >::value, const Scalar2 >::type &y=0, typename std::enable_if< ScalarTraits< Scalar3 >::value, const Scalar3 >::type &z=0)
Constructor-from-scalars.
LIBMESH_DEVICE_INLINE void add_scaled(const TypeVector< T2 > &, const T &)
Add a scaled value to this vector without creating a temporary.
void add(const TypeVector< T2 > &)
Add to this vector without creating a temporary.
bool operator<(const TypeVector< T > &rhs) const
void subtract(const TypeVector< T2 > &)
Subtract from this vector without creating a temporary.
LIBMESH_DEVICE_INLINE bool operator!=(const TypeVector< T > &rhs) const
LIBMESH_DEVICE_INLINE bool operator==(const TypeVector< T > &rhs) const
LIBMESH_DEVICE_INLINE bool is_zero() const
const TypeVector< T > & operator+=(const TypeVector< T2 > &)
Add to this vector.
std::enable_if< ScalarTraits< Scalar >::value, TypeVector< typenameCompareTypes< T, Scalar >::supertype > >::type operator/(const Scalar &) const
Divide each entry of this vector by scalar value.
TypeVector(const TypeVector< T2 > &p)
Copy-constructor.
LIBMESH_DEVICE_INLINE T & slice(const unsigned int i)
void print(std::ostream &os=libMesh::out) const
Formatted print, by default to libMesh::out.
The libMesh namespace provides an interface to certain functionality in the library.
bool relative_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
bool absolute_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
auto l1_norm(const NumericVector< T > &vec)
LIBMESH_DEVICE_INLINE T cross_norm_sq(const TypeVector< T > &b, const TypeVector< T > &c)
Compute |b x c|^2 without creating the extra temporary produced by calling b.cross(c)....
LIBMESH_DEVICE_INLINE T cross_norm(const TypeVector< T > &b, const TypeVector< T > &c)
Calls cross_norm_sq() and takes the square root of the result.
static constexpr std::size_t libmesh_dim
LIBMESH_DEVICE_INLINE bool isfinite(std::complex< T > a)
LIBMESH_DEVICE_INLINE T triple_product(const TypeVector< T > &a, const TypeVector< T > &b, const TypeVector< T > &c)
LIBMESH_DEVICE_INLINE bool isnan(std::complex< T > a)
void libmesh_ignore(const Args &...)
LIBMESH_DEVICE_INLINE T solid_angle(const TypeVector< T > &v01, const TypeVector< T > &v02, const TypeVector< T > &v03)
std::enable_if< ScalarTraits< Scalar >::value, TypeNTensor< N, typenameCompareTypes< Scalar, T >::supertype > >::type operator*(const Scalar &, const TypeNTensor< N, T > &)
LIBMESH_DEVICE_INLINE TypeTensor< typename CompareTypes< T, T2 >::supertype > outer_product(const TypeVector< T > &a, const TypeVector< T2 > &b)
LIBMESH_DEVICE_INLINE bool isinf(std::complex< T > a)
static constexpr Real TOLERANCE
auto l1_norm_diff(const NumericVector< T > &vec1, const NumericVector< T > &vec2)
TypeVector< T > circumcenter(const TypeVector< T > &p0, const TypeVector< T > &p1, const TypeVector< T > &p2)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
LIBMESH_DEVICE_INLINE T libmesh_conj(T a)
auto norm(const libMesh::TypeVector< T > &vector) -> decltype(std::norm(T()))
TypeVector< typename CompareTypes< T, T2 >::supertype > supertype
TypeVector< T > supertype