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