/src/rdkit/Code/Numerics/SquareMatrix.h
Line | Count | Source |
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 | | #include <RDGeneral/export.h> |
11 | | #ifndef __RD_SQUARE_MATRIX_H__ |
12 | | #define __RD_SQUARE_MATRIX_H__ |
13 | | |
14 | | #include "Matrix.h" |
15 | | |
16 | | namespace RDNumeric { |
17 | | template <typename TYPE> |
18 | | class SquareMatrix : public Matrix<TYPE> { |
19 | | public: |
20 | | //! brief Square matrix of size N |
21 | | SquareMatrix() {} |
22 | | |
23 | | explicit SquareMatrix(unsigned int N) : Matrix<TYPE>(N, N) {} |
24 | | |
25 | 0 | SquareMatrix(unsigned int N, TYPE val) : Matrix<TYPE>(N, N, val) {} |
26 | | |
27 | | SquareMatrix(unsigned int N, typename Matrix<TYPE>::DATA_SPTR data) |
28 | | : Matrix<TYPE>(N, N, data) {} |
29 | | |
30 | | SquareMatrix(const SquareMatrix &B) = default; |
31 | | SquareMatrix(SquareMatrix<TYPE> &&B) = default; |
32 | | SquareMatrix &operator=(const SquareMatrix<TYPE> &B) = default; |
33 | | SquareMatrix &operator=(SquareMatrix<TYPE> &&B) = default; |
34 | | ~SquareMatrix() = default; |
35 | | |
36 | 0 | SquareMatrix<TYPE> &operator*=(TYPE scale) override { |
37 | 0 | Matrix<TYPE>::operator*=(scale); |
38 | 0 | return *this; |
39 | 0 | } |
40 | | |
41 | | //! In place matrix multiplication |
42 | 0 | virtual SquareMatrix<TYPE> &operator*=(const SquareMatrix<TYPE> &B) { |
43 | 0 | CHECK_INVARIANT(this->d_nCols == B.numRows(), |
44 | 0 | "Size mismatch during multiplication"); |
45 | |
|
46 | 0 | const TYPE *bData = B.getData(); |
47 | 0 | TYPE *newData = new TYPE[this->d_dataSize]; |
48 | 0 | unsigned int i, j, k; |
49 | 0 | unsigned int idA, idAt, idC, idCt, idB; |
50 | 0 | TYPE *data = this->d_data.get(); |
51 | 0 | for (i = 0; i < this->d_nRows; i++) { |
52 | 0 | idA = i * this->d_nRows; |
53 | 0 | idC = idA; |
54 | 0 | for (j = 0; j < this->d_nCols; j++) { |
55 | 0 | idCt = idC + j; |
56 | 0 | newData[idCt] = (TYPE)(0.0); |
57 | 0 | for (k = 0; k < this->d_nCols; k++) { |
58 | 0 | idAt = idA + k; |
59 | 0 | idB = k * this->d_nRows + j; |
60 | 0 | newData[idCt] += (data[idAt] * bData[idB]); |
61 | 0 | } |
62 | 0 | } |
63 | 0 | } |
64 | 0 | boost::shared_array<TYPE> tsptr(newData); |
65 | 0 | this->d_data.swap(tsptr); |
66 | 0 | return (*this); |
67 | 0 | } |
68 | | |
69 | | //! In place matrix transpose |
70 | 0 | virtual SquareMatrix<TYPE> &transposeInplace() { |
71 | 0 | unsigned int i, j; |
72 | 0 | unsigned int id1, id1t, id2; |
73 | 0 | TYPE temp; |
74 | 0 | TYPE *data = this->d_data.get(); |
75 | 0 | for (i = 1; i < this->d_nRows; i++) { |
76 | 0 | id1 = i * this->d_nCols; |
77 | 0 | for (j = 0; j < i; j++) { |
78 | 0 | id1t = id1 + j; |
79 | 0 | id2 = j * this->d_nCols + i; |
80 | 0 | temp = data[id1t]; |
81 | 0 | data[id1t] = data[id2]; |
82 | 0 | data[id2] = temp; |
83 | 0 | } |
84 | 0 | } |
85 | 0 | return (*this); |
86 | 0 | } |
87 | | }; |
88 | | typedef SquareMatrix<double> DoubleSquareMatrix; |
89 | | } // namespace RDNumeric |
90 | | |
91 | | #endif |