mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
@@ -372,6 +372,53 @@ bool MatrixBase<Derived>::isDiagonal(const RealScalar& prec) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** \returns DiagonalWrapper.
|
||||
*
|
||||
* Example: \include MatrixBase_diagonalView.cpp
|
||||
* Output: \verbinclude MatrixBase_diagonalView.out
|
||||
*
|
||||
* \sa diagonalView()
|
||||
*/
|
||||
|
||||
/** This is the non-const version of diagonalView() with DiagIndex_ . */
|
||||
template <typename Derived>
|
||||
template <int DiagIndex_>
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<Derived, DiagIndex_>> MatrixBase<Derived>::diagonalView() {
|
||||
typedef Diagonal<Derived, DiagIndex_> DiagType;
|
||||
typedef DiagonalWrapper<DiagType> ReturnType;
|
||||
DiagType diag(this->derived());
|
||||
return ReturnType(diag);
|
||||
}
|
||||
|
||||
/** This is the const version of diagonalView() with DiagIndex_ . */
|
||||
template <typename Derived>
|
||||
template <int DiagIndex_>
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<const Derived, DiagIndex_>> MatrixBase<Derived>::diagonalView() const {
|
||||
typedef Diagonal<const Derived, DiagIndex_> DiagType;
|
||||
typedef DiagonalWrapper<DiagType> ReturnType;
|
||||
DiagType diag(this->derived());
|
||||
return ReturnType(diag);
|
||||
}
|
||||
|
||||
/** This is the non-const version of diagonalView() with dynamic index. */
|
||||
template <typename Derived>
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<Derived, DynamicIndex>> MatrixBase<Derived>::diagonalView(Index index) {
|
||||
typedef Diagonal<Derived, DynamicIndex> DiagType;
|
||||
typedef DiagonalWrapper<DiagType> ReturnType;
|
||||
DiagType diag(this->derived(), index);
|
||||
return ReturnType(diag);
|
||||
}
|
||||
|
||||
/** This is the const version of diagonalView() with dynamic index. */
|
||||
template <typename Derived>
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<const Derived, DynamicIndex>> MatrixBase<Derived>::diagonalView(
|
||||
Index index) const {
|
||||
typedef Diagonal<const Derived, DynamicIndex> DiagType;
|
||||
typedef DiagonalWrapper<DiagType> ReturnType;
|
||||
DiagType diag(this->derived(), index);
|
||||
return ReturnType(diag);
|
||||
}
|
||||
|
||||
namespace internal {
|
||||
|
||||
template <>
|
||||
|
||||
@@ -274,6 +274,17 @@ class MatrixBase : public DenseBase<Derived> {
|
||||
const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
bool isUnitary(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||
|
||||
/* diagonalView */
|
||||
template <int DiagIndex_ = 0>
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<Derived, DiagIndex_>> diagonalView();
|
||||
|
||||
template <int DiagIndex_ = 0>
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<const Derived, DiagIndex_>> diagonalView() const;
|
||||
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<Derived, DynamicIndex>> diagonalView(Index index);
|
||||
|
||||
EIGEN_DEVICE_FUNC DiagonalWrapper<Diagonal<const Derived, DynamicIndex>> diagonalView(Index index) const;
|
||||
|
||||
/** \returns true if each coefficients of \c *this and \a other are all exactly equal.
|
||||
* \warning When using floating point scalar values you probably should rather use a
|
||||
* fuzzy comparison such as isApprox()
|
||||
|
||||
5
doc/snippets/MatrixBase_diagonalView.cpp
Normal file
5
doc/snippets/MatrixBase_diagonalView.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
Matrix3d mat3;
|
||||
mat3 << 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9;
|
||||
cout << "Here's the matrix m:" << endl << m << endl;
|
||||
cout << "m.diagonal().asDiagonal() returns: " << m.diagonal().asDiagonal() << endl;
|
||||
cout << "m.diagonalView() returns: " << m.diagonalView() << endl;
|
||||
@@ -211,6 +211,7 @@ ei_add_test(product_small)
|
||||
ei_add_test(product_large)
|
||||
ei_add_test(product_extra)
|
||||
ei_add_test(diagonalmatrices)
|
||||
ei_add_test(diagonalview)
|
||||
ei_add_test(skew_symmetric_matrix3)
|
||||
ei_add_test(adjoint)
|
||||
ei_add_test(diagonal)
|
||||
|
||||
55
test/diagonalview.cpp
Normal file
55
test/diagonalview.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
||||
//
|
||||
// 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/.
|
||||
|
||||
// discard stack allocation as that too bypasses malloc
|
||||
#define EIGEN_STACK_ALLOCATION_LIMIT 0
|
||||
// heap allocation will raise an assert if enabled at runtime
|
||||
#define EIGEN_RUNTIME_NO_MALLOC
|
||||
|
||||
#include "main.h"
|
||||
using namespace std;
|
||||
template <typename MatrixType>
|
||||
void diagonalview(const MatrixType& m) {
|
||||
enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
|
||||
Index rows = m.rows();
|
||||
Index cols = m.cols();
|
||||
// create random matrix
|
||||
MatrixType m1 = MatrixType::Random(rows, cols);
|
||||
|
||||
// check equivalence to diagonal(i).asDiagonal() for dynamic indexes
|
||||
VERIFY_IS_APPROX(m1.diagonal(0).asDiagonal().toDenseMatrix(), m1.diagonalView(0).toDenseMatrix());
|
||||
// subdiagonal
|
||||
VERIFY_IS_APPROX(m1.diagonal(-1).asDiagonal().toDenseMatrix(), m1.diagonalView(-1).toDenseMatrix());
|
||||
// superdiagonal
|
||||
VERIFY_IS_APPROX(m1.diagonal(1).asDiagonal().toDenseMatrix(), m1.diagonalView(1).toDenseMatrix());
|
||||
|
||||
// check equivalence to diagonal(i).asDiagonal() for compile time indexes
|
||||
VERIFY_IS_APPROX(m1.diagonal(0).asDiagonal().toDenseMatrix(), m1.template diagonalView<0>().toDenseMatrix());
|
||||
// sub
|
||||
VERIFY_IS_APPROX(m1.diagonal(-1).asDiagonal().toDenseMatrix(), m1.template diagonalView<-1>().toDenseMatrix());
|
||||
// super
|
||||
VERIFY_IS_APPROX(m1.diagonal(1).asDiagonal().toDenseMatrix(), m1.template diagonalView<1>().toDenseMatrix());
|
||||
|
||||
// check const overloads
|
||||
const auto m2(m1);
|
||||
typedef decltype(m1) Type1;
|
||||
typedef decltype(m2) Type2;
|
||||
constexpr bool types_are_same = std::is_same<Type1, Type2>::value;
|
||||
VERIFY(!types_are_same);
|
||||
|
||||
VERIFY_IS_APPROX(m2.diagonal(0).asDiagonal().toDenseMatrix(), m2.diagonalView(0).toDenseMatrix());
|
||||
VERIFY_IS_APPROX(m2.diagonal(1).asDiagonal().toDenseMatrix(), m2.template diagonalView<1>().toDenseMatrix());
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(diagonalview) {
|
||||
for (int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_1(diagonalview(Matrix<float, 3, 3>()));
|
||||
CALL_SUBTEST_2(diagonalview(Matrix<int, 50, 50>()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user