# LibMatrix:矩阵代数库(第一部分)

11 四月 2016, 15:29
0
1 136

### 1.库结构

• 一般数学函数（MathLerpMathInRangeRandomMathDoublesEqual
• 用于处理矩阵的辅助函数（MatrIndiciesToOffsetMatrCopy, MatrSetSizeMatrResize
• 用于处理行和列的函数（MatrSwapRowsMatrSwapColsMatrCopyRowMatrCopyCol, MatrRowIsZeroMatrColIsZero
• 用于按内容类型检查条件的函数（MatrIsZeroMatrIsDiagonalMatrIsIdentityMatrIsSymmetricMatrIsAntisymmetricMatrEqual
• 其他函数（MatrGaussianEliminationMatrGJBatchSolveMatrMinorMatrAlgebraicComplementMatrInvertUsingMinorsMatrInvertUsingGJMatrDetMatrDetTriangMatrRankMatrRankTriangMatrComputeConnectedMatrMatrLerpMatr

### 2.函数说明

2.1.一般数学函数

`double MathLerp(double rangeLowLimit, double rangeHighLimit, double balance);`

`double MathInRangeRandom(double rangeLowLimit, double rangeHighLimit);`

`bool MathDoublesEqual(double value1, double value2, double tolerance);`

2.2.用于处理矩阵的辅助函数

`int MatrIndiciesToOffset(int row, int col, int numRows, int numCols)`

`void MatrCopy(double& src[], double& dst[]);`

`void MatrSetSize(double& matr[], int numRows, int numCols);`

`void MatrResize(double& matr[], int numRowsOld, int numColsOld, int numRowsNew, int numColsNew);`

2.3.用于处理行和列的函数

`void MatrSwapRows(double& matr[], int numRows, int numCols, int row1, int row2);`

`void MatrSwapCols(double& matr[], int numRows, int numCols, int col1, int col2);`

`void MatrCopyRow(double& matr[], int numRows, int numCols, int rowToCopy, int rowToReplace);`

`void MatrCopyCol(double& matr[], int numRows, int numCols, int colToCopy, int colToReplace);`

`bool MatrRowIsZero(double& matr[], int numRows, int numCols, int row, double tolerance);`

`bool MatrColIsZero(double& matr[], int numRows, int numCols, int col, double tolerance);`

2.4.用于常规检查条件的函数

`bool MatrIsSquare(int numRows, int numCols);`

`bool MatrIsElemOnMainDiagonal(int row, int col);`

`bool MatrCompatiblityCheckAdd(int numRows1, int numCols1, int numRows2, int numCols2);`

`bool MatrCompatiblityCheckMul(int numRows1, int numCols1, int numRows2, int numCols2);`

2.5.矩阵内容初始化函数

`void MatrLoadZero(double& matr[], int numRows, int numCols);`

`void MatrLoadIdentity(double& matr[], int numRows, int numCols);`

`void MatrLoadInRangeRandom(double& matr[], int numRows, int numCols, double rangeLowLimit, double rangeHighLimit);`

2.6.用于按内容类型检查条件的函数

`bool MatrIsZero(double& matr[], int numRows, int numCols, double tolerance);`

`bool MatrIsDiagonal(double& matr[], int numRows, int numCols, double tolerance);`

`bool MatrIsIdentity(double& matr[], int numRows, int numCols, double tolerance);`

`bool MatrIsSymmetric(double& matr[], int numRows, int numCols, double tolerance);`

`bool MatrIsAntisymmetric(double& matr[], int numRows, int numCols, double tolerance);`

`bool MatrEqual(double& matr1[], double& matr2[], int numRows, int numCols, double tolerance);`

2.7.用于元素级标量运算的函数

`void MatrAddScalar(double& matr[], int numRows, int numCols, double scalar, double& result[]);`

`void MatrSubScalar(double& matr[], int numRows, int numCols, double scalar, double& result[]);`

