codac 1.5.6
Loading...
Searching...
No Matches
codac2_Matrix.h
Go to the documentation of this file.
1
12#ifndef __CODAC2_MATRIX_H__
13#define __CODAC2_MATRIX_H__
14
15#include <codac_Matrix.h>
16#include <codac2_eigen.h>
17
18namespace codac2
19{
20 using Eigen::Dynamic;
21
22 template<int R=Dynamic,int C=Dynamic>
23 class Matrix_ : public Eigen::Matrix<double,R,C>
24 {
25 public:
26
27 Matrix_()
28 : Eigen::Matrix<double,R,C>()
29 { }
30
31 Matrix_(int nb_rows, int nb_cols)
32 : Eigen::Matrix<double,R,C>(nb_rows, nb_cols)
33 {
34 assert(R == Dynamic || R == (int)nb_rows);
35 assert(C == Dynamic || C == (int)nb_cols);
36 }
37
38 Matrix_(int nb_rows, int nb_cols, double x)
39 : Eigen::Matrix<double,R,C>(nb_rows, nb_cols)
40 {
41 assert(R == Dynamic || R == (int)nb_rows);
42 assert(C == Dynamic || C == (int)nb_cols);
43 init(x);
44 }
45
46 explicit Matrix_(int nb_rows, int nb_cols, const double values[])
47 : Matrix_<R,C>(nb_rows, nb_cols)
48 {
49 int k = 0;
50 for(int i = 0 ; i < nb_rows ; i++)
51 for(int j = 0 ; j < nb_cols ; j++)
52 {
53 if(values == 0) // in case the user called Matrix_(r,c,0) and 0 is interpreted as NULL!
54 (*this)(i,j) = 0.;
55 else
56 (*this)(i,j) = values[k];
57 k++;
58 }
59 }
60
61 explicit Matrix_(const double values[])
62 : Matrix_<R,C>(R, C, values)
63 { }
64
65 Matrix_(std::initializer_list<std::initializer_list<double>> l)
66 : Matrix_<R,C>()
67 {
68 assert((R == Dynamic || (int)l.size() == R) && "ill-formed matrix");
69 int cols = -1;
70 for(const auto& ri : l) {
71 assert(cols == -1 || cols == (int)ri.size() && "ill-formed matrix");
72 cols = (int)ri.size();
73 }
74 this->resize(l.size(),cols);
75 size_t i = 0;
76 for(const auto& ri : l)
77 {
78 size_t j = 0;
79 for(const auto& ci : ri)
80 (*this)(i,j++) = ci;
81 i++;
82 }
83 // todo: use thias as faster? std::copy(l.begin(), l.end(), vec);
84 }
85
86 template<typename OtherDerived>
87 Matrix_(const Eigen::MatrixBase<OtherDerived>& other)
88 : Eigen::Matrix<double,R,C>(other)
89 { }
90
91 // This method allows you to assign Eigen expressions to Matrix_
92 template<typename OtherDerived>
93 Matrix_& operator=(const Eigen::MatrixBase<OtherDerived>& other)
94 {
95 this->Eigen::Matrix<double,R,C>::operator=(other);
96 return *this;
97 }
98
99 void init(double x)
100 {
101 for(size_t i = 0 ; i < this->size() ; i++)
102 *(this->data()+i) = x;
103 }
104
105 static Matrix_ eye()
106 {
107 return Eigen::Matrix<double,R,C>::Identity();
108 }
109
110 double min() const
111 {
112 return Eigen::Matrix<double,R,C>::minCoeff();
113 }
114
115 double max() const
116 {
117 return Eigen::Matrix<double,R,C>::maxCoeff();
118 }
119
120 auto operator+(const Matrix_<R,C>& x) const
121 {
122 auto y = *this;
123 return y += x;
124 }
125
126 auto operator-(const Matrix_<R,C>& x) const
127 {
128 auto y = *this;
129 return y -= x;
130 }
131
132 auto operator-() const
133 {
134 return Eigen::Matrix<double,R,C>::operator-();
135 }
136
137 auto operator&(const Matrix_<R,C>& x) const
138 {
139 auto y = *this;
140 return y &= x;
141 }
142
143 auto operator|(const Matrix_<R,C>& x) const
144 {
145 auto y = *this;
146 return y |= x;
147 }
148
149 auto& operator+=(const Matrix_<R,C>& x)
150 {
151 (*this).noalias() += x;//.template cast<Interval>();
152 return *this;
153 }
154
155 auto& operator-=(const Matrix_<R,C>& x)
156 {
157 (*this).noalias() -= x;//.template cast<Interval>();
158 return *this;
159 }
160
161 static Matrix_<R,C> zeros()
162 {
163 return Eigen::Matrix<double,R,C>::Zero();
164 }
165
166 static Matrix_<R,C> ones()
167 {
168 return Eigen::Matrix<double,R,C>::Ones();
169 }
170 };
171
172 template<int R,int C>
173 std::ostream& operator<<(std::ostream& os, const Matrix_<R,C>& x)
174 {
175 os << "(";
176 for(size_t i = 0 ; i < x.rows() ; i++)
177 {
178 os << "(";
179 for(size_t j = 0 ; j < x.cols() ; j++)
180 os << x(i,j) << (j<x.cols()-1 ? " ; " : "");
181 os << ")";
182 if(i < x.rows()-1) os << std::endl;
183 }
184 os << ")";
185 return os;
186 }
187
188 #include "ibex_Matrix.h"
189 template<int R=Dynamic,int C=Dynamic>
190 ibex::Matrix to_codac1(const Matrix_<R,C>& x)
191 {
192 ibex::Matrix x_(x.rows(), x.cols());
193 for(size_t i = 0 ; i < x.rows() ; i++)
194 for(size_t j = 0 ; j < x.cols() ; j++)
195 x_[i][j] = x(i,j);
196 return x_;
197 }
198
199 template<int R,int C>
200 Matrix_<R,C> floor(const Matrix_<R,C>& x)
201 {
202 Matrix_<R,C> f(x.rows(), x.cols());
203 for(size_t i = 0 ; i < x.size() ; i++)
204 *(f.data()+i) = std::floor(*(x.data()+i));
205 return f;
206 }
207
208 template<int R,int C>
209 Matrix_<R,C> round(const Matrix_<R,C>& x)
210 {
211 Matrix_<R,C> f(x.rows(), x.cols());
212 for(size_t i = 0 ; i < x.size() ; i++)
213 *(f.data()+i) = std::round(*(x.data()+i));
214 return f;
215 }
216
217 template<int R,int C>
218 Matrix_<R,C> ceil(const Matrix_<R,C>& x)
219 {
220 Matrix_<R,C> f(x.rows(), x.cols());
221 for(size_t i = 0 ; i < x.size() ; i++)
222 *(f.data()+i) = std::ceil(*(x.data()+i));
223 return f;
224 }
225
226 template<int R,int C>
227 Matrix_<R,C> abs(const Matrix_<R,C>& x)
228 {
229 Matrix_<R,C> f(x.rows(), x.cols());
230 for(size_t i = 0 ; i < x.size() ; i++)
231 *(f.data()+i) = std::fabs(*(x.data()+i));
232 return f;
233 }
234
235 class Matrix : public Matrix_<>
236 {
237 public:
238
239 explicit Matrix(size_t nb_rows, size_t nb_cols)
240 : Matrix_<>(nb_rows, nb_cols)
241 { }
242
243
244 explicit Matrix(size_t nb_rows, size_t nb_cols, double x)
245 : Matrix_<>(nb_rows, nb_cols, x)
246 { }
247
248 explicit Matrix(size_t nb_rows, size_t nb_cols, const double values[])
249 : Matrix_<>(nb_rows, nb_cols, values)
250 { }
251
252 Matrix(const Matrix_<>& x)
253 : Matrix_<>(x)
254 { }
255
256 Matrix(std::initializer_list<std::initializer_list<double>> l)
257 : Matrix_<>(l)
258 { }
259
260 template<int R,int C>
261 explicit Matrix(const Matrix_<R,C>& v)
262 : Matrix_<>(v)
263 { }
264 };
265} // namespace codac
266
267#endif