📄 gmatrix.h
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#ifndef __GMATRIX_H__#define __GMATRIX_H__class GVector;// A two-dimensional matrix. Elements are zero-indexed (not one-indexed like most math textbooks).class GMatrix{protected: double* m_pData; int m_nRows; int m_nColumns;public: GMatrix(int nRows = 0, int nColumns = 0); virtual ~GMatrix();#ifndef NO_TEST_CODE static void Test();#endif // !NO_TEST_CODE // Get an element inline double Get(int nRow, int nColumn) const { return m_pData[nRow * m_nColumns + nColumn]; } // Set an element inline void Set(int nRow, int nColumn, double dValue) { m_pData[nRow * m_nColumns + nColumn] = dValue; } // Returns the number of columns in the matrix int GetColumnCount() const { return m_nColumns; } // Returns the number of rows in the matrix int GetRowCount() const { return m_nRows; } // Sets the diagonals to 1 and all other elements to 0 void SetToIdentity(); // Rotates the matrix around the diagonal void Transpose(); // Resizes this matrix void Resize(int nRows, int nColumns); // Makes a deep copy of pMatrix void Copy(const GMatrix* pMatrix); // Multiplies this matrix by a scalar value void Multiply(double dScalar); // Multiplies a vector by this matrix. The input vector must have // the same number of elements as columns in the matrix. The output // vector will have the same number of elements as rows in the matrix. void Multiply(const double* pVectorIn, double* pVectorOut); // Returns pA * pB void Multiply(const GMatrix* pA, const GMatrix* pB); // Returns the vector that is the nRow'th row of the matrix double* GetRow(int nRow) { return &m_pData[nRow * m_nColumns]; } // Copies a column into the provided vector void GetColumn(int nCol, double* pVector); // Computes the average value in the specified column double ComputeColumnMean(int nCol) const; // Computes the sum of the values in the specified column double ComputeColumnSum(int nCol) const; // Dumps a representation of the matrix to stdout void Print(); // Dumps a partial representation of the matrix to stdout. // typically you will select a small number for n, like 2 or 3 void PrintCorners(int n); // Returns the sum of the diagonal values in the matrix double ComputeTrace(); void Solve(double* pVector); // Computes the first nCount eigenvectors of pInputMatrix void ComputeEigenVectors(int nCount, const GMatrix* pInputMatrix);
// Computes eigenvalues from the eigenvectors
void ComputeEigenValues(double* pOutValues, GMatrix* pEigenVectors);
int CountNonZeroElements(); // Returns the sum squared difference of all the elements in this matrix // with the corresponding elements in other. The matrixes must be the same size. double ComputeSumSquaredDifference(GMatrix* other); // This computes the square root of pIn. It returns true if successful. // It returns false if pIn is not positive definate. If you take the // matrix that this returns and multiply it by its transpose, you should get // pIn again. bool Cholesky(const GMatrix* pIn);};#endif // __GMATRIX_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -