cgv
Loading...
Searching...
No Matches
perm_mat.h
1#pragma once
2
3#include "mat.h"
4#include "vec.h"
5
6
7namespace cgv {
8 namespace math {
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
144template<typename T>
145mat<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
161template<typename T>
162mat<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
179template<typename T>
180vec<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
193inline perm_mat transpose(const perm_mat &p)
194{
195 perm_mat r=p;
196 r.transpose();
197 return r;
198}
199
200
201
202
203inline 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}
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
void zeros()
set zero matrix
Definition mat.h:1030
A column vector class.
Definition vec.h:28
void resize(unsigned dim)
resize the vector
Definition vec.h:496
unsigned size() const
number of elements
Definition vec.h:59
the cgv namespace
Definition print.h:11
std::ostream & operator<<(std::ostream &os, const vr_device_info &di)
stream out operator for device infos
Definition vr_info.cxx:10
a permutation matrix type
Definition perm_mat.h:13
void identity()
set to identity matrix
Definition perm_mat.h:102
void transpose()
compute the transpose permutation matrix (equal to inverse permutation)
Definition perm_mat.h:64
vec< unsigned > _data
internal storage of the permutation
Definition perm_mat.h:15
unsigned ncols() const
number of rows
Definition perm_mat.h:46
void resize(unsigned n)
resize the permutation matrix
Definition perm_mat.h:55
perm_mat operator*(const perm_mat &m)
product with another permutation matrix
Definition perm_mat.h:85
perm_mat(unsigned n)
create an nxn identity permutation matrix (storage is only n)
Definition perm_mat.h:21
unsigned size() const
number of stored elements
Definition perm_mat.h:34
perm_mat(vec< unsigned > p)
creates a permutation from a given permutation vector
Definition perm_mat.h:28
perm_mat()
standard constructor
Definition perm_mat.h:18
unsigned nrows() const
number of rows
Definition perm_mat.h:40