cgv
perm_mat.h
1 #pragma once
2 
3 #include "mat.h"
4 #include "vec.h"
5 
6 
7 namespace cgv {
8  namespace math {
12 struct perm_mat
13 {
16 
19 
21  explicit perm_mat(unsigned n)
22  {
23  resize(n);
24  identity();
25  }
26 
29  {
30  _data = p;
31  }
32 
34  unsigned size() const
35  {
36  return _data.size();
37  }
38 
40  unsigned nrows() const
41  {
42  return _data.size();
43  }
44 
46  unsigned ncols() const
47  {
48  return _data.size();
49  }
50 
51 
52 
53 
55  void resize(unsigned n)
56  {
57  _data.resize(n);
58  for(unsigned i = 0; i < n; i++)
59  _data[i] = i;
60 
61  }
62 
64  void transpose()
65  {
67  for(unsigned i = 0; i < _data.size(); i++)
68  temp[_data[i]] = i;
69  _data=temp;
70  }
71 
72  template <typename T>
73  operator const mat<T>() const
74  {
75  mat<T> m;
76  m.zeros(size(),size());
77 
78  for(unsigned i =0; i < size();i++)
79  m(i,_data[i])=(T)1.0;
80 
81  return m;
82  }
83 
86  {
87  assert(size() == m.size());
88  perm_mat r(size());
89 
90  for(unsigned i = 0; i < size();i++)
91  {
92  r(i) = m(operator()(i));
93  }
94 
95  return r;
96  }
97 
98 
99 
100 
102  void identity()
103  {
104  for(unsigned i = 0; i < _data.size(); i++)
105  _data[i] = i;
106  }
107 
108  //add permutation of the entries i and j
109  void swap(unsigned i, unsigned j)
110  {
111 
112  std::swap(_data[i],_data[j]);
113  }
114 
115  unsigned& operator()(unsigned i)
116  {
117  return _data[i];
118  }
119 
120 
121  unsigned& operator[](unsigned i)
122  {
123  return _data[i];
124  }
125 
126 
127  unsigned operator()(unsigned i) const
128  {
129  return _data[i];
130  }
131 
132 
133  unsigned operator[](unsigned i) const
134  {
135  return _data[i];
136  }
137 
138 
139 
140 };
141 
142 
143 //multiplies a permutation matrix from left to apply a rows permutation
144 template<typename T>
145 mat<T> operator*(const perm_mat& p, const mat<T>& m)
146 {
147  mat<T> r(m.nrows(),m.ncols());
148 
149  for(unsigned i = 0; i < m.nrows(); i++)
150  {
151  for(unsigned j = 0; j < m.ncols(); j++)
152  r(p(i),j)=m(i,j);
153  }
154  return r;
155 }
156 
157 
158 
159 
160 //multiplies a permutation matrix from right to perform a column permutation
161 template<typename T>
162 mat<T> operator*(const mat<T>& m, const perm_mat& p)
163 {
164  mat<T> r(m.nrows(),m.ncols());
165 
166  for(unsigned i = 0; i < m.nrows(); i++)
167  {
168  for(unsigned j = 0; j < m.ncols(); j++)
169  r(i,p(j)) = m(i,j);
170  }
171  return r;
172 }
173 
174 
175 
176 
177 
178 //multiplies a permutation matrix from left to perform an element permutation on vector v
179 template<typename T>
180 vec<T> operator*(const perm_mat& p, const vec<T>& v)
181 {
182  vec<T> r(v.size());
183  for(unsigned i = 0; i < v.size(); i++)
184  {
185 
186  r(p[i]) = v(i);
187 
188  }
189  return r;
190 }
191 
192 
193 inline perm_mat transpose(const perm_mat &p)
194 {
195  perm_mat r=p;
196  r.transpose();
197  return r;
198 }
199 
200 
201 
202 
203 inline std::ostream& operator<<(std::ostream& out,const perm_mat& m)
204 {
205 
206  for (int i=0;i<(int)m.size();++i)
207  out << i << "->"<< m(i)<<"\n";
208 
209  return out;
210 }
211 
212 
213 
214 
215 }
216 
217 }
cgv::math::vec::resize
void resize(unsigned dim)
resize the vector
Definition: vec.h:557
cgv::math::perm_mat::nrows
unsigned nrows() const
number of rows
Definition: perm_mat.h:40
cgv::math::perm_mat
Definition: perm_mat.h:13
cgv::math::perm_mat::perm_mat
perm_mat()
standard constructor
Definition: perm_mat.h:18
cgv::math::mat
Definition: mat.h:14
cgv::math::perm_mat::perm_mat
perm_mat(vec< unsigned > p)
creates a permutation from a given permutation vector
Definition: perm_mat.h:28
cgv::math::vec< unsigned >
cgv::math::perm_mat::operator*
perm_mat operator*(const perm_mat &m)
product with another permutation matrix
Definition: perm_mat.h:85
cgv::math::vec::size
unsigned size() const
number of elements
Definition: vec.h:97
cgv::math::perm_mat::_data
vec< unsigned > _data
internal storage of the permutation
Definition: perm_mat.h:15
cgv::math::perm_mat::size
unsigned size() const
number of stored elements
Definition: perm_mat.h:34
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::perm_mat::perm_mat
perm_mat(unsigned n)
create an nxn identity permutation matrix (storage is only n)
Definition: perm_mat.h:21
cgv::math::perm_mat::identity
void identity()
set to identity matrix
Definition: perm_mat.h:102
cgv::math::perm_mat::ncols
unsigned ncols() const
number of rows
Definition: perm_mat.h:46
cgv::math::perm_mat::resize
void resize(unsigned n)
resize the permutation matrix
Definition: perm_mat.h:55
cgv::math::perm_mat::transpose
void transpose()
compute the transpose permutation matrix (equal to inverse permutation)
Definition: perm_mat.h:64
cgv
the cgv namespace
Definition: vr_calib.cxx:9