2009-05-10 16:24:39 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2009-05-10 16:24:39 +00:00
|
|
|
//
|
|
|
|
|
// Copyright (C) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2012-02-03 23:18:26 +01:00
|
|
|
// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-05-10 16:24:39 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-05-10 16:24:39 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_DIAGONAL_H
|
|
|
|
|
#define EIGEN_DIAGONAL_H
|
|
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
namespace Eigen {
|
2012-04-15 11:06:28 +01:00
|
|
|
|
2009-05-10 16:24:39 +00:00
|
|
|
/** \class Diagonal
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2009-05-10 16:24:39 +00:00
|
|
|
*
|
|
|
|
|
* \brief Expression of a diagonal/subdiagonal/superdiagonal in a matrix
|
|
|
|
|
*
|
2021-12-21 16:46:09 +00:00
|
|
|
* \tparam MatrixType the type of the object in which we are taking a sub/main/super diagonal
|
|
|
|
|
* \tparam DiagIndex the index of the sub/super diagonal. The default is 0 and it means the main diagonal.
|
2009-05-10 16:24:39 +00:00
|
|
|
* A positive value means a superdiagonal, a negative value means a subdiagonal.
|
2017-01-21 22:09:59 +01:00
|
|
|
* You can also use DynamicIndex so the index can be set at runtime.
|
2009-05-10 16:24:39 +00:00
|
|
|
*
|
|
|
|
|
* The matrix is not required to be square.
|
|
|
|
|
*
|
|
|
|
|
* This class represents an expression of the main diagonal, or any sub/super diagonal
|
2010-05-30 16:00:58 -04:00
|
|
|
* of a square matrix. It is the return type of MatrixBase::diagonal() and MatrixBase::diagonal(Index) and most of the
|
2009-05-10 16:24:39 +00:00
|
|
|
* time this is the only way it is used.
|
|
|
|
|
*
|
2010-05-30 16:00:58 -04:00
|
|
|
* \sa MatrixBase::diagonal(), MatrixBase::diagonal(Index)
|
2009-05-10 16:24:39 +00:00
|
|
|
*/
|
2010-10-25 10:15:22 -04:00
|
|
|
|
|
|
|
|
namespace internal {
|
2010-05-30 16:00:58 -04:00
|
|
|
template <typename MatrixType, int DiagIndex>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct traits<Diagonal<MatrixType, DiagIndex> > : traits<MatrixType> {
|
2015-06-19 17:56:39 +02:00
|
|
|
typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
|
2010-05-30 16:00:58 -04:00
|
|
|
typedef typename MatrixType::StorageKind StorageKind;
|
2009-05-10 16:24:39 +00:00
|
|
|
enum {
|
2012-07-10 23:04:17 +02:00
|
|
|
RowsAtCompileTime = (int(DiagIndex) == DynamicIndex || int(MatrixType::SizeAtCompileTime) == Dynamic)
|
|
|
|
|
? Dynamic
|
2021-12-10 19:27:01 +00:00
|
|
|
: (plain_enum_min(MatrixType::RowsAtCompileTime - plain_enum_max(-DiagIndex, 0),
|
|
|
|
|
MatrixType::ColsAtCompileTime - plain_enum_max(DiagIndex, 0))),
|
2009-05-10 16:24:39 +00:00
|
|
|
ColsAtCompileTime = 1,
|
|
|
|
|
MaxRowsAtCompileTime =
|
|
|
|
|
int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
|
2021-12-10 19:27:01 +00:00
|
|
|
: DiagIndex == DynamicIndex
|
|
|
|
|
? min_size_prefer_fixed(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime)
|
|
|
|
|
: (plain_enum_min(MatrixType::MaxRowsAtCompileTime - plain_enum_max(-DiagIndex, 0),
|
|
|
|
|
MatrixType::MaxColsAtCompileTime - plain_enum_max(DiagIndex, 0))),
|
2009-05-10 16:24:39 +00:00
|
|
|
MaxColsAtCompileTime = 1,
|
2014-03-12 13:34:11 +01:00
|
|
|
MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
|
2022-01-10 20:53:29 +00:00
|
|
|
Flags = (unsigned int)MatrixTypeNested_::Flags & (RowMajorBit | MaskLvalueBit | DirectAccessBit) &
|
|
|
|
|
~RowMajorBit, // FIXME DirectAccessBit should not be handled by expressions
|
2010-10-25 10:15:22 -04:00
|
|
|
MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
|
2010-05-08 14:19:04 -04:00
|
|
|
InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride + 1,
|
|
|
|
|
OuterStrideAtCompileTime = 0
|
2009-05-10 16:24:39 +00:00
|
|
|
};
|
|
|
|
|
};
|
2021-08-04 22:41:52 +00:00
|
|
|
} // namespace internal
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2021-08-04 22:41:52 +00:00
|
|
|
template <typename MatrixType, int DiagIndex_>
|
|
|
|
|
class Diagonal : public internal::dense_xpr_base<Diagonal<MatrixType, DiagIndex_> >::type {
|
2009-05-10 16:24:39 +00:00
|
|
|
public:
|
2021-08-04 22:41:52 +00:00
|
|
|
enum { DiagIndex = DiagIndex_ };
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename internal::dense_xpr_base<Diagonal>::type Base;
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal)
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr explicit inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex)
|
2018-04-03 16:15:43 +02:00
|
|
|
: m_matrix(matrix), m_index(a_index) {
|
|
|
|
|
eigen_assert(a_index <= m_matrix.cols() && -a_index <= m_matrix.rows());
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2009-05-10 16:24:39 +00:00
|
|
|
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr inline Index rows() const {
|
2015-02-13 18:57:41 +01:00
|
|
|
return m_index.value() < 0 ? numext::mini<Index>(m_matrix.cols(), m_matrix.rows() + m_index.value())
|
|
|
|
|
: numext::mini<Index>(m_matrix.rows(), m_matrix.cols() - m_index.value());
|
2014-01-24 12:50:29 +01:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2025-04-22 00:58:47 +00:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return 1; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2025-04-22 00:58:47 +00:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept { return m_matrix.outerStride() + 1; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2025-04-22 00:58:47 +00:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return 0; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef std::conditional_t<internal::is_lvalue<MatrixType>::value, Scalar, const Scalar> ScalarWithConstIfNotLvalue;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2026-02-21 21:11:36 -08:00
|
|
|
EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() {
|
|
|
|
|
return rows() > 0 ? &(m_matrix.coeffRef(rowOffset(), colOffset())) : nullptr;
|
|
|
|
|
}
|
|
|
|
|
EIGEN_DEVICE_FUNC inline const Scalar* data() const {
|
|
|
|
|
return rows() > 0 ? &(m_matrix.coeffRef(rowOffset(), colOffset())) : nullptr;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index) {
|
2011-02-06 11:57:04 -05:00
|
|
|
EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
|
2016-01-28 21:43:20 +01:00
|
|
|
return m_matrix.coeffRef(row + rowOffset(), row + colOffset());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-12-22 17:45:37 -05:00
|
|
|
EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index) const {
|
2016-01-28 21:43:20 +01:00
|
|
|
return m_matrix.coeffRef(row + rowOffset(), row + colOffset());
|
2010-12-22 17:45:37 -05:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
EIGEN_DEVICE_FUNC inline CoeffReturnType coeff(Index row, Index) const {
|
2009-05-10 16:24:39 +00:00
|
|
|
return m_matrix.coeff(row + rowOffset(), row + colOffset());
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-06-22 16:32:45 +02:00
|
|
|
EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index idx) {
|
2011-02-06 11:57:04 -05:00
|
|
|
EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
|
2016-01-28 21:43:20 +01:00
|
|
|
return m_matrix.coeffRef(idx + rowOffset(), idx + colOffset());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-06-22 16:32:45 +02:00
|
|
|
EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index idx) const {
|
2016-01-28 21:43:20 +01:00
|
|
|
return m_matrix.coeffRef(idx + rowOffset(), idx + colOffset());
|
2010-12-22 17:45:37 -05:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2012-06-22 16:32:45 +02:00
|
|
|
EIGEN_DEVICE_FUNC inline CoeffReturnType coeff(Index idx) const {
|
|
|
|
|
return m_matrix.coeff(idx + rowOffset(), idx + colOffset());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr inline const internal::remove_all_t<typename MatrixType::Nested>& nestedExpression()
|
|
|
|
|
const {
|
2011-04-22 22:36:45 +01:00
|
|
|
return m_matrix;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr inline Index index() const { return m_index.value(); }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2009-05-10 16:24:39 +00:00
|
|
|
protected:
|
2011-04-22 22:36:45 +01:00
|
|
|
typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
|
2016-01-28 21:43:20 +01:00
|
|
|
const internal::variable_if_dynamicindex<Index, DiagIndex> m_index;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
private:
|
|
|
|
|
// some compilers may fail to optimize std::max etc in case of compile-time constants...
|
2026-02-20 19:50:52 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index absDiagIndex() const noexcept {
|
2021-03-08 12:39:11 -05:00
|
|
|
return m_index.value() > 0 ? m_index.value() : -m_index.value();
|
|
|
|
|
}
|
2026-02-20 19:50:52 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index rowOffset() const noexcept { return m_index.value() > 0 ? 0 : -m_index.value(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC constexpr Index colOffset() const noexcept { return m_index.value() > 0 ? m_index.value() : 0; }
|
2015-08-07 12:01:39 +02:00
|
|
|
// trigger a compile-time error if someone try to call packet
|
2010-07-07 11:07:12 +02:00
|
|
|
template <int LoadMode>
|
|
|
|
|
typename MatrixType::PacketReturnType packet(Index) const;
|
|
|
|
|
template <int LoadMode>
|
|
|
|
|
typename MatrixType::PacketReturnType packet(Index, Index) const;
|
2009-05-10 16:24:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \returns an expression of the main diagonal of the matrix \c *this
|
|
|
|
|
*
|
|
|
|
|
* \c *this is not required to be square.
|
|
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_diagonal.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_diagonal.out
|
|
|
|
|
*
|
|
|
|
|
* \sa class Diagonal */
|
|
|
|
|
template <typename Derived>
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::DiagonalReturnType MatrixBase<Derived>::diagonal() {
|
2014-09-23 14:28:23 +02:00
|
|
|
return DiagonalReturnType(derived());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** This is the const version of diagonal(). */
|
|
|
|
|
template <typename Derived>
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr const typename MatrixBase<Derived>::ConstDiagonalReturnType MatrixBase<Derived>::diagonal()
|
2009-05-10 16:24:39 +00:00
|
|
|
const {
|
2011-01-18 09:14:14 -05:00
|
|
|
return ConstDiagonalReturnType(derived());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
/** \returns an expression of the \a DiagIndex-th sub or super diagonal of the matrix \c *this
|
2009-05-10 16:24:39 +00:00
|
|
|
*
|
|
|
|
|
* \c *this is not required to be square.
|
|
|
|
|
*
|
2010-05-30 16:00:58 -04:00
|
|
|
* The template parameter \a DiagIndex represent a super diagonal if \a DiagIndex > 0
|
|
|
|
|
* and a sub diagonal otherwise. \a DiagIndex == 0 is equivalent to the main diagonal.
|
2009-05-10 16:24:39 +00:00
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_diagonal_int.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_diagonal_int.out
|
|
|
|
|
*
|
|
|
|
|
* \sa MatrixBase::diagonal(), class Diagonal */
|
|
|
|
|
template <typename Derived>
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Diagonal<Derived, DynamicIndex> MatrixBase<Derived>::diagonal(Index index) {
|
2022-04-08 18:05:32 +00:00
|
|
|
return Diagonal<Derived, DynamicIndex>(derived(), index);
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
/** This is the const version of diagonal(Index). */
|
2009-05-10 16:24:39 +00:00
|
|
|
template <typename Derived>
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr const Diagonal<const Derived, DynamicIndex> MatrixBase<Derived>::diagonal(
|
|
|
|
|
Index index) const {
|
2022-04-08 18:05:32 +00:00
|
|
|
return Diagonal<const Derived, DynamicIndex>(derived(), index);
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
/** \returns an expression of the \a DiagIndex-th sub or super diagonal of the matrix \c *this
|
2009-05-10 16:24:39 +00:00
|
|
|
*
|
|
|
|
|
* \c *this is not required to be square.
|
|
|
|
|
*
|
2010-05-30 16:00:58 -04:00
|
|
|
* The template parameter \a DiagIndex represent a super diagonal if \a DiagIndex > 0
|
|
|
|
|
* and a sub diagonal otherwise. \a DiagIndex == 0 is equivalent to the main diagonal.
|
2009-05-10 16:24:39 +00:00
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_diagonal_template_int.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_diagonal_template_int.out
|
|
|
|
|
*
|
|
|
|
|
* \sa MatrixBase::diagonal(), class Diagonal */
|
|
|
|
|
template <typename Derived>
|
2014-09-23 14:28:23 +02:00
|
|
|
template <int Index_>
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Diagonal<Derived, Index_> MatrixBase<Derived>::diagonal() {
|
2022-04-08 18:05:32 +00:00
|
|
|
return Diagonal<Derived, Index_>(derived());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** This is the const version of diagonal<int>(). */
|
|
|
|
|
template <typename Derived>
|
2014-09-23 14:28:23 +02:00
|
|
|
template <int Index_>
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr const Diagonal<const Derived, Index_> MatrixBase<Derived>::diagonal() const {
|
2022-04-08 18:05:32 +00:00
|
|
|
return Diagonal<const Derived, Index_>(derived());
|
2009-05-10 16:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2009-05-10 16:24:39 +00:00
|
|
|
#endif // EIGEN_DIAGONAL_H
|