cgv
fmat.h
1 #pragma once
2 
3 #include "fvec.h"
4 #include <cassert>
5 
6 namespace cgv {
8  namespace math {
9 
11 
21 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
22 class fmat : public fvec<T,N*M>
23 {
24 public:
30  fmat() {}
32  fmat(const T& c) : base_type(c) {}
34  fmat(cgv::type::uint32_type n, cgv::type::uint32_type m, const T* a, bool column_major = true) {
35  for (cgv::type::uint32_type j = 0; j < std::min(m, M); ++j)
36  for (cgv::type::uint32_type i = 0; i < std::min(n, N); ++i)
37  (*this)(i, j) = a[column_major ? j * n + i : i * m + j];
38  }
40  template <typename S>
41  fmat(cgv::type::uint32_type n, cgv::type::uint32_type m, const S* a, bool column_major = true) {
42  for (cgv::type::uint32_type j = 0; j < std::min(m, M); ++j)
43  for (cgv::type::uint32_type i = 0; i < std::min(n, N); ++i)
44  (*this)(i, j) = (T)a[column_major ? j * n + i : i * m + j];
45  }
47  template <typename S>
48  fmat(const fmat<S,N,M>& m) : base_type(m) {}
50  template <typename T1, typename T2>
51  fmat(const fvec<T1,N>& v, const fvec<T2,M>& w) {
52  for(unsigned i = 0; i < N; i++)
53  for(unsigned j = 0; j < M; j++)
54  (*this)(i,j) = (T)(v(i)*w(j));
55  }
57  static unsigned nrows() { return N; }
59  static unsigned ncols() { return M; }
61  template <typename S>
64  return *this;
65  }
67  this_type& operator = (const T& s) {
68  fill (s);
69  return *this;
70  }
72  bool is_square() const { return N == M; }
74  T& operator () (unsigned i, unsigned j) {
75  assert(i < N && j < M);
76  return base_type::v[j*N+i];
77  }
79  const T& operator () (unsigned i, unsigned j) const {
80  assert(i < N && j < M);
81  return base_type::v[j*N+i];
82  }
83  //in place scalar multiplication
84  this_type& operator *= (const T& s) { base_type::operator *= (s); return *this; }
86  this_type operator * (const T& s) const { this_type r=(*this); r*=(T)s; return r; }
88  fmat<T,N,M>& operator /= (const T& s) { base_type::operator /= (s); return *this; }
90  const fmat<T,N,M> operator / (const T& s) const { this_type r=(*this); r/=(T)s; return r; }
92  fmat<T,N,M>& operator += (const T& s) { base_type::operator += (s); return *this; }
94  const fmat<T,N,M> operator + (const T& s) { this_type r=(*this); r+=(T)s; return r; }
96  fmat<T,N,M>& operator -= (const T& s) { base_type::operator -= (s); return *this; }
98  const fmat<T,N,M> operator - (const T& s) { this_type r=(*this); r-=(T)s; return r; }
100  const fmat<T,N,M> operator-() const { return (*this)*(-1); }
102  template <typename S>
105  template <typename S>
108  template <typename S>
109  const fmat<T,N,M> operator+(const fmat<S,N,M> m2)const { fmat<T,N,M> r=(*this); r += m2; return r; }
111  template <typename S>
112  const fmat<T,N,M> operator-(const fmat<S,N,M> m2)const { fmat<T,N,M> r=(*this); r -= m2; return r; }
114  template <typename S>
116  {
117  assert(N == M);
118  fmat<T,N,N> r(T(0));
119  for(unsigned i = 0; i < N; i++)
120  for(unsigned j = 0; j < N;j++)
121  for(unsigned k = 0; k < N; k++)
122  r(i,j) += operator()(i,k) * (T)(m2(k,j));
123  (*this)=r;
124  return *this;
125  }
127  template <typename S, cgv::type::uint32_type L>
128  const fmat<T,N,L> operator*(const fmat<S,M,L>& m2) const
129  {
130  fmat<T,N,L> r; r.zeros();
131  for(unsigned i = 0; i < N; i++)
132  for(unsigned j = 0; j < L;j++)
133  for(unsigned k = 0; k < M; k++)
134  r(i,j) += operator()(i,k) * (T)(m2(k,j));
135  return r;
136  }
137 
139  template < typename S>
140  const fvec<T,N> operator * (const fvec<S,M>& v) const {
141  fvec<T,N> r;
142  for(unsigned i = 0; i < N; i++)
143  r(i) = dot(row(i),v);
144  return r;
145  }
147  const fvec<T,M> row(unsigned i) const {
148  fvec<T,M> r;
149  for(unsigned j = 0; j < M; j++)
150  r(j)=operator()(i,j);
151  return r;
152  }
154  void set_row(unsigned i,const fvec<T,M>& v) {
155  for(unsigned j = 0; j < M;j++)
156  operator()(i,j)=v(j);
157  }
159  const fvec<T,N>& col(unsigned j) const {
160  return *(const fvec<T,N>*)(&operator()(0,j));
161  }
163  void set_col(unsigned j,const fvec<T,N>& v) {
164  for(unsigned i = 0; i < N;i++)
165  operator()(i,j)=v(i);
166  }
168  T trace() const {
169  assert(N == M);
170  T t = 0;
171  for(unsigned i = 0; i < N;i++)
172  t+=operator()(i,i);
173  return t;
174  }
175 
177  void transpose() {
178  assert(N == M);
179  for(unsigned i = 1; i < N; i++)
180  for(unsigned j = 0; j < i; j++)
181  std::swap(operator()(i,j), operator()(j,i));
182  }
184  T frobenius_norm() const { return base_type::length(); }
186  void identity()
187  {
189  for(unsigned i = 0; i < M && i < N; ++i)
190  operator()(i,i)=1;
191  }
192 };
193 
195 template <typename T, cgv::type::uint32_type N>
197 {
198  fmat<T,N,N> m_t(m);
199  m_t.transpose();
200  return m_t;
201 }
202 
204 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
205 fmat<T,N,M> operator * (const T& s, const fmat<T,N,M>& m)
206 {
207  return m*s;
208 }
209 
211 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
212 fvec<T, M> operator * (const fvec<T, N>& v_row, const fmat<T, N, M>& m)
213 {
214  fvec<T, M> r_row;
215  for (unsigned i = 0; i < M; i++)
216  r_row(i) = dot(m.col(i), v_row);
217  return r_row;
218 }
219 
221 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
222 std::ostream& operator<<(std::ostream& out, const fmat<T,N,M>& m)
223 {
224  for (unsigned i=0;i<N;++i) {
225  for(unsigned j =0;j < M-1;++j)
226  out << m(i,j) << " ";
227  out << m(i,M-1);
228  if(i != N-1)
229  out <<"\n";
230  }
231  return out;
232 }
233 
235 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
236 std::istream& operator>>(std::istream& in, fmat<T,N,M>& m)
237 {
238  for (unsigned i=0;i<m.nrows();++i)
239  for(unsigned j =0;j < m.ncols();++j)
240  in >> m(i,j);
241  return in;
242 }
243 
244 
245 
247 template < typename T, cgv::type::uint32_type N, typename S, cgv::type::uint32_type M>
249 {
250  fmat<T, N, M> m;
251  for (unsigned i = 0; i < N; i++)
252  for (unsigned j = 0; j < M; j++)
253  m(i, j) = v(i)*(T)w(j);
254  return m;
255 }
256 
258 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
259 const fmat<T, N, M> lerp(const fmat<T, N, M>& m1, const fmat<T, N, M>& m2, T t)
260 {
261  fmat<T, N, M> m;
262  for(unsigned i = 0; i < N; i++)
263  for(unsigned j = 0; j < M; j++)
264  m(i, j) = ((T)1 - t)*m1(i, j) + t * m2(i, j);
265  return m;
266 }
267 
269 template <typename T, cgv::type::uint32_type N, cgv::type::uint32_type M>
271 {
272  fmat<T, N, M> m;
273  for(unsigned i = 0; i < N; i++)
274  for(unsigned j = 0; j < M; j++)
275  m(i, j) = ((T)1 - t(i, j))*m1(i, j) + t(i, j)*m2(i, j);
276  return m;
277 }
278 
279  }
280 
281 }
cgv::math::fvec< T, N *M >::operator-=
fvec< T, N > & operator-=(const T &s)
in place subtraction by scalar s
Definition: fvec.h:161
cgv::math::fmat::this_type
fmat< T, N, M > this_type
base type is a vector with sufficent number of elements
Definition: fmat.h:28
cgv::math::fmat
matrix of fixed size dimensions
Definition: fmat.h:23
cgv::math::fvec< T, N *M >::length
T length() const
length of the vector L2-Norm
Definition: fvec.h:219
cgv::math::fvec< T, N *M >::operator+=
fvec< T, N > & operator+=(const T &s)
in place addition of a scalar s
Definition: fvec.h:159
cgv::math::fvec< T, N *M >::zeros
void zeros()
fill the vector with zeros
Definition: fvec.h:115
cgv::math::fvec
Definition: fvec.h:18
cgv::math::fmat::operator*
const fmat< T, N, L > operator*(const fmat< S, M, L > &m2) const
multiplication with a ncols x M matrix m2
Definition: fmat.h:128
cgv::math::fmat::operator*=
const fmat< T, N, M > operator*=(const fmat< S, N, N > &m2)
in place matrix multiplication with a ncols x ncols matrix m2
Definition: fmat.h:115
cgv::math::fmat::set_col
void set_col(unsigned j, const fvec< T, N > &v)
set column j of the matrix to vector v
Definition: fmat.h:163
cgv::math::fmat::operator()
T & operator()(unsigned i, unsigned j)
access to the element in the ith row in column j
Definition: fmat.h:74
cgv::math::fmat::operator+=
fmat< T, N, M > & operator+=(const T &s)
in place addition by a scalar
Definition: fmat.h:92
cgv::math::fmat::operator+
const fmat< T, N, M > operator+(const T &s)
componentwise addition of a scalar
Definition: fmat.h:94
cgv::math::fvec< T, N *M >::w
T & w()
fourth element
Definition: fvec.h:141
cgv::math::fmat::set_row
void set_row(unsigned i, const fvec< T, M > &v)
set row i of the matrix to vector v
Definition: fmat.h:154
cgv::math::fvec< T, N *M >::operator*=
fvec< T, N > & operator*=(const T &s)
in place multiplication with s
Definition: fvec.h:163
cgv::math::fmat::operator+
const fmat< T, N, M > operator+(const fmat< S, N, M > m2) const
matrix addition
Definition: fmat.h:109
cgv::math::fmat::operator/
const fmat< T, N, M > operator/(const T &s) const
division by a scalar
Definition: fmat.h:90
cgv::math::fmat::transpose
void transpose()
transpose matrix
Definition: fmat.h:177
cgv::math::fmat::fmat
fmat(const fvec< T1, N > &v, const fvec< T2, M > &w)
construct from outer product of vector v and w
Definition: fmat.h:51
cgv::math::fmat::frobenius_norm
T frobenius_norm() const
returns the frobenius norm of matrix m
Definition: fmat.h:184
cgv::math::transpose
fmat< T, N, N > transpose(const fmat< T, N, N > &m)
return the transposed of a square matrix
Definition: fmat.h:196
cgv::math::operator<<
std::ostream & operator<<(std::ostream &out, const diag_mat< T > &m)
output of a diagonal matrix onto an ostream
Definition: diag_mat.h:442
cgv::math::fmat::base_type
fvec< T, N *M > base_type
base type is a vector with sufficent number of elements
Definition: fmat.h:26
cgv::math::fmat::is_square
bool is_square() const
returns true if matrix is a square matrix
Definition: fmat.h:72
cgv::math::fmat::operator=
fmat< T, N, M > & operator=(const fmat< S, N, M > &m)
assignment of a matrix with a different element type
Definition: fmat.h:62
cgv::math::dyad
fmat< T, N, M > dyad(const fvec< T, N > &v, const fvec< S, M > &w)
returns the outer product of vector v and w
Definition: fmat.h:248
cgv::math::lerp
const fmat< T, N, M > lerp(const fmat< T, N, M > &m1, const fmat< T, N, M > &m2, T t)
linear interpolation returns (1-t)*m1 + t*m2
Definition: fmat.h:259
cgv::math::fmat::nrows
static unsigned nrows()
number of rows
Definition: fmat.h:57
cgv::math::fvec< T, N *M >::fill
void fill(const T &a)
fill elements of vector with scalar v
Definition: fvec.h:113
cgv::math::operator>>
std::istream & operator>>(std::istream &in, diag_mat< T > &m)
input of a diagonal matrix from an istream
Definition: diag_mat.h:453
cgv::math::fmat::row
const fvec< T, M > row(unsigned i) const
extract a row from the matrix as a vector, this is done by a type cast
Definition: fmat.h:147
cgv::math::fmat::fmat
fmat()
standard constructor
Definition: fmat.h:30
cgv::type::uint32_type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
Definition: standard_types.h:20
cgv::math::fmat::fmat
fmat(const fmat< S, N, M > &m)
copy constructor for matrix with different element type
Definition: fmat.h:48
cgv::math::fvec< T, N *M >::operator=
fvec & operator=(const fvec< T, N > &rhs)
assign vector rhs, if vector and rhs have different sizes, vector has been resized to match the size ...
Definition: fvec.h:103
cgv::math::dot
T dot(const fvec< T, N > &v, const fvec< T, N > &w)
returns the dot product of vector v and w
Definition: fvec.h:300
cgv::math::fmat::operator-=
fmat< T, N, M > & operator-=(const T &s)
in place substraction of a scalar
Definition: fmat.h:96
cgv::math::fmat::identity
void identity()
set identity matrix
Definition: fmat.h:186
cgv::math::fmat::operator/=
fmat< T, N, M > & operator/=(const T &s)
in place division by a scalar
Definition: fmat.h:88
cgv::math::fmat::operator-
const fmat< T, N, M > operator-() const
negation operator
Definition: fmat.h:100
cgv::math::fmat::fmat
fmat(cgv::type::uint32_type n, cgv::type::uint32_type m, const T *a, bool column_major=true)
creates a matrix from an array a of given dimensions - by default in column major format
Definition: fmat.h:34
cgv::math::fmat::fmat
fmat(cgv::type::uint32_type n, cgv::type::uint32_type m, const S *a, bool column_major=true)
creates a matrix from an array a of given dimensions but different type - by default in column major ...
Definition: fmat.h:41
cgv::math::fmat::fmat
fmat(const T &c)
construct a matrix with all elements set to c
Definition: fmat.h:32
cgv::math::fmat::operator-
const fmat< T, N, M > operator-(const fmat< S, N, M > m2) const
matrix subtraction
Definition: fmat.h:112
cgv::math::fmat::col
const fvec< T, N > & col(unsigned j) const
extract a column of the matrix as a vector
Definition: fmat.h:159
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::math::fmat::operator*
this_type operator*(const T &s) const
scalar multiplication
Definition: fmat.h:86
cgv::math::fmat::ncols
static unsigned ncols()
number of columns
Definition: fmat.h:59
cgv::math::fmat::trace
T trace() const
returns the trace
Definition: fmat.h:168
cgv::math::fvec< T, N *M >::operator/=
fvec< T, N > & operator/=(const T &s)
in place division by scalar s
Definition: fvec.h:165