|
| 1 | +#include <stan/math/prim.hpp> |
1 | 2 | #include <test/unit/math/test_ad.hpp> |
2 | 3 | #include <complex> |
3 | 4 | #include <vector> |
4 | 5 |
|
5 | | -TEST(mixScalFun, conj) { |
6 | | - auto f = [](const auto& x) { return conj(x); }; |
| 6 | +TEST(mathMixMatFun, conj) { |
| 7 | + auto f = [](const auto& x) { return stan::math::conj(x); }; |
7 | 8 | stan::test::expect_complex_common(f); |
| 9 | + stan::test::expect_unary_vectorized<stan::test::ScalarSupport::ComplexOnly>( |
| 10 | + f); |
| 11 | +} |
| 12 | + |
| 13 | +template <typename T> |
| 14 | +void test_vectorized_conj() { |
| 15 | + using stan::math::value_of_rec; |
| 16 | + using complex_t = std::complex<T>; |
| 17 | + using complex_matrix = Eigen::Matrix<complex_t, -1, -1>; |
| 18 | + complex_matrix A(2, 2); |
| 19 | + A << complex_t(T(0), T(1)), complex_t(T(2), T(3)), complex_t(T(4), T(5)), |
| 20 | + complex_t(T(6), T(7)); |
| 21 | + auto A_conj = stan::math::conj(A); |
| 22 | + EXPECT_MATRIX_COMPLEX_FLOAT_EQ(value_of_rec(A_conj), |
| 23 | + value_of_rec(A.conjugate())) |
| 24 | + std::vector<complex_t> v{complex_t(T(0), T(1)), complex_t(T(2), T(3)), |
| 25 | + complex_t(T(4), T(5)), complex_t(T(6), T(7))}; |
| 26 | + |
| 27 | + std::vector<complex_t> v_conj = stan::math::conj(v); |
| 28 | + for (int i = 0; i < v.size(); ++i) { |
| 29 | + std::complex<double> vi = value_of_rec(v_conj[i]); |
| 30 | + std::complex<double> ci = value_of_rec(stan::math::conj(v[i])); |
| 31 | + EXPECT_FLOAT_EQ(vi.real(), ci.real()); |
| 32 | + EXPECT_FLOAT_EQ(vi.imag(), ci.imag()); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +TEST(mathMixMatFun, conj_vectorized) { |
| 37 | + using d_t = double; |
| 38 | + using v_t = stan::math::var; |
| 39 | + using fd_t = stan::math::fvar<d_t>; |
| 40 | + using ffd_t = stan::math::fvar<fd_t>; |
| 41 | + using fv_t = stan::math::fvar<v_t>; |
| 42 | + using ffv_t = stan::math::fvar<fv_t>; |
| 43 | + |
| 44 | + test_vectorized_conj<d_t>(); |
| 45 | + test_vectorized_conj<v_t>(); |
| 46 | + test_vectorized_conj<fd_t>(); |
| 47 | + test_vectorized_conj<ffd_t>(); |
| 48 | + test_vectorized_conj<fv_t>(); |
| 49 | + test_vectorized_conj<ffv_t>(); |
8 | 50 | } |
0 commit comments