2#include <cgv/math/vec.h>
3#include <cgv/math/mat.h>
14void Ax(
const sparse_mat<T>& A,
const vec<T>&v, vec<T>& r);
46 unsigned nrows()
const
52 unsigned ncols()
const
58 unsigned num_non_zeros()
const
68 for(
unsigned j = 0; j < _ncols; j++)
69 for(
unsigned i = _cols(j);i < _cols(j+1); i++)
70 m( _rows(i),j)=_data(i);
76 void compress(
const mat<T>& m, T eps=0)
83 for(
unsigned i =0; i < m.
nrows(); i++)
85 for(
unsigned j =0; j < m.
ncols(); j++)
96 for(
unsigned j =0; j < m.
ncols(); j++)
100 for(
unsigned i =0; i < m.
nrows(); i++)
119 assert(_ncols == v.
size());
123 for(c = 0; c < _ncols;c++)
124 for(
unsigned i = _cols(c); i < _cols(c+1);i++)
125 r(_rows(i)) += _data(i)*v(c);
143 sparse_mat<T> m = *
this;
148 sparse_mat<T>& operator/=(
const T& s)
155 sparse_mat<T> operator/(
const T& s)
157 sparse_mat<T> m = *
this;
173 for(
unsigned c = 0; c < _ncols; c++)
174 for(
unsigned i = _cols[c]; i < _cols[c+1]; i++)
176 _colsnew(_rows(i))++;
181 for(
unsigned i = 0; i < _colsnew.
size();i++)
183 unsigned temp = _colsnew(i);
195 for(
unsigned i = 0; i < _data.
size();i++)
197 unsigned idx =_colsnew(_rows(i));
199 _data(idx)= _datanew(i);
200 _rowsnew2(idx)= _rowsnew(i);
201 _colsnew(_rows(i))++;
214 friend std::ostream& operator<< <T>(std::ostream& out,
sparse_mat& sm);
229std::ostream& operator<<(std::ostream& out,sparse_mat<T>& sm)
231 out << sm._nrows <<
" "<< sm._ncols << std::endl;
232 out << sm._cols << std::endl;
233 out << sm._rows << std::endl;
234 out << sm._data << std::endl;
239void Ax(
const sparse_mat<T>& A,
const vec<T>&v, vec<T>& r)
241 assert(A._ncols == v.size());
244 for(c = 0; c < A._ncols;c++)
245 for(
unsigned i = A._cols(c); i < A._cols(c+1);i++)
246 r(A._rows(i)) += A._data(i)*v(c);
253void Atx(
const sparse_mat<T>& A,
const vec<T>&v, vec<T>& r)
255 assert(A._nrows == v.size());
258 for(c = 0; c < A._ncols;c++)
259 for(
unsigned i = A._cols(c); i < A._cols(c+1);i++)
260 r(c) += A._data(i)*v(A._rows(i));
265sparse_mat<T> transpose(
const sparse_mat<T>& sm)
267 sparse_mat<T> m = sm;
275sparse_mat<T> operator*(
const T& s,
const sparse_mat<T>& m)
281bool low_tri_solve(
const sparse_mat<T>& L,
const vec<T>& b, vec<T>& x)
284 for(
unsigned j = 0; j < x.size(); j++)
287 if(L._data(L.rows(L._cols(j))) != j )
290 x(j) =x(j) / L._data(_cols(j));
291 for(
unsigned i= L._cols(j)+1; i < L._cols(j+1);i++)
292 x(i) = x(i) - L._data(i)*x(j);
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
unsigned ncols() const
number of columns
void zeros()
set zero matrix
unsigned nrows() const
number of rows
void transpose()
transpose matrix
vec< T > operator*(const vec< T > &v)
matrix vector product
void resize(unsigned dim)
resize the vector
unsigned size() const
number of elements
void zeros()
fill the vector with zeros