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::Index Index;
15 typedef typename MatrixType::Scalar Scalar;
16 typedef typename NumTraits<Scalar>::Real RealScalar;
17 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrixType;
18
19 Index rows = _m.rows();
20 Index cols = _m.cols();
21 Index supers = _m.supers();
22 Index subs = _m.subs();
23
24 MatrixType m(rows,cols,supers,subs);
25
26 DenseMatrixType dm1(rows,cols);
27 dm1.setZero();
28
29 m.diagonal().setConstant(123);
30 dm1.diagonal().setConstant(123);
31 for (int i=1; i<=m.supers();++i)
32 {
33 m.diagonal(i).setConstant(static_cast<RealScalar>(i));
34 dm1.diagonal(i).setConstant(static_cast<RealScalar>(i));
35 }
36 for (int i=1; i<=m.subs();++i)
37 {
38 m.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
39 dm1.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
40 }
41 //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n";
42 VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
43
44 for (int i=0; i<cols; ++i)
45 {
46 m.col(i).setConstant(static_cast<RealScalar>(i+1));
47 dm1.col(i).setConstant(static_cast<RealScalar>(i+1));
48 }
49 Index d = (std::min)(rows,cols);
50 Index a = std::max<Index>(0,cols-d-supers);
51 Index b = std::max<Index>(0,rows-d-subs);
52 if(a>0) dm1.block(0,d+supers,rows,a).setZero();
53 dm1.block(0,supers+1,cols-supers-1-a,cols-supers-1-a).template triangularView<Upper>().setZero();
54 dm1.block(subs+1,0,rows-subs-1-b,rows-subs-1-b).template triangularView<Lower>().setZero();
55 if(b>0) dm1.block(d+subs,0,b,cols).setZero();
56 //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n";
57 VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
58
59 }
60
61 using Eigen::internal::BandMatrix;
62
test_bandmatrix()63 void test_bandmatrix()
64 {
65 typedef BandMatrix<float>::Index Index;
66
67 for(int i = 0; i < 10*g_repeat ; i++) {
68 Index rows = internal::random<Index>(1,10);
69 Index cols = internal::random<Index>(1,10);
70 Index sups = internal::random<Index>(0,cols-1);
71 Index subs = internal::random<Index>(0,rows-1);
72 CALL_SUBTEST(bandmatrix(BandMatrix<float>(rows,cols,sups,subs)) );
73 }
74 }
75