123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- // David Eberly, Geometric Tools, Redmond WA 98052
- // Copyright (c) 1998-2020
- // Distributed under the Boost Software License, Version 1.0.
- // https://www.boost.org/LICENSE_1_0.txt
- // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
- // Version: 4.0.2019.08.13
- #pragma once
- #include <Mathematics/GVector.h>
- #include <Mathematics/GaussianElimination.h>
- #include <algorithm>
- namespace WwiseGTE
- {
- template <typename Real>
- class GMatrix
- {
- public:
- // The table is length zero and mNumRows and mNumCols are set to zero.
- GMatrix()
- :
- mNumRows(0),
- mNumCols(0)
- {
- }
- // The table is length numRows*numCols and the elements are
- // initialized to zero.
- GMatrix(int numRows, int numCols)
- {
- SetSize(numRows, numCols);
- std::fill(mElements.begin(), mElements.end(), (Real)0);
- }
- // For 0 <= r < numRows and 0 <= c < numCols, element (r,c) is 1 and
- // all others are 0. If either of r or c is invalid, the zero matrix
- // is created. This is a convenience for creating the standard
- // Euclidean basis matrices; see also MakeUnit(int,int) and
- // Unit(int,int).
- GMatrix(int numRows, int numCols, int r, int c)
- {
- SetSize(numRows, numCols);
- MakeUnit(r, c);
- }
- // The copy constructor, destructor, and assignment operator are
- // generated by the compiler.
- // Member access for which the storage representation is transparent.
- // The matrix entry in row r and column c is A(r,c). The first
- // operator() returns a const reference rather than a Real value.
- // This supports writing via standard file operations that require a
- // const pointer to data.
- void SetSize(int numRows, int numCols)
- {
- if (numRows > 0 && numCols > 0)
- {
- mNumRows = numRows;
- mNumCols = numCols;
- mElements.resize(mNumRows * mNumCols);
- }
- else
- {
- mNumRows = 0;
- mNumCols = 0;
- mElements.clear();
- }
- }
- inline void GetSize(int& numRows, int& numCols) const
- {
- numRows = mNumRows;
- numCols = mNumCols;
- }
- inline int GetNumRows() const
- {
- return mNumRows;
- }
- inline int GetNumCols() const
- {
- return mNumCols;
- }
- inline int GetNumElements() const
- {
- return static_cast<int>(mElements.size());
- }
- inline Real const& operator()(int r, int c) const
- {
- if (0 <= r && r < GetNumRows() && 0 <= c && c < GetNumCols())
- {
- #if defined(GTE_USE_ROW_MAJOR)
- return mElements[c + mNumCols * r];
- #else
- return mElements[r + mNumRows * c];
- #endif
- }
- LogError("Invalid index.");
- }
- inline Real& operator()(int r, int c)
- {
- if (0 <= r && r < GetNumRows() && 0 <= c && c < GetNumCols())
- {
- #if defined(GTE_USE_ROW_MAJOR)
- return mElements[c + mNumCols * r];
- #else
- return mElements[r + mNumRows * c];
- #endif
- }
- LogError("Invalid index.");
- }
- // Member access by rows or by columns. The input vectors must have
- // the correct number of elements for the matrix size.
- void SetRow(int r, GVector<Real> const& vec)
- {
- if (0 <= r && r < mNumRows)
- {
- if (vec.GetSize() == GetNumCols())
- {
- for (int c = 0; c < mNumCols; ++c)
- {
- operator()(r, c) = vec[c];
- }
- }
- LogError("Mismatched sizes.");
- }
- LogError("Invalid index.");
- }
- void SetCol(int c, GVector<Real> const& vec)
- {
- if (0 <= c && c < mNumCols)
- {
- if (vec.GetSize() == GetNumRows())
- {
- for (int r = 0; r < mNumRows; ++r)
- {
- operator()(r, c) = vec[r];
- }
- return;
- }
- LogError("Mismatched sizes.");
- }
- LogError("Invalid index.");
- }
- GVector<Real> GetRow(int r) const
- {
- if (0 <= r && r < mNumRows)
- {
- GVector<Real> vec(mNumCols);
- for (int c = 0; c < mNumCols; ++c)
- {
- vec[c] = operator()(r, c);
- }
- return vec;
- }
- LogError("Invalid index.");
- }
- GVector<Real> GetCol(int c) const
- {
- if (0 <= c && c < mNumCols)
- {
- GVector<Real> vec(mNumRows);
- for (int r = 0; r < mNumRows; ++r)
- {
- vec[r] = operator()(r, c);
- }
- return vec;
- }
- LogError("Invalid index.");
- }
- // Member access by 1-dimensional index. NOTE: These accessors are
- // useful for the manipulation of matrix entries when it does not
- // matter whether storage is row-major or column-major. Do not use
- // constructs such as M[c+NumCols*r] or M[r+NumRows*c] that expose the
- // storage convention.
- inline Real const& operator[](int i) const
- {
- return mElements[i];
- }
- inline Real& operator[](int i)
- {
- return mElements[i];
- }
- // Comparisons for sorted containers and geometric ordering.
- inline bool operator==(GMatrix const& mat) const
- {
- return mNumRows == mat.mNumRows && mNumCols == mat.mNumCols
- && mElements == mat.mElements;
- }
- inline bool operator!=(GMatrix const& mat) const
- {
- return mNumRows == mat.mNumRows && mNumCols == mat.mNumCols
- && mElements != mat.mElements;
- }
- inline bool operator< (GMatrix const& mat) const
- {
- return mNumRows == mat.mNumRows && mNumCols == mat.mNumCols
- && mElements < mat.mElements;
- }
- inline bool operator<=(GMatrix const& mat) const
- {
- return mNumRows == mat.mNumRows && mNumCols == mat.mNumCols
- && mElements <= mat.mElements;
- }
- inline bool operator> (GMatrix const& mat) const
- {
- return mNumRows == mat.mNumRows && mNumCols == mat.mNumCols
- && mElements > mat.mElements;
- }
- inline bool operator>=(GMatrix const& mat) const
- {
- return mNumRows == mat.mNumRows && mNumCols == mat.mNumCols
- && mElements >= mat.mElements;
- }
- // Special matrices.
- // All components are 0.
- void MakeZero()
- {
- std::fill(mElements.begin(), mElements.end(), (Real)0);
- }
- // Component (r,c) is 1, all others zero.
- void MakeUnit(int r, int c)
- {
- if (0 <= r && r < mNumRows && 0 <= c && c < mNumCols)
- {
- MakeZero();
- operator()(r, c) = (Real)1;
- return;
- }
- LogError("Invalid index.");
- }
- // Diagonal entries 1, others 0, even when nonsquare.
- void MakeIdentity()
- {
- MakeZero();
- int const numDiagonal = (mNumRows <= mNumCols ? mNumRows : mNumCols);
- for (int i = 0; i < numDiagonal; ++i)
- {
- operator()(i, i) = (Real)1;
- }
- }
- static GMatrix Zero(int numRows, int numCols)
- {
- GMatrix<Real> M(numRows, numCols);
- M.MakeZero();
- return M;
- }
- static GMatrix Unit(int numRows, int numCols, int r, int c)
- {
- GMatrix<Real> M(numRows, numCols);
- M.MakeUnit(r, c);
- return M;
- }
- static GMatrix Identity(int numRows, int numCols)
- {
- GMatrix<Real> M(numRows, numCols);
- M.MakeIdentity();
- return M;
- }
- protected:
- // The matrix is stored as a 1-dimensional array. The convention of
- // row-major or column-major is your choice.
- int mNumRows, mNumCols;
- std::vector<Real> mElements;
- };
- // Unary operations.
- template <typename Real>
- GMatrix<Real> operator+(GMatrix<Real> const& M)
- {
- return M;
- }
- template <typename Real>
- GMatrix<Real> operator-(GMatrix<Real> const& M)
- {
- GMatrix<Real> result(M.GetNumRows(), M.GetNumCols());
- for (int i = 0; i < M.GetNumElements(); ++i)
- {
- result[i] = -M[i];
- }
- return result;
- }
- // Linear-algebraic operations.
- template <typename Real>
- GMatrix<Real> operator+(GMatrix<Real> const& M0, GMatrix<Real> const& M1)
- {
- GMatrix<Real> result = M0;
- return result += M1;
- }
- template <typename Real>
- GMatrix<Real> operator-(GMatrix<Real> const& M0, GMatrix<Real> const& M1)
- {
- GMatrix<Real> result = M0;
- return result -= M1;
- }
- template <typename Real>
- GMatrix<Real> operator*(GMatrix<Real> const& M, Real scalar)
- {
- GMatrix<Real> result = M;
- return result *= scalar;
- }
- template <typename Real>
- GMatrix<Real> operator*(Real scalar, GMatrix<Real> const& M)
- {
- GMatrix<Real> result = M;
- return result *= scalar;
- }
- template <typename Real>
- GMatrix<Real> operator/(GMatrix<Real> const& M, Real scalar)
- {
- GMatrix<Real> result = M;
- return result /= scalar;
- }
- template <typename Real>
- GMatrix<Real>& operator+=(GMatrix<Real>& M0, GMatrix<Real> const& M1)
- {
- if (M0.GetNumRows() == M1.GetNumRows() && M0.GetNumCols() == M1.GetNumCols())
- {
- for (int i = 0; i < M0.GetNumElements(); ++i)
- {
- M0[i] += M1[i];
- }
- return M0;
- }
- LogError("Mismatched sizes");
- }
- template <typename Real>
- GMatrix<Real>& operator-=(GMatrix<Real>& M0, GMatrix<Real> const& M1)
- {
- if (M0.GetNumRows() == M1.GetNumRows() && M0.GetNumCols() == M1.GetNumCols())
- {
- for (int i = 0; i < M0.GetNumElements(); ++i)
- {
- M0[i] -= M1[i];
- }
- return M0;
- }
- LogError("Mismatched sizes");
- }
- template <typename Real>
- GMatrix<Real>& operator*=(GMatrix<Real>& M, Real scalar)
- {
- for (int i = 0; i < M.GetNumElements(); ++i)
- {
- M[i] *= scalar;
- }
- return M;
- }
- template <typename Real>
- GMatrix<Real>& operator/=(GMatrix<Real>& M, Real scalar)
- {
- if (scalar != (Real)0)
- {
- Real invScalar = ((Real)1) / scalar;
- for (int i = 0; i < M.GetNumElements(); ++i)
- {
- M[i] *= invScalar;
- }
- return M;
- }
- LogError("Division by zero.");
- }
- // Geometric operations.
- template <typename Real>
- Real L1Norm(GMatrix<Real> const& M)
- {
- Real sum(0);
- for (int i = 0; i < M.GetNumElements(); ++i)
- {
- sum += std::fabs(M[i]);
- }
- return sum;
- }
- template <typename Real>
- Real L2Norm(GMatrix<Real> const& M)
- {
- Real sum(0);
- for (int i = 0; i < M.GetNumElements(); ++i)
- {
- sum += M[i] * M[i];
- }
- return std::sqrt(sum);
- }
- template <typename Real>
- Real LInfinityNorm(GMatrix<Real> const& M)
- {
- Real maxAbsElement(0);
- for (int i = 0; i < M.GetNumElements(); ++i)
- {
- Real absElement = std::fabs(M[i]);
- if (absElement > maxAbsElement)
- {
- maxAbsElement = absElement;
- }
- }
- return maxAbsElement;
- }
- template <typename Real>
- GMatrix<Real> Inverse(GMatrix<Real> const& M, bool* reportInvertibility = nullptr)
- {
- if (M.GetNumRows() == M.GetNumCols())
- {
- GMatrix<Real> invM(M.GetNumRows(), M.GetNumCols());
- Real determinant;
- bool invertible = GaussianElimination<Real>()(M.GetNumRows(), &M[0],
- &invM[0], determinant, nullptr, nullptr, nullptr, 0, nullptr);
- if (reportInvertibility)
- {
- *reportInvertibility = invertible;
- }
- return invM;
- }
- LogError("Matrix must be square.");
- }
- template <typename Real>
- Real Determinant(GMatrix<Real> const& M)
- {
- if (M.GetNumRows() == M.GetNumCols())
- {
- Real determinant;
- GaussianElimination<Real>()(M.GetNumRows(), &M[0], nullptr,
- determinant, nullptr, nullptr, nullptr, 0, nullptr);
- return determinant;
- }
- LogError("Matrix must be square.");
- }
- // M^T
- template <typename Real>
- GMatrix<Real> Transpose(GMatrix<Real> const& M)
- {
- GMatrix<Real> result(M.GetNumCols(), M.GetNumRows());
- for (int r = 0; r < M.GetNumRows(); ++r)
- {
- for (int c = 0; c < M.GetNumCols(); ++c)
- {
- result(c, r) = M(r, c);
- }
- }
- return result;
- }
- // M*V
- template <typename Real>
- GVector<Real> operator*(GMatrix<Real> const& M, GVector<Real> const& V)
- {
- if (V.GetSize() == M.GetNumCols())
- {
- GVector<Real> result(M.GetNumRows());
- for (int r = 0; r < M.GetNumRows(); ++r)
- {
- result[r] = (Real)0;
- for (int c = 0; c < M.GetNumCols(); ++c)
- {
- result[r] += M(r, c) * V[c];
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // V^T*M
- template <typename Real>
- GVector<Real> operator*(GVector<Real> const& V, GMatrix<Real> const& M)
- {
- if (V.GetSize() == M.GetNumRows())
- {
- GVector<Real> result(M.GetNumCols());
- for (int c = 0; c < M.GetNumCols(); ++c)
- {
- result[c] = (Real)0;
- for (int r = 0; r < M.GetNumRows(); ++r)
- {
- result[c] += V[r] * M(r, c);
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // A*B
- template <typename Real>
- GMatrix<Real> operator*(GMatrix<Real> const& A, GMatrix<Real> const& B)
- {
- return MultiplyAB(A, B);
- }
- template <typename Real>
- GMatrix<Real> MultiplyAB(GMatrix<Real> const& A, GMatrix<Real> const& B)
- {
- if (A.GetNumCols() == B.GetNumRows())
- {
- GMatrix<Real> result(A.GetNumRows(), B.GetNumCols());
- int const numCommon = A.GetNumCols();
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = (Real)0;
- for (int i = 0; i < numCommon; ++i)
- {
- result(r, c) += A(r, i) * B(i, c);
- }
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // A*B^T
- template <typename Real>
- GMatrix<Real> MultiplyABT(GMatrix<Real> const& A, GMatrix<Real> const& B)
- {
- if (A.GetNumCols() == B.GetNumCols())
- {
- GMatrix<Real> result(A.GetNumRows(), B.GetNumRows());
- int const numCommon = A.GetNumCols();
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = (Real)0;
- for (int i = 0; i < numCommon; ++i)
- {
- result(r, c) += A(r, i) * B(c, i);
- }
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // A^T*B
- template <typename Real>
- GMatrix<Real> MultiplyATB(GMatrix<Real> const& A, GMatrix<Real> const& B)
- {
- if (A.GetNumRows() == B.GetNumRows())
- {
- GMatrix<Real> result(A.GetNumCols(), B.GetNumCols());
- int const numCommon = A.GetNumRows();
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = (Real)0;
- for (int i = 0; i < numCommon; ++i)
- {
- result(r, c) += A(i, r) * B(i, c);
- }
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // A^T*B^T
- template <typename Real>
- GMatrix<Real> MultiplyATBT(GMatrix<Real> const& A, GMatrix<Real> const& B)
- {
- if (A.GetNumRows() == B.GetNumCols())
- {
- GMatrix<Real> result(A.GetNumCols(), B.GetNumRows());
- int const numCommon = A.GetNumRows();
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = (Real)0;
- for (int i = 0; i < numCommon; ++i)
- {
- result(r, c) += A(i, r) * B(c, i);
- }
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // M*D, D is square diagonal (stored as vector)
- template <typename Real>
- GMatrix<Real> MultiplyMD(GMatrix<Real> const& M, GVector<Real> const& D)
- {
- if (D.GetSize() == M.GetNumCols())
- {
- GMatrix<Real> result(M.GetNumRows(), M.GetNumCols());
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = M(r, c) * D[c];
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // D*M, D is square diagonal (stored as vector)
- template <typename Real>
- GMatrix<Real> MultiplyDM(GVector<Real> const& D, GMatrix<Real> const& M)
- {
- if (D.GetSize() == M.GetNumRows())
- {
- GMatrix<Real> result(M.GetNumRows(), M.GetNumCols());
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = D[r] * M(r, c);
- }
- }
- return result;
- }
- LogError("Mismatched sizes.");
- }
- // U*V^T, U is N-by-1, V is M-by-1, result is N-by-M.
- template <typename Real>
- GMatrix<Real> OuterProduct(GVector<Real> const& U, GVector<Real> const& V)
- {
- GMatrix<Real> result(U.GetSize(), V.GetSize());
- for (int r = 0; r < result.GetNumRows(); ++r)
- {
- for (int c = 0; c < result.GetNumCols(); ++c)
- {
- result(r, c) = U[r] * V[c];
- }
- }
- return result;
- }
- // Initialization to a diagonal matrix whose diagonal entries are the
- // components of D, even when nonsquare.
- template <typename Real>
- void MakeDiagonal(GVector<Real> const& D, GMatrix<Real>& M)
- {
- int const numRows = M.GetNumRows();
- int const numCols = M.GetNumCols();
- int const numDiagonal = (numRows <= numCols ? numRows : numCols);
- M.MakeZero();
- for (int i = 0; i < numDiagonal; ++i)
- {
- M(i, i) = D[i];
- }
- }
- }
|