libMesh
Loading...
Searching...
No Matches
type_vector.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_TYPE_VECTOR_H
21#define LIBMESH_TYPE_VECTOR_H
22
23// Local includes
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"
30
31// C++ includes
32#include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
33#include <cmath>
34#include <complex>
35#include <iostream>
36#include <iomanip> // for std::setw, std::setiosflags
37
38namespace libMesh
39{
40// Forward declarations
41template <typename T> class TypeTensor;
42template <typename T> class VectorValue;
43template <typename T> class TensorValue;
44}
45
46namespace std
47{
48template <typename T>
49auto norm(const libMesh::TypeVector<T> & vector) -> decltype(std::norm(T()));
50}
51
52namespace libMesh
53{
62template <typename T>
64{
65 template <typename T2>
66 friend class TypeVector;
67
68 friend class TypeTensor<T>;
69
70public:
76
77protected:
82 TypeVector (const T & x,
83 const T & y=0,
84 const T & z=0);
85
90 template <typename Scalar1, typename Scalar2, typename Scalar3>
91 TypeVector (typename
92 std::enable_if<ScalarTraits<Scalar1>::value,
93 const Scalar1>::type & x,
94 typename
95 std::enable_if<ScalarTraits<Scalar2>::value,
96 const Scalar2>::type & y=0,
97 typename
98 std::enable_if<ScalarTraits<Scalar3>::value,
99 const Scalar3>::type & z=0);
100
107 template <typename Scalar>
108 TypeVector (const Scalar & x,
109 typename
110 std::enable_if<ScalarTraits<Scalar>::value,
111 const Scalar>::type * sfinae = nullptr);
112
113public:
114
118 typedef T value_type;
119
123 typedef unsigned int index_type;
124
128 template <typename T2>
130
134 TypeVector (const TypeVector & p) = default;
135
139 ~TypeVector () = default;
140
144 template <typename T2>
145 LIBMESH_DEVICE_INLINE
146 void assign (const TypeVector<T2> &);
147
151 template <typename Scalar>
152 LIBMESH_DEVICE_INLINE
153 typename std::enable_if<
155 TypeVector &>::type
156 operator = (const Scalar & libmesh_dbg_var(p))
157 { libmesh_assert_equal_to (p, Scalar(0)); this->zero(); return *this; }
158
162 const T & operator () (const unsigned int i) const;
163 LIBMESH_DEVICE_INLINE
164 const T & slice (const unsigned int i) const { return (*this)(i); }
165
169 T & operator () (const unsigned int i);
170 LIBMESH_DEVICE_INLINE
171 T & slice (const unsigned int i) { return (*this)(i); }
172
178 template <typename T2>
181
187 template <typename T2>
189
193 template <typename T2>
194 void add (const TypeVector<T2> &);
195
199 template <typename T2>
200 LIBMESH_DEVICE_INLINE
201 void add_scaled (const TypeVector<T2> &, const T &);
202
208 template <typename T2>
211
217 template <typename T2>
219
223 template <typename T2>
224 void subtract (const TypeVector<T2> &);
225
230 template <typename T2>
231 LIBMESH_DEVICE_INLINE
232 void subtract_scaled (const TypeVector<T2> &, const T &);
233
238
244 template <typename Scalar>
245 typename std::enable_if<
248 operator * (const Scalar &) const;
249
255 const TypeVector<T> & operator *= (const T &);
256
262 template <typename Scalar>
263 typename std::enable_if<
266 operator / (const Scalar &) const;
267
273 const TypeVector<T> & operator /= (const T &);
274
281 template <typename T2>
284
288 template <typename T2>
289 LIBMESH_DEVICE_INLINE
291 contract (const TypeVector<T2> &) const;
292
296 template <typename T2>
298 cross(const TypeVector<T2> & v) const;
299
303 LIBMESH_DEVICE_INLINE
305
310 auto norm() const;
311
316 auto norm_sq() const;
317
321 LIBMESH_DEVICE_INLINE
322 auto l1_norm() const;
323
327 LIBMESH_DEVICE_INLINE
328 bool is_zero() const;
329
333 LIBMESH_DEVICE_INLINE
334 void zero();
335
341 Real tol = TOLERANCE) const;
342
348 Real tol = TOLERANCE) const;
349
357 LIBMESH_DEVICE_INLINE
358 bool operator == (const TypeVector<T> & rhs) const;
359
363 LIBMESH_DEVICE_INLINE
364 bool operator != (const TypeVector<T> & rhs) const;
365
372 bool operator < (const TypeVector<T> & rhs) const;
373
380 bool operator <= (const TypeVector<T> & rhs) const;
381
388 bool operator > (const TypeVector<T> & rhs) const;
389
396 bool operator >= (const TypeVector<T> & rhs) const;
397
401 void print(std::ostream & os = libMesh::out) const;
402
411 friend std::ostream & operator << (std::ostream & os, const TypeVector<T> & t)
412 {
413 t.print(os);
414 return os;
415 }
416
423 void write_unformatted (std::ostream & out_stream,
424 const bool newline = true) const;
425
426protected:
427
431 T _coords[LIBMESH_DIM];
432};
433
434
435
436
437
438//------------------------------------------------------
439// Inline functions
440
441template <typename T>
442LIBMESH_DEVICE_INLINE
444{
445 _coords[0] = {};
446
447#if LIBMESH_DIM > 1
448 _coords[1] = {};
449#endif
450
451#if LIBMESH_DIM > 2
452 _coords[2] = {};
453#endif
454}
455
456
457
458template <typename T>
459LIBMESH_DEVICE_INLINE
461 const T & y,
462 const T & z)
463{
464 _coords[0] = x;
465
466#if LIBMESH_DIM > 1
467 _coords[1] = y;
468#else
470 libmesh_assert_equal_to (y, 0);
471#endif
472
473#if LIBMESH_DIM > 2
474 _coords[2] = z;
475#else
477 libmesh_assert_equal_to (z, 0);
478#endif
479}
480
481
482template <typename T>
483template <typename Scalar1, typename Scalar2, typename Scalar3>
484LIBMESH_DEVICE_INLINE
486 std::enable_if<ScalarTraits<Scalar1>::value,
487 const Scalar1>::type & x,
488 typename
489 std::enable_if<ScalarTraits<Scalar2>::value,
490 const Scalar2>::type & y,
491 typename
492 std::enable_if<ScalarTraits<Scalar3>::value,
493 const Scalar3>::type & z)
494{
495 _coords[0] = x;
496
497#if LIBMESH_DIM > 1
498 _coords[1] = y;
499#else
500 libmesh_assert_equal_to (y, 0);
501#endif
502
503#if LIBMESH_DIM > 2
504 _coords[2] = z;
505#else
506 libmesh_assert_equal_to (z, 0);
507#endif
508}
509
510
511
512template <typename T>
513template <typename Scalar>
514LIBMESH_DEVICE_INLINE
516 typename
517 std::enable_if<ScalarTraits<Scalar>::value,
518 const Scalar>::type * /*sfinae*/)
519{
520 _coords[0] = x;
521
522#if LIBMESH_DIM > 1
523 _coords[1] = 0;
524#endif
525
526#if LIBMESH_DIM > 2
527 _coords[2] = 0;
528#endif
529}
530
531
532
533template <typename T>
534template <typename T2>
535LIBMESH_DEVICE_INLINE
537{
538 // copy the nodes from vector p to me
539 for (unsigned int i=0; i<LIBMESH_DIM; i++)
540 _coords[i] = p._coords[i];
541}
542
543
544
545template <typename T>
546template <typename T2>
547LIBMESH_DEVICE_INLINE
549{
550 for (unsigned int i=0; i<LIBMESH_DIM; i++)
551 _coords[i] = p._coords[i];
552}
553
554
555
556template <typename T>
557LIBMESH_DEVICE_INLINE
558const T & TypeVector<T>::operator () (const unsigned int i) const
559{
560 libmesh_assert_less (i, LIBMESH_DIM);
561
562 return _coords[i];
563}
564
565
566
567template <typename T>
568LIBMESH_DEVICE_INLINE
569T & TypeVector<T>::operator () (const unsigned int i)
570{
571 libmesh_assert_less (i, LIBMESH_DIM);
572
573 return _coords[i];
574}
575
576
577
578template <typename T>
579template <typename T2>
580LIBMESH_DEVICE_INLINE
583{
584 typedef typename CompareTypes<T, T2>::supertype TS;
585#if LIBMESH_DIM == 1
586 return TypeVector<TS> (_coords[0] + p._coords[0]);
587#endif
588
589#if LIBMESH_DIM == 2
590 return TypeVector<TS> (_coords[0] + p._coords[0],
591 _coords[1] + p._coords[1]);
592#endif
593
594#if LIBMESH_DIM == 3
595 return TypeVector<TS> (_coords[0] + p._coords[0],
596 _coords[1] + p._coords[1],
597 _coords[2] + p._coords[2]);
598#endif
599
600}
601
602
603
604template <typename T>
605template <typename T2>
606LIBMESH_DEVICE_INLINE
608{
609 this->add (p);
610
611 return *this;
612}
613
614
615
616template <typename T>
617template <typename T2>
618LIBMESH_DEVICE_INLINE
620{
621#if LIBMESH_DIM == 1
622 _coords[0] += p._coords[0];
623#endif
624
625#if LIBMESH_DIM == 2
626 _coords[0] += p._coords[0];
627 _coords[1] += p._coords[1];
628#endif
629
630#if LIBMESH_DIM == 3
631 _coords[0] += p._coords[0];
632 _coords[1] += p._coords[1];
633 _coords[2] += p._coords[2];
634#endif
635
636}
637
638
639
640template <typename T>
641template <typename T2>
642LIBMESH_DEVICE_INLINE
643void TypeVector<T>::add_scaled (const TypeVector<T2> & p, const T & factor)
644{
645#if LIBMESH_DIM == 1
646 _coords[0] += factor*p(0);
647#endif
648
649#if LIBMESH_DIM == 2
650 _coords[0] += factor*p(0);
651 _coords[1] += factor*p(1);
652#endif
653
654#if LIBMESH_DIM == 3
655 _coords[0] += factor*p(0);
656 _coords[1] += factor*p(1);
657 _coords[2] += factor*p(2);
658#endif
659
660}
661
662
663
664template <typename T>
665template <typename T2>
666LIBMESH_DEVICE_INLINE
669{
670 typedef typename CompareTypes<T, T2>::supertype TS;
671
672#if LIBMESH_DIM == 1
673 return TypeVector<TS>(_coords[0] - p._coords[0]);
674#endif
675
676#if LIBMESH_DIM == 2
677 return TypeVector<TS>(_coords[0] - p._coords[0],
678 _coords[1] - p._coords[1]);
679#endif
680
681#if LIBMESH_DIM == 3
682 return TypeVector<TS>(_coords[0] - p._coords[0],
683 _coords[1] - p._coords[1],
684 _coords[2] - p._coords[2]);
685#endif
686
687}
688
689
690
691template <typename T>
692template <typename T2>
693LIBMESH_DEVICE_INLINE
695{
696 this->subtract (p);
697
698 return *this;
699}
700
701
702
703template <typename T>
704template <typename T2>
705LIBMESH_DEVICE_INLINE
707{
708 for (unsigned int i=0; i<LIBMESH_DIM; i++)
709 _coords[i] -= p._coords[i];
710}
711
712
713
714template <typename T>
715template <typename T2>
716LIBMESH_DEVICE_INLINE
717void TypeVector<T>::subtract_scaled (const TypeVector<T2> & p, const T & factor)
718{
719 for (unsigned int i=0; i<LIBMESH_DIM; i++)
720 _coords[i] -= factor*p(i);
721}
722
723
724
725template <typename T>
726LIBMESH_DEVICE_INLINE
728{
729
730#if LIBMESH_DIM == 1
731 return TypeVector(-_coords[0]);
732#endif
733
734#if LIBMESH_DIM == 2
735 return TypeVector(-_coords[0],
736 -_coords[1]);
737#endif
738
739#if LIBMESH_DIM == 3
740 return TypeVector(-_coords[0],
741 -_coords[1],
742 -_coords[2]);
743#endif
744
745}
746
747
748
749template <typename T>
750template <typename Scalar>
751LIBMESH_DEVICE_INLINE
752typename std::enable_if<
755TypeVector<T>::operator * (const Scalar & factor) const
756{
757 typedef typename CompareTypes<T, Scalar>::supertype SuperType;
758
759#if LIBMESH_DIM == 1
760 return TypeVector<SuperType>(_coords[0]*factor);
761#endif
762
763#if LIBMESH_DIM == 2
764 return TypeVector<SuperType>(_coords[0]*factor,
765 _coords[1]*factor);
766#endif
767
768#if LIBMESH_DIM == 3
769 return TypeVector<SuperType>(_coords[0]*factor,
770 _coords[1]*factor,
771 _coords[2]*factor);
772#endif
773}
774
775
776
777template <typename T, typename Scalar>
778LIBMESH_DEVICE_INLINE
779typename std::enable_if<
782operator * (const Scalar & factor,
783 const TypeVector<T> & v)
784{
785 return v * factor;
786}
787
788
789
790template <typename T>
791LIBMESH_DEVICE_INLINE
793{
794#if LIBMESH_DIM == 1
795 _coords[0] *= factor;
796#endif
797
798#if LIBMESH_DIM == 2
799 _coords[0] *= factor;
800 _coords[1] *= factor;
801#endif
802
803#if LIBMESH_DIM == 3
804 _coords[0] *= factor;
805 _coords[1] *= factor;
806 _coords[2] *= factor;
807#endif
808
809 return *this;
810}
811
812
813
814template <typename T>
815template <typename Scalar>
816LIBMESH_DEVICE_INLINE
817typename std::enable_if<
820TypeVector<T>::operator / (const Scalar & factor) const
821{
822 typedef typename CompareTypes<T, Scalar>::supertype TS;
823
824 libmesh_assert_not_equal_to (static_cast<TS>(factor),
825 static_cast<TS>(0.));
826
827#if LIBMESH_DIM == 1
828 return TypeVector<TS>(_coords[0]/factor);
829#endif
830
831#if LIBMESH_DIM == 2
832 return TypeVector<TS>(_coords[0]/factor,
833 _coords[1]/factor);
834#endif
835
836#if LIBMESH_DIM == 3
837 return TypeVector<TS>(_coords[0]/factor,
838 _coords[1]/factor,
839 _coords[2]/factor);
840#endif
841
842}
843
844
845
846
847template <typename T>
848LIBMESH_DEVICE_INLINE
849const TypeVector<T> &
851{
852 libmesh_assert_not_equal_to (factor, static_cast<T>(0.));
853
854 for (unsigned int i=0; i<LIBMESH_DIM; i++)
855 _coords[i] /= factor;
856
857 return *this;
858}
859
860
861
862
863template <typename T>
864template <typename T2>
865LIBMESH_DEVICE_INLINE
868{
869#if LIBMESH_DIM == 1
870 return _coords[0]*p._coords[0];
871#endif
872
873#if LIBMESH_DIM == 2
874 return (_coords[0]*p._coords[0] +
875 _coords[1]*p._coords[1]);
876#endif
877
878#if LIBMESH_DIM == 3
879 return (_coords[0]*p(0) +
880 _coords[1]*p(1) +
881 _coords[2]*p(2));
882#endif
883}
884
885template <typename T>
886template <typename T2>
887LIBMESH_DEVICE_INLINE
890{
891 return (*this)*(p);
892}
893
894
895
896template <typename T>
897template <typename T2>
898LIBMESH_DEVICE_INLINE
901{
902 typedef typename CompareTypes<T, T2>::supertype TS;
903 libmesh_assert_equal_to (LIBMESH_DIM, 3);
904
905 // | i j k |
906 // |(*this)(0) (*this)(1) (*this)(2)|
907 // | p(0) p(1) p(2) |
908
909#if LIBMESH_DIM == 3
910 return TypeVector<TS>( _coords[1]*p._coords[2] - _coords[2]*p._coords[1],
911 -_coords[0]*p._coords[2] + _coords[2]*p._coords[0],
912 _coords[0]*p._coords[1] - _coords[1]*p._coords[0]);
913#else
915 return TypeVector<TS>(0);
916#endif
917}
918
919
920
921template <typename T>
922LIBMESH_DEVICE_INLINE
924{
925 using std::sqrt;
926
927 // The norm is not differentiable at the origin: the directional
928 // derivative there depends on the direction of approach, so there is no
929 // well-defined gradient, and computing sqrt(norm_sq()) directly would
930 // divide by zero in the derivative terms of AD types. We return a zero
931 // derivative in this case, consistent with the MetaPhysicL convention for
932 // other functions with the same kind of non-differentiable point (e.g.
933 // abs()).
934 if (this->is_zero())
935 return decltype(sqrt(this->norm_sq())){};
936
937 return sqrt(this->norm_sq());
938}
939
940
941
942template <typename T>
943LIBMESH_DEVICE_INLINE
945{
946 for (unsigned int i=0; i<LIBMESH_DIM; i++)
947 _coords[i] = 0.;
948}
949
950
951
952template <typename T>
953LIBMESH_DEVICE_INLINE
955{
956#if LIBMESH_DIM == 1
957 return (TensorTools::norm_sq(_coords[0]));
958#endif
959
960#if LIBMESH_DIM == 2
961 return (TensorTools::norm_sq(_coords[0]) +
962 TensorTools::norm_sq(_coords[1]));
963#endif
964
965#if LIBMESH_DIM == 3
966 return (TensorTools::norm_sq(_coords[0]) +
967 TensorTools::norm_sq(_coords[1]) +
968 TensorTools::norm_sq(_coords[2]));
969#endif
970}
971
972// float, double, and long double have no derivatives to regularize, and
973// sqrt(0) is already exactly 0 for them, so skip the zero check in the
974// generic norm() above for these scalar types to avoid the extra branch in
975// the common case.
976template <>
977inline
979{
980 using std::sqrt;
981 return sqrt(this->norm_sq());
982}
983
984template <>
985inline
987{
988 using std::sqrt;
989 return sqrt(this->norm_sq());
990}
991
992template <>
993inline
995{
996 using std::sqrt;
997 return sqrt(this->norm_sq());
998}
999
1000#ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION
1001// In quadruple precision builds Real is a distinct type from float, double,
1002// and long double, so it needs its own specialization to get the same
1003// unbranched treatment as the other scalar types above.
1004template <>
1005inline
1007{
1008 using std::sqrt;
1009 return sqrt(this->norm_sq());
1010}
1011#endif
1012
1013
1014template <typename T>
1015LIBMESH_DEVICE_INLINE
1017{
1018 for (const auto & val : _coords)
1019 if (val != T(0))
1020 return false;
1021 return true;
1022}
1023
1024template <>
1025auto
1027
1028template <typename T>
1029LIBMESH_DEVICE_INLINE
1030auto
1032{
1033 using std::abs;
1034 decltype(abs(T())) ret{};
1035 for (const auto i : make_range(libmesh_dim))
1036 ret += abs(_coords[i]);
1037
1038 return ret;
1039}
1040
1041template <typename T>
1042inline
1044{
1045 return libMesh::absolute_fuzzy_equals(*this, rhs, tol);
1046}
1047
1048
1049
1050template <typename T>
1051inline
1053{
1054 return libMesh::relative_fuzzy_equals(*this, rhs, tol);
1055}
1056
1057
1058
1059template <typename T>
1060LIBMESH_DEVICE_INLINE
1062{
1063#if LIBMESH_DIM == 1
1064 return (_coords[0] == rhs._coords[0]);
1065#endif
1066
1067#if LIBMESH_DIM == 2
1068 return (_coords[0] == rhs._coords[0] &&
1069 _coords[1] == rhs._coords[1]);
1070#endif
1071
1072#if LIBMESH_DIM == 3
1073 return (_coords[0] == rhs._coords[0] &&
1074 _coords[1] == rhs._coords[1] &&
1075 _coords[2] == rhs._coords[2]);
1076#endif
1077}
1078
1079
1080
1081template <typename T>
1082LIBMESH_DEVICE_INLINE
1084{
1085 return (!(*this == rhs));
1086}
1087
1088
1089//------------------------------------------------------
1090// Non-member functions on TypeVectors
1091
1092// Compute a * (b.cross(c)) without creating a temporary
1093// for the cross product. Equivalent to the determinant
1094// of the 3x3 tensor:
1095// [a0, a1, a2]
1096// [b0, b1, b2]
1097// [c0, c1, c2]
1098template <typename T>
1099LIBMESH_DEVICE_INLINE
1101 const TypeVector<T> & b,
1102 const TypeVector<T> & c)
1103{
1104#if LIBMESH_DIM == 3
1105 return
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));
1109#else
1110 libmesh_ignore(a, b, c);
1111 return 0;
1112#endif
1113}
1114
1115
1116// compute the solid angle subtended by the tetrahedral vertex 0, as
1117// defined by vectors v01, v02, and v03. The solid angle is defined
1118// to be positive if the vectors are obey the right-hand rule, or
1119// negative for a left-hand orientation.
1120template <typename T>
1121LIBMESH_DEVICE_INLINE
1123 const TypeVector<T> & v02,
1124 const TypeVector<T> & v03)
1125{
1126 using std::atan;
1127
1128 const Real norm01 = v01.norm(),
1129 norm02 = v02.norm(),
1130 norm03 = v03.norm();
1131 const T tan_half_angle =
1132 triple_product(v01, v02, v03) /
1133 ((v01*v02)*norm03 + (v01*v03)*norm02 + (v02*v03)*norm01 +
1134 norm01*norm02*norm03);
1135
1136 return Real(2)*atan(tan_half_angle);
1137}
1138
1139
1140
1141// Compute the (possibly 3D) circumcenter of three non-collinear points
1142//
1143// If perfectly collinear points are provided, this algorithm computes
1144// NaN. If near-collinear points are provided, the algorithm may be
1145// numerically unstable.
1146template <typename T>
1147inline
1149 const TypeVector<T> & p1,
1150 const TypeVector<T> & p2)
1151{
1152 // smallest subchords in Edge3 order, but this works for any order
1153 const TypeVector<T> e02 = p2 - p0;
1154 const TypeVector<T> e21 = p1 - p2;
1155
1156 const TypeVector<T> scaled_vec = e02.norm_sq()*e21 + e21.norm_sq()*e02;
1157#if LIBMESH_DIM == 3
1158 const TypeVector<T> numerator =
1159 (e02.cross(e21)).cross(scaled_vec);
1160#else
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)};
1164#endif
1165 const TypeVector<T> e2C =
1166 numerator*T(0.5)/cross_norm_sq(e02,e21);
1167
1168 return p2 + e2C;
1169}
1170
1171
1172
1177template <typename T>
1178LIBMESH_DEVICE_INLINE
1180 const TypeVector<T> & c)
1181{
1182 T z = b(0)*c(1) - b(1)*c(0);
1183
1184#if LIBMESH_DIM == 3
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;
1188#else
1189 return z*z;
1190#endif
1191}
1192
1193
1194
1198template <typename T>
1199LIBMESH_DEVICE_INLINE
1201 const TypeVector<T> & c)
1202{
1203 using std::sqrt;
1204 return sqrt(cross_norm_sq(b,c));
1205}
1206
1207template <typename T>
1208LIBMESH_DEVICE_INLINE
1210{
1211
1212 auto && length = norm();
1213
1214 libmesh_assert_not_equal_to (length, static_cast<Real>(0.));
1215
1216#if LIBMESH_DIM == 1
1217 return TypeVector<T>(_coords[0]/length);
1218#endif
1219
1220#if LIBMESH_DIM == 2
1221 return TypeVector<T>(_coords[0]/length,
1222 _coords[1]/length);
1223#endif
1224
1225#if LIBMESH_DIM == 3
1226 return TypeVector<T>(_coords[0]/length,
1227 _coords[1]/length,
1228 _coords[2]/length);
1229#endif
1230
1231}
1232
1233template <typename T>
1234void TypeVector<T>::print(std::ostream & os) const
1235{
1236#if LIBMESH_DIM == 1
1237
1238 os << "x=" << (*this)(0);
1239
1240#endif
1241#if LIBMESH_DIM == 2
1242
1243 os << "(x,y)=("
1244 << std::setw(8) << (*this)(0) << ", "
1245 << std::setw(8) << (*this)(1) << ")";
1246
1247#endif
1248#if LIBMESH_DIM == 3
1249
1250 os << "(x,y,z)=("
1251 << std::setw(8) << (*this)(0) << ", "
1252 << std::setw(8) << (*this)(1) << ", "
1253 << std::setw(8) << (*this)(2) << ")";
1254#endif
1255}
1256
1257
1258// A vector is finite iff every component is
1259template <typename T>
1260bool isfinite (const TypeVector<T> & var)
1261{
1262 using std::isfinite;
1263 using libMesh::isfinite; // for T==complex
1264 for (auto i : make_range(LIBMESH_DIM))
1265 if (!isfinite(var(i)))
1266 return false;
1267 return true;
1268}
1269
1270
1271// A vector is infinite iff some component is infinite but no
1272// component is NaN.
1273//
1274// This is arguably inconsistent with our std::complex overload (and
1275// the C99 Annex G recommendations for _Complex, and C++ std::complex
1276// arithmetic), which treats mixed (inf,NaN) pairs as infinite, but
1277// this is probably safer for users.
1278template <typename T>
1279bool isinf (const TypeVector<T> & var)
1280{
1281 using std::isinf;
1282 using libMesh::isinf; // for T==complex
1283 using std::isnan;
1284 using libMesh::isnan;
1285 bool has_inf = false;
1286 for (auto i : make_range(LIBMESH_DIM))
1287 {
1288 // NaN anywhere makes us NaN, not inf
1289 if (isnan(var(i)))
1290 return false;
1291 has_inf = has_inf || isinf(var(i));
1292 }
1293 return has_inf;
1294}
1295
1296
1297// A vector is NaN iff some component is NaN
1298//
1299// This is arguably inconsistent with our std::complex overload (and
1300// the C99 Annex G recommendations for _Complex, and C++ std::complex
1301// arithmetic), which treats mixed (inf,NaN) pairs as infinite, but
1302// this is probably safer for users.
1303template <typename T>
1304bool isnan (const TypeVector<T> & var)
1305{
1306 using std::isnan;
1307 using libMesh::isnan; // for T==complex
1308 for (auto i : make_range(LIBMESH_DIM))
1309 if (isnan(var(i)))
1310 return true;
1311 return false;
1312}
1313
1314
1315template <typename T>
1320
1321template <typename T, typename T2>
1326
1327template <typename T, typename T2, typename std::enable_if<ScalarTraits<T>::value, int>::type = 0>
1328LIBMESH_DEVICE_INLINE
1330outer_product(const T & a, const TypeVector<T2> & b)
1331{
1333 for (unsigned int i = 0; i < LIBMESH_DIM; i++)
1334 ret(i) = a * libmesh_conj(b(i));
1335
1336 return ret;
1337}
1338
1339template <typename T, typename T2, typename std::enable_if<ScalarTraits<T2>::value, int>::type = 0>
1340LIBMESH_DEVICE_INLINE
1341TypeVector<typename CompareTypes<T, T2>::supertype>
1342outer_product(const TypeVector<T> & a, const T2 & b)
1343{
1345 const auto conj_b = libmesh_conj(b);
1346 for (unsigned int i = 0; i < LIBMESH_DIM; i++)
1347 ret(i) = a(i) * conj_b;
1348
1349 return ret;
1350}
1351
1352template <typename T>
1353auto
1355{
1356 return var.l1_norm();
1357}
1358
1359template <typename T, typename T2>
1360auto
1361l1_norm_diff(const TypeVector<T> & vec1, const TypeVector<T2> & vec2)
1362{
1363 return l1_norm(vec1 - vec2);
1364}
1365
1366} // namespace libMesh
1367
1368namespace std
1369{
1370template <typename T>
1371LIBMESH_DEVICE_INLINE
1372auto norm(const libMesh::TypeVector<T> & vector) -> decltype(std::norm(T()))
1373{
1374 // Yea I agree it's dumb that the standard returns the square of the Euclidean norm
1375 return vector.norm_sq();
1376}
1377} // namespace std
1378
1379#ifdef LIBMESH_HAVE_METAPHYSICL
1380namespace MetaPhysicL
1381{
1382template <typename T>
1383struct RawType<libMesh::TypeVector<T>>
1384{
1386
1388 {
1389 value_type ret;
1390 for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
1391 ret(i) = raw_value(in(i));
1392
1393 return ret;
1394 }
1395};
1396
1397template <typename T, typename U>
1398struct ReplaceAlgebraicType<libMesh::TypeVector<T>, U>
1399{
1400 typedef U type;
1401};
1402}
1403#endif
1404
1405#endif // LIBMESH_TYPE_VECTOR_H
This class defines a tensor in LIBMESH_DIM dimensional space of type T.
Definition type_tensor.h:54
This class defines a vector in LIBMESH_DIM dimensional space of type T.
Definition type_vector.h:64
LIBMESH_DEVICE_INLINE auto l1_norm() const
bool operator>(const TypeVector< T > &rhs) const
Definition type_vector.C:95
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
Definition type_vector.C:80
void write_unformatted(std::ostream &out_stream, const bool newline=true) const
Unformatted print to the stream out.
Definition type_vector.C:51
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.
auto norm_sq() const
auto norm() const
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
Definition type_vector.C:66
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.
friend class TypeVector
Definition type_vector.h:66
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.
static const Real b
LIBMESH_DEVICE_INLINE auto norm_sq(const T &a)
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)
OStreamProxy out
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...
Definition int_range.h:176
LIBMESH_DEVICE_INLINE T libmesh_conj(T a)
auto norm(const libMesh::TypeVector< T > &vector) -> decltype(std::norm(T()))
libMesh::TypeVector< typename RawType< T >::value_type > value_type
static value_type value(const libMesh::TypeVector< T > &in)
TypeVector< typename CompareTypes< T, T2 >::supertype > supertype