RDCode: SquareMatrix.h Source File

# SquareMatrix.h

Go to the documentation of this file.

    00001 //
    00002 //  Copyright (C) 2004-2006 Rational Discovery LLC
    00003 //
    00004 //   @@ All Rights Reserved  @@
    00005 //
    00006 #ifndef __RD_SQUARE_MATRIX_H__
    00007 #define __RD_SQUARE_MATRIX_H__
    00008
    00009 #include "Matrix.h"
    00010
    00011 namespace RDNumeric {
    00012   template <typename TYPE> class SquareMatrix : public Matrix<TYPE> {
    00013   public:
    00014     //! brief Square matrix of size N
    00015     SquareMatrix() {};
    00016
    00017     explicit SquareMatrix(unsigned int N) : Matrix<TYPE>(N, N) {};
    00018
    00019     SquareMatrix(unsigned int N, TYPE val) : Matrix<TYPE>(N, N, val) {};
    00020
    00021     SquareMatrix(unsigned int N, typename Matrix<TYPE>::DATA_SPTR data) : Matrix<TYPE>(N, N, data) {};
    00022
    00023     //inline unsigned int size() const {
    00024     //  return d_nRows;
    00025     //};
    00026
    00027     virtual SquareMatrix<TYPE>& operator*=(TYPE scale) {
    00028       Matrix<TYPE>::operator*=(scale);
    00029       return *this;
    00030     }
    00031
    00032     //! In place matrix multiplication
    00033     virtual SquareMatrix<TYPE> & operator*=(const SquareMatrix<TYPE> & B) {
    00034       CHECK_INVARIANT(this->d_nCols == B.numRows(), "Size mismatch during multiplication");
    00035
    00036       const TYPE *bData = B.getData();
    00037       TYPE *newData = new TYPE[this->d_dataSize];
    00038       unsigned int i, j, k;
    00039       unsigned int idA, idAt, idC, idCt, idB;
    00040       TYPE* data = this->d_data.get();
    00041       for (i = 0; i < this->d_nRows; i++) {
    00042         idA = i*this->d_nRows;
    00043         idC = idA;
    00044         for (j = 0; j < this->d_nCols; j++) {
    00045           idCt = idC + j;
    00046           newData[idCt] = (TYPE)(0.0);
    00047           for (k = 0; k < this->d_nCols; k++) {
    00048             idAt = idA + k;
    00049             idB = k*this->d_nRows + j;
    00050             newData[idCt] += (data[idAt]*bData[idB]);
    00051           }
    00052         }
    00053       }
    00054       for (i = 0; i < this->d_dataSize; i++) {
    00055         data[i] = newData[i];
    00056       }
    00057       delete [] newData;
    00058       return (*this);
    00059     }
    00060
    00061     //! In place matrix transpose
    00062     virtual SquareMatrix<TYPE> &transposeInplace() {
    00063       unsigned int i,j;
    00064       unsigned int id1, id1t, id2;
    00065       TYPE temp;
    00066       TYPE *data = this->d_data.get();
    00067       for (i = 1; i < this->d_nRows; i++) {
    00068         id1 = i*this->d_nCols;
    00069         for (j = 0; j < i; j++) {
    00070
    00071           id1t = id1 + j;
    00072           id2 = j*this->d_nCols + i;
    00073           temp = data[id1t];
    00074           data[id1t] = data[id2];
    00075           data[id2] = temp;
    00076         }
    00077       }
    00078       return (*this);
    00079     }
    00080
    00081   };
    00082   typedef SquareMatrix<double> DoubleSquareMatrix;
    00083 }
    00084
    00085 #endif
    00086