29 #ifndef DINAMICA_DAMATRIX_T_H_ 30 #define DINAMICA_DAMATRIX_T_H_ 54 if (obj1.size()!=obj2.
nrows())
55 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::operator*: objects in vector/matrix multiplication must have compatible size.");
59 for(
unsigned int i=0; i<obj2.
ncols(); i++){
61 for (
unsigned int j=0; j<obj2.
nrows();j++)
62 temp[i] += obj1[j]*obj2.
at(j,i);
76 if (obj1.
ncols()!=obj2.size())
77 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::operator*: objects in vector/matrix multiplication must have compatible size.");
81 for(
unsigned int i=0; i<obj1.
nrows(); i++){
83 for (
unsigned int j=0; j<obj1.
ncols();j++)
84 temp[i] += obj1.
at(i,j)*obj2[j];
97 unsigned int oldrows = this->nrows();
98 unsigned int oldcols = this->ncols();
100 std::vector<T> temp(this->_data);
104 this->_data.resize(size*size);
107 for(
unsigned int irow=0; irow<(unsigned)size; irow++)
108 for(
unsigned int icol=0; icol<(unsigned)size; icol++)
109 if ((irow<oldrows)&&(icol<oldcols))
110 this->_data[irow*this->_ncols + icol] = temp[irow*oldcols+icol];
112 this->_data[irow*this->_ncols + icol] = 0.;
123 unsigned int oldrows = this->nrows();
124 unsigned int oldcols = this->ncols();
125 std::vector<T> temp(this->_data);
129 this->_data.resize(rows * cols);
132 for(
unsigned int irow=0; irow<(unsigned)rows; irow++)
133 for(
unsigned int icol=0; icol<(unsigned)cols; icol++)
134 if ((irow<oldrows)&&(icol<oldcols))
135 this->_data[irow*cols + icol] = temp[irow*oldcols+icol];
137 this->_data[irow*cols + icol] = 0.;
150 if ( !(irow<(this->_nrows) )&&!( icol<(this->_ncols) ))
151 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::at: matrix element position out of bound.");
153 return this->_data[irow*this->_ncols + icol];
163 if ( !(irow<(this->_nrows) )&&!( icol<(this->_ncols)) )
164 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::at: matrix element position out of bound.");
166 return this->_data[irow*this->_ncols + icol];
177 if( !(irow<(this->_nrows)) )
178 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::row: row out of bound.");
181 std::vector<T> temp(this->_ncols);
184 for(
unsigned int i=0; i<(this->_ncols); i++)
186 temp[i] = this->_data[irow*this->_ncols+i];
200 if( !(icol<(this->_ncols)) )
201 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::col: column out of bound.");
204 std::vector<T> temp(this->_nrows);
207 for(
unsigned int i=0; i<(this->_nrows); i++)
209 temp[i] = this->_data[i*this->_ncols+icol];
222 if (obj.size()!=this->_ncols)
223 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::setrow: vector too large to be stored in matrix.");
226 for(
unsigned int j=0; j<(this->_ncols); j++)
227 this->_data[irow*this->_ncols+j] = obj[j];
237 if (obj.size()!=this->_nrows)
238 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::setcol: vector too large to be stored in matrix.");
241 for(
unsigned int i=0; i<(this->_nrows); i++)
242 this->_data[i*this->_ncols+icol] = obj[i];
255 if( (last_row<first_row)||(last_col<first_col) )
256 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::submat: first row/column index larger than last row/column index.");
259 if( !(last_row<(this->_nrows))||!(last_col<(this->_ncols)) )
260 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::submat: last row/column index exceeds number of rows/columns.");
262 unsigned int nrows = last_row-first_row+1;
263 unsigned int ncols = last_col-first_col+1;
268 for (
unsigned int i=0; i<nrows; i++)
269 for (
unsigned int j=0; j<ncols; j++)
270 temp.
at(i,j) = this->at(i+first_row,j+first_col);
283 return this->submat(0,0,last_row,last_col);
298 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::operator+: Inputs must have the same size, unless one is a scalar value.");
300 unsigned int n_rows = obj1.
nrows();
301 unsigned int n_cols = obj1.
ncols();
305 for (
unsigned int i=0; i<n_rows; i++)
306 for (
unsigned int j=0; j<n_cols; j++)
307 temp.
at(i,j) = obj1.
at(i,j) + obj2.
at(i,j);
319 unsigned int n_rows = obj1.
nrows();
320 unsigned int n_cols = obj1.
ncols();
324 for (
unsigned int i=0; i<n_rows; i++)
325 for (
unsigned int j=0; j<n_cols; j++)
326 temp.
at(i,j) = obj1.
at(i,j) + obj2;
338 unsigned int n_rows = obj2.
nrows();
339 unsigned int n_cols = obj2.
ncols();
343 for (
unsigned int i=0; i<n_rows; i++)
344 for (
unsigned int j=0; j<n_cols; j++)
345 temp.
at(i,j) = obj1 + obj2.
at(i,j);
359 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::operator-: Inputs must have the same size, unless one is a scalar value.");
361 unsigned int n_rows = obj1.
nrows();
362 unsigned int n_cols = obj1.
ncols();
366 for (
unsigned int i=0; i<n_rows; i++)
367 for (
unsigned int j=0; j<n_cols; j++)
368 temp.
at(i,j) = obj1.
at(i,j)-obj2.
at(i,j);
380 unsigned int n_rows = obj1.
nrows();
381 unsigned int n_cols = obj1.
ncols();
385 for (
unsigned int i=0; i<n_rows; i++)
386 for (
unsigned int j=0; j<n_cols; j++)
387 temp.
at(i,j) = obj1.
at(i,j) - obj2;
400 unsigned int n_rows = obj2.
nrows();
401 unsigned int n_cols = obj2.
ncols();
405 for (
unsigned int i=0; i<n_rows; i++)
406 for (
unsigned int j=0; j<n_cols; j++)
407 temp.
at(i,j) = obj1 - obj2.
at(i,j);
421 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::operator*: Number of columns of first matrix must be equal to num,ber of rows of second matrix.");
423 unsigned int N = obj1.
nrows();
424 unsigned int M = obj1.
ncols();
425 unsigned int P = obj2.
ncols();
429 for (
unsigned int i=0; i<N; i++)
430 for (
unsigned int j=0; j<P; j++) {
432 for (
unsigned int k=0; k<M; k++)
433 temp.
at(i,j) += obj1.
at(i,k)*obj2.
at(k,j); }
445 unsigned int n_rows = obj1.
nrows();
446 unsigned int n_cols = obj1.
ncols();
450 for (
unsigned int i=0; i<n_rows; i++)
451 for (
unsigned int j=0; j<n_cols; j++)
452 temp.
at(i,j) = obj1.
at(i,j)*obj2;
464 unsigned int n_rows = obj2.
nrows();
465 unsigned int n_cols = obj2.
ncols();
469 for (
unsigned int i=0; i<n_rows; i++)
470 for (
unsigned int j=0; j<n_cols; j++)
471 temp.
at(i,j) = obj1*obj2.
at(i,j);
488 for(
unsigned int i=0; i<(this->_nrows); i++)
489 for(
unsigned int j=0; j<(this->_ncols); j++)
490 temp.
at(j,i) = this->_data[i*(this->_ncols)+j];
497 template<
class T>
unsigned int AlgebraicMatrix<T>::pivot(
unsigned int& k,
const unsigned int ii,
const AlgebraicMatrix<T>& A, std::vector<unsigned int>& P, std::vector<unsigned int>& R, std::vector<unsigned int>& C1, std::vector<unsigned int>& C2, T&
det) {
502 const unsigned int n = A.
nrows();
504 for (
unsigned int i=0; i<n; i++){
506 for (
unsigned int j=0; j<n; j++){
508 t =
abs( A.
at(R[i],j) );
523 det = det*A.
at( R[im], k );
524 if (im!=k) det = -1.0*
det;
525 std::swap(R[k], R[im]);
537 unsigned int n = A.
nrows();
548 for (
unsigned int j=0; j<n; j++){
550 A.
at(R[k],j) = A.
at(R[k],j)/A.
at(R[k],k);}
552 A.
at(R[k],k) = 1.0/A.
at(R[k],k);
554 for (
unsigned int i=0; i<n; i++){
556 for (
unsigned int j=0; j<n; j++){
557 if (j!=k) A.
at(R[i],j) = A.
at(R[i],j) - A.
at(R[i],k)*A.
at(R[k],j);
559 A.
at(R[i],k) = -1.0*A.
at(R[i],k)*A.
at(R[k],k);
574 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::inv: Matrix must be square to compute the inverse.");
576 const unsigned int n = _nrows;
579 std::vector<unsigned int> R(n);
580 std::vector<unsigned int> P(n,0);
581 std::vector<unsigned int> C1(n,0);
582 std::vector<unsigned int> C2(n,0);
586 for (
unsigned int i=0; i<n; i++) R[i] = i;
588 for (
unsigned int i=0; i<n; i++){
589 unsigned int err = pivot(k, i, AA, P, R, C1, C2, det);
593 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::inv: Matrix inverse does not exist.");
596 for (
unsigned int i=0; i<n; i++) P[i] = i;
597 for (
int i=n-1; i>=0; i--) std::swap(P[C1[i]], P[C2[i]]);
598 for (
unsigned int i=0; i<n; i++)
599 for (
unsigned int j=0; j<n; j++)
600 AI.
at(i,j) = AA.
at(R[i], P[j]);
612 throw std::runtime_error(
"DACE::AlgebraicMatrix<T>::inv: Matrix must be square to compute the inverse.");
614 const unsigned int n = _nrows;
617 std::vector<unsigned int> R(n);
618 std::vector<unsigned int> P(n,0);
619 std::vector<unsigned int> C1(n,0);
620 std::vector<unsigned int> C2(n,0);
623 for (
unsigned int i=0; i<n; i++) R[i] = i;
625 for (
unsigned int i=0; i<n; i++){
626 unsigned int err = pivot(k, i, AA, P, R, C1, C2, det);
647 for(
unsigned int i=0; i<(this->_nrows); i++)
648 for(
unsigned int j=0; j<(this->_ncols); j++)
649 temp.
at(i,j) =
DACE::cons(this->_data[i*this->_ncols+j]);
657 template<
typename U> std::ostream& operator<< (std::ostream &out, const AlgebraicMatrix<U> &obj){
664 unsigned int nrows = obj.
nrows();
665 unsigned int ncols = obj.ncols();
667 out <<
"[[[ " << nrows <<
"x" << ncols <<
" matrix" << std::endl;
668 for(
unsigned int i = 0; i<nrows;i++){
669 for(
unsigned int j = 0; j<ncols; j++){
678 out <<
"]]]" << std::endl;
690 std::string init_line;
691 std::getline(in, init_line);
694 unsigned int n_rows = 0;
695 unsigned int n_cols = 0;
700 std::size_t found = init_line.find_first_of(
"x");
702 std::string size_str;
704 size_str = size_str + init_line[j];
706 if (!(std::istringstream(size_str) >> n_rows)) n_rows = 0;
710 found = init_line.find_first_of(
"m",found);
713 size_str = size_str + init_line[j];
715 if (!(std::istringstream(size_str) >> n_cols)) n_cols = 0;
718 if ((obj.
nrows() != n_rows)&&(obj.
ncols() != n_cols))
719 obj.
resize(n_rows, n_cols);
723 while(in.good() && (i<n_rows)) {
725 for(
int j=0; j<n_cols; j++)
731 if (in.peek() ==
'\n')
735 std::string skip_line;
736 std::getline(in, skip_line);
AlgebraicMatrix< T > inv(const AlgebraicMatrix< T > &obj)
Definition: AlgebraicMatrix_t.h:762
std::istream & operator>>(std::istream &in, AlgebraicMatrix< DA > &obj)
DA specialization of input stream operator.
Definition: AlgebraicMatrix.cpp:65
unsigned int ncols() const
Definition: AlgebraicMatrix.h:82
T det() const
Matrix determinant.
Definition: AlgebraicMatrix_t.h:605
DA operator*(const DA &da1, const DA &da2)
Definition: DA.cpp:759
DA operator-(const DA &da1, const DA &da2)
Definition: DA.cpp:714
double abs(const DA &da)
Definition: DA.cpp:2558
AlgebraicMatrix< double > cons() const
Return the costant part of a AlgebraicMatrix<T>
Definition: AlgebraicMatrix_t.h:639
T det(const AlgebraicMatrix< T > &obj)
Definition: AlgebraicMatrix_t.h:753
void resize(int size)
Definition: AlgebraicMatrix_t.h:90
Definition: AlgebraicMatrix.h:43
AlgebraicMatrix< T > transpose() const
Matrix transpose.
Definition: AlgebraicMatrix_t.h:479
Definition: AlgebraicMatrix.h:46
std::vector< T > getcol(const unsigned int icol) const
Reading column.
Definition: AlgebraicMatrix_t.h:192
void setrow(const unsigned int irow, const std::vector< T > &obj)
Set row equal to std::vector.
Definition: AlgebraicMatrix_t.h:215
void setcol(const unsigned int icol, const std::vector< T > &obj)
Set column equal to std::vector.
Definition: AlgebraicMatrix_t.h:230
T & at(const unsigned int irow, const unsigned int icol)
Reading/Writing single element.
Definition: AlgebraicMatrix_t.h:143
Definition: AlgebraicMatrix.cpp:39
double cons(const DA &da)
Definition: DA.cpp:1970
#define abs(x)
Definition: f2c.h:157
AlgebraicMatrix< T > inv() const
Matrix inverse XXX: name.
Definition: AlgebraicMatrix_t.h:565
AlgebraicMatrix< T > submat(const unsigned int first_row, const unsigned int first_col, const unsigned int last_row, const unsigned int last_col) const
Extract submatrix.
Definition: AlgebraicMatrix_t.h:245
unsigned int size(const DA &da)
Definition: DA.cpp:2549
AlgebraicMatrix< T > transpose(const AlgebraicMatrix< T > &obj)
Definition: AlgebraicMatrix_t.h:744
std::vector< T > getrow(const unsigned int irow) const
Reading row.
Definition: AlgebraicMatrix_t.h:169
DA operator+(const DA &da1, const DA &da2)
Definition: DA.cpp:669
unsigned int nrows() const
Definition: AlgebraicMatrix.h:88