目前可以实现矩阵相加减,矩阵相乘,矩阵求逆以及行列式求值,离散数学求解关系闭包的功能,希望能帮助大家。这些算法的实现都在网盘的文件中。
例如:矩阵求逆算法
template <class T>
Status Matrix_Inversion(TriSparseMatrix<T>& tsma, TriSparseMatrix<T>& result)
{
T* p1 = tsma.Unseal(); double determinant_value=numarray_determinant_value(p1,tsma.rows);
if (determinant_value == 0)
{
return FAIL;
}
double Algebraic_cofactor;
T* inversion = new T[tsma.rows * tsma.cols]; T* sondet = new T[(tsma.rows - 1) * (tsma.cols - 1)];
result.rows = tsma.rows; result.cols = tsma.cols; delete[] result.triElems; result.maxSize = DEFAULT_SIZE;
result.triElems = new Triple<T>[result.maxSize]; result.num = 0;
for (int i = 0; i < tsma.rows; i++)
{
for (int j = 0; j < tsma.cols; j++)
{
int ct = 0;
for (int k = 0; k < tsma.rows; k++)
{
for (int l = 0; l < tsma.cols; l++)
{
if (k == i || l == j){continue;}
else
{
*(sondet + ct) = *(p1 + k * tsma.cols + l); ct++;
}
}
}
Algebraic_cofactor = numarray_determinant_value(sondet, tsma.rows - 1);
*(inversion + j * tsma.cols + i) = Algebraic_cofactor * pow(-1.0, (i + j + 2));
}
}
for (int i = 0; i < tsma.rows; i++)
{
for (int j = 0; j < tsma.cols; j++)
{
cout << *(inversion + i * tsma.cols + j) << " ";
}
cout << endl;
}
cout<< determinant_value <<endl;
for (int i = 0; i < tsma.rows; i++)
{
for (int j = 0; j < tsma.cols; j++)
{
*(inversion + i * tsma.cols + j)= *(inversion + i * tsma.cols + j)*(1.00/ determinant_value);
if (*(inversion + i * tsma.cols + j) != 0)
{
result.SetElem(i,j, *(inversion + i * tsma.cols + j));
}
}
}
delete[] p1; delete[] sondet; delete[] inversion; return SUCCESS;
}
例如矩阵求解行列式的算法(递归)
template<class T>
double numarray_determinant_value(T* deterarray,int n)//求行列式函数,运用递归思想
{
double detval = 0;
if (n == 1) return *(deterarray);
T* p_sondet = new T[(n - 1) * (n - 1)];
for (int i = 0; i < n; i++)
{
for (int j = 1; j < n; j++)
{
for (int k = 0; k < n ; k++)
{
if (k<i)
{
*(p_sondet + (j - 1) * (n - 1) + k) = *(deterarray + j * n + k);
}
else if (k == i) { continue; }
else
{
*(p_sondet + (j - 1) * (n - 1) + k-1) = *(deterarray + j * n + k);
}
}
}
detval = detval + *(deterarray + i) * pow(-1.0, i)* numarray_determinant_value(p_sondet,n-1);
}
delete[] p_sondet;
return detval;
}
网盘链接:https://pan.baidu.com/s/14ViVwyfVJeK9Xi-2rIBR8g
提取码:0625