Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 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/. |
Gael Guennebaud | 19bc418 | 2013-02-25 01:17:44 +0100 | [diff] [blame] | 9 | |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 10 | #include "main.h" |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 11 | |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 12 | |
| 13 | // Test the corner cases of pow(x, y) for real types. |
| 14 | template<typename Scalar> |
| 15 | void pow_test() { |
| 16 | const Scalar zero = Scalar(0); |
Antonio Sanchez | 8dfe102 | 2021-03-16 20:12:46 -0700 | [diff] [blame] | 17 | const Scalar eps = Eigen::NumTraits<Scalar>::epsilon(); |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 18 | const Scalar one = Scalar(1); |
Rasmus Munk Larsen | 6e3b795 | 2021-02-05 16:58:49 -0800 | [diff] [blame] | 19 | const Scalar two = Scalar(2); |
| 20 | const Scalar three = Scalar(3); |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 21 | const Scalar sqrt_half = Scalar(std::sqrt(0.5)); |
| 22 | const Scalar sqrt2 = Scalar(std::sqrt(2)); |
Antonio Sanchez | 8dfe102 | 2021-03-16 20:12:46 -0700 | [diff] [blame] | 23 | const Scalar inf = Eigen::NumTraits<Scalar>::infinity(); |
| 24 | const Scalar nan = Eigen::NumTraits<Scalar>::quiet_NaN(); |
Rasmus Munk Larsen | be0574e | 2021-02-17 02:50:32 +0000 | [diff] [blame] | 25 | const Scalar denorm_min = std::numeric_limits<Scalar>::denorm_min(); |
Rasmus Munk Larsen | 6e3b795 | 2021-02-05 16:58:49 -0800 | [diff] [blame] | 26 | const Scalar min = (std::numeric_limits<Scalar>::min)(); |
| 27 | const Scalar max = (std::numeric_limits<Scalar>::max)(); |
Antonio Sanchez | 8dfe102 | 2021-03-16 20:12:46 -0700 | [diff] [blame] | 28 | const Scalar max_exp = (static_cast<Scalar>(int(Eigen::NumTraits<Scalar>::max_exponent())) * Scalar(EIGEN_LN2)) / eps; |
Rasmus Munk Larsen | be0574e | 2021-02-17 02:50:32 +0000 | [diff] [blame] | 29 | |
Rasmus Munk Larsen | 6e3b795 | 2021-02-05 16:58:49 -0800 | [diff] [blame] | 30 | const static Scalar abs_vals[] = {zero, |
Rasmus Munk Larsen | be0574e | 2021-02-17 02:50:32 +0000 | [diff] [blame] | 31 | denorm_min, |
| 32 | min, |
| 33 | eps, |
Rasmus Munk Larsen | 6e3b795 | 2021-02-05 16:58:49 -0800 | [diff] [blame] | 34 | sqrt_half, |
| 35 | one, |
| 36 | sqrt2, |
| 37 | two, |
| 38 | three, |
Rasmus Munk Larsen | be0574e | 2021-02-17 02:50:32 +0000 | [diff] [blame] | 39 | max_exp, |
Rasmus Munk Larsen | 6e3b795 | 2021-02-05 16:58:49 -0800 | [diff] [blame] | 40 | max, |
| 41 | inf, |
| 42 | nan}; |
Rasmus Munk Larsen | be0574e | 2021-02-17 02:50:32 +0000 | [diff] [blame] | 43 | const int abs_cases = 13; |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 44 | const int num_cases = 2*abs_cases * 2*abs_cases; |
| 45 | // Repeat the same value to make sure we hit the vectorized path. |
| 46 | const int num_repeats = 32; |
| 47 | Array<Scalar, Dynamic, Dynamic> x(num_repeats, num_cases); |
| 48 | Array<Scalar, Dynamic, Dynamic> y(num_repeats, num_cases); |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 49 | int count = 0; |
| 50 | for (int i = 0; i < abs_cases; ++i) { |
| 51 | const Scalar abs_x = abs_vals[i]; |
| 52 | for (int sign_x = 0; sign_x < 2; ++sign_x) { |
| 53 | Scalar x_case = sign_x == 0 ? -abs_x : abs_x; |
| 54 | for (int j = 0; j < abs_cases; ++j) { |
| 55 | const Scalar abs_y = abs_vals[j]; |
| 56 | for (int sign_y = 0; sign_y < 2; ++sign_y) { |
| 57 | Scalar y_case = sign_y == 0 ? -abs_y : abs_y; |
| 58 | for (int repeat = 0; repeat < num_repeats; ++repeat) { |
| 59 | x(repeat, count) = x_case; |
| 60 | y(repeat, count) = y_case; |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 61 | } |
| 62 | ++count; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | Array<Scalar, Dynamic, Dynamic> actual = x.pow(y); |
| 69 | const Scalar tol = test_precision<Scalar>(); |
| 70 | bool all_pass = true; |
| 71 | for (int i = 0; i < 1; ++i) { |
| 72 | for (int j = 0; j < num_cases; ++j) { |
Rasmus Munk Larsen | be0574e | 2021-02-17 02:50:32 +0000 | [diff] [blame] | 73 | Scalar e = static_cast<Scalar>(std::pow(x(i,j), y(i,j))); |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 74 | Scalar a = actual(i, j); |
Rasmus Munk Larsen | 7a87ed1 | 2022-08-08 18:48:36 +0000 | [diff] [blame] | 75 | bool success = (a==e) || ((numext::isfinite)(e) && internal::isApprox(a, e, tol)) || ((numext::isnan)(a) && (numext::isnan)(e)); |
| 76 | all_pass &= success; |
| 77 | if (!success) { |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 78 | std::cout << "pow(" << x(i,j) << "," << y(i,j) << ") = " << a << " != " << e << std::endl; |
| 79 | } |
| 80 | } |
| 81 | } |
Charles Schlosser | 76a669f | 2022-08-16 21:32:36 +0000 | [diff] [blame] | 82 | |
| 83 | typedef typename internal::make_integer<Scalar>::type Int_t; |
| 84 | |
| 85 | // ensure both vectorized and non-vectorized paths taken |
| 86 | Index test_size = 2 * internal::packet_traits<Scalar>::size + 1; |
| 87 | |
| 88 | Array<Scalar, Dynamic, 1> eigenPow(test_size); |
| 89 | for (int i = 0; i < num_cases; ++i) { |
| 90 | Array<Scalar, Dynamic, 1> bases = x.col(i); |
| 91 | for (Scalar abs_exponent : abs_vals){ |
| 92 | for (Scalar exponent : {-abs_exponent, abs_exponent}){ |
| 93 | // test floating point exponent code path |
| 94 | eigenPow.setZero(); |
| 95 | eigenPow = bases.pow(exponent); |
| 96 | for (int j = 0; j < num_repeats; j++){ |
| 97 | Scalar e = static_cast<Scalar>(std::pow(bases(j), exponent)); |
| 98 | Scalar a = eigenPow(j); |
| 99 | bool success = (a == e) || ((numext::isfinite)(e) && internal::isApprox(a, e, tol)) || ((numext::isnan)(a) && (numext::isnan)(e)); |
| 100 | all_pass &= success; |
| 101 | if (!success) { |
| 102 | std::cout << "pow(" << x(i, j) << "," << y(i, j) << ") = " << a << " != " << e << std::endl; |
| 103 | } |
| 104 | } |
| 105 | // test integer exponent code path |
| 106 | bool exponent_is_integer = (numext::isfinite)(exponent) && (numext::round(exponent) == exponent) && (numext::abs(exponent) < static_cast<Scalar>(NumTraits<Int_t>::highest())); |
| 107 | if (exponent_is_integer) |
| 108 | { |
| 109 | Int_t exponent_as_int = static_cast<Int_t>(exponent); |
| 110 | eigenPow.setZero(); |
| 111 | eigenPow = bases.pow(exponent_as_int); |
| 112 | for (int j = 0; j < num_repeats; j++){ |
| 113 | Scalar e = static_cast<Scalar>(std::pow(bases(j), exponent)); |
| 114 | Scalar a = eigenPow(j); |
| 115 | bool success = (a == e) || ((numext::isfinite)(e) && internal::isApprox(a, e, tol)) || ((numext::isnan)(a) && (numext::isnan)(e)); |
| 116 | all_pass &= success; |
| 117 | if (!success) { |
| 118 | std::cout << "pow(" << x(i, j) << "," << y(i, j) << ") = " << a << " != " << e << std::endl; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 126 | VERIFY(all_pass); |
| 127 | } |
| 128 | |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 129 | template <typename Scalar, typename ScalarExponent> |
| 130 | Scalar calc_overflow_threshold(const ScalarExponent exponent) { |
| 131 | EIGEN_USING_STD(exp2); |
Antonio Sánchez | 69f50e3 | 2022-09-06 19:53:29 +0000 | [diff] [blame] | 132 | EIGEN_USING_STD(log2); |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 133 | EIGEN_STATIC_ASSERT((NumTraits<Scalar>::digits() < 2 * NumTraits<double>::digits()), BASE_TYPE_IS_TOO_BIG); |
| 134 | |
| 135 | if (exponent < 2) |
| 136 | return NumTraits<Scalar>::highest(); |
| 137 | else { |
Antonio Sánchez | 69f50e3 | 2022-09-06 19:53:29 +0000 | [diff] [blame] | 138 | // base^e <= highest ==> base <= 2^(log2(highest)/e) |
Antonio Sánchez | 3c37dd2 | 2022-09-08 17:40:45 +0000 | [diff] [blame] | 139 | // For floating-point types, consider the bound for integer values that can be reproduced exactly = 2 ^ digits |
| 140 | double highest_bits = numext::mini(static_cast<double>(NumTraits<Scalar>::digits()), |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 141 | static_cast<double>(log2(NumTraits<Scalar>::highest()))); |
Antonio Sánchez | 69f50e3 | 2022-09-06 19:53:29 +0000 | [diff] [blame] | 142 | return static_cast<Scalar>( |
Antonio Sánchez | 3c37dd2 | 2022-09-08 17:40:45 +0000 | [diff] [blame] | 143 | numext::floor(exp2(highest_bits / static_cast<double>(exponent)))); |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Rasmus Munk Larsen | dceb779 | 2022-09-12 15:51:27 -0700 | [diff] [blame] | 147 | template <typename Base, typename Exponent, bool ExpIsInteger = NumTraits<Exponent>::IsInteger> |
| 148 | struct ref_pow { |
| 149 | static Base run(Base base, Exponent exponent) { |
| 150 | EIGEN_USING_STD(pow); |
| 151 | return pow(base, static_cast<Base>(exponent)); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | template <typename Base, typename Exponent> |
| 156 | struct ref_pow<Base, Exponent, true> { |
| 157 | static Base run(Base base, Exponent exponent) { |
| 158 | EIGEN_USING_STD(pow); |
| 159 | return pow(base, exponent); |
| 160 | } |
| 161 | }; |
| 162 | |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 163 | template <typename Base, typename Exponent> |
| 164 | void test_exponent(Exponent exponent) { |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 165 | const Base max_abs_bases = static_cast<Base>(10000); |
| 166 | // avoid integer overflow in Base type |
| 167 | Base threshold = calc_overflow_threshold<Base, Exponent>(numext::abs(exponent)); |
| 168 | // avoid numbers that can't be verified with std::pow |
| 169 | double double_threshold = calc_overflow_threshold<double, Exponent>(numext::abs(exponent)); |
| 170 | // use the lesser of these two thresholds |
| 171 | Base testing_threshold = |
| 172 | static_cast<double>(threshold) < double_threshold ? threshold : static_cast<Base>(double_threshold); |
| 173 | // test both vectorized and non-vectorized code paths |
| 174 | const Index array_size = 2 * internal::packet_traits<Base>::size + 1; |
| 175 | |
| 176 | Base max_base = numext::mini(testing_threshold, max_abs_bases); |
| 177 | Base min_base = NumTraits<Base>::IsSigned ? -max_base : Base(0); |
| 178 | |
| 179 | ArrayX<Base> x(array_size), y(array_size); |
| 180 | bool all_pass = true; |
| 181 | for (Base base = min_base; base <= max_base; base++) { |
| 182 | if (exponent < 0 && base == 0) continue; |
| 183 | x.setConstant(base); |
| 184 | y = x.pow(exponent); |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 185 | for (Base a : y) { |
Rasmus Munk Larsen | dceb779 | 2022-09-12 15:51:27 -0700 | [diff] [blame] | 186 | Base e = ref_pow<Base, Exponent>::run(base, exponent); |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 187 | bool pass = (a == e); |
| 188 | if (!NumTraits<Base>::IsInteger) { |
| 189 | pass = pass || (((numext::isfinite)(e) && internal::isApprox(a, e)) || |
| 190 | ((numext::isnan)(a) && (numext::isnan)(e))); |
| 191 | } |
| 192 | all_pass &= pass; |
| 193 | if (!pass) { |
| 194 | std::cout << "pow(" << base << "," << exponent << ") = " << a << " != " << e << std::endl; |
| 195 | } |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 196 | } |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 197 | } |
| 198 | VERIFY(all_pass); |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 199 | } |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 200 | |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 201 | template <typename Base, typename Exponent> |
| 202 | void unary_pow_test() { |
| 203 | Exponent max_exponent = static_cast<Exponent>(NumTraits<Base>::digits()); |
| 204 | Exponent min_exponent = static_cast<Exponent>(NumTraits<Exponent>::IsSigned ? -max_exponent : 0); |
| 205 | |
| 206 | for (Exponent exponent = min_exponent; exponent < max_exponent; ++exponent) { |
| 207 | test_exponent<Base, Exponent>(exponent); |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | void mixed_pow_test() { |
| 212 | // The following cases will test promoting a smaller exponent type |
| 213 | // to a wider base type. |
| 214 | unary_pow_test<double, int>(); |
| 215 | unary_pow_test<double, float>(); |
| 216 | unary_pow_test<float, half>(); |
| 217 | unary_pow_test<double, half>(); |
| 218 | unary_pow_test<float, bfloat16>(); |
| 219 | unary_pow_test<double, bfloat16>(); |
| 220 | |
| 221 | // Although in the following cases the exponent cannot be represented exactly |
| 222 | // in the base type, we do not perform a conversion, but implement |
| 223 | // the operation using repeated squaring. |
| 224 | unary_pow_test<float, int>(); |
| 225 | unary_pow_test<double, long long>(); |
| 226 | |
| 227 | // The following cases will test promoting a wider exponent type |
| 228 | // to a narrower base type. This should compile but generate a |
| 229 | // deprecation warning: |
| 230 | unary_pow_test<float, double>(); |
| 231 | } |
| 232 | |
| 233 | void int_pow_test() { |
| 234 | unary_pow_test<int, int>(); |
| 235 | unary_pow_test<unsigned int, unsigned int>(); |
| 236 | unary_pow_test<long long, long long>(); |
| 237 | unary_pow_test<unsigned long long, unsigned long long>(); |
| 238 | |
| 239 | // Although in the following cases the exponent cannot be represented exactly |
| 240 | // in the base type, we do not perform a conversion, but implement the |
| 241 | // operation using repeated squaring. |
| 242 | unary_pow_test<long long, int>(); |
| 243 | unary_pow_test<int, unsigned int>(); |
| 244 | unary_pow_test<unsigned int, int>(); |
| 245 | unary_pow_test<long long, unsigned long long>(); |
| 246 | unary_pow_test<unsigned long long, long long>(); |
| 247 | unary_pow_test<long long, int>(); |
Charles Schlosser | e5af9f8 | 2022-08-29 19:23:54 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 250 | template<typename ArrayType> void array(const ArrayType& m) |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 251 | { |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 252 | typedef typename ArrayType::Scalar Scalar; |
Gael Guennebaud | d476cad | 2016-06-25 10:12:06 +0200 | [diff] [blame] | 253 | typedef typename ArrayType::RealScalar RealScalar; |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 254 | typedef Array<Scalar, ArrayType::RowsAtCompileTime, 1> ColVectorType; |
| 255 | typedef Array<Scalar, 1, ArrayType::ColsAtCompileTime> RowVectorType; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 256 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 257 | Index rows = m.rows(); |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 258 | Index cols = m.cols(); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 259 | |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 260 | ArrayType m1 = ArrayType::Random(rows, cols); |
| 261 | if (NumTraits<RealScalar>::IsInteger && NumTraits<RealScalar>::IsSigned |
| 262 | && !NumTraits<Scalar>::IsComplex) { |
| 263 | // Here we cap the size of the values in m1 such that pow(3)/cube() |
| 264 | // doesn't overflow and result in undefined behavior. Notice that because |
| 265 | // pow(int, int) promotes its inputs and output to double (according to |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 266 | // the C++ standard), we have to make sure that the result fits in 53 bits |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 267 | // for int64, |
| 268 | RealScalar max_val = |
| 269 | numext::mini(RealScalar(std::cbrt(NumTraits<RealScalar>::highest())), |
| 270 | RealScalar(std::cbrt(1LL << 53)))/2; |
| 271 | m1.array() = (m1.abs().array() <= max_val).select(m1, Scalar(max_val)); |
| 272 | } |
| 273 | ArrayType m2 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 274 | m3(rows, cols); |
Christoph Hertzberg | 3be9f5c | 2015-04-16 13:25:20 +0200 | [diff] [blame] | 275 | ArrayType m4 = m1; // copy constructor |
| 276 | VERIFY_IS_APPROX(m1, m4); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 277 | |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 278 | ColVectorType cv1 = ColVectorType::Random(rows); |
| 279 | RowVectorType rv1 = RowVectorType::Random(cols); |
| 280 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 281 | Scalar s1 = internal::random<Scalar>(), |
Hauke Heibel | 8cb3e36 | 2012-03-02 16:27:27 +0100 | [diff] [blame] | 282 | s2 = internal::random<Scalar>(); |
| 283 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 284 | // scalar addition |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 285 | VERIFY_IS_APPROX(m1 + s1, s1 + m1); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 286 | VERIFY_IS_APPROX(m1 + s1, ArrayType::Constant(rows,cols,s1) + m1); |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 287 | VERIFY_IS_APPROX(s1 - m1, (-m1)+s1 ); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 288 | VERIFY_IS_APPROX(m1 - s1, m1 - ArrayType::Constant(rows,cols,s1)); |
| 289 | VERIFY_IS_APPROX(s1 - m1, ArrayType::Constant(rows,cols,s1) - m1); |
| 290 | VERIFY_IS_APPROX((m1*Scalar(2)) - s2, (m1+m1) - ArrayType::Constant(rows,cols,s2) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 291 | m3 = m1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 292 | m3 += s2; |
| 293 | VERIFY_IS_APPROX(m3, m1 + s2); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 294 | m3 = m1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 295 | m3 -= s1; |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 296 | VERIFY_IS_APPROX(m3, m1 - s1); |
| 297 | |
Hauke Heibel | f578dc7 | 2010-12-16 17:34:13 +0100 | [diff] [blame] | 298 | // scalar operators via Maps |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 299 | m3 = m1; m4 = m1; |
| 300 | ArrayType::Map(m4.data(), m4.rows(), m4.cols()) -= ArrayType::Map(m2.data(), m2.rows(), m2.cols()); |
| 301 | VERIFY_IS_APPROX(m4, m3 - m2); |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 302 | |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 303 | m3 = m1; m4 = m1; |
| 304 | ArrayType::Map(m4.data(), m4.rows(), m4.cols()) += ArrayType::Map(m2.data(), m2.rows(), m2.cols()); |
| 305 | VERIFY_IS_APPROX(m4, m3 + m2); |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 306 | |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 307 | m3 = m1; m4 = m1; |
| 308 | ArrayType::Map(m4.data(), m4.rows(), m4.cols()) *= ArrayType::Map(m2.data(), m2.rows(), m2.cols()); |
| 309 | VERIFY_IS_APPROX(m4, m3 * m2); |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 310 | |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 311 | m3 = m1; m4 = m1; |
Hauke Heibel | f578dc7 | 2010-12-16 17:34:13 +0100 | [diff] [blame] | 312 | m2 = ArrayType::Random(rows,cols); |
| 313 | m2 = (m2==0).select(1,m2); |
Rasmus Munk Larsen | 98e51c9 | 2022-08-26 16:19:03 +0000 | [diff] [blame] | 314 | ArrayType::Map(m4.data(), m4.rows(), m4.cols()) /= ArrayType::Map(m2.data(), m2.rows(), m2.cols()); |
| 315 | VERIFY_IS_APPROX(m4, m3 / m2); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 316 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 317 | // reductions |
Gael Guennebaud | 42af587 | 2013-02-23 22:58:14 +0100 | [diff] [blame] | 318 | VERIFY_IS_APPROX(m1.abs().colwise().sum().sum(), m1.abs().sum()); |
| 319 | VERIFY_IS_APPROX(m1.abs().rowwise().sum().sum(), m1.abs().sum()); |
| 320 | using std::abs; |
| 321 | VERIFY_IS_MUCH_SMALLER_THAN(abs(m1.colwise().sum().sum() - m1.sum()), m1.abs().sum()); |
| 322 | VERIFY_IS_MUCH_SMALLER_THAN(abs(m1.rowwise().sum().sum() - m1.sum()), m1.abs().sum()); |
Gael Guennebaud | 28e139a | 2013-02-23 23:06:45 +0100 | [diff] [blame] | 323 | if (!internal::isMuchSmallerThan(abs(m1.sum() - (m1+m2).sum()), m1.abs().sum(), test_precision<Scalar>())) |
Hauke Heibel | 83e3c45 | 2010-12-16 18:53:02 +0100 | [diff] [blame] | 324 | VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum()); |
Gael Guennebaud | 66e99ab | 2016-06-06 15:11:41 +0200 | [diff] [blame] | 325 | VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar,Scalar>())); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 326 | |
| 327 | // vector-wise ops |
| 328 | m3 = m1; |
| 329 | VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1); |
| 330 | m3 = m1; |
| 331 | VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1); |
| 332 | m3 = m1; |
| 333 | VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1); |
| 334 | m3 = m1; |
| 335 | VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1); |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 336 | |
Gael Guennebaud | 0a18eec | 2014-09-19 13:25:28 +0200 | [diff] [blame] | 337 | // Conversion from scalar |
| 338 | VERIFY_IS_APPROX((m3 = s1), ArrayType::Constant(rows,cols,s1)); |
| 339 | VERIFY_IS_APPROX((m3 = 1), ArrayType::Constant(rows,cols,1)); |
| 340 | VERIFY_IS_APPROX((m3.topLeftCorner(rows,cols) = 1), ArrayType::Constant(rows,cols,1)); |
| 341 | typedef Array<Scalar, |
| 342 | ArrayType::RowsAtCompileTime==Dynamic?2:ArrayType::RowsAtCompileTime, |
| 343 | ArrayType::ColsAtCompileTime==Dynamic?2:ArrayType::ColsAtCompileTime, |
| 344 | ArrayType::Options> FixedArrayType; |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 345 | { |
| 346 | FixedArrayType f1(s1); |
| 347 | VERIFY_IS_APPROX(f1, FixedArrayType::Constant(s1)); |
| 348 | FixedArrayType f2(numext::real(s1)); |
| 349 | VERIFY_IS_APPROX(f2, FixedArrayType::Constant(numext::real(s1))); |
| 350 | FixedArrayType f3((int)100*numext::real(s1)); |
| 351 | VERIFY_IS_APPROX(f3, FixedArrayType::Constant((int)100*numext::real(s1))); |
| 352 | f1.setRandom(); |
| 353 | FixedArrayType f4(f1.data()); |
| 354 | VERIFY_IS_APPROX(f4, f1); |
| 355 | } |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 356 | { |
| 357 | FixedArrayType f1{s1}; |
| 358 | VERIFY_IS_APPROX(f1, FixedArrayType::Constant(s1)); |
| 359 | FixedArrayType f2{numext::real(s1)}; |
| 360 | VERIFY_IS_APPROX(f2, FixedArrayType::Constant(numext::real(s1))); |
| 361 | FixedArrayType f3{(int)100*numext::real(s1)}; |
| 362 | VERIFY_IS_APPROX(f3, FixedArrayType::Constant((int)100*numext::real(s1))); |
| 363 | f1.setRandom(); |
| 364 | FixedArrayType f4{f1.data()}; |
| 365 | VERIFY_IS_APPROX(f4, f1); |
| 366 | } |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 367 | |
Gael Guennebaud | d476cad | 2016-06-25 10:12:06 +0200 | [diff] [blame] | 368 | // pow |
| 369 | VERIFY_IS_APPROX(m1.pow(2), m1.square()); |
| 370 | VERIFY_IS_APPROX(pow(m1,2), m1.square()); |
| 371 | VERIFY_IS_APPROX(m1.pow(3), m1.cube()); |
| 372 | VERIFY_IS_APPROX(pow(m1,3), m1.cube()); |
| 373 | VERIFY_IS_APPROX((-m1).pow(3), -m1.cube()); |
| 374 | VERIFY_IS_APPROX(pow(2*m1,3), 8*m1.cube()); |
| 375 | ArrayType exponents = ArrayType::Constant(rows, cols, RealScalar(2)); |
| 376 | VERIFY_IS_APPROX(Eigen::pow(m1,exponents), m1.square()); |
| 377 | VERIFY_IS_APPROX(m1.pow(exponents), m1.square()); |
| 378 | VERIFY_IS_APPROX(Eigen::pow(2*m1,exponents), 4*m1.square()); |
| 379 | VERIFY_IS_APPROX((2*m1).pow(exponents), 4*m1.square()); |
| 380 | VERIFY_IS_APPROX(Eigen::pow(m1,2*exponents), m1.square().square()); |
| 381 | VERIFY_IS_APPROX(m1.pow(2*exponents), m1.square().square()); |
Gael Guennebaud | e61cee7 | 2016-07-04 11:49:03 +0200 | [diff] [blame] | 382 | VERIFY_IS_APPROX(Eigen::pow(m1(0,0), exponents), ArrayType::Constant(rows,cols,m1(0,0)*m1(0,0))); |
Gael Guennebaud | d476cad | 2016-06-25 10:12:06 +0200 | [diff] [blame] | 383 | |
Gael Guennebaud | 0a18eec | 2014-09-19 13:25:28 +0200 | [diff] [blame] | 384 | // Check possible conflicts with 1D ctor |
| 385 | typedef Array<Scalar, Dynamic, 1> OneDArrayType; |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 386 | { |
| 387 | OneDArrayType o1(rows); |
| 388 | VERIFY(o1.size()==rows); |
| 389 | OneDArrayType o2(static_cast<int>(rows)); |
| 390 | VERIFY(o2.size()==rows); |
| 391 | } |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 392 | { |
| 393 | OneDArrayType o1{rows}; |
| 394 | VERIFY(o1.size()==rows); |
| 395 | OneDArrayType o4{int(rows)}; |
| 396 | VERIFY(o4.size()==rows); |
| 397 | } |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 398 | // Check possible conflicts with 2D ctor |
| 399 | typedef Array<Scalar, Dynamic, Dynamic> TwoDArrayType; |
| 400 | typedef Array<Scalar, 2, 1> ArrayType2; |
| 401 | { |
| 402 | TwoDArrayType o1(rows,cols); |
| 403 | VERIFY(o1.rows()==rows); |
| 404 | VERIFY(o1.cols()==cols); |
| 405 | TwoDArrayType o2(static_cast<int>(rows),static_cast<int>(cols)); |
| 406 | VERIFY(o2.rows()==rows); |
| 407 | VERIFY(o2.cols()==cols); |
| 408 | |
| 409 | ArrayType2 o3(rows,cols); |
| 410 | VERIFY(o3(0)==Scalar(rows) && o3(1)==Scalar(cols)); |
| 411 | ArrayType2 o4(static_cast<int>(rows),static_cast<int>(cols)); |
| 412 | VERIFY(o4(0)==Scalar(rows) && o4(1)==Scalar(cols)); |
| 413 | } |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 414 | { |
| 415 | TwoDArrayType o1{rows,cols}; |
| 416 | VERIFY(o1.rows()==rows); |
| 417 | VERIFY(o1.cols()==cols); |
| 418 | TwoDArrayType o2{int(rows),int(cols)}; |
| 419 | VERIFY(o2.rows()==rows); |
| 420 | VERIFY(o2.cols()==cols); |
| 421 | |
| 422 | ArrayType2 o3{rows,cols}; |
| 423 | VERIFY(o3(0)==Scalar(rows) && o3(1)==Scalar(cols)); |
| 424 | ArrayType2 o4{int(rows),int(cols)}; |
| 425 | VERIFY(o4(0)==Scalar(rows) && o4(1)==Scalar(cols)); |
| 426 | } |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 429 | template<typename ArrayType> void comparisons(const ArrayType& m) |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 430 | { |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 431 | using std::abs; |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 432 | typedef typename ArrayType::Scalar Scalar; |
Benoit Jacob | 874ff5a | 2009-01-26 17:56:04 +0000 | [diff] [blame] | 433 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 434 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 435 | Index rows = m.rows(); |
| 436 | Index cols = m.cols(); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 437 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 438 | Index r = internal::random<Index>(0, rows-1), |
| 439 | c = internal::random<Index>(0, cols-1); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 440 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 441 | ArrayType m1 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 9fc1c92 | 2015-06-22 16:48:27 +0200 | [diff] [blame] | 442 | m2 = ArrayType::Random(rows, cols), |
| 443 | m3(rows, cols), |
| 444 | m4 = m1; |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 445 | |
Gael Guennebaud | 9fc1c92 | 2015-06-22 16:48:27 +0200 | [diff] [blame] | 446 | m4 = (m4.abs()==Scalar(0)).select(1,m4); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 447 | |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 448 | VERIFY(((m1 + Scalar(1)) > m1).all()); |
| 449 | VERIFY(((m1 - Scalar(1)) < m1).all()); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 450 | if (rows*cols>1) |
| 451 | { |
| 452 | m3 = m1; |
| 453 | m3(r,c) += 1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 454 | VERIFY(! (m1 < m3).all() ); |
| 455 | VERIFY(! (m1 > m3).all() ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 456 | } |
Christoph Hertzberg | 494fa99 | 2015-05-07 17:28:40 +0200 | [diff] [blame] | 457 | VERIFY(!(m1 > m2 && m1 < m2).any()); |
| 458 | VERIFY((m1 <= m2 || m1 >= m2).all()); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 459 | |
Christoph Hertzberg | 36052c4 | 2013-10-17 15:37:29 +0200 | [diff] [blame] | 460 | // comparisons array to scalar |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 461 | VERIFY( (m1 != (m1(r,c)+1) ).any() ); |
Christoph Hertzberg | 36052c4 | 2013-10-17 15:37:29 +0200 | [diff] [blame] | 462 | VERIFY( (m1 > (m1(r,c)-1) ).any() ); |
| 463 | VERIFY( (m1 < (m1(r,c)+1) ).any() ); |
| 464 | VERIFY( (m1 == m1(r,c) ).any() ); |
| 465 | |
| 466 | // comparisons scalar to array |
| 467 | VERIFY( ( (m1(r,c)+1) != m1).any() ); |
| 468 | VERIFY( ( (m1(r,c)-1) < m1).any() ); |
| 469 | VERIFY( ( (m1(r,c)+1) > m1).any() ); |
| 470 | VERIFY( ( m1(r,c) == m1).any() ); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 471 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 472 | // test Select |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 473 | VERIFY_IS_APPROX( (m1<m2).select(m1,m2), m1.cwiseMin(m2) ); |
| 474 | VERIFY_IS_APPROX( (m1>m2).select(m1,m2), m1.cwiseMax(m2) ); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 475 | Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2); |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 476 | for (int j=0; j<cols; ++j) |
| 477 | for (int i=0; i<rows; ++i) |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 478 | m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 479 | VERIFY_IS_APPROX( (m1.abs()<ArrayType::Constant(rows,cols,mid)) |
| 480 | .select(ArrayType::Zero(rows,cols),m1), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 481 | // shorter versions: |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 482 | VERIFY_IS_APPROX( (m1.abs()<ArrayType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 483 | .select(0,m1), m3); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 484 | VERIFY_IS_APPROX( (m1.abs()>=ArrayType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 485 | .select(m1,0), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 486 | // even shorter version: |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 487 | VERIFY_IS_APPROX( (m1.abs()<mid).select(0,m1), m3); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 488 | |
Gael Guennebaud | 56c7e16 | 2009-01-24 15:22:44 +0000 | [diff] [blame] | 489 | // count |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 490 | VERIFY(((m1.abs()+1)>RealScalar(0.1)).count() == rows*cols); |
Benoit Jacob | aaaade4 | 2010-05-30 16:00:58 -0400 | [diff] [blame] | 491 | |
Gael Guennebaud | 35c1158 | 2011-05-31 22:17:34 +0200 | [diff] [blame] | 492 | // and/or |
| 493 | VERIFY( (m1<RealScalar(0) && m1>RealScalar(0)).count() == 0); |
| 494 | VERIFY( (m1<RealScalar(0) || m1>=RealScalar(0)).count() == rows*cols); |
| 495 | RealScalar a = m1.abs().mean(); |
| 496 | VERIFY( (m1<-a || m1>a).count() == (m1.abs()>a).count()); |
| 497 | |
Gael Guennebaud | 12e1ebb | 2018-07-12 17:16:40 +0200 | [diff] [blame] | 498 | typedef Array<Index, Dynamic, 1> ArrayOfIndices; |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 499 | |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 500 | // TODO allows colwise/rowwise for array |
Benoit Jacob | aaaade4 | 2010-05-30 16:00:58 -0400 | [diff] [blame] | 501 | VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).colwise().count(), ArrayOfIndices::Constant(cols,rows).transpose()); |
| 502 | VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).rowwise().count(), ArrayOfIndices::Constant(rows, cols)); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 505 | template<typename ArrayType> void array_real(const ArrayType& m) |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 506 | { |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 507 | using std::abs; |
Gael Guennebaud | 9b9177f | 2013-07-05 13:35:34 +0200 | [diff] [blame] | 508 | using std::sqrt; |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 509 | typedef typename ArrayType::Scalar Scalar; |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 510 | typedef typename NumTraits<Scalar>::Real RealScalar; |
| 511 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 512 | Index rows = m.rows(); |
| 513 | Index cols = m.cols(); |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 514 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 515 | ArrayType m1 = ArrayType::Random(rows, cols), |
Gael Guennebaud | c695cbf | 2013-06-24 13:33:44 +0200 | [diff] [blame] | 516 | m2 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 9fc1c92 | 2015-06-22 16:48:27 +0200 | [diff] [blame] | 517 | m3(rows, cols), |
| 518 | m4 = m1; |
Benoit Steiner | 8314962 | 2015-12-10 13:13:45 -0800 | [diff] [blame] | 519 | |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 520 | m4 = (m4.abs()==Scalar(0)).select(Scalar(1),m4); |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 521 | |
Gael Guennebaud | 9b1ad5e | 2012-03-09 12:08:06 +0100 | [diff] [blame] | 522 | Scalar s1 = internal::random<Scalar>(); |
| 523 | |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 524 | // these tests are mostly to check possible compilation issues with free-functions. |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 525 | VERIFY_IS_APPROX(m1.sin(), sin(m1)); |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 526 | VERIFY_IS_APPROX(m1.cos(), cos(m1)); |
Deanna Hood | f89fcef | 2015-03-11 13:13:30 +1000 | [diff] [blame] | 527 | VERIFY_IS_APPROX(m1.tan(), tan(m1)); |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 528 | VERIFY_IS_APPROX(m1.asin(), asin(m1)); |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 529 | VERIFY_IS_APPROX(m1.acos(), acos(m1)); |
Jitse Niesen | de150b1 | 2014-06-19 15:12:33 +0100 | [diff] [blame] | 530 | VERIFY_IS_APPROX(m1.atan(), atan(m1)); |
Deanna Hood | f89fcef | 2015-03-11 13:13:30 +1000 | [diff] [blame] | 531 | VERIFY_IS_APPROX(m1.sinh(), sinh(m1)); |
| 532 | VERIFY_IS_APPROX(m1.cosh(), cosh(m1)); |
| 533 | VERIFY_IS_APPROX(m1.tanh(), tanh(m1)); |
Rasmus Munk Larsen | 28ba1b2 | 2019-01-11 17:45:37 -0800 | [diff] [blame] | 534 | #if EIGEN_HAS_CXX11_MATH |
| 535 | VERIFY_IS_APPROX(m1.tanh().atanh(), atanh(tanh(m1))); |
| 536 | VERIFY_IS_APPROX(m1.sinh().asinh(), asinh(sinh(m1))); |
| 537 | VERIFY_IS_APPROX(m1.cosh().acosh(), acosh(cosh(m1))); |
| 538 | #endif |
Rasmus Munk Larsen | d6e283b | 2018-08-13 11:14:50 -0700 | [diff] [blame] | 539 | VERIFY_IS_APPROX(m1.logistic(), logistic(m1)); |
Gael Guennebaud | 2f7e261 | 2016-07-08 11:13:55 +0200 | [diff] [blame] | 540 | |
Deanna Hood | a5e4997 | 2015-03-11 08:56:42 +1000 | [diff] [blame] | 541 | VERIFY_IS_APPROX(m1.arg(), arg(m1)); |
Deanna Hood | 31fdd67 | 2015-03-11 06:39:23 +1000 | [diff] [blame] | 542 | VERIFY_IS_APPROX(m1.round(), round(m1)); |
Ilya Tokar | 19876ce | 2019-12-16 16:00:35 -0500 | [diff] [blame] | 543 | VERIFY_IS_APPROX(m1.rint(), rint(m1)); |
Deanna Hood | 31fdd67 | 2015-03-11 06:39:23 +1000 | [diff] [blame] | 544 | VERIFY_IS_APPROX(m1.floor(), floor(m1)); |
| 545 | VERIFY_IS_APPROX(m1.ceil(), ceil(m1)); |
Christoph Hertzberg | 61e0977 | 2015-08-14 17:32:34 +0200 | [diff] [blame] | 546 | VERIFY((m1.isNaN() == (Eigen::isnan)(m1)).all()); |
| 547 | VERIFY((m1.isInf() == (Eigen::isinf)(m1)).all()); |
| 548 | VERIFY((m1.isFinite() == (Eigen::isfinite)(m1)).all()); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 549 | VERIFY_IS_APPROX(m4.inverse(), inverse(m4)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 550 | VERIFY_IS_APPROX(m1.abs(), abs(m1)); |
| 551 | VERIFY_IS_APPROX(m1.abs2(), abs2(m1)); |
| 552 | VERIFY_IS_APPROX(m1.square(), square(m1)); |
| 553 | VERIFY_IS_APPROX(m1.cube(), cube(m1)); |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 554 | VERIFY_IS_APPROX(cos(m1+RealScalar(3)*m2), cos((m1+RealScalar(3)*m2).eval())); |
Gael Guennebaud | 3f32f5e | 2015-11-27 16:27:53 +0100 | [diff] [blame] | 555 | VERIFY_IS_APPROX(m1.sign(), sign(m1)); |
Rasmus Munk Larsen | 6964ae8 | 2020-07-07 01:54:04 +0000 | [diff] [blame] | 556 | VERIFY((m1.sqrt().sign().isNaN() == (Eigen::isnan)(sign(sqrt(m1)))).all()); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 557 | |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 558 | // avoid inf and NaNs so verification doesn't fail |
| 559 | m3 = m4.abs(); |
| 560 | VERIFY_IS_APPROX(m3.sqrt(), sqrt(abs(m3))); |
| 561 | VERIFY_IS_APPROX(m3.rsqrt(), Scalar(1)/sqrt(abs(m3))); |
| 562 | VERIFY_IS_APPROX(rsqrt(m3), Scalar(1)/sqrt(abs(m3))); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 563 | VERIFY_IS_APPROX(m3.log(), log(m3)); |
Gael Guennebaud | 89099b0 | 2016-06-01 17:00:08 +0200 | [diff] [blame] | 564 | VERIFY_IS_APPROX(m3.log1p(), log1p(m3)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 565 | VERIFY_IS_APPROX(m3.log10(), log10(m3)); |
Rasmus Munk Larsen | f9fac1d | 2020-12-04 21:45:09 +0000 | [diff] [blame] | 566 | VERIFY_IS_APPROX(m3.log2(), log2(m3)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 567 | |
| 568 | |
| 569 | VERIFY((!(m1>m2) == (m1<=m2)).all()); |
| 570 | |
| 571 | VERIFY_IS_APPROX(sin(m1.asin()), m1); |
| 572 | VERIFY_IS_APPROX(cos(m1.acos()), m1); |
| 573 | VERIFY_IS_APPROX(tan(m1.atan()), m1); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 574 | VERIFY_IS_APPROX(sinh(m1), Scalar(0.5)*(exp(m1)-exp(-m1))); |
| 575 | VERIFY_IS_APPROX(cosh(m1), Scalar(0.5)*(exp(m1)+exp(-m1))); |
| 576 | VERIFY_IS_APPROX(tanh(m1), (Scalar(0.5)*(exp(m1)-exp(-m1)))/(Scalar(0.5)*(exp(m1)+exp(-m1)))); |
| 577 | VERIFY_IS_APPROX(logistic(m1), (Scalar(1)/(Scalar(1)+exp(-m1)))); |
| 578 | VERIFY_IS_APPROX(arg(m1), ((m1<Scalar(0)).template cast<Scalar>())*Scalar(std::acos(Scalar(-1)))); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 579 | VERIFY((round(m1) <= ceil(m1) && round(m1) >= floor(m1)).all()); |
Ilya Tokar | 19876ce | 2019-12-16 16:00:35 -0500 | [diff] [blame] | 580 | VERIFY((rint(m1) <= ceil(m1) && rint(m1) >= floor(m1)).all()); |
| 581 | VERIFY(((ceil(m1) - round(m1)) <= Scalar(0.5) || (round(m1) - floor(m1)) <= Scalar(0.5)).all()); |
| 582 | VERIFY(((ceil(m1) - round(m1)) <= Scalar(1.0) && (round(m1) - floor(m1)) <= Scalar(1.0)).all()); |
| 583 | VERIFY(((ceil(m1) - rint(m1)) <= Scalar(0.5) || (rint(m1) - floor(m1)) <= Scalar(0.5)).all()); |
| 584 | VERIFY(((ceil(m1) - rint(m1)) <= Scalar(1.0) && (rint(m1) - floor(m1)) <= Scalar(1.0)).all()); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 585 | VERIFY((Eigen::isnan)((m1*Scalar(0))/Scalar(0)).all()); |
| 586 | VERIFY((Eigen::isinf)(m4/Scalar(0)).all()); |
| 587 | VERIFY(((Eigen::isfinite)(m1) && (!(Eigen::isfinite)(m1*Scalar(0)/Scalar(0))) && (!(Eigen::isfinite)(m4/Scalar(0)))).all()); |
| 588 | VERIFY_IS_APPROX(inverse(inverse(m4)),m4); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 589 | VERIFY((abs(m1) == m1 || abs(m1) == -m1).all()); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 590 | VERIFY_IS_APPROX(m3, sqrt(abs2(m3))); |
Joel Holdsworth | d5c6657 | 2020-03-19 17:45:20 +0000 | [diff] [blame] | 591 | VERIFY_IS_APPROX(m1.absolute_difference(m2), (m1 > m2).select(m1 - m2, m2 - m1)); |
Gael Guennebaud | 3f32f5e | 2015-11-27 16:27:53 +0100 | [diff] [blame] | 592 | VERIFY_IS_APPROX( m1.sign(), -(-m1).sign() ); |
| 593 | VERIFY_IS_APPROX( m1*m1.sign(),m1.abs()); |
| 594 | VERIFY_IS_APPROX(m1.sign() * m1.abs(), m1); |
Benoit Jacob | 5d63d2c | 2010-04-28 22:42:34 -0400 | [diff] [blame] | 595 | |
Gael Guennebaud | 62670c8 | 2013-06-10 23:40:56 +0200 | [diff] [blame] | 596 | VERIFY_IS_APPROX(numext::abs2(numext::real(m1)) + numext::abs2(numext::imag(m1)), numext::abs2(m1)); |
Gael Guennebaud | 87427d2 | 2019-10-08 09:15:17 +0200 | [diff] [blame] | 597 | VERIFY_IS_APPROX(numext::abs2(Eigen::real(m1)) + numext::abs2(Eigen::imag(m1)), numext::abs2(m1)); |
Benoit Jacob | 5d63d2c | 2010-04-28 22:42:34 -0400 | [diff] [blame] | 598 | if(!NumTraits<Scalar>::IsComplex) |
Gael Guennebaud | 62670c8 | 2013-06-10 23:40:56 +0200 | [diff] [blame] | 599 | VERIFY_IS_APPROX(numext::real(m1), m1); |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 600 | |
Jitse Niesen | 6fecb6f | 2014-02-24 14:10:17 +0000 | [diff] [blame] | 601 | // shift argument of logarithm so that it is not zero |
| 602 | Scalar smallNumber = NumTraits<Scalar>::dummy_precision(); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 603 | VERIFY_IS_APPROX((m3 + smallNumber).log() , log(abs(m3) + smallNumber)); |
| 604 | VERIFY_IS_APPROX((m3 + smallNumber + Scalar(1)).log() , log1p(abs(m3) + smallNumber)); |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 605 | |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 606 | VERIFY_IS_APPROX(m1.exp() * m2.exp(), exp(m1+m2)); |
| 607 | VERIFY_IS_APPROX(m1.exp(), exp(m1)); |
| 608 | VERIFY_IS_APPROX(m1.exp() / m2.exp(),(m1-m2).exp()); |
Gael Guennebaud | 575ac54 | 2010-06-19 23:17:07 +0200 | [diff] [blame] | 609 | |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 610 | VERIFY_IS_APPROX(m1.expm1(), expm1(m1)); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 611 | VERIFY_IS_APPROX((m3 + smallNumber).exp() - Scalar(1), expm1(abs(m3) + smallNumber)); |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 612 | |
Gael Guennebaud | 575ac54 | 2010-06-19 23:17:07 +0200 | [diff] [blame] | 613 | VERIFY_IS_APPROX(m3.pow(RealScalar(0.5)), m3.sqrt()); |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 614 | VERIFY_IS_APPROX(pow(m3,RealScalar(0.5)), m3.sqrt()); |
Benoit Steiner | e25e3a0 | 2015-12-03 18:16:35 -0800 | [diff] [blame] | 615 | |
| 616 | VERIFY_IS_APPROX(m3.pow(RealScalar(-0.5)), m3.rsqrt()); |
| 617 | VERIFY_IS_APPROX(pow(m3,RealScalar(-0.5)), m3.rsqrt()); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 618 | |
| 619 | // Avoid inf and NaN. |
| 620 | m3 = (m1.square()<NumTraits<Scalar>::epsilon()).select(Scalar(1),m3); |
| 621 | VERIFY_IS_APPROX(m3.pow(RealScalar(-2)), m3.square().inverse()); |
Rasmus Munk Larsen | cdd8fdc | 2021-01-18 13:25:16 +0000 | [diff] [blame] | 622 | pow_test<Scalar>(); |
Benoit Steiner | e25e3a0 | 2015-12-03 18:16:35 -0800 | [diff] [blame] | 623 | |
Antonio Sanchez | 4c42d5e | 2021-01-23 11:54:00 -0800 | [diff] [blame] | 624 | VERIFY_IS_APPROX(log10(m3), log(m3)/numext::log(Scalar(10))); |
| 625 | VERIFY_IS_APPROX(log2(m3), log(m3)/numext::log(Scalar(2))); |
Hauke Heibel | 81c1336 | 2012-03-07 08:58:42 +0100 | [diff] [blame] | 626 | |
| 627 | // scalar by array division |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 628 | const RealScalar tiny = sqrt(std::numeric_limits<RealScalar>::epsilon()); |
Hauke Heibel | dd9365e | 2012-03-09 14:04:13 +0100 | [diff] [blame] | 629 | s1 += Scalar(tiny); |
| 630 | m1 += ArrayType::Constant(rows,cols,Scalar(tiny)); |
Rasmus Munk Larsen | 96dc37a | 2022-01-07 01:10:17 +0000 | [diff] [blame] | 631 | VERIFY_IS_CWISE_APPROX(s1/m1, s1 * m1.inverse()); |
Eugene Brevdo | f736277 | 2015-12-24 21:15:38 -0800 | [diff] [blame] | 632 | |
Gael Guennebaud | c695cbf | 2013-06-24 13:33:44 +0200 | [diff] [blame] | 633 | // check inplace transpose |
| 634 | m3 = m1; |
| 635 | m3.transposeInPlace(); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 636 | VERIFY_IS_APPROX(m3, m1.transpose()); |
Gael Guennebaud | c695cbf | 2013-06-24 13:33:44 +0200 | [diff] [blame] | 637 | m3.transposeInPlace(); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 638 | VERIFY_IS_APPROX(m3, m1); |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 639 | } |
| 640 | |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 641 | |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 642 | template<typename ArrayType> void array_complex(const ArrayType& m) |
| 643 | { |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 644 | typedef typename ArrayType::Scalar Scalar; |
| 645 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 646 | |
| 647 | Index rows = m.rows(); |
| 648 | Index cols = m.cols(); |
| 649 | |
| 650 | ArrayType m1 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 9fc1c92 | 2015-06-22 16:48:27 +0200 | [diff] [blame] | 651 | m2(rows, cols), |
| 652 | m4 = m1; |
Srinivas Vasudevan | 218764e | 2016-12-02 14:13:01 -0800 | [diff] [blame] | 653 | |
Gael Guennebaud | 9fc1c92 | 2015-06-22 16:48:27 +0200 | [diff] [blame] | 654 | m4.real() = (m4.real().abs()==RealScalar(0)).select(RealScalar(1),m4.real()); |
| 655 | m4.imag() = (m4.imag().abs()==RealScalar(0)).select(RealScalar(1),m4.imag()); |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 656 | |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 657 | Array<RealScalar, -1, -1> m3(rows, cols); |
| 658 | |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 659 | for (Index i = 0; i < m.rows(); ++i) |
| 660 | for (Index j = 0; j < m.cols(); ++j) |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame] | 661 | m2(i,j) = sqrt(m1(i,j)); |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 662 | |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 663 | // these tests are mostly to check possible compilation issues with free-functions. |
Deanna Hood | f89fcef | 2015-03-11 13:13:30 +1000 | [diff] [blame] | 664 | VERIFY_IS_APPROX(m1.sin(), sin(m1)); |
| 665 | VERIFY_IS_APPROX(m1.cos(), cos(m1)); |
| 666 | VERIFY_IS_APPROX(m1.tan(), tan(m1)); |
| 667 | VERIFY_IS_APPROX(m1.sinh(), sinh(m1)); |
| 668 | VERIFY_IS_APPROX(m1.cosh(), cosh(m1)); |
| 669 | VERIFY_IS_APPROX(m1.tanh(), tanh(m1)); |
Rasmus Munk Larsen | d6e283b | 2018-08-13 11:14:50 -0700 | [diff] [blame] | 670 | VERIFY_IS_APPROX(m1.logistic(), logistic(m1)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 671 | VERIFY_IS_APPROX(m1.arg(), arg(m1)); |
Christoph Hertzberg | 61e0977 | 2015-08-14 17:32:34 +0200 | [diff] [blame] | 672 | VERIFY((m1.isNaN() == (Eigen::isnan)(m1)).all()); |
| 673 | VERIFY((m1.isInf() == (Eigen::isinf)(m1)).all()); |
| 674 | VERIFY((m1.isFinite() == (Eigen::isfinite)(m1)).all()); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 675 | VERIFY_IS_APPROX(m4.inverse(), inverse(m4)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 676 | VERIFY_IS_APPROX(m1.log(), log(m1)); |
| 677 | VERIFY_IS_APPROX(m1.log10(), log10(m1)); |
Rasmus Munk Larsen | f9fac1d | 2020-12-04 21:45:09 +0000 | [diff] [blame] | 678 | VERIFY_IS_APPROX(m1.log2(), log2(m1)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 679 | VERIFY_IS_APPROX(m1.abs(), abs(m1)); |
| 680 | VERIFY_IS_APPROX(m1.abs2(), abs2(m1)); |
| 681 | VERIFY_IS_APPROX(m1.sqrt(), sqrt(m1)); |
| 682 | VERIFY_IS_APPROX(m1.square(), square(m1)); |
| 683 | VERIFY_IS_APPROX(m1.cube(), cube(m1)); |
| 684 | VERIFY_IS_APPROX(cos(m1+RealScalar(3)*m2), cos((m1+RealScalar(3)*m2).eval())); |
Gael Guennebaud | 3f32f5e | 2015-11-27 16:27:53 +0100 | [diff] [blame] | 685 | VERIFY_IS_APPROX(m1.sign(), sign(m1)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 686 | |
| 687 | |
| 688 | VERIFY_IS_APPROX(m1.exp() * m2.exp(), exp(m1+m2)); |
| 689 | VERIFY_IS_APPROX(m1.exp(), exp(m1)); |
| 690 | VERIFY_IS_APPROX(m1.exp() / m2.exp(),(m1-m2).exp()); |
| 691 | |
Srinivas Vasudevan | 88062b7 | 2019-12-12 01:56:54 +0000 | [diff] [blame] | 692 | VERIFY_IS_APPROX(m1.expm1(), expm1(m1)); |
| 693 | VERIFY_IS_APPROX(expm1(m1), exp(m1) - 1.); |
| 694 | // Check for larger magnitude complex numbers that expm1 matches exp - 1. |
| 695 | VERIFY_IS_APPROX(expm1(10. * m1), exp(10. * m1) - 1.); |
| 696 | |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 697 | VERIFY_IS_APPROX(sinh(m1), 0.5*(exp(m1)-exp(-m1))); |
| 698 | VERIFY_IS_APPROX(cosh(m1), 0.5*(exp(m1)+exp(-m1))); |
| 699 | VERIFY_IS_APPROX(tanh(m1), (0.5*(exp(m1)-exp(-m1)))/(0.5*(exp(m1)+exp(-m1)))); |
Rasmus Munk Larsen | d6e283b | 2018-08-13 11:14:50 -0700 | [diff] [blame] | 700 | VERIFY_IS_APPROX(logistic(m1), (1.0/(1.0 + exp(-m1)))); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 701 | |
| 702 | for (Index i = 0; i < m.rows(); ++i) |
| 703 | for (Index j = 0; j < m.cols(); ++j) |
Gael Guennebaud | 87427d2 | 2019-10-08 09:15:17 +0200 | [diff] [blame] | 704 | m3(i,j) = std::atan2(m1(i,j).imag(), m1(i,j).real()); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 705 | VERIFY_IS_APPROX(arg(m1), m3); |
| 706 | |
| 707 | std::complex<RealScalar> zero(0.0,0.0); |
Christoph Hertzberg | 61e0977 | 2015-08-14 17:32:34 +0200 | [diff] [blame] | 708 | VERIFY((Eigen::isnan)(m1*zero/zero).all()); |
Gael Guennebaud | 4a985e7 | 2015-11-20 14:52:08 +0100 | [diff] [blame] | 709 | #if EIGEN_COMP_MSVC |
| 710 | // msvc complex division is not robust |
| 711 | VERIFY((Eigen::isinf)(m4/RealScalar(0)).all()); |
| 712 | #else |
Gael Guennebaud | 40f326e | 2015-06-17 15:33:09 +0200 | [diff] [blame] | 713 | #if EIGEN_COMP_CLANG |
Gael Guennebaud | 4a985e7 | 2015-11-20 14:52:08 +0100 | [diff] [blame] | 714 | // clang's complex division is notoriously broken too |
Christoph Hertzberg | 61e0977 | 2015-08-14 17:32:34 +0200 | [diff] [blame] | 715 | if((numext::isinf)(m4(0,0)/RealScalar(0))) { |
Gael Guennebaud | 40f326e | 2015-06-17 15:33:09 +0200 | [diff] [blame] | 716 | #endif |
Gael Guennebaud | 4a985e7 | 2015-11-20 14:52:08 +0100 | [diff] [blame] | 717 | VERIFY((Eigen::isinf)(m4/zero).all()); |
Gael Guennebaud | 40f326e | 2015-06-17 15:33:09 +0200 | [diff] [blame] | 718 | #if EIGEN_COMP_CLANG |
| 719 | } |
| 720 | else |
| 721 | { |
Christoph Hertzberg | 61e0977 | 2015-08-14 17:32:34 +0200 | [diff] [blame] | 722 | VERIFY((Eigen::isinf)(m4.real()/zero.real()).all()); |
Gael Guennebaud | 40f326e | 2015-06-17 15:33:09 +0200 | [diff] [blame] | 723 | } |
| 724 | #endif |
Gael Guennebaud | 4a985e7 | 2015-11-20 14:52:08 +0100 | [diff] [blame] | 725 | #endif // MSVC |
| 726 | |
Christoph Hertzberg | 61e0977 | 2015-08-14 17:32:34 +0200 | [diff] [blame] | 727 | VERIFY(((Eigen::isfinite)(m1) && (!(Eigen::isfinite)(m1*zero/zero)) && (!(Eigen::isfinite)(m1/zero))).all()); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 728 | |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 729 | VERIFY_IS_APPROX(inverse(inverse(m4)),m4); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 730 | VERIFY_IS_APPROX(conj(m1.conjugate()), m1); |
Gael Guennebaud | 87427d2 | 2019-10-08 09:15:17 +0200 | [diff] [blame] | 731 | VERIFY_IS_APPROX(abs(m1), sqrt(square(m1.real())+square(m1.imag()))); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 732 | VERIFY_IS_APPROX(abs(m1), sqrt(abs2(m1))); |
| 733 | VERIFY_IS_APPROX(log10(m1), log(m1)/log(10)); |
Rasmus Munk Larsen | f9fac1d | 2020-12-04 21:45:09 +0000 | [diff] [blame] | 734 | VERIFY_IS_APPROX(log2(m1), log(m1)/log(2)); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 735 | |
Gael Guennebaud | 3f32f5e | 2015-11-27 16:27:53 +0100 | [diff] [blame] | 736 | VERIFY_IS_APPROX( m1.sign(), -(-m1).sign() ); |
| 737 | VERIFY_IS_APPROX( m1.sign() * m1.abs(), m1); |
| 738 | |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 739 | // scalar by array division |
Eugene Brevdo | f736277 | 2015-12-24 21:15:38 -0800 | [diff] [blame] | 740 | Scalar s1 = internal::random<Scalar>(); |
Benoit Steiner | 2d74ef9 | 2016-05-17 07:20:11 -0700 | [diff] [blame] | 741 | const RealScalar tiny = std::sqrt(std::numeric_limits<RealScalar>::epsilon()); |
Deanna Hood | 41b717d | 2015-03-18 03:11:03 +1000 | [diff] [blame] | 742 | s1 += Scalar(tiny); |
| 743 | m1 += ArrayType::Constant(rows,cols,Scalar(tiny)); |
| 744 | VERIFY_IS_APPROX(s1/m1, s1 * m1.inverse()); |
| 745 | |
| 746 | // check inplace transpose |
| 747 | m2 = m1; |
| 748 | m2.transposeInPlace(); |
| 749 | VERIFY_IS_APPROX(m2, m1.transpose()); |
| 750 | m2.transposeInPlace(); |
| 751 | VERIFY_IS_APPROX(m2, m1); |
Rasmus Munk Larsen | b47c777 | 2020-04-27 18:55:15 -0700 | [diff] [blame] | 752 | // Check vectorized inplace transpose. |
Rasmus Munk Larsen | 74ec8e6 | 2020-05-07 17:29:56 +0000 | [diff] [blame] | 753 | ArrayType m5 = ArrayType::Random(131, 131); |
Rasmus Munk Larsen | b47c777 | 2020-04-27 18:55:15 -0700 | [diff] [blame] | 754 | ArrayType m6 = m5; |
| 755 | m6.transposeInPlace(); |
| 756 | VERIFY_IS_APPROX(m6, m5.transpose()); |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Abraham Bachrach | 039408c | 2012-01-11 11:00:30 -0500 | [diff] [blame] | 759 | template<typename ArrayType> void min_max(const ArrayType& m) |
| 760 | { |
Abraham Bachrach | 039408c | 2012-01-11 11:00:30 -0500 | [diff] [blame] | 761 | typedef typename ArrayType::Scalar Scalar; |
| 762 | |
| 763 | Index rows = m.rows(); |
| 764 | Index cols = m.cols(); |
| 765 | |
| 766 | ArrayType m1 = ArrayType::Random(rows, cols); |
| 767 | |
| 768 | // min/max with array |
| 769 | Scalar maxM1 = m1.maxCoeff(); |
| 770 | Scalar minM1 = m1.minCoeff(); |
| 771 | |
| 772 | VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, minM1), (m1.min)(ArrayType::Constant(rows,cols, minM1))); |
| 773 | VERIFY_IS_APPROX(m1, (m1.min)(ArrayType::Constant(rows,cols, maxM1))); |
| 774 | |
| 775 | VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, maxM1), (m1.max)(ArrayType::Constant(rows,cols, maxM1))); |
| 776 | VERIFY_IS_APPROX(m1, (m1.max)(ArrayType::Constant(rows,cols, minM1))); |
| 777 | |
| 778 | // min/max with scalar input |
| 779 | VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, minM1), (m1.min)( minM1)); |
| 780 | VERIFY_IS_APPROX(m1, (m1.min)( maxM1)); |
| 781 | |
| 782 | VERIFY_IS_APPROX(ArrayType::Constant(rows,cols, maxM1), (m1.max)( maxM1)); |
| 783 | VERIFY_IS_APPROX(m1, (m1.max)( minM1)); |
| 784 | |
Rasmus Munk Larsen | 841c898 | 2021-02-24 17:49:20 -0800 | [diff] [blame] | 785 | |
| 786 | // min/max with various NaN propagation options. |
| 787 | if (m1.size() > 1 && !NumTraits<Scalar>::IsInteger) { |
Antonio Sanchez | 8dfe102 | 2021-03-16 20:12:46 -0700 | [diff] [blame] | 788 | m1(0,0) = NumTraits<Scalar>::quiet_NaN(); |
Rasmus Munk Larsen | 841c898 | 2021-02-24 17:49:20 -0800 | [diff] [blame] | 789 | maxM1 = m1.template maxCoeff<PropagateNaN>(); |
| 790 | minM1 = m1.template minCoeff<PropagateNaN>(); |
| 791 | VERIFY((numext::isnan)(maxM1)); |
| 792 | VERIFY((numext::isnan)(minM1)); |
| 793 | |
| 794 | maxM1 = m1.template maxCoeff<PropagateNumbers>(); |
| 795 | minM1 = m1.template minCoeff<PropagateNumbers>(); |
| 796 | VERIFY(!(numext::isnan)(maxM1)); |
| 797 | VERIFY(!(numext::isnan)(minM1)); |
| 798 | } |
Abraham Bachrach | 039408c | 2012-01-11 11:00:30 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Antonio Sanchez | fc9d352 | 2021-08-17 20:04:48 -0700 | [diff] [blame] | 801 | template<int N> |
| 802 | struct shift_left { |
| 803 | template<typename Scalar> |
| 804 | Scalar operator()(const Scalar& v) const { |
| 805 | return v << N; |
| 806 | } |
| 807 | }; |
| 808 | |
| 809 | template<int N> |
| 810 | struct arithmetic_shift_right { |
| 811 | template<typename Scalar> |
| 812 | Scalar operator()(const Scalar& v) const { |
| 813 | return v >> N; |
| 814 | } |
| 815 | }; |
| 816 | |
| 817 | template<typename ArrayType> void array_integer(const ArrayType& m) |
| 818 | { |
| 819 | Index rows = m.rows(); |
| 820 | Index cols = m.cols(); |
| 821 | |
| 822 | ArrayType m1 = ArrayType::Random(rows, cols), |
| 823 | m2(rows, cols); |
| 824 | |
| 825 | m2 = m1.template shiftLeft<2>(); |
| 826 | VERIFY( (m2 == m1.unaryExpr(shift_left<2>())).all() ); |
| 827 | m2 = m1.template shiftLeft<9>(); |
| 828 | VERIFY( (m2 == m1.unaryExpr(shift_left<9>())).all() ); |
| 829 | |
| 830 | m2 = m1.template shiftRight<2>(); |
| 831 | VERIFY( (m2 == m1.unaryExpr(arithmetic_shift_right<2>())).all() ); |
| 832 | m2 = m1.template shiftRight<9>(); |
| 833 | VERIFY( (m2 == m1.unaryExpr(arithmetic_shift_right<9>())).all() ); |
| 834 | } |
| 835 | |
Gael Guennebaud | e0f6d35 | 2018-09-20 18:07:32 +0200 | [diff] [blame] | 836 | EIGEN_DECLARE_TEST(array_cwise) |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 837 | { |
| 838 | for(int i = 0; i < g_repeat; i++) { |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 839 | CALL_SUBTEST_1( array(Array<float, 1, 1>()) ); |
Hauke Heibel | b31e124 | 2010-12-16 19:07:23 +0100 | [diff] [blame] | 840 | CALL_SUBTEST_2( array(Array22f()) ); |
| 841 | CALL_SUBTEST_3( array(Array44d()) ); |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 842 | CALL_SUBTEST_4( array(ArrayXXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 843 | CALL_SUBTEST_5( array(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 844 | CALL_SUBTEST_6( array(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Gael Guennebaud | 543529d | 2019-01-22 15:30:50 +0100 | [diff] [blame] | 845 | CALL_SUBTEST_6( array(Array<Index,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Antonio Sanchez | fc9d352 | 2021-08-17 20:04:48 -0700 | [diff] [blame] | 846 | CALL_SUBTEST_6( array_integer(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 847 | CALL_SUBTEST_6( array_integer(Array<Index,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 848 | } |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 849 | for(int i = 0; i < g_repeat; i++) { |
| 850 | CALL_SUBTEST_1( comparisons(Array<float, 1, 1>()) ); |
Hauke Heibel | b31e124 | 2010-12-16 19:07:23 +0100 | [diff] [blame] | 851 | CALL_SUBTEST_2( comparisons(Array22f()) ); |
| 852 | CALL_SUBTEST_3( comparisons(Array44d()) ); |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 853 | CALL_SUBTEST_5( comparisons(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 854 | CALL_SUBTEST_6( comparisons(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 855 | } |
| 856 | for(int i = 0; i < g_repeat; i++) { |
Abraham Bachrach | 039408c | 2012-01-11 11:00:30 -0500 | [diff] [blame] | 857 | CALL_SUBTEST_1( min_max(Array<float, 1, 1>()) ); |
| 858 | CALL_SUBTEST_2( min_max(Array22f()) ); |
| 859 | CALL_SUBTEST_3( min_max(Array44d()) ); |
| 860 | CALL_SUBTEST_5( min_max(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 861 | CALL_SUBTEST_6( min_max(ArrayXXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 862 | } |
| 863 | for(int i = 0; i < g_repeat; i++) { |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 864 | CALL_SUBTEST_1( array_real(Array<float, 1, 1>()) ); |
Hauke Heibel | b31e124 | 2010-12-16 19:07:23 +0100 | [diff] [blame] | 865 | CALL_SUBTEST_2( array_real(Array22f()) ); |
| 866 | CALL_SUBTEST_3( array_real(Array44d()) ); |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 867 | CALL_SUBTEST_5( array_real(ArrayXXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Antonio Sanchez | f0e46ed | 2021-01-22 11:10:54 -0800 | [diff] [blame] | 868 | CALL_SUBTEST_7( array_real(Array<Eigen::half, 32, 32>()) ); |
| 869 | CALL_SUBTEST_8( array_real(Array<Eigen::bfloat16, 32, 32>()) ); |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 870 | } |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 871 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 872 | CALL_SUBTEST_4( array_complex(ArrayXXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Jitse Niesen | 837db08 | 2011-05-09 10:17:41 +0100 | [diff] [blame] | 873 | } |
Benoit Jacob | 5d63d2c | 2010-04-28 22:42:34 -0400 | [diff] [blame] | 874 | |
Rasmus Munk Larsen | afc014f | 2022-09-12 21:55:30 +0000 | [diff] [blame] | 875 | for(int i = 0; i < g_repeat; i++) { |
| 876 | CALL_SUBTEST_6( int_pow_test() ); |
| 877 | CALL_SUBTEST_7( mixed_pow_test() ); |
| 878 | } |
| 879 | |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 880 | VERIFY((internal::is_same< internal::global_math_functions_filtering_base<int>::type, int >::value)); |
| 881 | VERIFY((internal::is_same< internal::global_math_functions_filtering_base<float>::type, float >::value)); |
| 882 | VERIFY((internal::is_same< internal::global_math_functions_filtering_base<Array2i>::type, ArrayBase<Array2i> >::value)); |
Gael Guennebaud | 64fcfd3 | 2016-06-14 11:26:57 +0200 | [diff] [blame] | 883 | typedef CwiseUnaryOp<internal::scalar_abs_op<double>, ArrayXd > Xpr; |
Hauke Heibel | 2d0dfe5 | 2010-12-16 17:36:10 +0100 | [diff] [blame] | 884 | VERIFY((internal::is_same< internal::global_math_functions_filtering_base<Xpr>::type, |
| 885 | ArrayBase<Xpr> |
| 886 | >::value)); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 887 | } |