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