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