Skip to content
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

added transpose of matrix with the same object with tests #13

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions MatrixAlgorithms/Matrix2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ std::ostream& operator<<(std::ostream& os, const Matrix2d& matrix)
}

return os;
}
void Matrix2d::transpose()
{
size_t cols = getCols();
size_t rows = getRows();
std::vector<double> vd = toVector();
std::vector<std::vector<double>> mat_t;
for (int i = 0; i < cols; i++)
{
std::vector<double> row_t;
for (int j = 0; j < rows; j++)
{
double cell = getValue(j, i);
row_t.push_back(cell);
}
mat_t.push_back(row_t);
}
this->numbersArray = mat_t;
}
1 change: 1 addition & 0 deletions MatrixAlgorithms/Matrix2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Matrix2d
std::size_t getRows() const;
std::size_t getCols() const;
std::vector<double> toVector() const;
void transpose();

private:
/*
Expand Down
11 changes: 11 additions & 0 deletions MatrixAlgorithms/MatrixAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ void TEST_018_operations_chain()
Matrix2d result = (mat1 - mat2) * mat3;
assert(result == expectedResult1);
}
void TEST_019_transpose()
{
Matrix2d mat1 = {{2, 3, 4, 8}, {5, 6, 7, 9}};
mat1.transpose();
std::vector<double> expected = {2, 5, 3, 6, 4, 7, 8, 9};
assert(mat1.toVector() == expected);
Matrix2d mat2 = {{7}, {9}, {2}};
std::vector<double> expectedResult2 = {7, 9, 2};
assert(mat2.toVector() == expectedResult2);
}

int main()
{
Expand All @@ -346,4 +356,5 @@ int main()
TEST_016_subtraction();
TEST_017_subtraction_2();
TEST_018_operations_chain();
TEST_019_transpose();
}