问题描述
我正在编写应使用Cramer规则求解矩阵等式的程序,对此我具有这样的功能:
function solveKramers(AMatr: Matrix; BMatr: Vector): vector;
var
detA: real;
solvingMatrix: Matrix;
i,j: Integer;
begin
detA := getDet(AMatr);
if (not (detA = 0) or not (Length(AMatr) = Length(BMatr))) then begin
SetLength(Result,Length(BMatr));
for i := 0 to High(BMatr) do begin
solvingMatrix := system.copy(AMatr);
for j := 0 to High(solvingMatrix) do begin
solvingMatrix[j,i] := BMatr[j];
end;
Result[i] := getDet(solvingMatrix) / detA;
end;
Exit;
end;
end;
我创建了matrix = array of vector
和vector = array of real
当我尝试使用它时,solvingMatrix := system.copy(AMatr);
会创建对AMatr的引用,而不是创建此矩阵的副本。
解决方法
嗯,我不知道这是如何工作的,但是我通过分别复制每一行来解决它(奇怪的是我在getDet函数中做了类似的操作,但是还好吧)
我添加的代码如下:
for j := 0 to High(AMatr) do begin
solvingMatrix[j] := system.copy(AMatr[j]);
end;