1 // This file is triangularView of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10 #include "main.h"
11
bandmatrix(const MatrixType & _m)12 template<typename MatrixType> void bandmatrix(const MatrixType& _m)
13 {
14 typedef typename MatrixType::Scalar Scalar;
15 typedef typename NumTraits<Scalar>::Real RealScalar;
16 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrixType;
17
18 Index rows = _m.rows();
19 Index cols = _m.cols();
20 Index supers = _m.supers();
21 Index subs = _m.subs();
22
23 MatrixType m(rows,cols,supers,subs);
24
25 DenseMatrixType dm1(rows,cols);
26 dm1.setZero();
27
28 m.diagonal().setConstant(123);
29 dm1.diagonal().setConstant(123);
30 for (int i=1; i<=m.supers();++i)
31 {
32 m.diagonal(i).setConstant(static_cast<RealScalar>(i));
33 dm1.diagonal(i).setConstant(static_cast<RealScalar>(i));
34 }
35 for (int i=1; i<=m.subs();++i)
36 {
37 m.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
38 dm1.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
39 }
40 //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n";
41 VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
42
43 for (int i=0; i<cols; ++i)
44 {
45 m.col(i).setConstant(static_cast<RealScalar>(i+1));
46 dm1.col(i).setConstant(static_cast<RealScalar>(i+1));
47 }
48 Index d = (std::min)(rows,cols);
49 Index a = std::max<Index>(0,cols-d-supers);
50 Index b = std::max<Index>(0,rows-d-subs);
51 if(a>0) dm1.block(0,d+supers,rows,a).setZero();
52 dm1.block(0,supers+1,cols-supers-1-a,cols-supers-1-a).template triangularView<Upper>().setZero();
53 dm1.block(subs+1,0,rows-subs-1-b,rows-subs-1-b).template triangularView<Lower>().setZero();
54 if(b>0) dm1.block(d+subs,0,b,cols).setZero();
55 //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n";
56 VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
57
58 }
59
60 using Eigen::internal::BandMatrix;
61
test_bandmatrix()62 void test_bandmatrix()
63 {
64 for(int i = 0; i < 10*g_repeat ; i++) {
65 Index rows = internal::random<Index>(1,10);
66 Index cols = internal::random<Index>(1,10);
67 Index sups = internal::random<Index>(0,cols-1);
68 Index subs = internal::random<Index>(0,rows-1);
69 CALL_SUBTEST(bandmatrix(BandMatrix<float>(rows,cols,sups,subs)) );
70 }
71 }
72