blob: e94398a65dad7af79a638c5d50dcb8cbc5793126 [file] [log] [blame]
Gael Guennebaudc70d5422010-01-18 22:54:20 +01001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
Gael Guennebaud28e64b02010-06-24 23:21:58 +02004// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
Gael Guennebaudc70d5422010-01-18 22:54:20 +01005//
Benoit Jacob69124cf2012-07-13 14:42:47 -04006// 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/.
Gael Guennebaudc70d5422010-01-18 22:54:20 +01009
10#include "main.h"
Gael Guennebaudc70d5422010-01-18 22:54:20 +010011
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000012template <typename MatrixType>
13void array_for_matrix(const MatrixType& m) {
Gael Guennebaudc70d5422010-01-18 22:54:20 +010014 typedef typename MatrixType::Scalar Scalar;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010015 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType;
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000016 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010017
Hauke Heibelf1679c72010-06-20 17:37:56 +020018 Index rows = m.rows();
19 Index cols = m.cols();
Gael Guennebaudc70d5422010-01-18 22:54:20 +010020
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000021 MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols);
Gael Guennebaudc70d5422010-01-18 22:54:20 +010022 ColVectorType cv1 = ColVectorType::Random(rows);
23 RowVectorType rv1 = RowVectorType::Random(cols);
Rasmus Munk Larsen3460f352023-02-01 13:49:56 -080024
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000025 Scalar s1 = internal::random<Scalar>(), s2 = internal::random<Scalar>();
Rasmus Munk Larsen3460f352023-02-01 13:49:56 -080026
Antonio Sánchez3ebaab82024-02-05 20:07:15 +000027 // Prevent overflows for integer types.
28 if (Eigen::NumTraits<Scalar>::IsInteger) {
Antonio Sánchezde8013f2024-05-16 18:47:36 +000029 Scalar kMaxVal = Scalar(1000);
Antonio Sánchez3ebaab82024-02-05 20:07:15 +000030 m1.array() = m1.array() - kMaxVal * (m1.array() / kMaxVal);
31 m2.array() = m2.array() - kMaxVal * (m2.array() / kMaxVal);
32 }
33
Gael Guennebaudc70d5422010-01-18 22:54:20 +010034 // scalar addition
35 VERIFY_IS_APPROX(m1.array() + s1, s1 + m1.array());
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000036 VERIFY_IS_APPROX((m1.array() + s1).matrix(), MatrixType::Constant(rows, cols, s1) + m1);
37 VERIFY_IS_APPROX(((m1 * Scalar(2)).array() - s2).matrix(), (m1 + m1) - MatrixType::Constant(rows, cols, s2));
Gael Guennebaudc70d5422010-01-18 22:54:20 +010038 m3 = m1;
39 m3.array() += s2;
40 VERIFY_IS_APPROX(m3, (m1.array() + s2).matrix());
41 m3 = m1;
42 m3.array() -= s1;
43 VERIFY_IS_APPROX(m3, (m1.array() - s1).matrix());
44
45 // reductions
Hauke Heibelb5d82992013-02-28 12:33:34 +010046 VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum().sum() - m1.sum(), m1.squaredNorm());
47 VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum().sum() - m1.sum(), m1.squaredNorm());
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000048 VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum() + m2.colwise().sum() - (m1 + m2).colwise().sum(),
49 (m1 + m2).squaredNorm());
50 VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum() - m2.rowwise().sum() - (m1 - m2).rowwise().sum(),
51 (m1 - m2).squaredNorm());
52 VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar, Scalar>()));
Gael Guennebaudc70d5422010-01-18 22:54:20 +010053
54 // vector-wise ops
55 m3 = m1;
56 VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1);
57 m3 = m1;
58 VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1);
59 m3 = m1;
60 VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1);
61 m3 = m1;
62 VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1);
Gael Guennebaudc0c3be22018-10-09 22:54:54 +020063
Rasmus Munk Larsen3460f352023-02-01 13:49:56 -080064 // empty objects
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000065 VERIFY_IS_EQUAL((m1.template block<0, Dynamic>(0, 0, 0, cols).colwise().sum()), RowVectorType::Zero(cols));
66 VERIFY_IS_EQUAL((m1.template block<Dynamic, 0>(0, 0, rows, 0).rowwise().sum()), ColVectorType::Zero(rows));
67 VERIFY_IS_EQUAL((m1.template block<0, Dynamic>(0, 0, 0, cols).colwise().prod()), RowVectorType::Ones(cols));
68 VERIFY_IS_EQUAL((m1.template block<Dynamic, 0>(0, 0, rows, 0).rowwise().prod()), ColVectorType::Ones(rows));
Rasmus Munk Larsen3460f352023-02-01 13:49:56 -080069
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000070 VERIFY_IS_EQUAL(m1.block(0, 0, 0, cols).colwise().sum(), RowVectorType::Zero(cols));
71 VERIFY_IS_EQUAL(m1.block(0, 0, rows, 0).rowwise().sum(), ColVectorType::Zero(rows));
72 VERIFY_IS_EQUAL(m1.block(0, 0, 0, cols).colwise().prod(), RowVectorType::Ones(cols));
73 VERIFY_IS_EQUAL(m1.block(0, 0, rows, 0).rowwise().prod(), ColVectorType::Ones(rows));
Rasmus Munk Larsen3460f352023-02-01 13:49:56 -080074
Hauke Heibeld5e81d82011-01-27 16:37:06 +010075 // verify the const accessors exist
76 const Scalar& ref_m1 = m.matrix().array().coeffRef(0);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000077 const Scalar& ref_m2 = m.matrix().array().coeffRef(0, 0);
Hauke Heibeld5e81d82011-01-27 16:37:06 +010078 const Scalar& ref_a1 = m.array().matrix().coeffRef(0);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000079 const Scalar& ref_a2 = m.array().matrix().coeffRef(0, 0);
Hauke Heibeld5e81d82011-01-27 16:37:06 +010080 VERIFY(&ref_a1 == &ref_m1);
81 VERIFY(&ref_a2 == &ref_m2);
Gael Guennebaudc1d900a2016-01-28 21:43:20 +010082
83 // Check write accessors:
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000084 m1.array().coeffRef(0, 0) = 1;
85 VERIFY_IS_APPROX(m1(0, 0), Scalar(1));
86 m1.array()(0, 0) = 2;
87 VERIFY_IS_APPROX(m1(0, 0), Scalar(2));
88 m1.array().matrix().coeffRef(0, 0) = 3;
89 VERIFY_IS_APPROX(m1(0, 0), Scalar(3));
90 m1.array().matrix()(0, 0) = 4;
91 VERIFY_IS_APPROX(m1(0, 0), Scalar(4));
Gael Guennebaudc70d5422010-01-18 22:54:20 +010092}
93
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000094template <typename MatrixType>
95void comparisons(const MatrixType& m) {
Gael Guennebauda76fbbf2012-11-06 15:25:50 +010096 using std::abs;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010097 typedef typename MatrixType::Scalar Scalar;
98 typedef typename NumTraits<Scalar>::Real RealScalar;
Gael Guennebaudc70d5422010-01-18 22:54:20 +010099
Hauke Heibelf1679c72010-06-20 17:37:56 +0200100 Index rows = m.rows();
101 Index cols = m.cols();
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100102
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000103 Index r = internal::random<Index>(0, rows - 1), c = internal::random<Index>(0, cols - 1);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100104
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000105 MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100106
107 VERIFY(((m1.array() + Scalar(1)) > m1.array()).all());
108 VERIFY(((m1.array() - Scalar(1)) < m1.array()).all());
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000109 if (rows * cols > 1) {
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100110 m3 = m1;
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000111 m3(r, c) += 1;
112 VERIFY(!(m1.array() < m3.array()).all());
113 VERIFY(!(m1.array() > m3.array()).all());
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100114 }
115
116 // comparisons to scalar
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000117 VERIFY((m1.array() != (m1(r, c) + 1)).any());
118 VERIFY((m1.array() > (m1(r, c) - 1)).any());
119 VERIFY((m1.array() < (m1(r, c) + 1)).any());
120 VERIFY((m1.array() == m1(r, c)).any());
121 VERIFY(m1.cwiseEqual(m1(r, c)).any());
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100122
123 // test Select
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000124 VERIFY_IS_APPROX((m1.array() < m2.array()).select(m1, m2), m1.cwiseMin(m2));
125 VERIFY_IS_APPROX((m1.array() > m2.array()).select(m1, m2), m1.cwiseMax(m2));
Antonio Sánchezde8013f2024-05-16 18:47:36 +0000126 Scalar mid = m1.cwiseAbs().minCoeff() / Scalar(2) + m1.cwiseAbs().maxCoeff() / Scalar(2);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000127 for (int j = 0; j < cols; ++j)
128 for (int i = 0; i < rows; ++i) m3(i, j) = abs(m1(i, j)) < mid ? 0 : m1(i, j);
129 VERIFY_IS_APPROX(
130 (m1.array().abs() < MatrixType::Constant(rows, cols, mid).array()).select(MatrixType::Zero(rows, cols), m1), m3);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100131 // shorter versions:
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000132 VERIFY_IS_APPROX((m1.array().abs() < MatrixType::Constant(rows, cols, mid).array()).select(0, m1), m3);
133 VERIFY_IS_APPROX((m1.array().abs() >= MatrixType::Constant(rows, cols, mid).array()).select(m1, 0), m3);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100134 // even shorter version:
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000135 VERIFY_IS_APPROX((m1.array().abs() < mid).select(0, m1), m3);
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100136
137 // count
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000138 VERIFY(((m1.array().abs() + 1) > RealScalar(0.1)).count() == rows * cols);
Benoit Jacobaaaade42010-05-30 16:00:58 -0400139
Gael Guennebaud2e334f52016-11-14 18:47:02 +0100140 // and/or
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000141 VERIFY(((m1.array() < RealScalar(0)).matrix() && (m1.array() > RealScalar(0)).matrix()).count() == 0);
142 VERIFY(((m1.array() < RealScalar(0)).matrix() || (m1.array() >= RealScalar(0)).matrix()).count() == rows * cols);
Tyler Venessd165c732024-05-20 23:02:42 +0000143 VERIFY(((m1.array() < -mid).matrix() || (m1.array() > mid).matrix()).count() ==
144 (m1.cwiseAbs().array() > mid).count());
Gael Guennebaud2e334f52016-11-14 18:47:02 +0100145
Gael Guennebaud12e1ebb2018-07-12 17:16:40 +0200146 typedef Matrix<Index, Dynamic, 1> VectorOfIndices;
Benoit Jacobaaaade42010-05-30 16:00:58 -0400147
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100148 // TODO allows colwise/rowwise for array
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000149 VERIFY_IS_APPROX(((m1.array().abs() + 1) > RealScalar(0.1)).matrix().colwise().count(),
150 VectorOfIndices::Constant(cols, rows).transpose());
151 VERIFY_IS_APPROX(((m1.array().abs() + 1) > RealScalar(0.1)).matrix().rowwise().count(),
152 VectorOfIndices::Constant(rows, cols));
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100153}
154
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000155template <typename VectorType>
156void lpNorm(const VectorType& v) {
Gael Guennebauda76fbbf2012-11-06 15:25:50 +0100157 using std::sqrt;
Gael Guennebaud8b6f5322016-06-02 15:29:59 +0200158 typedef typename VectorType::RealScalar RealScalar;
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100159 VectorType u = VectorType::Random(v.size());
160
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000161 if (v.size() == 0) {
Gael Guennebaud8b6f5322016-06-02 15:29:59 +0200162 VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), RealScalar(0));
163 VERIFY_IS_APPROX(u.template lpNorm<1>(), RealScalar(0));
164 VERIFY_IS_APPROX(u.template lpNorm<2>(), RealScalar(0));
165 VERIFY_IS_APPROX(u.template lpNorm<5>(), RealScalar(0));
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000166 } else {
Gael Guennebaud8b6f5322016-06-02 15:29:59 +0200167 VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwiseAbs().maxCoeff());
168 }
169
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100170 VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwiseAbs().sum());
Gael Guennebauda76fbbf2012-11-06 15:25:50 +0100171 VERIFY_IS_APPROX(u.template lpNorm<2>(), sqrt(u.array().abs().square().sum()));
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000172 VERIFY_IS_APPROX(numext::pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)),
173 u.array().abs().pow(5).sum());
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100174}
175
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000176template <typename MatrixType>
177void cwise_min_max(const MatrixType& m) {
Abraham Bachrach039408c2012-01-11 11:00:30 -0500178 typedef typename MatrixType::Scalar Scalar;
179
180 Index rows = m.rows();
181 Index cols = m.cols();
182
183 MatrixType m1 = MatrixType::Random(rows, cols);
184
185 // min/max with array
186 Scalar maxM1 = m1.maxCoeff();
187 Scalar minM1 = m1.minCoeff();
188
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000189 VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, minM1), m1.cwiseMin(MatrixType::Constant(rows, cols, minM1)));
190 VERIFY_IS_APPROX(m1, m1.cwiseMin(MatrixType::Constant(rows, cols, maxM1)));
Abraham Bachrach039408c2012-01-11 11:00:30 -0500191
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000192 VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, maxM1), m1.cwiseMax(MatrixType::Constant(rows, cols, maxM1)));
193 VERIFY_IS_APPROX(m1, m1.cwiseMax(MatrixType::Constant(rows, cols, minM1)));
Abraham Bachrach039408c2012-01-11 11:00:30 -0500194
195 // min/max with scalar input
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000196 VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, minM1), m1.cwiseMin(minM1));
Gael Guennebaud2c0303c2013-10-15 23:51:01 +0200197 VERIFY_IS_APPROX(m1, m1.cwiseMin(maxM1));
198 VERIFY_IS_APPROX(-m1, (-m1).cwiseMin(-minM1));
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000199 VERIFY_IS_APPROX(-m1.array(), ((-m1).array().min)(-minM1));
Abraham Bachrach039408c2012-01-11 11:00:30 -0500200
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000201 VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, maxM1), m1.cwiseMax(maxM1));
Gael Guennebaud2c0303c2013-10-15 23:51:01 +0200202 VERIFY_IS_APPROX(m1, m1.cwiseMax(minM1));
203 VERIFY_IS_APPROX(-m1, (-m1).cwiseMax(-maxM1));
204 VERIFY_IS_APPROX(-m1.array(), ((-m1).array().max)(-maxM1));
Abraham Bachrach039408c2012-01-11 11:00:30 -0500205
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000206 VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, minM1).array(), (m1.array().min)(minM1));
207 VERIFY_IS_APPROX(m1.array(), (m1.array().min)(maxM1));
Gael Guennebaud0c584dc2012-09-12 17:50:07 +0200208
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000209 VERIFY_IS_APPROX(MatrixType::Constant(rows, cols, maxM1).array(), (m1.array().max)(maxM1));
210 VERIFY_IS_APPROX(m1.array(), (m1.array().max)(minM1));
Gael Guennebaud0c584dc2012-09-12 17:50:07 +0200211
Rasmus Munk Larsen2d3fec82021-10-21 12:00:50 -0700212 // Test NaN propagation for min/max.
213 if (!NumTraits<Scalar>::IsInteger) {
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000214 m1(0, 0) = NumTraits<Scalar>::quiet_NaN();
Rasmus Munk Larsen2d3fec82021-10-21 12:00:50 -0700215 // Elementwise.
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000216 VERIFY((numext::isnan)(m1.template cwiseMax<PropagateNaN>(MatrixType::Constant(rows, cols, Scalar(1)))(0, 0)));
217 VERIFY((numext::isnan)(m1.template cwiseMin<PropagateNaN>(MatrixType::Constant(rows, cols, Scalar(1)))(0, 0)));
218 VERIFY(!(numext::isnan)(m1.template cwiseMax<PropagateNumbers>(MatrixType::Constant(rows, cols, Scalar(1)))(0, 0)));
219 VERIFY(!(numext::isnan)(m1.template cwiseMin<PropagateNumbers>(MatrixType::Constant(rows, cols, Scalar(1)))(0, 0)));
220 VERIFY((numext::isnan)(m1.template cwiseMax<PropagateNaN>(Scalar(1))(0, 0)));
221 VERIFY((numext::isnan)(m1.template cwiseMin<PropagateNaN>(Scalar(1))(0, 0)));
222 VERIFY(!(numext::isnan)(m1.template cwiseMax<PropagateNumbers>(Scalar(1))(0, 0)));
223 VERIFY(!(numext::isnan)(m1.template cwiseMin<PropagateNumbers>(Scalar(1))(0, 0)));
Antonio Sánchezf845a8b2022-04-16 05:07:44 +0000224
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000225 VERIFY((numext::isnan)(
226 m1.array().template max<PropagateNaN>(MatrixType::Constant(rows, cols, Scalar(1)).array())(0, 0)));
227 VERIFY((numext::isnan)(
228 m1.array().template min<PropagateNaN>(MatrixType::Constant(rows, cols, Scalar(1)).array())(0, 0)));
229 VERIFY(!(numext::isnan)(
230 m1.array().template max<PropagateNumbers>(MatrixType::Constant(rows, cols, Scalar(1)).array())(0, 0)));
231 VERIFY(!(numext::isnan)(
232 m1.array().template min<PropagateNumbers>(MatrixType::Constant(rows, cols, Scalar(1)).array())(0, 0)));
233 VERIFY((numext::isnan)(m1.array().template max<PropagateNaN>(Scalar(1))(0, 0)));
234 VERIFY((numext::isnan)(m1.array().template min<PropagateNaN>(Scalar(1))(0, 0)));
235 VERIFY(!(numext::isnan)(m1.array().template max<PropagateNumbers>(Scalar(1))(0, 0)));
236 VERIFY(!(numext::isnan)(m1.array().template min<PropagateNumbers>(Scalar(1))(0, 0)));
Rasmus Munk Larsen2d3fec82021-10-21 12:00:50 -0700237
238 // Reductions.
239 VERIFY((numext::isnan)(m1.template maxCoeff<PropagateNaN>()));
240 VERIFY((numext::isnan)(m1.template minCoeff<PropagateNaN>()));
241 if (m1.size() > 1) {
242 VERIFY(!(numext::isnan)(m1.template maxCoeff<PropagateNumbers>()));
243 VERIFY(!(numext::isnan)(m1.template minCoeff<PropagateNumbers>()));
244 } else {
245 VERIFY((numext::isnan)(m1.template maxCoeff<PropagateNumbers>()));
246 VERIFY((numext::isnan)(m1.template minCoeff<PropagateNumbers>()));
247 }
248 }
Abraham Bachrach039408c2012-01-11 11:00:30 -0500249}
250
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000251template <typename MatrixTraits>
252void resize(const MatrixTraits& t) {
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200253 typedef typename MatrixTraits::Scalar Scalar;
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000254 typedef Matrix<Scalar, Dynamic, Dynamic> MatrixType;
255 typedef Array<Scalar, Dynamic, Dynamic> Array2DType;
256 typedef Matrix<Scalar, Dynamic, 1> VectorType;
257 typedef Array<Scalar, Dynamic, 1> Array1DType;
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200258
259 Index rows = t.rows(), cols = t.cols();
260
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000261 MatrixType m(rows, cols);
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200262 VectorType v(rows);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000263 Array2DType a2(rows, cols);
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200264 Array1DType a1(rows);
265
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000266 m.array().resize(rows + 1, cols + 1);
267 VERIFY(m.rows() == rows + 1 && m.cols() == cols + 1);
268 a2.matrix().resize(rows + 1, cols + 1);
269 VERIFY(a2.rows() == rows + 1 && a2.cols() == cols + 1);
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200270 v.array().resize(cols);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000271 VERIFY(v.size() == cols);
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200272 a1.matrix().resize(cols);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000273 VERIFY(a1.size() == cols);
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200274}
275
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000276template <int>
277void regression_bug_654() {
Gael Guennebaud07417bd2013-09-07 00:01:04 +0200278 ArrayXf a = RowVectorXf(3);
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000279 VectorXf v = Array<float, 1, Dynamic>(3);
Gael Guennebaud07417bd2013-09-07 00:01:04 +0200280}
281
Gael Guennebaudfb1ee042017-06-09 13:13:03 +0200282// Check propagation of LvalueBit through Array/Matrix-Wrapper
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000283template <int>
284void regrrssion_bug_1410() {
Gael Guennebaudfb1ee042017-06-09 13:13:03 +0200285 const Matrix4i M;
286 const Array4i A;
287 ArrayWrapper<const Matrix4i> MA = M.array();
288 MA.row(0);
289 MatrixWrapper<const Array4i> AM = A.matrix();
290 AM.row(0);
291
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000292 VERIFY((internal::traits<ArrayWrapper<const Matrix4i> >::Flags & LvalueBit) == 0);
293 VERIFY((internal::traits<MatrixWrapper<const Array4i> >::Flags & LvalueBit) == 0);
Gael Guennebaudfb1ee042017-06-09 13:13:03 +0200294
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000295 VERIFY((internal::traits<ArrayWrapper<Matrix4i> >::Flags & LvalueBit) == LvalueBit);
296 VERIFY((internal::traits<MatrixWrapper<Array4i> >::Flags & LvalueBit) == LvalueBit);
Gael Guennebaudfb1ee042017-06-09 13:13:03 +0200297}
298
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000299EIGEN_DECLARE_TEST(array_for_matrix) {
300 for (int i = 0; i < g_repeat; i++) {
301 CALL_SUBTEST_1(array_for_matrix(Matrix<float, 1, 1>()));
302 CALL_SUBTEST_2(array_for_matrix(Matrix2f()));
303 CALL_SUBTEST_3(array_for_matrix(Matrix4d()));
304 CALL_SUBTEST_4(array_for_matrix(
305 MatrixXcf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
306 CALL_SUBTEST_5(array_for_matrix(
307 MatrixXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
308 CALL_SUBTEST_6(array_for_matrix(
309 MatrixXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100310 }
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000311 for (int i = 0; i < g_repeat; i++) {
312 CALL_SUBTEST_1(comparisons(Matrix<float, 1, 1>()));
313 CALL_SUBTEST_2(comparisons(Matrix2f()));
314 CALL_SUBTEST_3(comparisons(Matrix4d()));
315 CALL_SUBTEST_5(comparisons(
316 MatrixXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
317 CALL_SUBTEST_6(comparisons(
318 MatrixXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100319 }
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000320 for (int i = 0; i < g_repeat; i++) {
321 CALL_SUBTEST_1(cwise_min_max(Matrix<float, 1, 1>()));
322 CALL_SUBTEST_2(cwise_min_max(Matrix2f()));
323 CALL_SUBTEST_3(cwise_min_max(Matrix4d()));
324 CALL_SUBTEST_5(cwise_min_max(
325 MatrixXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
326 CALL_SUBTEST_6(cwise_min_max(
327 MatrixXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
Abraham Bachrach039408c2012-01-11 11:00:30 -0500328 }
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000329 for (int i = 0; i < g_repeat; i++) {
330 CALL_SUBTEST_1(lpNorm(Matrix<float, 1, 1>()));
331 CALL_SUBTEST_2(lpNorm(Vector2f()));
332 CALL_SUBTEST_7(lpNorm(Vector3d()));
333 CALL_SUBTEST_8(lpNorm(Vector4f()));
334 CALL_SUBTEST_5(lpNorm(VectorXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
335 CALL_SUBTEST_4(lpNorm(VectorXcf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100336 }
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000337 CALL_SUBTEST_5(lpNorm(VectorXf(0)));
338 CALL_SUBTEST_4(lpNorm(VectorXcf(0)));
339 for (int i = 0; i < g_repeat; i++) {
340 CALL_SUBTEST_4(resize(
341 MatrixXcf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
342 CALL_SUBTEST_5(
343 resize(MatrixXf(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
344 CALL_SUBTEST_6(
345 resize(MatrixXi(internal::random<int>(1, EIGEN_TEST_MAX_SIZE), internal::random<int>(1, EIGEN_TEST_MAX_SIZE))));
Gael Guennebaud9da41cc2012-08-30 16:28:53 +0200346 }
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000347 CALL_SUBTEST_6(regression_bug_654<0>());
348 CALL_SUBTEST_6(regrrssion_bug_1410<0>());
Gael Guennebaudc70d5422010-01-18 22:54:20 +0100349}