18template <
typename RandomAccessIterator>
22 typedef RandomAccessIterator base_type;
23 typedef typename base_type::pointer pointer;
24 typedef typename base_type::reference reference;
25 typedef typename base_type::value_type value_type;
26 typedef typename base_type::difference_type difference_type;
27 typedef typename base_type::iterator_category iterator_category;
29 RandomAccessIterator internal_iter;
33 friend class mat<value_type>;
35 step_iterator( RandomAccessIterator begin,
int step=1):internal_iter(begin),step(step)
49 internal_iter=other.internal_iter;
57 internal_iter = other.internal_iter;
65 return internal_iter == other.internal_iter;
70 return !(*
this==other);
75 return *internal_iter;
78 reference operator*()
const
80 return *internal_iter;
87 pointer operator->()
const
122 internal_iter+=n*step;
129 internal_iter-=n*step;
141 return (internal_iter - right.internal_iter)/step;
152 reference operator[](difference_type offset)
const
154 return (*(*
this + offset));
160 return internal_iter < other.internal_iter;
162 return internal_iter > other.internal_iter;
168 return internal_iter > other.internal_iter;
170 return internal_iter < other.internal_iter;
176 return internal_iter <= other.internal_iter;
178 return internal_iter >= other.internal_iter;
184 return internal_iter >= other.internal_iter;
186 return internal_iter <= other.internal_iter;
219 typedef typename vec<T>::value_type value_type;
220 typedef typename vec<T>::reference reference;
221 typedef typename vec<T>::const_reference const_reference;
222 typedef typename vec<T>::pointer pointer;
223 typedef typename vec<T>::const_pointer const_pointer;
224 typedef typename vec<T>::iterator iterator;
225 typedef typename vec<T>::const_iterator const_iterator;
226 typedef typename vec<T>::reverse_iterator reverse_iterator;
227 typedef typename vec<T>::const_reverse_iterator const_reverse_iterator;
229 typedef iterator col_iterator;
230 typedef const col_iterator const_col_iterator;
231 typedef std::reverse_iterator<col_iterator> reverse_col_iterator;
232 typedef std::reverse_iterator<const_col_iterator> const_reverse_col_iterator;
236 typedef std::reverse_iterator<row_iterator> reverse_row_iterator;
237 typedef std::reverse_iterator<const_row_iterator> const_reverse_row_iterator;
241 typedef std::reverse_iterator<diag_iterator> reverse_diag_iterator;
242 typedef std::reverse_iterator<const_diag_iterator> const_reverse_diag_iterator;
246 typedef std::reverse_iterator<anti_diag_iterator> reverse_anti_diag_iterator;
247 typedef std::reverse_iterator<const_anti_diag_iterator> const_reverse_anti_diag_iterator;
253 iterator begin(){
return _data.begin();}
254 iterator end(){
return _data.end();}
255 const_iterator begin()
const{
return _data.begin();}
256 const_iterator end()
const{
return _data.end();}
257 reverse_iterator rbegin(){
return _data.rbegin();}
258 reverse_iterator rend(){
return _data.rend();}
259 const_reverse_iterator rbegin()
const{
return _data.rbegin();}
260 const_reverse_iterator rend()
const {
return _data.rend();}
264 col_iterator col_begin(
int c)
268 col_iterator col_end(
int c)
272 const_col_iterator col_begin(
int c)
const
274 return const_col_iterator(
_data.begin()+(c*
_nrows));
276 const_col_iterator col_end(
int c)
const
278 return const_col_iterator(
_data.begin()+(c+1)*
_nrows );
280 reverse_col_iterator col_rbegin(
int c)
282 return (reverse_col_iterator(col_end(c)));
284 reverse_col_iterator col_rend(
int c)
286 return (reverse_col_iterator(col_begin(c)));
288 const_reverse_col_iterator col_rbegin(
int c)
const
290 return (const_reverse_col_iterator(col_end(c)));
292 const_reverse_col_iterator col_rend(
int c)
const
294 return (const_reverse_col_iterator(col_begin(c)));
297 row_iterator row_begin(
int r)
301 row_iterator row_end(
int r)
305 const_row_iterator row_begin(
int r)
const
309 const_row_iterator row_end(
int r)
const
314 reverse_row_iterator row_rbegin(
int r)
316 return (reverse_row_iterator(row_end(r)));
319 reverse_row_iterator row_rend(
int r)
321 return (reverse_row_iterator(row_begin(r)));
324 const_reverse_row_iterator row_rbegin(
int r)
const
326 return (const_reverse_row_iterator(row_end(r)));
329 const_reverse_row_iterator row_rend(
int r)
const
331 return (const_reverse_row_iterator(row_begin(r)));
337 diag_iterator diag_begin(
int d=0)
345 diag_iterator diag_end(
int d=0)
358 const_diag_iterator diag_begin(
int d=0)
const
361 return const_diag_iterator(begin()-d ,
_nrows+1);
366 const_diag_iterator diag_end(
int d=0)
const
379 reverse_diag_iterator diag_rbegin(
int d=0)
381 return (reverse_diag_iterator(diag_end(d)));
383 reverse_diag_iterator diag_rend(
int d=0)
385 return (reverse_diag_iterator(diag_begin(d)));
387 const_reverse_diag_iterator diag_rbegin(
int d=0)
const
389 return (const_reverse_diag_iterator(diag_end(d)));
391 const_reverse_diag_iterator diag_rend(
int d=0)
const
393 return (const_reverse_diag_iterator(diag_begin(d)));
396 anti_diag_iterator anti_diag_begin(
int d=0)
404 anti_diag_iterator anti_diag_end(
int d=0)
418 const_anti_diag_iterator anti_diag_begin(
int d=0)
const
426 const_anti_diag_iterator anti_diag_end(
int d=0)
const
440 reverse_anti_diag_iterator anti_diag_rbegin(
int d=0)
442 return (reverse_anti_diag_iterator(anti_diag_end(d)));
445 reverse_anti_diag_iterator anti_diag_rend(
int d=0)
447 return (reverse_anti_diag_iterator(anti_diag_begin(d)));
450 const_reverse_anti_diag_iterator anti_diag_rbegin(
int d=0)
const
452 return (const_reverse_anti_diag_iterator(anti_diag_end(d)));
455 const_reverse_anti_diag_iterator anti_diag_rend(
int d=0)
const
457 return (const_reverse_anti_diag_iterator(anti_diag_begin(d)));
492 memcpy(
_data.data(), marray,
size()*
sizeof(T));
496 for(
unsigned i = 0; i <
_nrows; i++)
497 for(
unsigned j = 0; j <
_ncols;j++)
509 template <
typename S>
514 for(
unsigned i = 0; i <
_nrows; i++)
515 for(
unsigned j = 0; j <
_ncols;j++)
554 template <
typename S>
558 for(
unsigned i = 0; i <
_nrows; i++)
559 for(
unsigned j = 0; j <
_ncols;j++)
574 void resize(
unsigned rows,
unsigned cols)
577 unsigned newsize = rows*cols;
580 _data.resize(newsize);
590 operator const T*()
const
592 return (
const T*)
_data;
602 template <
typename S>
619 template <
typename S>
629 template <
typename S>
640 mat<T>& operator *= (
const T& s)
708 template <
typename S>
717 template <
typename S>
729 template <
typename S>
738 template <
typename S>
748 template <
typename S>
754 for(
unsigned i = 0; i <
_nrows; i++)
755 for(
unsigned j = 0; j <
_ncols;j++)
756 for(
unsigned k = 0; k <
_ncols; k++)
766 template <
typename S>
770 unsigned M = m2.
ncols();
772 for(
unsigned i = 0; i <
_nrows; i++)
773 for(
unsigned j = 0; j < M;j++)
774 for(
unsigned k = 0; k <
_ncols; k++)
782 template <
typename S>
789 for(
unsigned j = 0; j <
_ncols; j++)
790 for(
unsigned i = 0; i <
_nrows; i++)
797 mat<T> sub_mat(
unsigned top,
unsigned left,
unsigned rows,
unsigned cols)
const
802 for(
unsigned i = 0; i < rows;i++)
803 for(
unsigned j = 0; j < cols;j++)
816 for(
unsigned j = 0; j <
_ncols;j++)
826 for(
unsigned j = 0; j <
_ncols;j++)
827 operator()(i,j)=v(j);
839 for(
unsigned i = 0; i <
_nrows;i++)
849 for(
unsigned i = 0; i <
_nrows;i++)
850 operator()(i,j)=v(i);
857 void copy(
unsigned top,
unsigned left,
unsigned rows,
unsigned cols,
mat<T>& submat)
const
859 assert(submat.
nrows() == rows && submat.
ncols() == cols);
861 for(
unsigned i = 0; i < rows;i++)
862 for(
unsigned j = 0; j < cols;j++)
871 for(
unsigned i = 0; i < m.
nrows(); i++)
872 for(
unsigned j = 0; j < m.
ncols(); j++)
873 operator()(i+top,j+left)=m(i,j);
882 for(
unsigned k = 0; k <
_ncols;k++)
883 std::swap(
operator()(i,k),
operator()(j,k));
890 for(
unsigned k = 0; k <
_nrows;k++)
891 std::swap(
operator()(k,i),
operator()(k,j));
899 std::swap(
operator()(i,i),
operator()(j,j));
910 for(
unsigned i = 0; i <
_nrows;i++)
918 for (
unsigned i = 0; i <
_nrows; i++)
919 for (
unsigned j = 0; j <
_ncols; j++)
920 if (t <
operator()(i, j))
928 for (
unsigned i = 0; i <
_nrows; i++)
929 for (
unsigned j = 0; j <
_ncols; j++)
930 if (t >
operator()(i, j))
940 for(
unsigned i = 0; i <
_nrows;i++)
941 for(
unsigned j = 0; j <
_ncols;j++)
952 for(
unsigned i = 0; i <
_nrows;i++)
953 for(
unsigned j = 0; j <
_ncols;j++)
965 for(
unsigned i = 0; i <
_nrows;i++)
966 for(
unsigned j = 0; j <
_ncols;j++)
976 for(
unsigned i = 0; i <
_nrows;i++)
977 for(
unsigned j = 0; j <
_ncols;j++)
978 operator()(i,j) =
::ceil(
operator()(i,j));
984 for(
unsigned i = 0; i <
_nrows;i++)
985 for(
unsigned j = 0; j <
_ncols;j++)
986 operator()(i,j) =
::floor(
operator()(i,j));
992 for(
unsigned i = 0; i <
_nrows;i++)
993 for(
unsigned j = 0; j <
_ncols;j++)
994 operator()(i,j) =
::floor(
operator()(i,j)+(T)0.5);
1004 for(
unsigned i =0; i <
_nrows;i++)
1005 for(
unsigned j=0;j <
_ncols;j++)
1006 n+=
operator()(i,j)*
operator()(i,j);
1008 return (T)sqrt((
double)n);
1017 for(
unsigned i = 0; i <
_ncols;++i)
1025 for(
unsigned i = 0; i <
_ncols;++i)
1036 void zeros(
unsigned rows,
unsigned cols)
1043 void ones(
unsigned rows,
unsigned cols)
1055#define CGV_MATH_MAT_DECLARED
1057template <
typename T>
1058mat<T> zeros(
unsigned rows,
unsigned cols)
1065template <
typename T>
1066mat<T> ones(
unsigned rows,
unsigned cols)
1068 mat<T> m(rows,cols);
1074mat<T> identity(
unsigned dim)
1083T frobenius_norm(
const mat<T>& m)
1085 return m.frobenius_norm();
1090T trace(
const mat<T>& m)
1097template<
typename T,
typename S>
1098const mat<T> horzcat(
const mat<T>& m1,
const mat<S>& m2)
1101 assert(m1.nrows() == m2.nrows());
1102 mat<T> r(m1.nrows(),m1.ncols()+m2.ncols());
1103 for(
unsigned i = 0; i < m1.nrows();i++)
1105 for(
unsigned j = 0; j < m1.ncols(); j++)
1107 for(
unsigned j = 0; j < m2.ncols(); j++)
1108 r(i,j+m1.ncols())=(T)m2(i,j);
1114template<
typename T,
typename S>
1115const mat<T> horzcat(
const mat<T>& m1,
const vec<S>& v)
1118 assert(m1.nrows() == v.size());
1119 mat<T> r(m1.nrows(),m1.ncols()+1);
1120 for(
unsigned i = 0; i < m1.nrows();i++)
1122 for(
unsigned j = 0; j < m1.ncols(); j++)
1125 r(i,m1.ncols())=(T)v(i);
1131template<
typename T,
typename S>
1132const mat<T> horzcat(
const vec<T>& v1,
const vec<S>& v2)
1135 assert(v1.size() == v2.size());
1136 mat<T> r(v1.size(),2);
1137 for(
unsigned i = 0; i < v1.size();i++)
1146template<
typename T,
typename S>
1147const vec<T> vertcat(
const vec<T>& v1,
const vec<S>& v2)
1150 vec<T> r(v1.size()+v2.size());
1151 unsigned off = v1.size();
1152 for(
unsigned i = 0; i < v1.size();i++)
1154 for(
unsigned i = 0; i < v2.size();i++)
1162template<
typename T,
typename S>
1163const mat<T> vertcat(
const mat<T>& m1,
const mat<S>& m2)
1166 assert(m1.ncols() == m2.ncols());
1167 mat<T> r(m1.nrows()+m2.nrows(),m1.ncols());
1168 for(
unsigned i = 0; i < m1.nrows();i++)
1170 for(
unsigned j = 0; j < m1.ncols(); j++)
1174 for(
unsigned i = 0; i < m2.nrows();i++)
1176 for(
unsigned j = 0; j < m1.ncols(); j++)
1177 r(i+m1.nrows(),j) = m2(i,j);
1187template <
typename T>
1188const mat<T> transpose(
const mat<T> &m)
1195template <
typename T>
1196const mat<T> transpose(
const vec<T> &v)
1198 mat<T> r(1, v.size());
1199 for(
unsigned i = 0; i < v.size(); i++)
1206template <
typename T>
1207const mat<T> ceil(
const mat<T> &m)
1215template <
typename T>
1216const mat<T> floor(
const mat<T> &m)
1224template <
typename T>
1225const mat<T> round(
const mat<T> &m)
1236template <
typename T>
1237mat<T> operator * (
const T& s,
const mat<T>& m)
1245template <
typename T>
1246std::ostream&
operator<<(std::ostream& out,
const mat<T>& m)
1249 for (
unsigned i=0;i<m.nrows();++i)
1251 for(
unsigned j =0;j < m.ncols()-1;++j)
1253 out << m(i,m.ncols()-1);
1254 if(i != m.nrows()-1)
1264template <
typename T>
1265std::istream& operator>>(std::istream& in, mat<T>& m)
1267 assert(m.size() > 0);
1268 for (
unsigned i=0;i<m.nrows();++i)
1269 for(
unsigned j =0;j < m.ncols();++j)
1283template <
typename T>
1284void AtA(
const mat<T>& a, mat<T>& ata)
1286 ata.resize(a.ncols(),a.ncols());
1288 for(
unsigned r = 0; r < a.nrows();r++)
1290 for(
unsigned i = 0; i < a.ncols();i++)
1292 for(
unsigned j = 0; j < a.ncols();j++)
1294 ata(i,j)+=a(r,i)*a(r,j);
1300template <
typename T>
1301void AAt(
const mat<T>& a, mat<T>& aat)
1303 aat.resize(a.nrows(),a.nrows());
1305 for(
unsigned c = 0; c < a.ncols();c++)
1307 for(
unsigned i = 0; i < a.nrows();i++)
1309 for(
unsigned j = 0; j < a.nrows();j++)
1311 aat(i,j)+=a(i,c)*a(j,c);
1318template <
typename T>
1319mat<T> reshape(
const vec<T>& v,
unsigned r,
unsigned c)
1321 assert(v.size() == r*c);
1322 return mat<T>(r,c,(
const T*)v);
1325template <
typename T>
1326mat<T> reshape(
const mat<T>& m,
unsigned r,
unsigned c)
1328 assert(m.size() == r*c);
1329 return mat<T>(r,c,(
const T*)m);
1333template <
typename T>
1334void AtB(
const mat<T>& a,
const mat<T>& b, mat<T>& atb)
1336 atb.resize(a.ncols(),b.ncols());
1339 for(
unsigned i = 0; i < a.ncols(); i++)
1340 for(
unsigned j = 0; j < b.ncols();j++)
1341 for(
unsigned k = 0; k < a.nrows(); k++)
1342 atb(i,j) += a(k,i)*b(k,j);
1348template <
typename T>
1349void Atx(
const mat<T>& a,
const vec<T>& x, vec<T>& atx)
1351 atx.resize(a.ncols());
1354 for(
unsigned i = 0; i < a.ncols(); i++)
1355 for(
unsigned j = 0; j < a.nrows(); j++)
1356 atx(i) += a(j,i) * (T)(x(j));
1363template <
typename T,
typename S>
1364mat<T> dyad(
const vec<T>& v,
const vec<S>& w)
1366 mat<T> m(v.size(),w.size());
1368 for(
unsigned i = 0; i < v.size();i++)
1369 for(
unsigned j = 0; j < w.size();j++)
1370 m(i,j) =v(i)*(T)w(j);
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
void copy(unsigned top, unsigned left, unsigned rows, unsigned cols, mat< T > &submat) const
copy submatrix m(top,left)...m(top+rows,left+cols) into submat
const mat< T > operator-() const
negation operator
T trace() const
returns the trace
unsigned ncols() const
number of columns
mat< T > sub_mat(unsigned top, unsigned left, unsigned rows, unsigned cols) const
create submatrix m(top,left)...m(top+rows,left+cols)
mat(unsigned nrows, unsigned ncols, const T *marray, bool column_major=true)
creates a matrix from an array if the matrix data is stored in a row major fashion set column_major t...
mat< T > & operator/=(const T &s)
in place division by a scalar
void set_col(unsigned j, const vec< T > &v)
set column j of the matrix to vector v
void identity(unsigned dim)
set dim x dim identity matrix
const mat< T > operator/(const T &s) const
division by a scalar
const mat< T > operator*(const mat< S > &m2) const
multiplication with a ncols x M matrix m2
void floor()
floor all components of the matrix
mat()
standard constructor
unsigned _ncols
number of columns
const mat< T > operator*=(const mat< S > &m2)
in place matrix multiplication with a ncols x ncols matrix m2
T & operator()(unsigned i, unsigned j)
access to the element in the ith row in column j
bool operator!=(const mat< S > &m) const
test for inequality
void paste(int top, int left, const mat< T > &m)
paste matrix m at position: top, left
void swap_columns(unsigned i, unsigned j)
exchange column i with column j
mat< T > & operator+=(const T &s)
in place addition by a scalar
void ceil()
ceil all components of the matrix
mat(unsigned nrows, unsigned ncols)
constructor creates a nrows x ncols full matrix
void zeros(unsigned rows, unsigned cols)
resize and fill matrix with zeros
vec< T > _data
pointer to data storage
const mat< T > operator-(const mat< S > m2) const
matrix subtraction
void resize(unsigned rows, unsigned cols)
resize the matrix, the content of the matrix will be destroyed
void identity()
set identity matrix
void swap_rows(unsigned i, unsigned j)
exchange row i with row j
mat(unsigned nrows, unsigned ncols, const T &c)
construct a matrix with all elements set to c
const mat< T > operator+(const T &s)
componentwise addition of a scalar
void set_row(unsigned i, const vec< T > &v)
set row i of the matrix to vector v
void fill(const S &v)
fills all elements of the matrix with v
unsigned size() const
number of stored elements
mat< T > & operator-=(const T &s)
in place substraction of a scalar
const vec< T > col(unsigned j) const
extract a column of the matrix as a vector
void round()
round to integer
void ones(unsigned rows, unsigned cols)
resize and fill matrix with ones
void zeros()
set zero matrix
void set_extern_data(unsigned nrows, unsigned ncols, T *data)
set data pointer to an external data array
void swap_diagonal_elements(unsigned i, unsigned j)
exchange diagonal elements (i,i) (j,j)
mat< T > & operator=(const mat< S > &m)
assignment of a matrix with a different element type
T frobenius_norm() const
returns the frobenius norm of matrix m
bool operator==(const mat< S > &m) const
test for equality
unsigned _nrows
number of rows
const mat< T > operator*(const T &s) const
scalar multiplication
mat(const mat< S > &m)
copy constructor for matrix with different element type
bool is_square() const
returns true if matrix is a square matrix
const mat< T > operator+(const mat< S > m2) const
matrix addition
void transpose()
transpose matrix
const vec< T > operator*(const vec< S > &v) const
matrix vector multiplication
unsigned nrows() const
number of rows
const vec< T > row(unsigned i) const
extract a row from the matrix as a vector
unsigned size() const
number of elements
void zeros()
fill the vector with zeros
cgv::math::mat< double > dmatn
declare type of matrices of varying dimensions
cgv::math::mat< float > matn
declare type of matrices of varying dimensions
std::ostream & operator<<(std::ostream &os, const vr_device_info &di)
stream out operator for device infos