`void MatrMulByScalar(double& matr[], int numRows, int numCols, double scalar, double& result[]);`

`void MatrDivByScalar(double& matr[], int numRows, int numCols, double scalar, double& result[]);`

2.8.用于基本矩阵运算的函数

`void MatrAddMatr(double& matr1[], double& matr2[], int numRows, int numCols, double& result[]);`

`void MatrSubMatr(double& matr1[], double& matr2[], int numRows, int numCols, double& result[]);`

```void MatrMulMatr(
double& matr1[], int numRows1, int numCols1,
double& matr2[], int numRows2, int numCols2,
double& result[], int& numRowsRes, int& numColsRes);```

`double MatrTrace(double& matr[], int numRows, int numCols);`

`void MatrTranspose(double& matr[], int numRows, int numCols, double& result[], int& numRowsRes, int& numColsRes);`

2.9.其他函数

`int MatrGaussianElimination(double& matr[], int numRows, int numCols, double& result[], double tolerance);`

```bool MatrGJBatchSolve(
double& matr[], int numRows, int numCols,
double& rhs[], int numRowsRHS, int numColsRHS,
double& roots[], int& numRowsRoots, int& numColsRoots,
double tolerance
);```

```void MatrMinor(
double& matr[], int numRows, int numCols,
int rowToExclude, int colToExclude,
double& result[], int& numRowsRes, int& numColsRes);```

`double MatrAlgebraicComplement(double& matr[], int numRows, int numCols, int elemRow, int elemCol, double tolerance);`

`bool MatrInvertUsingMinors(double& matr[], int numRows, int numCols, double& result[], double tolerance);`

`bool MatrInvertUsingGJ(double& matr[], int numRows, int numCols, double& result[], double tolerance);`

`double MatrDet(double& matr[], int numRows, int numCols, double tolerance);`

`double MatrDetTriang(double& matr[], int numRows, int numCols);`

`double MatrRank(double& matr[], int numRows, int numCols, double tolerance);`

`double MatrRankTriang(double& matr[], int numRows, int numCols, double tolerance);`

`void MatrComputeConnectedMatr(double& matr[], int numRows, int numCols, double& result[], double tolerance);`

`void MatrLerpMatr(double& matr1[], double& matr2[], int numRows, int numCols, double balance, double& result[]);`

2.10.输入/输出函数

`void MatrPrint(double& matr[], int numRows, int numCols);`

`void FileWriteMatr(double& matr[], int numRows, int numCols, int handle);`

`void FileReadMatr(double& matr[], int& numRows, int& numCols, int handle);`

### 3.使用用例

```// creating polynomial regression bool Regression(double& x[], double& y[], int numPoints, int polyDegree, double& poly[]) {
// create system matrix    double A[];
int numRowsA = polyDegree + 1;
int numColsA = polyDegree + 1;
MatrSetSize(A, numRowsA, numColsA);
// fill the matrix    for (int row = 0; row < numRowsA; row++) {
for (int col = 0; col < numColsA; col++) {
int offset = MatrIndiciesToOffset(row, col, numRowsA, numColsA);
A[offset] = SXY(x, y, numPoints, row + col, 0);
}
}
// create a right hand side vector    double B[];
int numRowsB = polyDegree + 1;
int numColsB = 1;
MatrSetSize(B, numRowsB, numColsB);
// fill the right hand side vector    for (row = 0; row < numRowsB; row++) {
offset = MatrIndiciesToOffset(row, 0, numRowsB, numColsB);
B[offset] = SXY(x, y, numPoints, row, 1);
}
// solve a system of linear algebraic equations    int numRowsX, numColsX;
bool status =
MatrGJBatchSolve(
A, numRowsA, numColsA,
B, numRowsB, numColsB,
poly, numRowsX, numColsX,
DEFAULT_TOLERANCE
);
if (!status) {
Print("Error solving the system");
}
return (status); }```

### 总结

LibMatrix.zip (14.03 KB)
Chuvashov 的三角形机械交易系统