cgv
Loading...
Searching...
No Matches
low_tri_mat.h
1#pragma once
2
3#include "vec.h"
4#include "mat.h"
5#include "perm_mat.h"
6
7namespace cgv {
8 namespace math {
9
10//lower triangular matrix type
11template <typename T>
13{
14
15protected:
16 //data storage
17 vec<T> _data;
18 //matrix dimension
19 unsigned _dim;
20
21
22public:
23
24 //standard constructor of a lower triangular matrix
26 {
27 _dim=0;
28 }
29
30 //creates a dim x dim lower triangular matrix
31 low_tri_mat(unsigned dim)
32 {
33 resize(dim);
34 fill(0);
35 }
36
37 //copy constructor
38 low_tri_mat(const low_tri_mat& m)
39 {
40 resize(m.dim());
41 memcpy(_data,m._data,size()*sizeof(T));
42 }
43
44 //create a dim x dim lower triangular matrix with all non-zero elements set to c
45 low_tri_mat(unsigned dim, const T& c)
46 {
47 resize(dim);
48 fill(c);
49 }
50
51 //destructor
52 virtual ~low_tri_mat()
53 {
54 }
55
56 //resize the matrix to an n x n matrix
57 void resize(unsigned n)
58 {
59 _dim = n;
60 _data.resize(n*(n+1)/2);
61 }
62
63 //cast into full storage matrix
64 operator mat<T>()
65 {
66 mat<T> m(_dim,_dim);
67 for(unsigned i =0; i < _dim;i++)
68 {
69 for(unsigned j = 0; j < _dim;j++)
70 {
71 if(i<j)
72 m(i,j)=0;
73 else
74 m(i,j)=operator()(i,j);
75 }
76
77 }
78 return m;
79 }
80
83 {
84
85 resize(m.dim());
86 _data=m._data;
87 return *this;
88 }
89
90 //cast into const full storage matrix
91 operator const mat<T>() const
92 {
93 mat<T> m(_dim,_dim);
94 for(unsigned i =0; i < _dim;i++)
95 {
96 for(unsigned j = 0; j < _dim;j++)
97 {
98 if(i<j)
99 m(i,j)=0;
100 else
101 m(i,j)=operator()(i,j);
102 }
103
104 }
105 return m;
106 }
107
108
109 //return number of stored elements
110 unsigned size() const
111 {
112 return _data.size();
113 }
114
115 //return dimension of the matrix
116 unsigned dim() const
117 {
118 return _dim;
119 }
120
121 //returns the number of columns / rows of the matrix
122 unsigned nrows() const
123 {
124 return _dim;
125 }
126
127 unsigned ncols() const
128 {
129 return _dim;
130 }
131
132 //fills all lower triangular elements with c
133 void fill(const T& c)
134 {
135 for(unsigned i=0;i < size(); i++)
136 {
137 _data[i]=c;
138 }
139 }
140
141 //access to the element (i,j)
142 T& operator() (const unsigned i,const unsigned j)
143 {
144 assert( i >= j && i < _dim);
145 return _data[i*(i+1)/2+j];
146 }
147
148 //const access to the element (i,j)
149 const T& operator() (unsigned i, unsigned j) const
150 {
151 assert( i >= j && i < _dim);
152 return _data[i*(i+1)/2+j];
153 }
154
155 //return true if (i,j) is a valid index pair
156 bool valid_ind(int i, int j) const
157 {
158 return ( i >= j && i < (int)_dim && i >= 0 && j >= 0);
159 }
160
161 template < typename S>
162 const mat<T> operator*(const mat<S>& m2)
163 {
164 assert(m2.nrows() == nrows());
165 unsigned M = m2.ncols();
166 mat<T> r(nrows(),M,(T)0);
167 for(unsigned i = 0; i < nrows(); i++)
168 for(unsigned j = 0; j < M;j++)
169 for(unsigned k = 0; k <= i; k++)
170 r(i,j) += operator()(i,k) * (T)(m2(k,j));
171
172 return r;
173 }
174
175
176};
177
179template <typename T>
180std::ostream& operator<<(std::ostream& out, const low_tri_mat<T>& m)
181{
182
183 for(unsigned i =0;i < m.nrows() ;++i)
184 {
185 unsigned j = 0;
186 for (; j<i; ++j)
187 out << m(i,j)<<"\t";
188 out << m(i,j)<<"\n";
189 }
190
191 return out;
192
193}
194
196template <typename T, typename S>
197const mat<T> operator*(const low_tri_mat<T>& m1, const low_tri_mat<S>& m2)
198{
199 assert(m2.nrows() == m1.nrows());
200 unsigned M = m2.ncols();
201 mat<T> r(m1.nrows(),M,(T)0);
202 for(unsigned i = 0; i < m1.nrows(); i++)
203 for(unsigned j = 0; j <= i;j++)
204 for(unsigned k = j; k <= i; k++)
205 r(i,j) += m1(i,k) * (T)(m2(k,j));
206
207 return r;
208}
209
210
211
212template <typename T, typename S>
213const vec<T> operator*(const low_tri_mat<T>& m1, const vec<S>& v)
214{
215 assert(m1.ncols() == v.size());
216 unsigned M = v.size();
217 vec<T> r(M,(T)0);
218 for(unsigned i = 0; i < m1.nrows(); i++)
219 for(unsigned k = 0; k <= i; k++)
220 r(i) += m1(i,k) * (T)(v(k));
221
222 return r;
223}
224
225template <typename T, typename S>
226const mat<T> operator*(const mat<S>& m1, const low_tri_mat<T>& m2)
227{
228 assert(m1.ncols() == m2.dim());
229 unsigned M = m2.dim();
230 mat<T> r(m1.nrows(),M,0);
231 for(unsigned i = 0; i < m1.nrows(); i++)
232 for(unsigned j = 0; j < M;j++)
233 for(unsigned k = j; k < m1.ncols(); k++)
234 r(i,j) += m1(i,k) * (T)(m2(k,j));
235
236 return r;
237}
238
239
240//multiplies a permutation matrix from left to apply a rows permutation
241template<typename T>
242mat<T> operator*(const perm_mat& p, const low_tri_mat<T>& m)
243{
244 mat<T> r=m;
245 return p*r;
246}
247
248//multiplies a permutation matrix from right to apply a rows permutation
249template<typename T>
250mat<T> operator*(const low_tri_mat<T>& m,const perm_mat& p)
251{
252 mat<T> r=m;
253 return r*p;
254}
255
256}
257}
low_tri_mat< T > & operator=(const low_tri_mat< T > &m)
assignment of a matrix with the same element type
Definition low_tri_mat.h:82
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
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