codac 2.0.0
Loading...
Searching...
No Matches
codac2_matrices.h
Go to the documentation of this file.
1
19
20#pragma once
21
22/* On MSVC arm64, Eigen does not enable NEON and its matrix product kernel
23 * fails for mixed scalar types (e.g., Matrix * IntervalMatrix).
24 * Use the pmadd path to support these products.
25 */
26#if defined(_MSC_VER) && defined(_M_ARM64)
27#ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
28#define EIGEN_HAS_SINGLE_INSTRUCTION_MADD
29#endif
30#endif
31
32/* Eigen 5.0.0 overstates byte alignment in std::assume_aligned, triggering
33 * UBSan on macOS/Clang. Disable this hint without changing SIMD loads/stores.
34 */
35#if defined(__APPLE__) && defined(__clang__)
36#ifndef EIGEN_ASSUME_ALIGNED
37#define EIGEN_ASSUME_ALIGNED(PTR, ALIGN_BYTES)
38#endif
39#endif
40
41#include <type_traits>
42#include "codac2_Interval.h"
44#include "codac2_assert.h"
45
46namespace Eigen
47{
48 template<int R,int C>
49 concept IsVectorOrRow = (C == 1 || R == 1);
50
51 template<typename Scalar>
52 concept IsIntervalDomain = std::is_same_v<Scalar,codac2::Interval>;
53}
54
55#define EIGEN_MATRIXBASE_PLUGIN "codac2_MatrixBase_addons_include.h"
56#define EIGEN_MATRIX_PLUGIN "codac2_Matrix_addons_include.h"
57// The option #define EIGEN_INITIALIZE_MATRICES_BY_ZERO is not used because
58// of the interval cases: an IntervalMatrix would have its components initialized
59// to 0 instead of [-oo,oo].
60
61#ifndef EIGEN_NO_DEBUG
62/* Disables Eigen's assertions if defined.
63 * Not defined by default, unless the NDEBUG macro is defined
64 * (this is a standard C++ macro which disables all asserts).
65 * https://eigen.tuxfamily.org/dox/TopicPreprocessorDirectives.html
66 */
67//#define EIGEN_NO_DEBUG // uncomment to disable Eigen's assertions
68#endif
69
70#include <Eigen/Core>
71#include <Eigen/Dense>
72
73namespace Eigen
74{
75 template<> struct NumTraits<codac2::Interval>
76 : NumTraits<double> // permits to get the epsilon, dummy_precision, lowest, highest functions
77 {
78 typedef codac2::Interval Real;
79 typedef codac2::Interval NonInteger;
80 typedef codac2::Interval Nested;
81 typedef codac2::Interval Scalar;
82 typedef double RealScalar;
83
84 enum {
85 IsComplex = 0,
86 IsInteger = 0,
87 IsSigned = 1,
88 RequireInitialization = 1,
89 ReadCost = 1,
90 AddCost = 3,
91 MulCost = 3
92 };
93 };
94
95 template<typename BinOp>
96 struct ScalarBinaryOpTraits<codac2::Interval,double,BinOp>
97 { typedef codac2::Interval ReturnType; };
98
99 template<typename BinOp>
100 struct ScalarBinaryOpTraits<double,codac2::Interval,BinOp>
101 { typedef codac2::Interval ReturnType; };
102}
103
104namespace codac2
105{
106 using Eigen::Dynamic;
107
108 inline const Interval& conj(const Interval& x) { return x; }
109 inline const Interval& real(const Interval& x) { return x; }
110 inline Interval imag(const Interval&) { return 0.; }
111 //inline Interval abs(const Interval& x) { return codac2::abs(x); }
112 inline Interval abs2(const Interval& x) { return codac2::sqr(x); }
113
114 template<typename Scalar,int R,int C>
115 using Mat = Eigen::Matrix<Scalar,R,C>;
116
117 template<int R,int C>
118 struct is_interval_based<Eigen::Matrix<double,R,C>> : std::false_type {};
119
120 template<int R,int C>
121 struct is_interval_based<Eigen::Matrix<Interval,R,C>> : std::true_type {};
122
123 template<typename T,int R,int C>
124 struct is_ctc<Eigen::Matrix<T,R,C>> : std::false_type {};
125
126 template<typename T,int R,int C>
127 struct is_sep<Eigen::Matrix<T,R,C>> : std::false_type {};
128
129 template<typename T>
130 concept is_matrix_base_double =
131 requires {
132 typename Eigen::internal::traits<std::decay_t<T>>::Scalar;
133 requires std::same_as<
134 typename Eigen::internal::traits<std::decay_t<T>>::Scalar,
135 double
136 >;
137 };
138}
139
140namespace codac2
141{
154 template<typename OtherDerived>
155 inline auto abs(const Eigen::MatrixBase<OtherDerived>& x)
156 {
157 using M = Eigen::MatrixBase<OtherDerived>;
158 Eigen::Matrix<typename M::Scalar,M::RowsAtCompileTime,M::ColsAtCompileTime> a(x.rows(),x.cols());
159
160 for(Index i = 0 ; i < x.rows() ; i++)
161 for(Index j = 0 ; j < x.cols() ; j++)
162 {
163 if constexpr(std::is_same_v<typename M::Scalar,double>)
164 a(i,j) = fabs(x(i,j));
165 else
166 a(i,j) = abs(x(i,j));
167 }
168
169 return a;
170 }
171
183 template<typename OtherDerived>
184 requires (!Eigen::IsIntervalDomain<typename OtherDerived::Scalar>)
185 inline auto floor(const Eigen::MatrixBase<OtherDerived>& x)
186 {
187 return x.array().floor().matrix();
188 }
189
201 template<typename OtherDerived>
202 requires (!Eigen::IsIntervalDomain<typename OtherDerived::Scalar>)
203 inline auto ceil(const Eigen::MatrixBase<OtherDerived>& x)
204 {
205 return x.array().ceil().matrix();
206 }
207
219 template<typename OtherDerived>
220 requires (!Eigen::IsIntervalDomain<typename OtherDerived::Scalar>)
221 inline auto round(const Eigen::MatrixBase<OtherDerived>& x)
222 {
223 return x.array().round().matrix();
224 }
225
234 inline Eigen::IOFormat codac_row_fmt()
235 {
236 return Eigen::IOFormat(Eigen::StreamPrecision, Eigen::DontAlignCols, " ", "", "", "", "[ ", " ]");
237 }
238
247 inline Eigen::IOFormat codac_vector_fmt()
248 {
249 return Eigen::IOFormat(Eigen::StreamPrecision, Eigen::DontAlignCols, "", " ; ", "", "", "[ ", " ]");
250 }
251
261 inline Eigen::IOFormat codac_matrix_fmt()
262 {
263 return Eigen::IOFormat(Eigen::StreamPrecision, 0, " , ", "\n", "[ ", " ]", "[", "]");
264 }
265}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:50
Definition codac2_matrices.h:47
Definition codac2_OctaSym.h:21
Interval ceil(const Interval &x)
Returns ceil of .
Definition codac2_Interval_operations_impl.h:294
Eigen::IOFormat codac_vector_fmt()
Provides an Eigen IOFormat for formatting column vectors.
Definition codac2_matrices.h:247
Eigen::IOFormat codac_row_fmt()
Provides an Eigen IOFormat for formatting row vectors.
Definition codac2_matrices.h:234
Interval floor(const Interval &x)
Returns floor of .
Definition codac2_Interval_operations_impl.h:289
auto round(const Eigen::MatrixBase< OtherDerived > &x)
Compute the element-wise rounding of a matrix.
Definition codac2_matrices.h:221
Eigen::Matrix< double,-1,-1 > Matrix
Alias for a dynamic-size matrix of doubles.
Definition codac2_Matrix.h:26
Interval sqr(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:21
Eigen::IOFormat codac_matrix_fmt()
Provides an Eigen IOFormat for formatting matrices.
Definition codac2_matrices.h:261
Interval abs(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:264