|
| 1 | +#include <test/unit/math/test_ad.hpp> |
| 2 | + |
| 3 | +TEST(MathMixMatFun, traceDot) { |
| 4 | + auto f = [](const auto& x, const auto& y) { |
| 5 | + return stan::math::trace_dot(x, y); |
| 6 | + }; |
| 7 | + |
| 8 | + // 1x1 |
| 9 | + Eigen::MatrixXd a11{{3}}; |
| 10 | + Eigen::MatrixXd b11{{7}}; |
| 11 | + stan::test::expect_ad(f, a11, b11); |
| 12 | + stan::test::expect_ad_matvar(f, a11, b11); |
| 13 | + |
| 14 | + // 0x0 |
| 15 | + Eigen::MatrixXd m00(0, 0); |
| 16 | + stan::test::expect_ad(f, m00, m00); |
| 17 | + stan::test::expect_ad_matvar(f, m00, m00); |
| 18 | + |
| 19 | + // 2x2 |
| 20 | + Eigen::MatrixXd a22{{1, 2}, {3, 4}}; |
| 21 | + Eigen::MatrixXd b22{{5, 6}, {7, 8}}; |
| 22 | + stan::test::expect_ad(f, a22, b22); |
| 23 | + stan::test::expect_ad_matvar(f, a22, b22); |
| 24 | + |
| 25 | + // 2x3 times 3x2 (rectangular) |
| 26 | + Eigen::MatrixXd a23{{1, 2, 3}, {4, 5, 6}}; |
| 27 | + Eigen::MatrixXd b32{{7, 8}, {9, 10}, {11, 12}}; |
| 28 | + stan::test::expect_ad(f, a23, b32); |
| 29 | + stan::test::expect_ad_matvar(f, a23, b32); |
| 30 | + |
| 31 | + // 3x2 times 2x3 (rectangular, transposed shape) |
| 32 | + stan::test::expect_ad(f, b32, a23); |
| 33 | + stan::test::expect_ad_matvar(f, b32, a23); |
| 34 | + |
| 35 | + // 3x3 |
| 36 | + Eigen::MatrixXd a33{{1, -2, 3}, {0.5, 7, -1}, {2, 0, 4}}; |
| 37 | + Eigen::MatrixXd b33{{3, 1, -2}, {0, 5, 1}, {-1, 2, 6}}; |
| 38 | + stan::test::expect_ad(f, a33, b33); |
| 39 | + stan::test::expect_ad_matvar(f, a33, b33); |
| 40 | + |
| 41 | + // dimension mismatch: A.cols() != B.rows() |
| 42 | + stan::test::expect_ad(f, a22, b32); |
| 43 | + stan::test::expect_ad_matvar(f, a22, b32); |
| 44 | + |
| 45 | + stan::test::expect_ad(f, a23, b22); |
| 46 | + stan::test::expect_ad_matvar(f, a23, b22); |
| 47 | + |
| 48 | + // dimension mismatch: A.cols() == B.rows() but A.rows() != B.cols() |
| 49 | + stan::test::expect_ad(f, a23, a33); |
| 50 | + stan::test::expect_ad_matvar(f, a23, a33); |
| 51 | +} |
0 commit comments