blob: 498421b4cd50a55a22de050a96723e47c23190e0 [file] [log] [blame]
Narayan Kamathc981c482012-11-02 10:59:05 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
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
12#include <Eigen/Core>
13
14using namespace Eigen;
15
16template <typename Scalar, int Storage>
17void run_matrix_tests()
18{
19 typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Storage> MatrixType;
20 typedef typename MatrixType::Index Index;
21
22 MatrixType m, n;
23
24 // boundary cases ...
25 m = n = MatrixType::Random(50,50);
26 m.conservativeResize(1,50);
27 VERIFY_IS_APPROX(m, n.block(0,0,1,50));
28
29 m = n = MatrixType::Random(50,50);
30 m.conservativeResize(50,1);
31 VERIFY_IS_APPROX(m, n.block(0,0,50,1));
32
33 m = n = MatrixType::Random(50,50);
34 m.conservativeResize(50,50);
35 VERIFY_IS_APPROX(m, n.block(0,0,50,50));
36
37 // random shrinking ...
38 for (int i=0; i<25; ++i)
39 {
40 const Index rows = internal::random<Index>(1,50);
41 const Index cols = internal::random<Index>(1,50);
42 m = n = MatrixType::Random(50,50);
43 m.conservativeResize(rows,cols);
44 VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
45 }
46
47 // random growing with zeroing ...
48 for (int i=0; i<25; ++i)
49 {
50 const Index rows = internal::random<Index>(50,75);
51 const Index cols = internal::random<Index>(50,75);
52 m = n = MatrixType::Random(50,50);
53 m.conservativeResizeLike(MatrixType::Zero(rows,cols));
54 VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
55 VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
56 VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
57 }
58}
59
60template <typename Scalar>
61void run_vector_tests()
62{
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070063 typedef Matrix<Scalar, 1, Eigen::Dynamic> VectorType;
Narayan Kamathc981c482012-11-02 10:59:05 +000064
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070065 VectorType m, n;
Narayan Kamathc981c482012-11-02 10:59:05 +000066
67 // boundary cases ...
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070068 m = n = VectorType::Random(50);
Narayan Kamathc981c482012-11-02 10:59:05 +000069 m.conservativeResize(1);
70 VERIFY_IS_APPROX(m, n.segment(0,1));
71
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070072 m = n = VectorType::Random(50);
Narayan Kamathc981c482012-11-02 10:59:05 +000073 m.conservativeResize(50);
74 VERIFY_IS_APPROX(m, n.segment(0,50));
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070075
76 m = n = VectorType::Random(50);
77 m.conservativeResize(m.rows(),1);
78 VERIFY_IS_APPROX(m, n.segment(0,1));
79
80 m = n = VectorType::Random(50);
81 m.conservativeResize(m.rows(),50);
82 VERIFY_IS_APPROX(m, n.segment(0,50));
Narayan Kamathc981c482012-11-02 10:59:05 +000083
84 // random shrinking ...
85 for (int i=0; i<50; ++i)
86 {
87 const int size = internal::random<int>(1,50);
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070088 m = n = VectorType::Random(50);
Narayan Kamathc981c482012-11-02 10:59:05 +000089 m.conservativeResize(size);
90 VERIFY_IS_APPROX(m, n.segment(0,size));
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070091
92 m = n = VectorType::Random(50);
93 m.conservativeResize(m.rows(), size);
94 VERIFY_IS_APPROX(m, n.segment(0,size));
Narayan Kamathc981c482012-11-02 10:59:05 +000095 }
96
97 // random growing with zeroing ...
98 for (int i=0; i<50; ++i)
99 {
100 const int size = internal::random<int>(50,100);
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -0700101 m = n = VectorType::Random(50);
102 m.conservativeResizeLike(VectorType::Zero(size));
103 VERIFY_IS_APPROX(m.segment(0,50), n);
104 VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
105
106 m = n = VectorType::Random(50);
107 m.conservativeResizeLike(Matrix<Scalar,Dynamic,Dynamic>::Zero(1,size));
Narayan Kamathc981c482012-11-02 10:59:05 +0000108 VERIFY_IS_APPROX(m.segment(0,50), n);
109 VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
110 }
111}
112
113void test_conservative_resize()
114{
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -0700115 for(int i=0; i<g_repeat; ++i)
116 {
117 CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
118 CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
119 CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
120 CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
121 CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
122 CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
123 CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
124 CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
125 CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
126 CALL_SUBTEST_6((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
Narayan Kamathc981c482012-11-02 10:59:05 +0000127
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -0700128 CALL_SUBTEST_1((run_vector_tests<int>()));
129 CALL_SUBTEST_2((run_vector_tests<float>()));
130 CALL_SUBTEST_3((run_vector_tests<double>()));
131 CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
132 CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
133 }
Narayan Kamathc981c482012-11-02 10:59:05 +0000134}