1 #ifndef CARMA_SERVICES_MATRIX_H
2 #define CARMA_SERVICES_MATRIX_H
33 std::ostringstream&
operator<<(std::ostringstream& os,
76 Matrix(
unsigned nRow,
unsigned nCol);
156 type
det(
unsigned i,
unsigned j);
166 type
cofactor(
unsigned iRow,
unsigned iCol);
182 friend std::ostream& carma::services::operator << <>
188 friend std::ostringstream& carma::services::operator << <>
246 for(
unsigned irow=0; irow < nRow_; irow++) {
247 data_[irow].resize(nCol_);
261 for(
unsigned irow=0; irow < nRow; irow++)
262 data_[irow].resize(nCol);
280 if(iRow > data_.size()-1) {
281 std::ostringstream os;
282 os <<
"Matrix::operator[] : Matrix has no row: " << iRow;
301 if(mat.data_.size() == 0 || data_.size() == 0) {
303 "Matrix::operator*(Matrix) : Zero dimension encountered"
309 for(
unsigned iRow=0; iRow < mat.data_.size(); iRow++)
310 if(mat.data_[iRow].size() != mat.nCol_) {
312 "Matrix::operator*(Matrix) : Matrix has variable dimensions"
316 for(
unsigned iRow=0; iRow < data_.size(); iRow++)
317 if(data_[iRow].size() != nCol_) {
319 "Matrix::operator*(Matrix) : Matrix has variable dimensions"
325 if(nRow_ != mat.nCol_ || nCol_ != mat.nRow_)
327 "Matrix::operator*(Matrix) : Matrices have incompatible dimensions"
333 for(
unsigned iRow=0; iRow < nRow_; iRow++)
334 for(
unsigned iCol=0; iCol < mat.nCol_; iCol++) {
336 for(
unsigned j=0; j < nCol_; j++) {
338 result[iRow][iCol] = data_[iRow][j] * mat.data_[j][iCol];
341 result[iRow][iCol] += data_[iRow][j] * mat.data_[j][iCol];
355 if(nCol_==0 || vec.
size()==0) {
357 "Matrix::operator*(Vector) : Zero dimension encountered"
361 if(nCol_ != vec.
size()) {
363 "Matrix::operator*(Vector) : Vector has incompatible dimensions"
375 for(
unsigned iRow=0; iRow < nRow_; iRow++)
376 for(
unsigned j=0; j < nCol_; j++) {
378 result[iRow] = data_[iRow][j] * vec[j];
381 result[iRow] += data_[iRow][j] * vec[j];
396 for(
unsigned iRow=0; iRow < nRow_; iRow++)
397 for(
unsigned iCol=0; iCol < nCol_; iCol++)
398 result[iRow][iCol] *= fac;
409 for(
unsigned iRow=0; iRow < nRow_; iRow++)
410 for(
unsigned iCol=0; iCol < nCol_; iCol++)
411 result[iRow][iCol] /= fac;
422 for(
unsigned iRow=0; iRow < nRow_; iRow++)
423 for(
unsigned iCol=0; iCol < nCol_; iCol++)
424 result[iRow][iCol] += fac;
435 for(
unsigned iRow=0; iRow < nRow_; iRow++)
436 for(
unsigned iCol=0; iCol < nCol_; iCol++)
437 result[iRow][iCol] -= fac;
453 if(mat.nRow_==0 || vec.
size()==0) {
455 "operator*(Vector,Matrix) : zero dimension encountered"
459 if(mat.nRow_ != vec.
size()) {
461 "operator*(Vector,Matrix) : Vector has incompatible dimension"
473 for(
unsigned iCol=0; iCol < mat.nCol_; iCol++)
474 for(
unsigned j=0; j < mat.nRow_; j++) {
476 result[iCol] = mat.data_[j][iCol] * vec[j];
479 result[iCol] += mat.data_[j][iCol] * vec[j];
496 Matrix<type> operator*(
int fac, Matrix<type>& mat)
502 Matrix<type> operator*(
float fac, Matrix<type>& mat)
508 Matrix<type> operator*(
double fac, Matrix<type>& mat)
520 for(
unsigned iRow=0; iRow < mat.nRow_; iRow++) {
522 for(
unsigned iCol=0; iCol < mat.nCol_; iCol++) {
525 os << ::std::setw(15) << ::std::setprecision(8) << mat.data_[iRow][iCol];
527 os <<
"|" << ::std::endl;
539 for(
unsigned iRow=0; iRow < mat.nRow_; iRow++) {
541 for(
unsigned iCol=0; iCol < mat.nCol_; iCol++) {
544 os << mat.data_[iRow][iCol];
546 os <<
"|" << ::std::endl;
559 for(
unsigned i=0; i < nRow_; i++)
560 for(
unsigned j=0; j < nCol_; j++)
561 result.data_[j][i] = data_[i][j];
581 for(
unsigned i=0; i < nRow_; i++)
582 for(
unsigned j=0; j < nCol_; j++) {
583 prefac = (i+j)%2 == 0 ? 1 : -1;
584 result.data_[i][j] = prefac * determinant(i, j);
603 type deter = determinant();
605 if(::std::isfinite(1.0/(
double)deter))
609 "Matrix::inverse() : Matrix is not invertible"
628 "Matrix::trace() : Not an N x N matrix"
632 type result = data_[0][0];
634 for(
unsigned i=1; i < nRow_; i++)
635 result += data_[i][i];
648 "Matrix::cofactor() : Not an N x N matrix"
654 "Matrix::cofactor() : Cannot determine cofactors for dimensions < 2"
658 int prefac = (i+j)%2 == 0 ? 1 : -1;
660 return prefac * determinant(i, j);
671 "Matrix::determinant() : Not and N x N matrix"
684 return data_[0][0] * data_[1][1] - data_[1][0] * data_[0][1];
686 type sum = data_[0][0] * cofactor(0, 0);
688 for(
unsigned iCol=1; iCol < nCol_; iCol++) {
689 sum += data_[0][iCol] * cofactor(0, iCol);
701 return determinant();
712 "Matrix::determinant(i,j) : Not and N x N matrix"
718 "Matrix::determinant(i,j) : Cannot compute determinant for dimensions < 2"
733 return determinant(i, j);
742 if(nCol_ == 1 || nRow_ == 1) {
744 "Matrix dimensions cannot be reduced.");
753 for(
unsigned iRow=0, iRowRed=0; iRow < nRow_; iRow++) {
757 for(
unsigned iCol=0, iColRed=0; iCol < nCol_; iCol++) {
760 result.data_[iRowRed][iColRed] = data_[iRow][iCol];
776 #endif // End #ifndef CARMA_SERVICES_MATRIX_H
This class handles standard mathematical vector operations.
type det()
Determinant of a matrix.
Exception class for errors.
Matrix< type > adjoint()
Define an adjoint operator.
Matrix< type > operator+(T)
Define a matrix addition operator.
Matrix< type > inverse()
Define an inverse operator.
Matrix< type > operator*(Matrix< type > &mat)
Define a matrix multiplication operator.
This class handles standard mathematical matrix operations.
virtual ~Matrix()
Destructor.
unsigned size() const
Query the size of the vector.
Vector< type > & operator[](unsigned iRow)
Define an operator for accessing rows of the matrix.
Exception class for errors The exception comes with a text string that can be printed or logged...
#define CARMA_EXCEPTION(x, y)
Trick to get the file name and line number passed to the exception handler.
Matrix< type > operator/(T)
Define a matrix division operator.
std::ostream & operator<<(std::ostream &os, const carma::services::Angle &angle)
Define the << operator to allow, e.g.
type cofactor(unsigned iRow, unsigned iCol)
Return the cofactor of an element.
void resize(unsigned n)
Private resize operator.
type trace()
Get the trace of a matrix (sum of the diagonals)
Matrix< type > operator-(T)
Define a matrix subtraction operator.
type determinant()
Get the determinant of a matrix.
Matrix< type > transpose()
Define a transpose operator.
void operator=(const Matrix< type > &mat)
Assignment operator.
Matrix< type > reduce(unsigned iRow, unsigned iCol)
Define a reduction operator.