cgv
Loading...
Searching...
No Matches
up_tri_mat.h
1#pragma once
2
3#include "vec.h"
4#include "mat.h"
5#include "low_tri_mat.h"
6
7namespace cgv {
8 namespace math {
9
10template <typename T>
12{
13
14protected:
15 vec<T> _data;
16
17 unsigned _dim;
18
19
20public:
21
22 //standard constructor of a upper triangular matrix
24 {
25 _dim=0;
26 }
27
28 //constructs a upper triangular matrix from the upper triangular part of m
29 up_tri_mat(const mat<T>& m)
30 {
31 assert(m.ncols()==m.nrows());
32 resize(m.ncols());
33
34 for(unsigned i =0; i < _dim;i++)
35 {
36 for(unsigned j = i; j < _dim;j++)
37 {
38 operator()(i,j)=m(i,j);
39 }
40 }
41 }
42
43 //creates a dim x dim upper triangular matrix
44 up_tri_mat(unsigned dim)
45 {
46 resize(dim);
47 fill(0);
48 }
49
50 up_tri_mat(const up_tri_mat& m)
51 {
52 resize(m.dim());
53 memcpy(_data,m._data,size()*sizeof(T));
54 }
55
56 //create a dim x dim upper triangular matrix with all non-zero elements set to c
57 up_tri_mat(unsigned dim, const T& c)
58 {
59 resize(dim);
60 fill(c);
61 }
62
63 virtual ~up_tri_mat()
64 {
65 }
66
67 //resize the matrix to an n x n matrix
68 void resize(unsigned n)
69 {
70 _dim = n;
71 _data.resize(n*(n+1)/2);
72
73 }
74
75 operator mat<T>()
76 {
77 mat<T> m(_dim,_dim);
78 for(unsigned i =0; i < _dim; i++)
79 {
80 for(unsigned j = 0; j < _dim; j++)
81 {
82 if(i>j)
83 m(i,j)=0;
84 else
85 m(i,j)=operator()(i,j);
86 }
87
88 }
89 return m;
90 }
91
92 operator const mat<T>() const
93 {
94 mat<T> m(_dim,_dim);
95 for(unsigned i =0; i < _dim;i++)
96 {
97 for(unsigned j = 0; j < _dim;j++)
98 {
99 if(i>j)
100 m(i,j)=0;
101 else
102 m(i,j)=operator()(i,j);
103 }
104
105 }
106 return m;
107 }
108
111 {
112 _dim = m.dim();
113 _data = m._data;
114 return *this;
115 }
116
117 //return number of stored elements
118 unsigned size() const
119 {
120 return _data.size();
121 }
122
123 //return number of stored elements
124 unsigned dim() const
125 {
126 return _dim;
127 }
128
129 //returns the number of columns
130 unsigned ncols() const
131 {
132 return _dim;
133 }
134
135 //returns the number of columns
136 unsigned nrows() const
137 {
138 return _dim;
139 }
140
141 //fills all upper triangular elements with c
142 void fill(const T& c)
143 {
144 for(unsigned i=0;i < size();i++)
145 {
146 _data[i]=c;
147 }
148 }
149
150 //access to the element (i,j)
151 T& operator() (unsigned i, unsigned j)
152 {
153 assert( j >= i && j < _dim);
154 return _data[j*(j+1)/2+i];
155 }
156
157 //const access to the element (i,j)
158 const T& operator() (unsigned i, unsigned j) const
159 {
160 assert( j >= i && j < _dim);
161 return _data[j*(j+1)/2+i];
162 }
163
166 {
167 T val = (T)s;
168 for(unsigned i = 0; i < size(); i++)
169
170 _data[i] /= val;
171 return *this;
172 }
173
176 {
177 up_tri_mat<T> r=(*this);
178 r/=s;
179 return r;
180 }
181
182
183 const mat<T> operator*( const up_tri_mat<T>& m2)
184 {
185 assert(m2.nrows() == nrows());
186 unsigned M = m2.ncols();
187 mat<T> r(nrows(),M,(T)0);
188 for(unsigned i = 0; i < nrows(); i++)
189 for(unsigned j = i; j < M;j++)
190 for(unsigned k = i; k <= j; k++)
191 r(i,j) += operator()(i,k) * (T)(m2(k,j));
192
193 return r;
194 }
195
196
197 const mat<T> operator*( const mat<T>& m2)
198 {
199 assert(m2.nrows() == nrows());
200 unsigned M = m2.ncols();
201 mat<T> r(nrows(),M,(T)0);
202 for(unsigned i = 0; i < nrows(); i++)
203 for(unsigned j = 0; j < M;j++)
204 for(unsigned k = i; k < nrows(); k++)
205 r(i,j) += operator()(i,k) * (T)(m2(k,j));
206
207 return r;
208 }
209
210
211};
212
213//transpose of a matrix m
214template <typename T>
215const low_tri_mat<T> transpose(const up_tri_mat<T> &m)
216{
217 low_tri_mat<T> r(m.nrows());
218 for(unsigned j = 0; j < m.ncols();j++)
219 for(unsigned i = j; i < m.nrows();i++)
220 r(i,j) = m(j,i);
221
222
223
224 return r;
225}
226
227//transpose of a matrix m
228template <typename T>
229const up_tri_mat<T> transpose(const low_tri_mat<T> &m)
230{
231 up_tri_mat<T> r(m.nrows());
232 for(unsigned j = 0; j < m.ncols();j++)
233 for(unsigned i = j; i < m.nrows();i++)
234 r(j,i) = m(i,j);
235
236
237
238 return r;
239}
240
241
242
243
244
245
246template <typename T, typename S>
247const vec<T> operator*(const up_tri_mat<T>& m1, const vec<S>& v)
248{
249 assert(m1.ncols() == v.size());
250 unsigned M = v.size();
251 vec<T> r(M,(T)0);
252 for(unsigned i = 0; i < m1.nrows(); i++)
253 for(unsigned k = i; k < m1.ncols(); k++)
254 r(i) += m1(i,k) * (T)(v(k));
255
256 return r;
257}
258
259
260template <typename T, typename S>
261const mat<T> operator*(const mat<S>& m1, const up_tri_mat<T>& m2)
262{
263 assert(m1.ncols() == m2.nrows());
264 unsigned M = m2.nrows();
265 mat<T> r(m1.nrows(),M,(T)0);
266 for(unsigned i = 0; i < m1.nrows(); i++)
267 for(unsigned j = 0; j < M;j++)
268 for(unsigned k = 0; k <= j; k++)
269 r(i,j) += m1(i,k) * (T)(m2(k,j));
270
271 return r;
272}
273
274//product of a lower and an upper triangular matrix
275template <typename T>
276const mat<T> operator*(const low_tri_mat<T>& m1, const up_tri_mat<T>& m2)
277{
278 assert(m1.nrows() == m2.nrows());
279
280 mat<T> r(m1.nrows(),m2.nrows(),(T)0);
281 for(unsigned i = 0; i < m1.nrows(); i++)
282 for(unsigned j = 0; j < m1.nrows();j++)
283 {
284 unsigned h = std::min(i,j);
285 for(unsigned k = 0; k <= h; k++)
286 r(i,j) += m1(i,k) * (T)(m2(k,j));
287 }
288
289 return r;
290}
291
292
293//multiplies a permutation matrix from left to apply a rows permutation
294template<typename T>
295mat<T> operator*(const perm_mat& p, const up_tri_mat<T>& m)
296{
297 mat<T> r=m;
298 return p*r;
299}
300
301//multiplies a permutation matrix from right to apply a rows permutation
302template<typename T>
303mat<T> operator*(const up_tri_mat<T>& m,const perm_mat& p)
304{
305 mat<T> r=m;
306 return r*p;
307}
308
309
311template <typename T>
312std::ostream& operator<<(std::ostream& out, const up_tri_mat<T>& m)
313{
314
315 for(unsigned i =0;i< m.nrows() ;++i)
316 {
317 unsigned j = 0;
318 for (; j<m.ncols(); ++j)
319 if(j <i)
320 out << "\t";
321 else
322 out << m(i,j) <<"\t";
323 if(i < m.nrows()-1)
324 out <<"\n";
325 }
326
327 return out;
328
329}
330
331}
332
333}
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
unsigned ncols() const
number of columns
Definition mat.h:546
unsigned nrows() const
number of rows
Definition mat.h:540
up_tri_mat< T > operator/(const T &s)
division by a scalar
Definition up_tri_mat.h:175
up_tri_mat< T > & operator/=(const T &s)
in place division by a scalar
Definition up_tri_mat.h:165
up_tri_mat< T > & operator=(const up_tri_mat< T > &m)
assignment of a matrix with the same element type
Definition up_tri_mat.h:110
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