Infinite size matrix design.

xiaoxiao2021-03-05  22

This long time in learning kinetics, encounter a very tricky problem - the calculation of contact force, that is, for a certain point of one object with another problem (VR = (V1 - V2) points N) For the force of 0 or near 0, it is necessary to use a matrix of random rows and columns (I call it Bigmatrix), and Microsoft's DXSDK's math library does not provide a matrix greater than 4x4, so I decide yourself. Some efforts, complete it, now I put the source code, I hope to help everyone in the future!

Bigmatrix is ​​a class, =, - =, * = and / = a member function of BigmaTrix, and overloaded, returns a reference! And , -, *, / is non-member function, these operators are not Overload, the reason is due to the defect of C itself, in this case, only return values, can not be a pointer or reference, because , -, *, / as a member function, return pointer or reference can only be Pointer or reference for local variables, this is a serious error (why please see more about C books for more understanding) and return value to call the copy constructor, this efficiency is very low, so I use one more here. Contains functions of the return value, such as MatrixMultiply, in fact Microsoft is really doing this in the DXSDK math library!!

The code is as follows: Two files, one is matrix.cpp, the other is Matrix.h.

//Matrix.cpp//#include "matrix.h"

BigMatrix :: Bigmatrix (int IROW, IC iColumn) {m_IROW = IROW; m_IColumn = iColumn; // Now defines an array m_f_pmatrix = new float * [iRow]; for (int I = 0; i

For (int i = 0; i

/ / Now define an array /*matrix.resize (I = 0; i

} BigMatrix :: ~ Bigmatrix (Void) {for (int i = 0; i getRow (); // can only be a square inverse, only need;

/ / Establish a Row X ROW unit matrix, used to store the result float ** f_presult = new float * [iRow]; for (int i = 0; i

// Initialization unit matrix; for (int i = 0; i m_f_pmatrix [i] [i]; if (pivot! = 1) {for (int K = 0; k m_f_pmatrix [i] [k] / = pivot; f_presult [ I] [k] / = pivot;}} for (int J = 0; j m_f_pmatrix [j] [i]; if (pivot! = 0 && j! = I) {for (int K = 0; k m_f_pmatrix [j] [k] = pm-> m_f_pmatrix [j] [k] - pm-> m_f_pmatrix [i] [k] * pivot; f_presult [j] [k] = f_presult [ J] [k] - f_presult [i] [k] * pivot;}}}} for (int i = 0; i m_f_pmatrix [i] [j] = f_presult [i] [j];}} for (int i = 0; i

Return pout;}

Bigmatrix * MatrixTranspose (Bigmatrix * Pout, Const BigmaTrix * PM) {float ** f_ptemp = new float * [PM-> getColumn ()]; for (int i = 0; i getColumn (); i ) f_ptemp [i] = new float [PM-> getRow ()];

For (int i = 0; i getRow (); i ) {for (int J = 0; j getColumn (); j ) {f_ptemp [j] [i] = PM-> m_f_pmatrix [i] [j];}}

For (int i = 0; i getrow (); i ) {for (int J = 0; j getColumn (); j ) {pout-> m_f_pmatrix [i] [ J] = f_ptemp [i] [j];}}

Return pout;}

Bigmatrix * MatrixAdd (Bigmatrix * Pout, Const Bigmatrix * PM1, Const Bigmatrix * PM2) {for (INT i = 0; I getRow (); i ) {for (int J = 0; J getColumn (); j ) {pout-> m_f_pmatrix [i] [j] = pm1-> m_f_pmatrix [i] [j] pm2-> m_f_pmatrix [i] [j];}} return pout;

Bigmatrix * MatrixSubtract (Bigmatrix * Pout, Const Bigmatrix * PM1, Const BigmaTrix * PM2) {

For (int i = 0; I getRow (); i ) {for (int J = 0; j getColumn (); J ) {pout-> m_f_pmatrix [i] [ J] = PM1-> m_f_pmatrix [i] [j] - pm2-> m_f_pmatrix [i] [j];}} return pout;

Bigmatrix * Matrixmultiply (Bigmatrix * POUT, Const Bigmatrix * PM1, Const Bigmatrix * PM2) {for (int i = 0; i getRow (); i ) {for (int J = 0; J getColumn (); j ) {for (int K = 0; k getColumn (); k ) {pout-> m_f_pmatrix [i] [j] = pm1-> m_f_pmatrix [i ] [K] * PM2-> m_f_pmatrix [k] [j];}

}

Return pout;}

///Matrix.h///matr @ include using namespace std; class bigmatrix {public: bigmatrix (int rt irow, int icolumn); ~ Bigmatrix (void); float ** m_f_pmatrix; / * vector < vector > Matrix; * / BigMatrix & operator = (BigMatrix cM); BigMatrix & operator - = (BigMatrix cM); BigMatrix & operator * = (float fMultiplicand); BigMatrix & operator / = (float fMultiplicand); inline int GetRow (void ) Const {return m_irow;} inline int getColumn (void) const {return m_IColumn;}

PRIVATE: INT M_IROW, M_ICOLUMN;

}

BigMatrix * MatrixInverse (BigMatrix * pOut, const BigMatrix * pM); BigMatrix * MatrixTranspose (BigMatrix * pOut, const BigMatrix * pM); BigMatrix * MatrixAdd (BigMatrix * pOut, const BigMatrix * pM1, const BigMatrix * pM2); BigMatrix * MatrixSubtract (Bigmatrix * Punt, Const Bigmatrix * PM1, Const BigmaTrix * PM2); Bigmatrix * Matrixmultiply (Bigmatrix * Pout, Const Bigmatrix * PM1, Const Bigmatrix * PM2); Execute Demo Results:

转载请注明原文地址:https://www.9cbs.com/read-37735.html

New Post(0)