Mantid
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2008 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 "MantidKernel/DllConfig.h"
10#include <cfloat>
11#include <iosfwd>
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include <vector>
16
17// Our aim with Matrix is to provide the definition for
18// predefined types and avoid multiple copies of the same
19// definition in multiple shared libraries.
20// On MSVC template declarations cannot have
21// both extern and __declspec(dllexport) but gcc
22// requires the __attribute__((visibility("default")))
23// attribute on the class definition.
24#if defined(_MSC_VER)
25#define KERNEL_MATRIX_CLASS_DLL
26#else
27#define KERNEL_MATRIX_CLASS_DLL MANTID_KERNEL_DLL
28#endif
29
30namespace Mantid {
31
32namespace Kernel {
33//-------------------------------------------------------------------------
34// Forward declarations
35//-------------------------------------------------------------------------
36class V3D;
37
42template <typename T> class KERNEL_MATRIX_CLASS_DLL Matrix {
43public:
45 using value_type = T;
46
47private:
48 size_t m_numRows;
49 size_t m_numColumns;
50
52 template <class U> using CMemoryArray = std::unique_ptr<U[]>;
54 template <typename U> using MatrixMemoryPtrs = std::unique_ptr<U *[]>;
55
60
61 void lubcmp(int *, int &);
62 void lubksb(int const *, double *);
63 void rotate(double const, double const, int const, int const, int const, int const);
64
65public:
66 Matrix(const size_t nrow = 0, const size_t ncol = 0, bool const makeIdentity = false);
69 Matrix(const std::vector<T> &, const std::vector<T> &);
72 Matrix(const std::vector<T> &);
74 Matrix(const std::vector<T> &, const size_t nrow, const size_t ncol);
75
76 Matrix(const Matrix<T> &, const size_t nrow, const size_t ncol);
77
78 Matrix(const Matrix<T> &);
79 Matrix<T> &operator=(const Matrix<T> &);
80 Matrix(Matrix<T> &&) noexcept;
81 Matrix<T> &operator=(Matrix<T> &&) noexcept;
82
84 const T *operator[](const size_t row) const { return m_rawData[row]; }
85
87 T *operator[](const size_t row) { return m_rawData[row]; }
88
90 Matrix<T> operator+(const Matrix<T> &) const;
91
93 Matrix<T> operator-(const Matrix<T> &) const;
94
95 Matrix<T> operator*(const Matrix<T> &) const;
96 std::vector<T> operator*(const std::vector<T> &) const;
97 void multiplyPoint(const std::vector<T> &in,
98 std::vector<T> &out) const;
99 V3D operator*(const V3D &) const;
100 Matrix<T> operator*(const T &) const;
101
102 Matrix<T> &operator*=(const Matrix<T> &);
103 Matrix<T> &operator*=(const T &);
104 Matrix<T> &operator/=(const T &);
105
106 bool operator<(const Matrix<T> &) const;
107 bool operator>=(const Matrix<T> &) const;
108 bool operator!=(const Matrix<T> &) const;
109 bool operator==(const Matrix<T> &) const;
110 bool equals(const Matrix<T> &A, const double Tolerance = FLT_EPSILON) const;
111 T item(size_t row, size_t col) const { return m_rawData[row][col]; }
112
113 void print() const;
114 void write(std::ostream &, int const = 0) const;
115 std::string str() const;
116
117 // returns this matrix in 1D vector representation
118 std::vector<T> getVector() const;
119 // explicit conversion into the vector
120 operator std::vector<T>() const {
121 std::vector<T> tmp = this->getVector();
122 return tmp;
123 }
124 //
125 void setColumn(const size_t nCol, const std::vector<T> &newCol);
126 void setRow(const size_t nRow, const std::vector<T> &newRow);
127 void zeroMatrix();
128 void identityMatrix();
129 void setRandom(size_t seed = 0, double rMin = -1,
130 double rMax = 1);
131 void normVert();
132 T Trace() const;
133
134 std::vector<T> Diagonal() const;
135 Matrix<T> preMultiplyByDiagonal(const std::vector<T> &) const;
136 Matrix<T> postMultiplyByDiagonal(const std::vector<T> &) const;
137
138 void setMem(const size_t, const size_t);
139
141 std::pair<size_t, size_t> size() const { return std::pair<size_t, size_t>(m_numRows, m_numColumns); }
142
144 size_t numRows() const { return m_numRows; }
145
147 size_t numCols() const { return m_numColumns; }
148
150 size_t Ssize() const { return (m_numRows > m_numColumns) ? m_numColumns : m_numRows; }
151
152 void swapRows(const size_t, const size_t);
153 void swapCols(const size_t, const size_t);
154
155 T Invert();
156 // Optimized inversion routine for tridiagonal matrices
157 void invertTridiagonal();
158 void averSymmetric();
159 int Diagonalise(Matrix<T> &, Matrix<T> &) const;
160 void sortEigen(Matrix<T> &);
161 Matrix<T> Tprime() const;
162 Matrix<T> &Transpose();
163
164 T factor();
165 T determinant() const;
166
167 void GaussJordan(Matrix<T> &);
168 T compSum() const;
169
170 // Check if a rotation matrix
171 bool isRotation() const;
172 // Check if orthogonal
173 bool isOrthogonal() const;
174 // Transform to a rotation matrix
175 std::vector<T> toRotation();
176
177private:
178 template <typename TYPE> friend void dumpToStream(std::ostream &, const Kernel::Matrix<TYPE> &, const char);
179 template <typename TYPE> friend void fillFromStream(std::istream &, Kernel::Matrix<TYPE> &, const char);
180};
181
182template <>
188{
189 throw std::invalid_argument("Gauss-Jordan inversion not valid for integer matrix");
190}
191
192// Explicit declarations required by Visual C++. Symbols provided by matching
193// explicit instantiations in source file
194#if defined(_MSC_VER)
195extern template class Matrix<double>;
196extern template class Matrix<int>;
197extern template class Matrix<float>;
198#endif
199
200// clean up
201#undef KERNEL_MATRIX_CLASS_DLL
202
203//-------------------------------------------------------------------------
204// Typedefs
205//-------------------------------------------------------------------------
209
210//-------------------------------------------------------------------------
211// Utility methods
212//-------------------------------------------------------------------------
213template <typename T> std::ostream &operator<<(std::ostream &, const Kernel::Matrix<T> &);
214template <typename T> void dumpToStream(std::ostream &, const Kernel::Matrix<T> &, const char);
215
216template <typename T> std::istream &operator>>(std::istream &, Kernel::Matrix<T> &);
217template <typename T> void fillFromStream(std::istream &, Kernel::Matrix<T> &, const char);
218} // namespace Kernel
219} // namespace Mantid
gsl_vector * tmp
#define KERNEL_MATRIX_CLASS_DLL
Definition: Matrix.h:27
Numerical Matrix class.
Definition: Matrix.h:42
std::unique_ptr< U[]> CMemoryArray
1D memory allocation to be wrapped with pointers to the rows
Definition: Matrix.h:52
T * operator[](const size_t row)
Array accessor. Use, e.g. Matrix[row][col].
Definition: Matrix.h:87
size_t Ssize() const
Return the smallest matrix size.
Definition: Matrix.h:150
void GaussJordan(Matrix< T > &)
Create a Gauss-Jordan Inversion.
Definition: Matrix.cpp:838
T item(size_t row, size_t col) const
Definition: Matrix.h:111
CMemoryArray< T > m_rawDataAlloc
Pointer to allocated memory.
Definition: Matrix.h:57
T value_type
Enable users to retrieve the element type.
Definition: Matrix.h:45
friend void fillFromStream(std::istream &, Kernel::Matrix< TYPE > &, const char)
size_t numRows() const
Return the number of rows in the matrix.
Definition: Matrix.h:144
size_t m_numRows
Number of rows (x coordinate)
Definition: Matrix.h:48
size_t m_numColumns
Number of columns (y coordinate)
Definition: Matrix.h:49
size_t numCols() const
Return the number of columns in the matrix.
Definition: Matrix.h:147
std::unique_ptr< U *[]> MatrixMemoryPtrs
Pointers to rows in the raw data array to make it appear 2D.
Definition: Matrix.h:54
friend void dumpToStream(std::ostream &, const Kernel::Matrix< TYPE > &, const char)
std::pair< size_t, size_t > size() const
Access matrix sizes.
Definition: Matrix.h:141
MatrixMemoryPtrs< T > m_rawData
Representation of 2D data.
Definition: Matrix.h:59
Class for 3D vectors.
Definition: V3D.h:34
MatrixWorkspace_sptr MANTID_API_DLL operator*(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Multiply two workspaces.
bool MANTID_API_DLL equals(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs, double tolerance=0.0)
Performs a comparison operation on two workspaces, using the CompareWorkspaces algorithm.
MatrixWorkspace_sptr MANTID_API_DLL operator/=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Divide two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator+=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator-=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Subtracts two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator-(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Subtracts two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator*=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Multiply two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator+(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition: CPUTimer.cpp:86
MANTID_KERNEL_DLL std::istream & operator>>(std::istream &, Interpolation &)
Reads in parameter value.
void dumpToStream(std::ostream &, const Kernel::Matrix< T > &, const char)
Write a Matrix to a stream.
Definition: Matrix.cpp:1598
void fillFromStream(std::istream &, Kernel::Matrix< T > &, const char)
Fill a Matrix from a stream using the given separator.
Definition: Matrix.cpp:1630
Helper class which provides the Collimation Length for SANS instruments.
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator>=(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator!=(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator<(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)