Skip to content

Commit

Permalink
Merge branch 'feature_rework_getRow_getCol' into feature_evolve_method
Browse files Browse the repository at this point in the history
  • Loading branch information
Grufoony committed Dec 14, 2023
2 parents d8c3f88 + 0e15b0c commit f8c8264
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/dsm/headers/SparseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ namespace dsm {
/// @param index row index
/// @return a row vector
/// @throw std::out_of_range if the index is out of range
SparseMatrix getRow(Index index) const;
SparseMatrix getRow(Index index, bool keepIndex = false) const;

/// @brief get a column as a column vector
/// @param index column index
/// @return a column vector
/// @throw std::out_of_range if the index is out of range
SparseMatrix getCol(Index index) const;
SparseMatrix getCol(Index index, bool keepIndex = false) const;

/// @brief get a matrix of double with every row normalized to 1
/// @return a matrix of double
Expand Down Expand Up @@ -544,33 +544,39 @@ namespace dsm {

template <typename Index, typename T>
requires std::unsigned_integral<Index>
SparseMatrix<Index, T> SparseMatrix<Index, T>::getRow(Index index) const {
SparseMatrix<Index, T> SparseMatrix<Index, T>::getRow(Index index, bool keepIndex) const {
if (index >= _rows) {
std::string errorMsg{"Error at line " + std::to_string(__LINE__) + " in file " + __FILE__ + ": " +
"Index out of range"};
throw std::out_of_range(errorMsg);
}
SparseMatrix row(1, _cols);
if (keepIndex) {
row.reshape(_rows, _cols);
}
for (auto& it : _matrix) {
if (it.first / _cols == index) {
row.insert(it.first % _cols, it.second);
keepIndex ? row.insert(it.first, it.second) : row.insert(it.first % _cols, it.second);
}
}
return row;
}

template <typename Index, typename T>
requires std::unsigned_integral<Index>
SparseMatrix<Index, T> SparseMatrix<Index, T>::getCol(Index index) const {
SparseMatrix<Index, T> SparseMatrix<Index, T>::getCol(Index index, bool keepIndex) const {
if (index >= _cols) {
std::string errorMsg{"Error at line " + std::to_string(__LINE__) + " in file " + __FILE__ + ": " +
"Index out of range"};
throw std::out_of_range(errorMsg);
}
SparseMatrix col(_rows, 1);
if (keepIndex) {
col.reshape(_rows, _cols);
}
for (auto& it : _matrix) {
if (it.first % _cols == index) {
col.insert(it.first / _cols, it.second);
keepIndex ? col.insert(it.first, it.second) : col.insert(it.first / _cols, it.second);
}
}
return col;
Expand Down
44 changes: 42 additions & 2 deletions test/Test_sparsematrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ TEST_CASE("Boolean Matrix") {
CHECK_THROWS(m.getRow(-1));
CHECK_THROWS(m.getRow(4));
}
SUBCASE("Get row") {
SUBCASE("getRow") {
/*This test tests if the getRow function works correctly
The getRow function should return a vector containing the elements of the
row
Expand All @@ -193,6 +193,26 @@ TEST_CASE("Boolean Matrix") {
CHECK(!row(1));
CHECK(row(2));
}
SUBCASE("getRow with keepIndex true") {
/*This test tests if the getRow function works correctly
The getRow function should return a vector containing the elements of the
row
GIVEN: the getRow function is called
WHEN: the function is called on a matrix
THEN: the function should return a vector (SparseMatrix) containing the
elements of the row
*/
SparseMatrix<uint32_t, bool> m(3, 3);
// Create a row
m.insert(0, 0, true);
m.insert(0, 2, true);
auto row = m.getRow(0, true);
// verify attributes and values
CHECK(row.size() == 2);
CHECK(row(0));
CHECK(!row(1));
CHECK(row(2));
}
SUBCASE("getColumn - exceptions") {
/*This test tests if the getColumn function throws exceptions correctly
The getColumn function should throw an exception if the column is out of
Expand All @@ -204,7 +224,7 @@ TEST_CASE("Boolean Matrix") {
SparseMatrix<uint32_t, bool> m(3, 6);
CHECK_THROWS(m.getCol(6));
}
SUBCASE("Get column") {
SUBCASE("getCol") {
/*This test tests if the getCol function works correctly
The getCol function should return a vector containing the elements of the
column
Expand All @@ -224,6 +244,26 @@ TEST_CASE("Boolean Matrix") {
CHECK(!col(1));
CHECK(col(2));
}
SUBCASE("getCol with keepIndex true") {
/*This test tests if the getCol function works correctly
The getCol function should return a vector containing the elements of the
column
GIVEN: the getCol function is called
WHEN: the function is called on a matrix
THEN: the function should return a vector (SparseMatrix) containingthe
elements of the column
*/
SparseMatrix<uint32_t, bool> m(3, 3);
// Create a column
m.insert(0, 0, true);
m.insert(2, 0, true);
auto col = m.getCol(0, true);
// verify attributes and values
CHECK(col.size() == 2);
CHECK(col(0));
CHECK(!col(1));
CHECK(col(6));
}
SUBCASE("Get row dimension") {
/*This test tests if the getRowDim function works correctly
The getRowDim function should return the number of rows in the matrix
Expand Down

0 comments on commit f8c8264

Please sign in to comment.