RDKit
Open-source cheminformatics and machine learning.
SquareMatrix.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef __RD_SQUARE_MATRIX_H__
11 #define __RD_SQUARE_MATRIX_H__
12 
13 #include "Matrix.h"
14 
15 namespace RDNumeric {
16  template <typename TYPE> class SquareMatrix : public Matrix<TYPE> {
17  public:
18  //! brief Square matrix of size N
20 
21  explicit SquareMatrix(unsigned int N) : Matrix<TYPE>(N, N) {};
22 
23  SquareMatrix(unsigned int N, TYPE val) : Matrix<TYPE>(N, N, val) {};
24 
25  SquareMatrix(unsigned int N, typename Matrix<TYPE>::DATA_SPTR data) : Matrix<TYPE>(N, N, data) {};
26 
27  //inline unsigned int size() const {
28  // return d_nRows;
29  //};
30 
31  virtual SquareMatrix<TYPE>& operator*=(TYPE scale) {
33  return *this;
34  }
35 
36  //! In place matrix multiplication
38  CHECK_INVARIANT(this->d_nCols == B.numRows(), "Size mismatch during multiplication");
39 
40  const TYPE *bData = B.getData();
41  TYPE *newData = new TYPE[this->d_dataSize];
42  unsigned int i, j, k;
43  unsigned int idA, idAt, idC, idCt, idB;
44  TYPE* data = this->d_data.get();
45  for (i = 0; i < this->d_nRows; i++) {
46  idA = i*this->d_nRows;
47  idC = idA;
48  for (j = 0; j < this->d_nCols; j++) {
49  idCt = idC + j;
50  newData[idCt] = (TYPE)(0.0);
51  for (k = 0; k < this->d_nCols; k++) {
52  idAt = idA + k;
53  idB = k*this->d_nRows + j;
54  newData[idCt] += (data[idAt]*bData[idB]);
55  }
56  }
57  }
58  boost::shared_array<TYPE> tsptr(newData);
59  this->d_data.swap(tsptr);
60  return (*this);
61  }
62 
63  //! In place matrix transpose
65  unsigned int i,j;
66  unsigned int id1, id1t, id2;
67  TYPE temp;
68  TYPE *data = this->d_data.get();
69  for (i = 1; i < this->d_nRows; i++) {
70  id1 = i*this->d_nCols;
71  for (j = 0; j < i; j++) {
72 
73  id1t = id1 + j;
74  id2 = j*this->d_nCols + i;
75  temp = data[id1t];
76  data[id1t] = data[id2];
77  data[id2] = temp;
78  }
79  }
80  return (*this);
81  }
82 
83  };
85 }
86 
87 #endif
88 
DATA_SPTR d_data
Definition: Matrix.h:238
SquareMatrix(unsigned int N, TYPE val)
Definition: SquareMatrix.h:23
unsigned int numRows() const
returns the number of rows
Definition: Matrix.h:77
virtual SquareMatrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
Definition: SquareMatrix.h:31
#define CHECK_INVARIANT(expr, mess)
Definition: Invariant.h:114
virtual Matrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
Definition: Matrix.h:186
SquareMatrix()
brief Square matrix of size N
Definition: SquareMatrix.h:19
SquareMatrix< double > DoubleSquareMatrix
Definition: SquareMatrix.h:84
unsigned int d_nCols
Definition: Matrix.h:236
A matrix class for general, non-square matrices.
Definition: Matrix.h:27
boost::shared_array< TYPE > DATA_SPTR
Definition: Matrix.h:30
SquareMatrix(unsigned int N, typename Matrix< TYPE >::DATA_SPTR data)
Definition: SquareMatrix.h:25
virtual SquareMatrix< TYPE > & operator*=(const SquareMatrix< TYPE > &B)
In place matrix multiplication.
Definition: SquareMatrix.h:37
SquareMatrix(unsigned int N)
Definition: SquareMatrix.h:21
unsigned int d_nRows
Definition: Matrix.h:234
unsigned int d_dataSize
Definition: Matrix.h:237
TYPE * getData()
returns a pointer to our data array
Definition: Matrix.h:132
virtual SquareMatrix< TYPE > & transposeInplace()
In place matrix transpose.
Definition: SquareMatrix.h:64