-
-
Notifications
You must be signed in to change notification settings - Fork 206
Add matrix/vector array reshaping helpers #3312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fatimmajumder
wants to merge
2
commits into
stan-dev:develop
Choose a base branch
from
fatimmajumder:feat/reshape-functions-3233
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #ifndef STAN_MATH_PRIM_FUN_TO_ROW_VECTOR_ARRAY_HPP | ||
| #define STAN_MATH_PRIM_FUN_TO_ROW_VECTOR_ARRAY_HPP | ||
|
|
||
| #include <stan/math/prim/meta.hpp> | ||
| #include <stan/math/prim/fun/Eigen.hpp> | ||
| #include <vector> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
|
|
||
| /** | ||
| * Returns a standard vector of Eigen row vectors from the rows of the input | ||
| * matrix. | ||
| * | ||
| * @tparam EigMat type of the input matrix | ||
| * @param matrix input matrix | ||
| * @return the array of row vectors representation of the input | ||
| */ | ||
| template <typename EigMat, require_eigen_t<EigMat>* = nullptr> | ||
| inline std::vector<Eigen::Matrix<value_type_t<EigMat>, 1, Eigen::Dynamic>> | ||
| to_row_vector_array(const EigMat& matrix) { | ||
| using T = value_type_t<EigMat>; | ||
| std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic>> result; | ||
| result.reserve(matrix.rows()); | ||
| for (int i = 0; i < matrix.rows(); ++i) { | ||
| result.push_back(matrix.row(i)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace math | ||
| } // namespace stan | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #ifndef STAN_MATH_PRIM_FUN_TO_VECTOR_ARRAY_HPP | ||
| #define STAN_MATH_PRIM_FUN_TO_VECTOR_ARRAY_HPP | ||
|
|
||
| #include <stan/math/prim/meta.hpp> | ||
| #include <stan/math/prim/fun/Eigen.hpp> | ||
| #include <vector> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
|
|
||
| /** | ||
| * Returns a standard vector of Eigen column vectors from the columns of | ||
| * the input matrix. | ||
| * | ||
| * @tparam EigMat type of the input matrix | ||
| * @param matrix input matrix | ||
| * @return the array of column vectors representation of the input | ||
| */ | ||
| template <typename EigMat, require_eigen_t<EigMat>* = nullptr> | ||
| inline std::vector<Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, 1>> | ||
| to_vector_array(const EigMat& matrix) { | ||
| using T = value_type_t<EigMat>; | ||
| std::vector<Eigen::Matrix<T, Eigen::Dynamic, 1>> result; | ||
| result.reserve(matrix.cols()); | ||
| for (int j = 0; j < matrix.cols(); ++j) { | ||
| result.push_back(matrix.col(j)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace math | ||
| } // namespace stan | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include <stan/math/prim.hpp> | ||
| #include <test/unit/util.hpp> | ||
| #include <gtest/gtest.h> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| TEST(ToRowVectorArray, values) { | ||
| using stan::math::to_matrix; | ||
| using stan::math::to_row_vector_array; | ||
|
|
||
| Eigen::MatrixXd matrix(3, 2); | ||
| matrix << 1.1, 4.4, 2.2, 5.5, 3.3, 6.6; | ||
|
|
||
| auto result = to_row_vector_array(matrix); | ||
| ASSERT_EQ(3, result.size()); | ||
|
|
||
| Eigen::RowVectorXd expected_row_0(2); | ||
| expected_row_0 << 1.1, 4.4; | ||
| Eigen::RowVectorXd expected_row_1(2); | ||
| expected_row_1 << 2.2, 5.5; | ||
| Eigen::RowVectorXd expected_row_2(2); | ||
| expected_row_2 << 3.3, 6.6; | ||
|
|
||
| EXPECT_MATRIX_FLOAT_EQ(expected_row_0, result[0]); | ||
| EXPECT_MATRIX_FLOAT_EQ(expected_row_1, result[1]); | ||
| EXPECT_MATRIX_FLOAT_EQ(expected_row_2, result[2]); | ||
| EXPECT_MATRIX_FLOAT_EQ(matrix, to_matrix(result)); | ||
| } | ||
|
|
||
| TEST(ToRowVectorArray, emptyShapes) { | ||
| using stan::math::to_matrix; | ||
| using stan::math::to_row_vector_array; | ||
|
|
||
| Eigen::MatrixXd zero_by_zero(0, 0); | ||
| auto zero_by_zero_result = to_row_vector_array(zero_by_zero); | ||
| EXPECT_EQ(0, zero_by_zero_result.size()); | ||
|
|
||
| Eigen::MatrixXd zero_by_three(0, 3); | ||
| auto zero_by_three_result = to_row_vector_array(zero_by_three); | ||
| EXPECT_EQ(0, zero_by_three_result.size()); | ||
|
|
||
| Eigen::MatrixXd three_by_zero(3, 0); | ||
| auto three_by_zero_result = to_row_vector_array(three_by_zero); | ||
| ASSERT_EQ(3, three_by_zero_result.size()); | ||
| for (const auto& result : three_by_zero_result) { | ||
| EXPECT_EQ(0, result.size()); | ||
| } | ||
| EXPECT_MATRIX_FLOAT_EQ(three_by_zero, to_matrix(three_by_zero_result)); | ||
| } | ||
|
|
||
| TEST(ToRowVectorArray, preservesScalarType) { | ||
| using stan::math::to_row_vector_array; | ||
|
|
||
| Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> matrix(2, 2); | ||
| matrix << 1, 3, 2, 4; | ||
|
|
||
| auto result = to_row_vector_array(matrix); | ||
| static_assert(std::is_same<decltype(result)::value_type, | ||
| Eigen::Matrix<int, 1, Eigen::Dynamic>>::value, | ||
| "to_row_vector_array should preserve the matrix scalar type"); | ||
| ASSERT_EQ(2, result.size()); | ||
| EXPECT_EQ(1, result[0](0)); | ||
| EXPECT_EQ(3, result[0](1)); | ||
| EXPECT_EQ(2, result[1](0)); | ||
| EXPECT_EQ(4, result[1](1)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #include <stan/math/prim.hpp> | ||
| #include <test/unit/util.hpp> | ||
| #include <gtest/gtest.h> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| TEST(ToVectorArray, values) { | ||
| using stan::math::to_matrix; | ||
| using stan::math::to_vector_array; | ||
|
|
||
| Eigen::MatrixXd matrix(3, 2); | ||
| matrix << 1.1, 4.4, 2.2, 5.5, 3.3, 6.6; | ||
|
|
||
| auto result = to_vector_array(matrix); | ||
| ASSERT_EQ(2, result.size()); | ||
|
|
||
| Eigen::VectorXd expected_col_0(3); | ||
| expected_col_0 << 1.1, 2.2, 3.3; | ||
| Eigen::VectorXd expected_col_1(3); | ||
| expected_col_1 << 4.4, 5.5, 6.6; | ||
|
|
||
| EXPECT_MATRIX_FLOAT_EQ(expected_col_0, result[0]); | ||
| EXPECT_MATRIX_FLOAT_EQ(expected_col_1, result[1]); | ||
| EXPECT_MATRIX_FLOAT_EQ(matrix, to_matrix(result)); | ||
| } | ||
|
|
||
| TEST(ToVectorArray, emptyShapes) { | ||
| using stan::math::to_matrix; | ||
| using stan::math::to_vector_array; | ||
|
|
||
| Eigen::MatrixXd zero_by_zero(0, 0); | ||
| auto zero_by_zero_result = to_vector_array(zero_by_zero); | ||
| EXPECT_EQ(0, zero_by_zero_result.size()); | ||
|
|
||
| Eigen::MatrixXd zero_by_three(0, 3); | ||
| auto zero_by_three_result = to_vector_array(zero_by_three); | ||
| ASSERT_EQ(3, zero_by_three_result.size()); | ||
| for (const auto& result : zero_by_three_result) { | ||
| EXPECT_EQ(0, result.size()); | ||
| } | ||
| EXPECT_MATRIX_FLOAT_EQ(zero_by_three, to_matrix(zero_by_three_result)); | ||
|
|
||
| Eigen::MatrixXd three_by_zero(3, 0); | ||
| auto three_by_zero_result = to_vector_array(three_by_zero); | ||
| EXPECT_EQ(0, three_by_zero_result.size()); | ||
| } | ||
|
|
||
| TEST(ToVectorArray, preservesScalarType) { | ||
| using stan::math::to_vector_array; | ||
|
|
||
| Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> matrix(2, 2); | ||
| matrix << 1, 3, 2, 4; | ||
|
|
||
| auto result = to_vector_array(matrix); | ||
| static_assert(std::is_same<decltype(result)::value_type, | ||
| Eigen::Matrix<int, Eigen::Dynamic, 1>>::value, | ||
| "to_vector_array should preserve the matrix scalar type"); | ||
| ASSERT_EQ(2, result.size()); | ||
| EXPECT_EQ(1, result[0](0)); | ||
| EXPECT_EQ(2, result[0](1)); | ||
| EXPECT_EQ(3, result[1](0)); | ||
| EXPECT_EQ(4, result[1](1)); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WardBrian I think for
std::vector<Eigen::Matrix<T, Eigen::Dynamic, 1>>we need to have ato_row_matrixandto_col_matrixso that the user can tell us if the vectors in the array are the rows or columns. Do you have thoughts about this?