#ifndef Magnum_Math_RectangularMatrix_h #define Magnum_Math_RectangularMatrix_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Class @ref Magnum::Math::RectangularMatrix, alias @ref Magnum::Math::Matrix2x3, @ref Magnum::Math::Matrix3x2, @ref Magnum::Math::Matrix2x4, @ref Magnum::Math::Matrix4x2, @ref Magnum::Math::Matrix3x4, @ref Magnum::Math::Matrix4x3 */ #include "Magnum/Math/Vector.h" namespace Magnum { namespace Math { namespace Implementation { template struct RectangularMatrixConverter; } /** @brief Rectangular matrix @tparam cols Column count @tparam rows Row count @tparam T Underlying data type See @ref matrix-vector for brief introduction. See also @ref Matrix (square) and @ref Vector. The data are stored in column-major order, to reflect that, all indices in math formulas are in reverse order (i.e. @f$ \boldsymbol A_{ji} @f$ instead of @f$ \boldsymbol A_{ij} @f$). @see @ref Matrix2x3, @ref Matrix3x2, @ref Matrix2x4, @ref Matrix4x2, @ref Matrix3x4, @ref Matrix4x3 */ template class RectangularMatrix { static_assert(cols != 0 && rows != 0, "RectangularMatrix cannot have zero elements"); template friend class RectangularMatrix; public: typedef T Type; /**< @brief Underlying data type */ enum: std::size_t { Cols = cols, /**< Matrix column count */ Rows = rows, /**< Matrix row count */ /** * Size of matrix diagonal * @see @ref fromDiagonal(), @ref diagonal() */ DiagonalSize = (cols < rows ? cols : rows) }; /** * @brief Matrix from array * @return Reference to the data as if it was matrix, thus doesn't * perform any copying. * * @attention Use with caution, the function doesn't check whether the * array is long enough. */ static RectangularMatrix& from(T* data) { return *reinterpret_cast*>(data); } /** @overload */ static const RectangularMatrix& from(const T* data) { return *reinterpret_cast*>(data); } /** * @brief Construct matrix from vector * * Rolls the vector into matrix, i.e. first `rows` elements of the * vector will make first column of resulting matrix. * @see @ref toVector() */ static RectangularMatrix fromVector(const Vector& vector) { return *reinterpret_cast*>(vector.data()); } /** * @brief Construct diagonal matrix * * @see @ref diagonal() */ constexpr static RectangularMatrix fromDiagonal(const Vector& diagonal) noexcept { return RectangularMatrix(typename Implementation::GenerateSequence::Type(), diagonal); } /** @brief Construct zero-filled matrix */ constexpr /*implicit*/ RectangularMatrix(ZeroInitT = ZeroInit) noexcept /** @todoc remove workaround when doxygen is sane */ #ifndef DOXYGEN_GENERATING_OUTPUT : RectangularMatrix{typename Implementation::GenerateSequence::Type{}, ZeroInit} #endif {} /** @brief Construct matrix without initializing the contents */ explicit RectangularMatrix(NoInitT) noexcept /** @todoc remove workaround when doxygen is sane */ #ifndef DOXYGEN_GENERATING_OUTPUT : RectangularMatrix{typename Implementation::GenerateSequence::Type{}, NoInit} #endif {} /** @brief Construct matrix from column vectors */ template constexpr /*implicit*/ RectangularMatrix(const Vector& first, const U&... next) noexcept: _data{first, next...} { static_assert(sizeof...(next)+1 == cols, "Improper number of arguments passed to RectangularMatrix constructor"); } /** @brief Construct matrix with one value for all components */ constexpr explicit RectangularMatrix(T value) noexcept /** @todoc remove workaround when doxygen is sane */ #ifndef DOXYGEN_GENERATING_OUTPUT : RectangularMatrix{typename Implementation::GenerateSequence::Type(), value} #endif {} /** * @brief Construct matrix from another of different type * * Performs only default casting on the values, no rounding or * anything else. Example usage: * * @snippet MagnumMath.cpp RectangularMatrix-conversion */ template constexpr explicit RectangularMatrix(const RectangularMatrix& other) noexcept: RectangularMatrix(typename Implementation::GenerateSequence::Type(), other) {} /** @brief Construct matrix from external representation */ template::from(std::declval()))> constexpr explicit RectangularMatrix(const U& other): RectangularMatrix(Implementation::RectangularMatrixConverter::from(other)) {} /** @brief Copy constructor */ constexpr /*implicit*/ RectangularMatrix(const RectangularMatrix&) noexcept = default; /** @brief Convert matrix to external representation */ template::to(std::declval>()))> constexpr explicit operator U() const { return Implementation::RectangularMatrixConverter::to(*this); } /** * @brief Raw data * @return One-dimensional array of `cols*rows` length in column-major * order. * * @see @ref operator[]() */ T* data() { return _data[0].data(); } constexpr const T* data() const { return _data[0].data(); } /**< @overload */ /** * @brief Matrix column * * Particular elements can be accessed using @ref Vector::operator[](), * e.g.: * * @snippet MagnumMath.cpp RectangularMatrix-access * * @see @ref row(), @ref data() */ Vector& operator[](std::size_t col) { return _data[col]; } /* returns const& so [][] operations are also constexpr */ constexpr const Vector& operator[](std::size_t col) const { return _data[col]; } /**< @overload */ /** * @brief Matrix row * * Consider using @ref transposed() when accessing rows frequently, as * this is slower than accessing columns due to the way the matrix is * stored. * @see @ref setRow(), @ref operator[]() */ Vector row(std::size_t row) const; /** * @brief Set matrix row * * Consider using @ref transposed() when accessing rows frequently, as * this is slower than accessing columns due to the way the matrix is * stored. * @see @ref row(), @ref operator[]() */ void setRow(std::size_t row, const Vector& data); /** @brief Equality comparison */ bool operator==(const RectangularMatrix& other) const { for(std::size_t i = 0; i != cols; ++i) if(_data[i] != other._data[i]) return false; return true; } /** * @brief Non-equality operator * * @see @ref Vector::operator<(), @ref Vector::operator<=(), * @ref Vector::operator>=(), @ref Vector::operator>() */ bool operator!=(const RectangularMatrix& other) const { return !operator==(other); } /** * @brief Component-wise less than * * Calls @ref Vector::operator<() on @ref toVector(). */ BoolVector operator<(const RectangularMatrix& other) const { return toVector() < other.toVector(); } /** * @brief Component-wise less than or equal * * Calls @ref Vector::operator<=() on @ref toVector(). */ BoolVector operator<=(const RectangularMatrix& other) const { return toVector() <= other.toVector(); } /** * @brief Component-wise greater than or equal * * Calls @ref Vector::operator>=() on @ref toVector(). */ BoolVector operator>=(const RectangularMatrix& other) const { return toVector() >= other.toVector(); } /** * @brief Component-wise greater than * * Calls @ref Vector::operator>() on @ref toVector(). */ BoolVector operator>(const RectangularMatrix& other) const { return toVector() > other.toVector(); } /** * @brief Negated matrix * * The computation is done column-wise. @f[ * \boldsymbol B_j = -\boldsymbol A_j * @f] */ RectangularMatrix operator-() const; /** * @brief Add and assign matrix * * The computation is done column-wise in-place. @f[ * \boldsymbol A_j = \boldsymbol A_j + \boldsymbol B_j * @f] */ RectangularMatrix& operator+=(const RectangularMatrix& other) { for(std::size_t i = 0; i != cols; ++i) _data[i] += other._data[i]; return *this; } /** * @brief Add matrix * * @see @ref operator+=() */ RectangularMatrix operator+(const RectangularMatrix& other) const { return RectangularMatrix(*this)+=other; } /** * @brief Subtract and assign matrix * * The computation is done column-wise in-place. @f[ * \boldsymbol A_j = \boldsymbol A_j - \boldsymbol B_j * @f] */ RectangularMatrix& operator-=(const RectangularMatrix& other) { for(std::size_t i = 0; i != cols; ++i) _data[i] -= other._data[i]; return *this; } /** * @brief Subtract matrix * * @see @ref operator-=() */ RectangularMatrix operator-(const RectangularMatrix& other) const { return RectangularMatrix(*this)-=other; } /** * @brief Multiply matrix with number and assign * * The computation is done column-wise in-place. @f[ * \boldsymbol A_j = a \boldsymbol A_j * @f] */ RectangularMatrix& operator*=(T number) { for(std::size_t i = 0; i != cols; ++i) _data[i] *= number; return *this; } /** * @brief Multiply matrix with number * * @see @ref operator*=(T), @ref operator*(T, const RectangularMatrix&) */ RectangularMatrix operator*(T number) const { return RectangularMatrix(*this) *= number; } /** * @brief Divide matrix with number and assign * * The computation is done column-wise in-place. @f[ * \boldsymbol A_j = \frac{\boldsymbol A_j} a * @f] */ RectangularMatrix& operator/=(T number) { for(std::size_t i = 0; i != cols; ++i) _data[i] /= number; return *this; } /** * @brief Divide matrix with number * * @see @ref operator/=(T), * @ref operator/(T, const RectangularMatrix&) */ RectangularMatrix operator/(T number) const { return RectangularMatrix(*this) /= number; } /** * @brief Multiply matrix * * @f[ * (\boldsymbol {AB})_{ji} = \sum_{k=0}^{m-1} \boldsymbol A_{ki} \boldsymbol B_{jk} * @f] * @m_keyword{outerProduct(),GLSL outerProduct(),} */ template RectangularMatrix operator*(const RectangularMatrix& other) const; /** * @brief Multiply vector * * Internally the same as multiplying with one-column matrix, but * returns vector. @f[ * (\boldsymbol {Aa})_i = \sum_{k=0}^{m-1} \boldsymbol A_{ki} \boldsymbol a_k * @f] */ Vector operator*(const Vector& other) const { return operator*(RectangularMatrix<1, cols, T>(other))[0]; } /** * @brief Transposed matrix * * @f[ * \boldsymbol{A}^T_ij = \boldsymbol{A}_ji * @f] * @see @ref row(), @ref flippedCols(), @ref flippedRows() * @m_keyword{transpose(),GLSL transpose(),} */ RectangularMatrix transposed() const; /** * @brief Matrix with flipped cols * * The order of columns is reversed. * @see @ref transposed(), @ref flippedRows(), @ref Vector::flipped() */ constexpr RectangularMatrix flippedCols() const { return flippedColsInternal(typename Implementation::GenerateSequence::Type{}); } /** * @brief Matrix with flipped rows * * The order of rows is reversed. * @see @ref transposed(), @ref flippedCols(), @ref Vector::flipped() */ constexpr RectangularMatrix flippedRows() const { return flippedRowsInternal(typename Implementation::GenerateSequence::Type{}); } /** * @brief Values on diagonal * * @see @ref fromDiagonal() */ constexpr Vector diagonal() const; /** * @brief Convert matrix to vector * * Returns the matrix unrolled into one large vector, i.e. first column * of the matrix will make first `rows` elements of resulting vector. * Useful for performing vector operations with the matrix (e.g. * summing the elements etc.). * @see @ref fromVector() */ Vector toVector() const { return *reinterpret_cast*>(data()); } #ifndef DOXYGEN_GENERATING_OUTPUT protected: #else private: #endif /* Implementation for RectangularMatrix::fromDiagonal() and Matrix(IdentityInitT, T) */ template constexpr explicit RectangularMatrix(Implementation::Sequence, const Vector& diagonal); /* Implementation for RectangularMatrix::RectangularMatrix(T) and Matrix(T) */ /* MSVC 2015 can't handle {} here */ template constexpr explicit RectangularMatrix(Implementation::Sequence, T value) noexcept: _data{Vector((static_cast(sequence), value))...} {} private: /* Implementation for RectangularMatrix::RectangularMatrix(const RectangularMatrix&) */ template constexpr explicit RectangularMatrix(Implementation::Sequence, const RectangularMatrix& matrix) noexcept: _data{Vector(matrix[sequence])...} {} /* Implementation for RectangularMatrix::RectangularMatrix(ZeroInitT) and RectangularMatrix::RectangularMatrix(NoInitT) */ /* MSVC 2015 can't handle {} here */ template constexpr explicit RectangularMatrix(Implementation::Sequence, U) noexcept: _data{Vector((static_cast(sequence), U{typename U::Init{}}))...} {} template constexpr RectangularMatrix flippedColsInternal(Implementation::Sequence) const { return {(*this)[cols - 1 - sequence]...}; } template constexpr RectangularMatrix flippedRowsInternal(Implementation::Sequence) const { return {(*this)[sequence].flipped()...}; } template constexpr Vector diagonalInternal(Implementation::Sequence) const; Vector _data[cols]; }; /** @brief Matrix with 2 columns and 3 rows Convenience alternative to @cpp RectangularMatrix<2, 3, T> @ce. See @ref RectangularMatrix for more information. @see @ref Magnum::Matrix2x3, @ref Magnum::Matrix2x3d */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using Matrix2x3 = RectangularMatrix<2, 3, T>; #endif /** @brief Matrix with 3 columns and 2 rows Convenience alternative to @cpp RectangularMatrix<3, 2, T> @ce. See @ref RectangularMatrix for more information. @see @ref Magnum::Matrix3x2, @ref Magnum::Matrix3x2d */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using Matrix3x2 = RectangularMatrix<3, 2, T>; #endif /** @brief Matrix with 2 columns and 4 rows Convenience alternative to @cpp RectangularMatrix<2, 4, T> @ce. See @ref RectangularMatrix for more information. @see @ref Magnum::Matrix2x4, @ref Magnum::Matrix2x4d */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using Matrix2x4 = RectangularMatrix<2, 4, T>; #endif /** @brief Matrix with 4 columns and 2 rows Convenience alternative to @cpp RectangularMatrix<4, 2, T> @ce. See @ref RectangularMatrix for more information. @see @ref Magnum::Matrix4x2, @ref Magnum::Matrix4x2d */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using Matrix4x2 = RectangularMatrix<4, 2, T>; #endif /** @brief Matrix with 3 columns and 4 rows Convenience alternative to @cpp RectangularMatrix<3, 4, T> @ce. See @ref RectangularMatrix for more information. @see @ref Magnum::Matrix3x4, @ref Magnum::Matrix3x4d */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using Matrix3x4 = RectangularMatrix<3, 4, T>; #endif /** @brief Matrix with 4 columns and 3 rows Convenience alternative to @cpp RectangularMatrix<4, 3, T> @ce. See @ref RectangularMatrix for more information. @see @ref Magnum::Matrix4x3, @ref Magnum::Matrix4x3d */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using Matrix4x3 = RectangularMatrix<4, 3, T>; #endif /** @relates RectangularMatrix @brief Multiply number with matrix Same as @ref RectangularMatrix::operator*(T) const. */ template inline RectangularMatrix operator*( #ifdef DOXYGEN_GENERATING_OUTPUT T #else typename std::common_type::type #endif number, const RectangularMatrix& matrix) { return matrix*number; } /** @relates RectangularMatrix @brief Divide matrix with number and invert The computation is done column-wise. @f[ \boldsymbol B_j = \frac a {\boldsymbol A_j} @f] @see @ref RectangularMatrix::operator/(T) const */ template inline RectangularMatrix operator/( #ifdef DOXYGEN_GENERATING_OUTPUT T #else typename std::common_type::type #endif number, const RectangularMatrix& matrix) { RectangularMatrix out{NoInit}; for(std::size_t i = 0; i != cols; ++i) out[i] = number/matrix[i]; return out; } /** @relates RectangularMatrix @brief Multiply vector with rectangular matrix Internally the same as multiplying one-column matrix with one-row matrix. @f[ (\boldsymbol {aA})_{ji} = \boldsymbol a_i \boldsymbol A_j @f] @see @ref RectangularMatrix::operator*(const RectangularMatrix&) const */ template inline RectangularMatrix operator*(const Vector& vector, const RectangularMatrix& matrix) { return RectangularMatrix<1, size, T>(vector)*matrix; } /** @debugoperator{RectangularMatrix} */ template Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Magnum::Math::RectangularMatrix& value) { debug << "Matrix(" << Corrade::Utility::Debug::nospace; for(std::size_t row = 0; row != rows; ++row) { if(row != 0) debug << Corrade::Utility::Debug::nospace << ",\n "; for(std::size_t col = 0; col != cols; ++col) { if(col != 0) debug << Corrade::Utility::Debug::nospace << ","; debug << value[col][row]; } } return debug << Corrade::Utility::Debug::nospace << ")"; } /* Explicit instantiation for commonly used types */ #ifndef DOXYGEN_GENERATING_OUTPUT /* Square matrices */ extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<2, 2, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<3, 3, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<4, 4, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<2, 2, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<3, 3, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<4, 4, Double>&); /* Rectangular matrices */ extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<2, 3, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<3, 2, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<2, 4, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<4, 2, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<3, 4, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<4, 3, Float>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<2, 3, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<3, 2, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<2, 4, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<4, 2, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<3, 4, Double>&); extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const RectangularMatrix<4, 3, Double>&); #define MAGNUM_RECTANGULARMATRIX_SUBCLASS_IMPLEMENTATION(cols, rows, ...) \ static __VA_ARGS__& from(T* data) { \ return *reinterpret_cast<__VA_ARGS__*>(data); \ } \ static const __VA_ARGS__& from(const T* data) { \ return *reinterpret_cast(data); \ } \ constexpr static __VA_ARGS__ fromDiagonal(const Vector::DiagonalSize, T>& diagonal) { \ return Math::RectangularMatrix::fromDiagonal(diagonal); \ } \ \ __VA_ARGS__ operator-() const { \ return Math::RectangularMatrix::operator-(); \ } \ __VA_ARGS__& operator+=(const Math::RectangularMatrix& other) { \ Math::RectangularMatrix::operator+=(other); \ return *this; \ } \ __VA_ARGS__ operator+(const Math::RectangularMatrix& other) const { \ return Math::RectangularMatrix::operator+(other); \ } \ __VA_ARGS__& operator-=(const Math::RectangularMatrix& other) { \ Math::RectangularMatrix::operator-=(other); \ return *this; \ } \ __VA_ARGS__ operator-(const Math::RectangularMatrix& other) const { \ return Math::RectangularMatrix::operator-(other); \ } \ __VA_ARGS__& operator*=(T number) { \ Math::RectangularMatrix::operator*=(number); \ return *this; \ } \ __VA_ARGS__ operator*(T number) const { \ return Math::RectangularMatrix::operator*(number); \ } \ __VA_ARGS__& operator/=(T number) { \ Math::RectangularMatrix::operator/=(number); \ return *this; \ } \ __VA_ARGS__ operator/(T number) const { \ return Math::RectangularMatrix::operator/(number); \ } \ constexpr __VA_ARGS__ flippedCols() const { \ return Math::RectangularMatrix::flippedCols(); \ } \ constexpr __VA_ARGS__ flippedRows() const { \ return Math::RectangularMatrix::flippedRows(); \ } \ #define MAGNUM_MATRIX_OPERATOR_IMPLEMENTATION(...) \ template inline __VA_ARGS__ operator*(typename std::common_type::type number, const __VA_ARGS__& matrix) { \ return number*static_cast&>(matrix); \ } \ template inline __VA_ARGS__ operator/(typename std::common_type::type number, const __VA_ARGS__& matrix) { \ return number/static_cast&>(matrix); \ } \ template inline __VA_ARGS__ operator*(const Vector& vector, const RectangularMatrix& matrix) { \ return Math::RectangularMatrix<1, size, T>(vector)*matrix; \ } #define MAGNUM_MATRIXn_OPERATOR_IMPLEMENTATION(size, Type) \ template inline Type operator*(typename std::common_type::type number, const Type& matrix) { \ return number*static_cast&>(matrix); \ } \ template inline Type operator/(typename std::common_type::type number, const Type& matrix) { \ return number/static_cast&>(matrix); \ } \ template inline Type operator*(const Vector& vector, const RectangularMatrix& matrix) { \ return Math::RectangularMatrix<1, size, T>(vector)*matrix; \ } #endif namespace Implementation { template constexpr Vector diagonalMatrixColumn2(Implementation::Sequence, const T& number) { return {(sequence == i ? number : T(0))...}; } template constexpr Vector diagonalMatrixColumn(const T& number) { return diagonalMatrixColumn2(typename Implementation::GenerateSequence::Type(), number); } } template template constexpr RectangularMatrix::RectangularMatrix(Implementation::Sequence, const Vector& diagonal): _data{Implementation::diagonalMatrixColumn(sequence < DiagonalSize ? diagonal[sequence] : T{})...} {} template inline Vector RectangularMatrix::row(std::size_t row) const { Vector out; for(std::size_t i = 0; i != cols; ++i) out[i] = _data[i][row]; return out; } template inline void RectangularMatrix::setRow(std::size_t row, const Vector& data) { for(std::size_t i = 0; i != cols; ++i) _data[i][row] = data[i]; } template inline RectangularMatrix RectangularMatrix::operator-() const { RectangularMatrix out; for(std::size_t i = 0; i != cols; ++i) out._data[i] = -_data[i]; return out; } template template inline RectangularMatrix RectangularMatrix::operator*(const RectangularMatrix& other) const { RectangularMatrix out{ZeroInit}; for(std::size_t col = 0; col != size; ++col) for(std::size_t row = 0; row != rows; ++row) for(std::size_t pos = 0; pos != cols; ++pos) out[col][row] += _data[pos][row]*other._data[col][pos]; return out; } template inline RectangularMatrix RectangularMatrix::transposed() const { RectangularMatrix out{NoInit}; for(std::size_t col = 0; col != cols; ++col) for(std::size_t row = 0; row != rows; ++row) out[row][col] = _data[col][row]; return out; } template constexpr auto RectangularMatrix::diagonal() const -> Vector { return diagonalInternal(typename Implementation::GenerateSequence::Type()); } #ifndef DOXYGEN_GENERATING_OUTPUT template template constexpr auto RectangularMatrix::diagonalInternal(Implementation::Sequence) const -> Vector { return {(*this)[sequence][sequence]...}; } #endif namespace Implementation { template struct StrictWeakOrdering> { bool operator()(const RectangularMatrix& a, const RectangularMatrix& b) const { StrictWeakOrdering> o; for(std::size_t i = 0; i < cols; ++i) { if(o(a[i], b[i])) return true; if(o(b[i], a[i])) return false; } return false; /* a and b are equivalent */ } }; } }} #endif