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>
17 class SquareMatrix : public Matrix<TYPE> {
18  public:
19  //! brief Square matrix of size N
21 
22  explicit SquareMatrix(unsigned int N) : Matrix<TYPE>(N, N){};
23 
24  SquareMatrix(unsigned int N, TYPE val) : Matrix<TYPE>(N, N, val){};
25 
26  SquareMatrix(unsigned int N, typename Matrix<TYPE>::DATA_SPTR data)
27  : Matrix<TYPE>(N, N, data){};
28 
29  // inline unsigned int size() const {
30  // return d_nRows;
31  //};
32 
33  virtual SquareMatrix<TYPE> &operator*=(TYPE scale) {
35  return *this;
36  }
37 
38  //! In place matrix multiplication
40  CHECK_INVARIANT(this->d_nCols == B.numRows(),
41  "Size mismatch during multiplication");
42 
43  const TYPE *bData = B.getData();
44  TYPE *newData = new TYPE[this->d_dataSize];
45  unsigned int i, j, k;
46  unsigned int idA, idAt, idC, idCt, idB;
47  TYPE *data = this->d_data.get();
48  for (i = 0; i < this->d_nRows; i++) {
49  idA = i * this->d_nRows;
50  idC = idA;
51  for (j = 0; j < this->d_nCols; j++) {
52  idCt = idC + j;
53  newData[idCt] = (TYPE)(0.0);
54  for (k = 0; k < this->d_nCols; k++) {
55  idAt = idA + k;
56  idB = k * this->d_nRows + j;
57  newData[idCt] += (data[idAt] * bData[idB]);
58  }
59  }
60  }
61  boost::shared_array<TYPE> tsptr(newData);
62  this->d_data.swap(tsptr);
63  return (*this);
64  }
65 
66  //! In place matrix transpose
68  unsigned int i, j;
69  unsigned int id1, id1t, id2;
70  TYPE temp;
71  TYPE *data = this->d_data.get();
72  for (i = 1; i < this->d_nRows; i++) {
73  id1 = i * this->d_nCols;
74  for (j = 0; j < i; j++) {
75  id1t = id1 + j;
76  id2 = j * this->d_nCols + i;
77  temp = data[id1t];
78  data[id1t] = data[id2];
79  data[id2] = temp;
80  }
81  }
82  return (*this);
83  }
84 };
86 }
87 
88 #endif
DATA_SPTR d_data
Definition: Matrix.h:235
SquareMatrix(unsigned int N, TYPE val)
Definition: SquareMatrix.h:24
unsigned int numRows() const
returns the number of rows
Definition: Matrix.h:78
virtual SquareMatrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
Definition: SquareMatrix.h:33
#define CHECK_INVARIANT(expr, mess)
Definition: Invariant.h:99
virtual Matrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
Definition: Matrix.h:183
SquareMatrix()
brief Square matrix of size N
Definition: SquareMatrix.h:20
SquareMatrix< double > DoubleSquareMatrix
Definition: SquareMatrix.h:85
unsigned int d_nCols
Definition: Matrix.h:233
A matrix class for general, non-square matrices.
Definition: Matrix.h:28
boost::shared_array< TYPE > DATA_SPTR
Definition: Matrix.h:30
SquareMatrix(unsigned int N, typename Matrix< TYPE >::DATA_SPTR data)
Definition: SquareMatrix.h:26
virtual SquareMatrix< TYPE > & operator*=(const SquareMatrix< TYPE > &B)
In place matrix multiplication.
Definition: SquareMatrix.h:39
SquareMatrix(unsigned int N)
Definition: SquareMatrix.h:22
unsigned int d_nRows
Definition: Matrix.h:231
unsigned int d_dataSize
Definition: Matrix.h:234
TYPE * getData()
returns a pointer to our data array
Definition: Matrix.h:127
virtual SquareMatrix< TYPE > & transposeInplace()
In place matrix transpose.
Definition: SquareMatrix.h:67