// This file is triangularView of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008-2009 Gael Guennebaud // // 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/. #include "main.h" template void bandmatrix(const MatrixType& _m) { typedef typename MatrixType::Scalar Scalar; typedef typename NumTraits::Real RealScalar; typedef Matrix DenseMatrixType; Index rows = _m.rows(); Index cols = _m.cols(); Index supers = _m.supers(); Index subs = _m.subs(); MatrixType m(rows, cols, supers, subs); DenseMatrixType dm1(rows, cols); dm1.setZero(); m.diagonal().setConstant(123); dm1.diagonal().setConstant(123); for (int i = 1; i <= m.supers(); ++i) { m.diagonal(i).setConstant(static_cast(i)); dm1.diagonal(i).setConstant(static_cast(i)); } for (int i = 1; i <= m.subs(); ++i) { m.diagonal(-i).setConstant(-static_cast(i)); dm1.diagonal(-i).setConstant(-static_cast(i)); } // std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n"; VERIFY_IS_APPROX(dm1, m.toDenseMatrix()); for (int i = 0; i < cols; ++i) { m.col(i).setConstant(static_cast(i + 1)); dm1.col(i).setConstant(static_cast(i + 1)); } Index d = (std::min)(rows, cols); Index a = std::max(0, cols - d - supers); Index b = std::max(0, rows - d - subs); if (a > 0) dm1.block(0, d + supers, rows, a).setZero(); dm1.block(0, supers + 1, cols - supers - 1 - a, cols - supers - 1 - a).template triangularView().setZero(); dm1.block(subs + 1, 0, rows - subs - 1 - b, rows - subs - 1 - b).template triangularView().setZero(); if (b > 0) dm1.block(d + subs, 0, b, cols).setZero(); // std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n"; VERIFY_IS_APPROX(dm1, m.toDenseMatrix()); } using Eigen::internal::BandMatrix; EIGEN_DECLARE_TEST(bandmatrix) { for (int i = 0; i < 10 * g_repeat; i++) { Index rows = internal::random(1, 10); Index cols = internal::random(1, 10); Index sups = internal::random(0, cols - 1); Index subs = internal::random(0, rows - 1); CALL_SUBTEST(bandmatrix(BandMatrix(rows, cols, sups, subs))); } }