RDKit: SquareMatrix.h Source File

| 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>
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 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 {
35 Matrix<TYPE>::operator*=(scale);
36 return *this;
37 }
38
39 //! In place matrix multiplication
40 virtual SquareMatrix<TYPE> &operator*=(const SquareMatrix<TYPE> &B) {
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
68 virtual SquareMatrix<TYPE> &transposeInplace() {
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};
86 typedef SquareMatrix<double> DoubleSquareMatrix;
87} // namespace RDNumeric
88
89#endif
CHECK_INVARIANT
#define CHECK_INVARIANT(expr, mess)
Definition Invariant.h:100
Matrix.h
RDNumeric::Matrix::Matrix
Matrix(unsigned int nRows, unsigned int nCols)
Initialize with a size.
Definition Matrix.h:33
RDNumeric::Matrix< double >::d_dataSize
unsigned int d_dataSize
Definition Matrix.h:247
RDNumeric::Matrix< double >::d_nRows
unsigned int d_nRows
Definition Matrix.h:245
RDNumeric::Matrix::getData
TYPE * getData()
returns a pointer to our data array
Definition Matrix.h:140
RDNumeric::Matrix::numRows
unsigned int numRows() const
returns the number of rows
Definition Matrix.h:78
RDNumeric::Matrix::operator*=
virtual Matrix< TYPE > & operator*=(TYPE scale)
Multiplication by a scalar.
Definition Matrix.h:196
RDNumeric::Matrix< double >::d_data
DATA_SPTR d_data
Definition Matrix.h:248
RDNumeric::Matrix< double >::d_nCols
unsigned int d_nCols
Definition Matrix.h:246
RDNumeric::Matrix::DATA_SPTR
boost::shared_array< TYPE > DATA_SPTR
Definition Matrix.h:30
RDNumeric::SquareMatrix
Definition SquareMatrix.h:18
RDNumeric::SquareMatrix::SquareMatrix
SquareMatrix()
brief Square matrix of size N
Definition SquareMatrix.h:21
RDNumeric::SquareMatrix::SquareMatrix
SquareMatrix(unsigned int N, typename Matrix< TYPE >::DATA_SPTR data)
Definition SquareMatrix.h:27
RDNumeric::SquareMatrix::transposeInplace
virtual SquareMatrix< TYPE > & transposeInplace()
In place matrix transpose.
Definition SquareMatrix.h:68
RDNumeric::SquareMatrix::SquareMatrix
SquareMatrix(unsigned int N, TYPE val)
Definition SquareMatrix.h:25
RDNumeric::SquareMatrix::operator*=
virtual SquareMatrix< TYPE > & operator*=(const SquareMatrix< TYPE > &B)
In place matrix multiplication.
Definition SquareMatrix.h:40
RDNumeric::SquareMatrix::operator*=
SquareMatrix< TYPE > & operator*=(TYPE scale) override
Multiplication by a scalar.
Definition SquareMatrix.h:34
RDNumeric::SquareMatrix::SquareMatrix
SquareMatrix(unsigned int N)
Definition SquareMatrix.h:23
export.h
RDNumeric
Definition AlignPoints.h:18
RDNumeric::DoubleSquareMatrix
SquareMatrix< double > DoubleSquareMatrix
Definition SquareMatrix.h:86