Mantid
Loading...
Searching...
No Matches
EigenFortranMatrix.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2016 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <cstddef>
10#include <stdexcept>
11
12namespace Mantid {
13namespace CurveFitting {
14
21template <class MatrixClass> class FortranMatrix : public MatrixClass {
28 using ElementConstType = decltype(std::declval<const MatrixClass>().operator()(0, 0));
29 using ElementRefType = decltype(std::declval<MatrixClass>().operator()(0, 0));
30
31public:
35 FortranMatrix(const int nx, const int ny);
37 FortranMatrix(const int iFrom, const int iTo, const int jFrom, const int jTo);
39 void allocate(const int iFrom, const int iTo, const int jFrom, const int jTo);
41 void allocate(const int nx, const int ny);
43 int len1() const;
45 int len2() const;
47 ElementConstType operator()(const int i, const int j) const;
48 ElementRefType operator()(const int i, const int j);
51
53 MatrixClass moveToBaseMatrix();
56
57private:
59 static size_t makeSize(const int firstIndex, const int lastIndex);
60};
61
62//----------------- Method implementations -------------------------//
63
65template <class MatrixClass> size_t FortranMatrix<MatrixClass>::makeSize(const int firstIndex, const int lastIndex) {
66 if (lastIndex < firstIndex) {
67 throw std::invalid_argument("Matrix defined with invalid index range.");
68 }
69 return static_cast<size_t>(lastIndex - firstIndex + 1);
70}
71
73template <class MatrixClass>
75 : MatrixClass(this->makeSize(1, 1), this->makeSize(1, 1)), m_base1(1), m_base2(1) {}
76
78template <class MatrixClass>
80 : MatrixClass(this->makeSize(1, nx), this->makeSize(1, ny)), m_base1(1), m_base2(1) {}
81
93template <class MatrixClass>
94FortranMatrix<MatrixClass>::FortranMatrix(const int iFirst, const int iLast, const int jFirst, const int jLast)
95 : MatrixClass(this->makeSize(iFirst, iLast), this->makeSize(jFirst, jLast)), m_base1(iFirst), m_base2(jFirst) {}
96
102template <class MatrixClass>
103void FortranMatrix<MatrixClass>::allocate(const int iFirst, const int iLast, const int jFirst, const int jLast) {
104 m_base1 = iFirst;
105 m_base2 = jFirst;
106 this->resize(this->makeSize(iFirst, iLast), this->makeSize(jFirst, jLast));
107}
108
112template <class MatrixClass> void FortranMatrix<MatrixClass>::allocate(const int nx, const int ny) {
113 m_base1 = 1;
114 m_base2 = 1;
115 this->resize(this->makeSize(1, nx), this->makeSize(1, ny));
116}
117
119template <class MatrixClass>
121 const int j) const {
122 return this->MatrixClass::operator()(static_cast<size_t>(i - m_base1), static_cast<size_t>(j - m_base2));
123}
124
126template <class MatrixClass>
128 return this->MatrixClass::operator()(static_cast<size_t>(i - m_base1), static_cast<size_t>(j - m_base2));
129}
130
132template <class MatrixClass> MatrixClass FortranMatrix<MatrixClass>::moveToBaseMatrix() {
133 return MatrixClass(std::move(*this));
134}
135
137template <class MatrixClass> int FortranMatrix<MatrixClass>::len1() const { return static_cast<int>(this->size1()); }
138
140template <class MatrixClass> int FortranMatrix<MatrixClass>::len2() const { return static_cast<int>(this->size2()); }
141
145 res = this->tr();
146 return res;
147}
148
150template <class MatrixClass> FortranMatrix<MatrixClass> &FortranMatrix<MatrixClass>::operator=(const MatrixClass &m) {
151 this->resize(m.size1(), m.size2());
152 for (size_t i = 0; i < this->size1(); i++) {
153 for (size_t j = 0; j < this->size2(); j++) {
154 this->operator()((int)i + 1, (int)j + 1) = m.get(i, j);
155 }
156 }
157 return *this;
158}
159
160} // namespace CurveFitting
161} // namespace Mantid
FortranMatrix is a wrapper template for EigenMatrix and EigenComplexMatrix to simplify porting fortra...
void allocate(const int iFrom, const int iTo, const int jFrom, const int jTo)
Resize the matrix.
int m_base1
Base for the first index.
ElementConstType operator()(const int i, const int j) const
Index operator.
int len2() const
Get the size along the second dimension as an int.
static size_t makeSize(const int firstIndex, const int lastIndex)
Calculate the size (1D) of a matrix First.
int len1() const
Get the size along the first dimension as an int.
int m_base2
Base for the second index.
ElementRefType operator()(const int i, const int j)
Get the reference to the data element.
decltype(std::declval< MatrixClass >().operator()(0, 0)) ElementRefType
MatrixClass moveToBaseMatrix()
Move the data to a new matrix of MatrixClass.
FortranMatrix(const int nx, const int ny)
Constructor.
void allocate(const int nx, const int ny)
Resize the matrix.
FortranMatrix< MatrixClass > transpose() const
copy and transpose the matrix
decltype(std::declval< const MatrixClass >().operator()(0, 0)) ElementConstType
Typedef the types returned by the base class's operators [].
FortranMatrix(const int iFrom, const int iTo, const int jFrom, const int jTo)
Constructor.
FortranMatrix< MatrixClass > & operator=(const MatrixClass &m)
Assignment operator - Matrix Class.
Helper class which provides the Collimation Length for SANS instruments.