目标:
我做了什么:
这是行列式计算的代码
public BoolDK Determinant(BoolDK[,] Inputmatrix)
{
if (Inputmatrix.GetLength(1) == 1)
{
DeResult = Inputmatrix[0, 0];
return DeResult;
}
else
{
if (Inputmatrix.GetLength(1) == 2)
{
DeResult = (Inputmatrix[0, 0] * Inputmatrix[1, 1]) - (Inputmatrix[0, 1] * Inputmatrix[1, 0]);
return DeResult;
}
else
{
for (int i = 0; i < Inputmatrix.GetLength(1); i++)
{
BoolDK[,] InsideMatrix = new BoolDK[Inputmatrix.GetLength(1), Inputmatrix.GetLength(1)];
InsideMatrix = EliminateColumn(Inputmatrix, i);
BoolDK[,] InsideMatrix_2 = new BoolDK[Inputmatrix.GetLength(1), Inputmatrix.GetLength(1)];
InsideMatrix_2 = EliminateRow(InsideMatrix, 0);
if (i == 0)
{
DeResult = Inputmatrix[0, i] * Determinant(InsideMatrix_2);
}
else
{
if (Inputmatrix[0, i].MatrixElement != "0")
{
DeResult = DeResult + (Inputmatrix[0, i] * Determinant(InsideMatrix_2));
}
else
{
DeResult = DeResult;
}
}
}
}
return DeResult;
}
这是运算符重载的代码
public static BoolDK operator *(BoolDK op1, BoolDK op2)
{
BoolDK Result = new BoolDK();
if (op1.MatrixElement == "0" | op2.MatrixElement == "0")
{
Result.MatrixElement = "0";
}
else if (op1.MatrixElement == op2.MatrixElement)
{
Result.MatrixElement = "1";
}
else if (op1.MatrixElement == "1" & op1.MatrixElement != "1")
{
Result.MatrixElement = op2.MatrixElement;
}
else if (op1.MatrixElement != "1" & op1.MatrixElement != "\u00AC" & op2.MatrixElement == "1")
{
Result.MatrixElement = op1.MatrixElement;
}
else if ((op1.MatrixElement.Length + op2.MatrixElement.Length) == 2)
{
Result.MatrixElement = op1.MatrixElement + "*" + op2.MatrixElement;
}
else if (op2.MatrixElement.Contains("\u2299"))
{
Result.MatrixElement = op1.MatrixElement + "*" + op2.MatrixElement;
}
else if (op2.MatrixElement.Contains("\u2295"))
{
Result.MatrixElement = op1.MatrixElement + "*" + op2.MatrixElement;
}
else
{
if (!op2.MatrixElement.Contains("||"))
{
Result.MatrixElement = "(" + op1.MatrixElement + ")*(" + op2.MatrixElement + ")";
}
else
{
string[] temporarystring = op2.MatrixElement.Split(new string[] { "||" }, StringSplitOptions.None);
int Lengthoftemporarystring = temporarystring.Length;
string[] temporarystring2 = new string[Lengthoftemporarystring];
for (int i = 0; i < Lengthoftemporarystring; i++)
{
temporarystring2[i] = op1.MatrixElement + "*" + temporarystring[i];
}
String Stringresult;
String Sep = "||";
Stringresult = String.Join(Sep, temporarystring2);
Result.MatrixElement = Stringresult;
}
}
return Result;
}//Operator Overloading
public static BoolDK operator -(BoolDK op1, BoolDK op2)
{
BoolDK Result = new BoolDK();
if (op1.MatrixElement != "0" & op2.MatrixElement != "0")
{
Result.MatrixElement = "(" + op1.MatrixElement + ")||(" + op2.MatrixElement + ")";
}
else if (op1.MatrixElement != "0" & op2.MatrixElement == "0")
{
Result = op1;
}
else if (op1.MatrixElement == "0" & op2.MatrixElement != "0")
{
Result.MatrixElement = "(" + op1.MatrixElement + ")||(" + op2.MatrixElement + ")";
}
else if (op1.MatrixElement == "0" & op2.MatrixElement == "0")
{
Result.MatrixElement = "0";
}
else
{
Result.MatrixElement = "(" + op1.MatrixElement + ")||(" + op2.MatrixElement + ")";
}
return Result;
}//Operator Overloading(checked!!!!)
public static BoolDK operator +(BoolDK op1, BoolDK op2)
{
BoolDK Result = new BoolDK();
if (op1.MatrixElement != "0" & op2.MatrixElement != "0")
{
Result.MatrixElement = op1.MatrixElement + "||" + op2.MatrixElement;
}
else if (op1.MatrixElement != "0" & op2.MatrixElement == "0")
{
Result = op1;
}
else if (op1.MatrixElement == "0" & op2.MatrixElement != "0")
{
Result = op2;
}
else if (op1.MatrixElement == "0" & op2.MatrixElement == "0")
{
Result.MatrixElement = "0";
}
return Result;
}//Operator Overloading
问题是:
此方法无法计算尺寸超过12 * 12的矩阵,这将导致OutOfMemoryException。目标是计算25 * 25矩阵。
递归会降低性能还是字符串操作会降低性能?
因此,任何人都知道如何进行这项工作?谢谢 !!!丹克!