diff --git a/.vs/3dMath/v15/.suo b/.vs/3dMath/v15/.suo index 33c6da6..769db18 100644 Binary files a/.vs/3dMath/v15/.suo and b/.vs/3dMath/v15/.suo differ diff --git a/.vs/3dMath/v15/Browse.VC.opendb b/.vs/3dMath/v15/Browse.VC.opendb deleted file mode 100644 index f2a154f..0000000 Binary files a/.vs/3dMath/v15/Browse.VC.opendb and /dev/null differ diff --git a/GMath.cpp b/GMath.cpp index 8c3566e..50e40c4 100644 --- a/GMath.cpp +++ b/GMath.cpp @@ -1022,7 +1022,7 @@ int Nullity(const GMatrix & m) return m.GetColNum() - rank; } -GMatrix Mij(const GMatrix & m, int rom, int col) +GMatrix MijMatrix(const GMatrix & m, int rom, int col) { int i,j = 0; int r = m.GetRowNum(); @@ -1055,16 +1055,17 @@ GMatrix Mij(const GMatrix & m, int rom, int col) return ret; } -GMatrix Cij(const GMatrix & m, int r, int c) +float Mij(const GMatrix & m, int r, int c) { - GMatrix ret = Mij(m, r, c); + GMatrix M = MijMatrix(m, r, c); + return Det(M); +} + +float Cij(const GMatrix & m, int r, int c) +{ + GMatrix M = MijMatrix(m, r, c); int t = pow(-1, r + c); - if (t == -1) { - for (int i = 0; i < ret.GetColNum(); i++) { - ret[0][i] = -ret[0][i];//行列式运算的-号只能影响一行 - } - } - return ret; + return t*Det(M); } float Det(const GMatrix & m) @@ -1083,17 +1084,42 @@ float Det(const GMatrix & m) } else { for (i = 0; i < c; i++) { - //GMatrix t = Mij(m, 0, i); - //ret += pow(-1, 0+ i) * m[0][i] * Det(t); - GMatrix t = Cij(m, 0, i); - //cout << t << endl; - ret += m[0][i] * Det(t); + GMatrix t = MijMatrix(m, 0, i); + ret += pow(-1, 0+ i) * m[0][i] * Det(t); } } return ret; } +GMatrix Inverse(const GMatrix & m) +{ + GMatrix ret(m); + ret.SetInverse(); + return ret; +} + +GMatrix Transpose(const GMatrix & m) +{ + GMatrix ret(m); + ret.SetTranspose(); + return ret; +} + +GMatrix Adjoint(const GMatrix & m) +{ + GMatrix tM = Transpose(m); + GMatrix ret(tM.r, tM.c); + + int i, j = 0; + for (i = 0; i < tM.r; i++) { + for (j = 0; j < tM.c; j++) { + ret[i][j] = Cij(tM, i, j); + } + } + return ret; +} + GLine::GLine(const GPoint3 & _p, const GVector3 & _v) { p = _p; diff --git a/GMath.h b/GMath.h index 0ba5737..ec8d62e 100644 --- a/GMath.h +++ b/GMath.h @@ -200,9 +200,13 @@ public: friend int Rank(const GMatrix& m);//获取矩阵的秩 friend int Nullity(const GMatrix& m);//获取矩阵的零化度 - friend GMatrix Mij(const GMatrix& m, int r, int c); //计算余子式矩阵 - friend GMatrix Cij(const GMatrix& m, int r, int c); //计算代数余子式矩阵 + friend GMatrix MijMatrix(const GMatrix& m, int r, int c); //计算余子式矩阵 + friend float Mij(const GMatrix& m, int r, int c); //计算余子式 + friend float Cij(const GMatrix& m, int r, int c); //计算代数余子式矩阵的行列式值 friend float Det(const GMatrix& m); + friend GMatrix Inverse(const GMatrix& m); + friend GMatrix Transpose(const GMatrix& m); + friend GMatrix Adjoint(const GMatrix& m);//求伴随矩阵 private: int r; diff --git a/main.cpp b/main.cpp index c5e531f..c638222 100644 --- a/main.cpp +++ b/main.cpp @@ -2,20 +2,30 @@ int main() { - float m1[] = { - 2, 3, 8, 4, - 6, 0, -3, 8, - -1, 3, 2, 9, - 2, 0, 4, 10 + float a[] = { + 0, 0, 0, 2, + 0, 0, 2, 1, + 0, 2, 1, 2, + 2, 1, 2, 3 }; + GMatrix A(4, 4, a); + //cout << B*A << endl; + cout << Det(A) << endl; - GMatrix M1(4, 4, m1); - GMatrix M2(4, 4, m1); + /*float a[] = { + 1,0, + 1,4 + }; + GMatrix A(2, 2, a); - M1.SetInverse(); - cout << M1 << endl; + float c[] = { + 0,1, + 1,0 + }; + GMatrix C(2, 2, c); + cout << A*A << endl;*/ - return 0; + return 0; } \ No newline at end of file