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"
11#include <cfloat>
12#include <iosfwd>
13#include <memory>
14#include <stdexcept>
15#include <string>
16#include <vector>
17
18// Our aim with Matrix is to provide the definition for
19// predefined types and avoid multiple copies of the same
20// definition in multiple shared libraries.
21// On MSVC template declarations cannot have
22// both extern and __declspec(dllexport) but gcc
23// requires the __attribute__((visibility("default")))
24// attribute on the class definition.
25#if defined(_MSC_VER)
26#define KERNEL_MATRIX_CLASS_DLL
27#else
28#define KERNEL_MATRIX_CLASS_DLL MANTID_KERNEL_DLL
29#endif
30
31namespace Mantid {
32
33namespace Kernel {
34//-------------------------------------------------------------------------
35// Forward declarations
36//-------------------------------------------------------------------------
41template <typename T> class KERNEL_MATRIX_CLASS_DLL Matrix {
42public:
44 using value_type = T;
45
46private:
47 size_t m_numRows;
48 size_t m_numColumns;
49
51 template <class U> using CMemoryArray = std::unique_ptr<U[]>;
53 template <typename U> using MatrixMemoryPtrs = std::unique_ptr<U *[]>;
54
59
60 void lubcmp(int *, int &);
61 void lubksb(int const *, double *);
62 void rotate(double const, double const, int const, int const, int const, int const);
63
64public:
65 Matrix(const size_t nrow = 0, const size_t ncol = 0, bool const makeIdentity = false);
68 Matrix(const std::vector<T> &, const std::vector<T> &);
71 Matrix(const std::vector<T> &);
73 Matrix(const std::vector<T> &, const size_t nrow, const size_t ncol);
74
75 Matrix(const Matrix<T> &, const size_t nrow, const size_t ncol);
76
77 Matrix(const Matrix<T> &);
78 Matrix<T> &operator=(const Matrix<T> &);
79 Matrix(Matrix<T> &&) noexcept;
80 Matrix<T> &operator=(Matrix<T> &&) noexcept;
81
83 const T *operator[](const size_t row) const { return m_rawData[row]; }
84
86 T *operator[](const size_t row) { return m_rawData[row]; }
87
89 Matrix<T> operator+(const Matrix<T> &) const;
90
92 Matrix<T> operator-(const Matrix<T> &) const;
93
94 Matrix<T> operator*(const Matrix<T> &) const;
95 std::vector<T> operator*(const std::vector<T> &) const;
96 void multiplyPoint(const std::vector<T> &in,
97 std::vector<T> &out) const;
98 V3D operator*(const V3D &) const;
99 Matrix<T> operator*(const T &) const;
100
101 Matrix<T> &operator*=(const Matrix<T> &);
102 Matrix<T> &operator*=(const T &);
103 Matrix<T> &operator/=(const T &);
104
105 bool operator<(const Matrix<T> &) const;
106 bool operator>=(const Matrix<T> &) const;
107 bool operator!=(const Matrix<T> &) const;
108 bool operator==(const Matrix<T> &) const;
109 bool equals(const Matrix<T> &A, const double Tolerance = FLT_EPSILON) const;
110 T item(size_t row, size_t col) const { return m_rawData[row][col]; }
111
112 void print() const;
113 void write(std::ostream &, int const = 0) const;
114 std::string str() const;
115
116 // returns this matrix in 1D vector representation
117 std::vector<T> getVector() const;
118 // explicit conversion into the vector
119 operator std::vector<T>() const {
120 std::vector<T> tmp = this->getVector();
121 return tmp;
122 }
123 //
124 void setColumn(const size_t nCol, const std::vector<T> &newCol);
125 void setRow(const size_t nRow, const std::vector<T> &newRow);
126 void zeroMatrix();
127 void identityMatrix();
128 void setRandom(size_t seed = 0, double rMin = -1,
129 double rMax = 1);
130 void normVert();
131 T Trace() const;
132
133 std::vector<T> Diagonal() const;
134 Matrix<T> preMultiplyByDiagonal(const std::vector<T> &) const;
135 Matrix<T> postMultiplyByDiagonal(const std::vector<T> &) const;
136
137 void setMem(const size_t, const size_t);
138
140 std::pair<size_t, size_t> size() const { return std::pair<size_t, size_t>(m_numRows, m_numColumns); }
141
143 size_t numRows() const { return m_numRows; }
144
146 size_t numCols() const { return m_numColumns; }
147
149 size_t Ssize() const { return (m_numRows > m_numColumns) ? m_numColumns : m_numRows; }
150
151 void swapRows(const size_t, const size_t);
152 void swapCols(const size_t, const size_t);
153
154 T Invert();
155 // Optimized inversion routine for tridiagonal matrices
156 void invertTridiagonal(double tolerance = FLT_EPSILON);
157 void averSymmetric();
158 int Diagonalise(Matrix<T> &, Matrix<T> &) const;
159 void sortEigen(Matrix<T> &);
160 Matrix<T> Tprime() const;
161 Matrix<T> &Transpose();
162
163 T factor();
164 T determinant() const;
165
166 void GaussJordan(Matrix<T> &);
167 T compSum() const;
168
169 // Check if a rotation matrix
170 bool isRotation() const;
171 // Check if orthogonal
172 bool isOrthogonal() const;
173 // Transform to a rotation matrix
174 std::vector<T> toRotation();
175
176private:
177 template <typename TYPE> friend void dumpToStream(std::ostream &, const Kernel::Matrix<TYPE> &, const char);
178 template <typename TYPE> friend void fillFromStream(std::istream &, Kernel::Matrix<TYPE> &, const char);
179};
180
181template <>
187{
188 throw std::invalid_argument("Gauss-Jordan inversion not valid for integer matrix");
189}
190
191// Explicit declarations required by Visual C++. Symbols provided by matching
192// explicit instantiations in source file
193#if defined(_MSC_VER)
194extern template class Matrix<double>;
195extern template class Matrix<int>;
196extern template class Matrix<float>;
197#endif
198
199// clean up
200#undef KERNEL_MATRIX_CLASS_DLL
201
202//-------------------------------------------------------------------------
203// Typedefs
204//-------------------------------------------------------------------------
208
209//-------------------------------------------------------------------------
210// Utility methods
211//-------------------------------------------------------------------------
212template <typename T> std::ostream &operator<<(std::ostream &, const Kernel::Matrix<T> &);
213template <typename T> void dumpToStream(std::ostream &, const Kernel::Matrix<T> &, const char);
214
215template <typename T> std::istream &operator>>(std::istream &, Kernel::Matrix<T> &);
216template <typename T> void fillFromStream(std::istream &, Kernel::Matrix<T> &, const char);
217} // namespace Kernel
218} // namespace Mantid
gsl_vector * tmp
#define KERNEL_MATRIX_CLASS_DLL
Definition Matrix.h:28
MANTID_NEXUS_DLL bool operator!=(std::string const &s, Mantid::Nexus::NexusAddress const &p)
MANTID_NEXUS_DLL std::string operator+(std::string const &s, Mantid::Nexus::NexusAddress const &p)
MANTID_NEXUS_DLL bool operator==(std::string const &s, Mantid::Nexus::NexusAddress const &p)
double tolerance
Numerical Matrix class.
Definition Matrix.h:41
std::unique_ptr< U[]> CMemoryArray
1D memory allocation to be wrapped with pointers to the rows
Definition Matrix.h:51
T * operator[](const size_t row)
Array accessor. Use, e.g. Matrix[row][col].
Definition Matrix.h:86
size_t Ssize() const
Return the smallest matrix size.
Definition Matrix.h:149
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:110
CMemoryArray< T > m_rawDataAlloc
Pointer to allocated memory.
Definition Matrix.h:56
T value_type
Enable users to retrieve the element type.
Definition Matrix.h:44
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:143
size_t m_numRows
Number of rows (x coordinate)
Definition Matrix.h:47
size_t m_numColumns
Number of columns (y coordinate)
Definition Matrix.h:48
size_t numCols() const
Return the number of columns in the matrix.
Definition Matrix.h:146
std::unique_ptr< U *[]> MatrixMemoryPtrs
Pointers to rows in the raw data array to make it appear 2D.
Definition Matrix.h:53
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:140
MatrixMemoryPtrs< T > m_rawData
Representation of 2D data.
Definition Matrix.h:58
Class for 3D vectors.
Definition V3D.h:35
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.
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.
Mantid::Kernel::Matrix< float > FloatMatrix
Definition Matrix.h:207
void dumpToStream(std::ostream &, const Kernel::Matrix< T > &, const char)
Write a Matrix to a stream.
Definition Matrix.cpp:1596
void fillFromStream(std::istream &, Kernel::Matrix< T > &, const char)
Fill a Matrix from a stream using the given separator.
Definition Matrix.cpp:1628
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)