Skip to content

Commit 3172588

Browse files
committed
adds tests for x + x.transpose
1 parent 4c5f2e5 commit 3172588

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

test/unit/math/mix/core/operator_addition_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,18 @@ TEST(mathMixCore, operatorAdditionMatrixFailures) {
139139
stan::test::expect_ad_matvar(tols, f, u, vv);
140140
stan::test::expect_ad_matvar(tols, f, rvv, u);
141141
}
142+
TEST(mathMixCore, operatorAdditionMatrixLinearAccess) {
143+
Eigen::MatrixXd matrix_m11(3, 3);
144+
for (Eigen::Index i = 0; i < matrix_m11.size(); ++i) {
145+
matrix_m11(i) = i;
146+
}
147+
stan::math::var_value<Eigen::MatrixXd> A(matrix_m11);
148+
stan::math::var_value<Eigen::MatrixXd> B = stan::math::add(A, A.transpose());
149+
B.adj()(2, 0) = 1;
150+
stan::math::grad();
151+
Eigen::MatrixXd expected_adj = Eigen::MatrixXd::Zero(3, 3);
152+
expected_adj(2, 0) = 1;
153+
expected_adj(0, 2) = 1;
154+
EXPECT_MATRIX_FLOAT_EQ(A.adj(), expected_adj);
155+
stan::math::recover_memory();
156+
}

test/unit/math/mix/core/operator_subtraction_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,20 @@ TEST(mathMixCore, operatorSubtractionMatrixFailures) {
143143
stan::test::expect_ad_matvar(tols, f, u, vv);
144144
stan::test::expect_ad_matvar(tols, f, rvv, u);
145145
}
146+
147+
148+
TEST(mathMixCore, operatorSubtractionMatrixLinearAccess) {
149+
Eigen::MatrixXd matrix_m11(3, 3);
150+
for (Eigen::Index i = 0; i < matrix_m11.size(); ++i) {
151+
matrix_m11(i) = i;
152+
}
153+
stan::math::var_value<Eigen::MatrixXd> A(matrix_m11);
154+
stan::math::var_value<Eigen::MatrixXd> B = stan::math::subtract(A, A.transpose());
155+
B.adj()(2, 0) = 1;
156+
stan::math::grad();
157+
Eigen::MatrixXd expected_adj = Eigen::MatrixXd::Zero(3, 3);
158+
expected_adj(2, 0) = 1;
159+
expected_adj(0, 2) = -1;
160+
EXPECT_MATRIX_FLOAT_EQ(A.adj(), expected_adj);
161+
stan::math::recover_memory();
162+
}

0 commit comments

Comments
 (0)