SquareMatrix.h

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