Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit 01580bb

Browse files
committed
finish merge of inverse.
2 parents bbae17f + 13a1ce1 commit 01580bb

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Matrix.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ namespace Mat_pro {
220220

221221
T determinant() const;
222222

223+
Matrix<T> inverse() const;//
224+
223225
Matrix<T> convolution(const Matrix<T> &kernel, int32_t padding = 0, int32_t stride = 1) const;
224226

225227
Matrix<T>
@@ -713,6 +715,30 @@ namespace Mat_pro {
713715
return determinant_in(this->vec);
714716
}
715717

718+
template<typename T>
719+
Matrix<T> Matrix<T>::inverse() const {
720+
if (this->is_square() && this->rows() > 1 && this->determinant() != 0) {//可逆条件
721+
vector<vector<T>> res(this->rows(), vector<T>(this->cols(), static_cast<T>(0)));
722+
for (int32_t i = 0; i < this->rows(); i++) {
723+
for (int32_t j = 0; j < this->cols(); j++) {
724+
//vector<vector<T>> submatrix(this->rows() - 1, vector<T>(this->cols() - 1, static_cast<T>(0)));
725+
vector<vector<T>> submatrix(this->vec);
726+
submatrix.erase(submatrix.begin() + i);
727+
for (int m = 0; m < this->rows() - 1; ++m) {
728+
submatrix[m].erase(submatrix[m].begin() + j);
729+
}
730+
res[j][i] = (((i + j) % 2) ? -1 : 1) * determinant_in(submatrix);//子矩阵展开得到伴随矩阵
731+
}
732+
}
733+
Matrix<T> will_return(res);
734+
will_return = will_return / this->determinant();
735+
//will_return[i][j] = res[i][j] / this->determinant();//逆
736+
return will_return;
737+
} else if (this->is_square() && this->rows() == 1 && this->determinant() != 0) {
738+
return Matrix<T>::values(1, 1, static_cast<T>(1) / this->vec[0][0]);
739+
}
740+
return Matrix<T>(1, 1);
741+
}
716742

717743
template<typename T>
718744
Matrix<T> Matrix<T>::conj() const {

src/test_matrix_1.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,41 @@ TEST_CASE("eigenvector", "[test 5]") {
947947
cout << endl << "m3's eigenvectors:" << endl << m3.eigenvector();
948948
}
949949

950+
TEST_CASE("inverse", "[test 5]") {
951+
Matrix<double> m1 = {{2, 2},
952+
{1, 2}};
953+
auto temp = m1.inverse();
954+
Matrix<double> m2 = {{1, -1},
955+
{-0.5, 1}};
956+
Matrix<double> m3 = {{-2, 1, 1},
957+
{0, 2, 0},
958+
{-4, 1, 3}};
959+
auto temp2 = m3.inverse();
960+
Matrix<double> m4{{-1.5, 0.5, 0.5},
961+
{0, 0.5, 0},
962+
{-2, 0.5, 1}};
963+
Matrix<double> m5 = {{2, 0, 0, 0, 0, 0, 0, 0},
964+
{0, 2, 0, 0, 0, 0, 0, 0},
965+
{0, 0, 2, 0, 0, 0, 0, 0},
966+
{0, 0, 0, 2, 0, 0, 0, 0},
967+
{0, 0, 0, 0, 2, 0, 0, 0},
968+
{0, 0, 0, 0, 0, 2, 0, 0},
969+
{0, 0, 0, 0, 0, 0, 2, 0},
970+
{0, 0, 0, 0, 0, 0, 0, 2}};
971+
auto temp3 = m5.inverse();
972+
Matrix<double> m6 = {{0.5, 0, 0, 0, 0, 0, 0, 0},
973+
{0, 0.5, 0, 0, 0, 0, 0, 0},
974+
{0, 0, 0.5, 0, 0, 0, 0, 0},
975+
{0, 0, 0, 0.5, 0, 0, 0, 0},
976+
{0, 0, 0, 0, 0.5, 0, 0, 0},
977+
{0, 0, 0, 0, 0, 0.5, 0, 0},
978+
{0, 0, 0, 0, 0, 0, 0.5, 0},
979+
{0, 0, 0, 0, 0, 0, 0, 0.5}};
980+
Matrix<double> m7 = {2};
981+
auto temp4 = m7.inverse();
982+
Matrix<double> m8 = {0.5};
983+
CHECK(Matrix<double>::inside_equal(m1.inverse(), m2));
984+
CHECK(Matrix<double>::inside_equal(m3.inverse(), m4));
985+
CHECK(Matrix<double>::inside_equal(m5.inverse(), m6));
986+
CHECK(Matrix<double>::inside_equal(m7.inverse(), m8));
987+
}

0 commit comments

Comments
 (0)