我为高斯矩阵做了这段代码,但是代码有问题,我不知道如何解决它。高斯矩阵没有显示应该是什么。我使用[0,0,1,2,2,3,0,-2,3,3,0,-2,3,3,6,-9]作为矩阵A,它对它进行了很好的排序,但当涉及到高斯时,我得到了[3.0,3.0,6.0,-9.0,3.0,5.25,-3.0,0.00,0,0,1.0,2.0],当正确答案是[3.0,3.0,6.0,-9.0,0.0,-1.5,6.0,-6.0,0,0,0,1.0,2.0]时。谁能告诉我我做错了什么?在进阶时谢谢。
public static double[][] sortMatrix(double[][] matrix)
{
int m=matrix.length;
int n=matrix[0].length;
double[] temp=new double[n];
int a=0;
for(int fixedCol=a;fixedCol<n;fixedCol++)//Working and fixing the column only.
{
for(int fixedRow=a;fixedRow<m;fixedRow++)//First process.
{
//Second process.
for(int i=fixedRow;i<m;i++)//Checking if the column element is bigger.
{
if(matrix[fixedRow][fixedCol]<matrix[i][fixedCol])
{
for(int j=0;j<n;j++)
{
temp[j]=matrix[fixedRow][j];
matrix[fixedRow][j]=matrix[i][j];
matrix[i][j]=temp[j];//Until now, changes the row and since fixed row and column points to a value, nothing to change.
}
}
}//End of second process.
}
for(int fixedRow=a;fixedRow<m;fixedRow++)
{
if(matrix[fixedRow][fixedCol]!=0)
{
a=a+1;
}
else
{
break;
}
}
}//End of first process
return matrix;
}
//Gauss Matrix
public static double[][] gauss(double[][] matrix)
{
double tempPrincipal,tempSecondary;
int m=matrix.length;
int n=matrix[0].length;
sortMatrix(matrix);
for(int fixedRowPrincipal=0;fixedRowPrincipal<(m-1);fixedRowPrincipal++)
{
for(int fixedColPrincipal=0;fixedColPrincipal<n;fixedColPrincipal++)//First process: Finding the first element different from 0.
{
if(matrix[fixedRowPrincipal][fixedColPrincipal]!=0)//Found the first element different from 0.
{
if(matrix[fixedRowPrincipal+1][fixedColPrincipal]!=0)//Conditioning the element below isn´t 0.
{
tempPrincipal=matrix[fixedRowPrincipal][fixedColPrincipal];//Assigning fixed values for the elements.
tempSecondary=matrix[fixedRowPrincipal+1][fixedColPrincipal];
for(int j=(fixedColPrincipal);j<n;j++)
{
if(tempPrincipal<0)
{
if(tempSecondary<0)
{
matrix[fixedRowPrincipal+1][j]=matrix[fixedRowPrincipal][j]+(-1.0)*matrix[fixedRowPrincipal+1][j]*(tempPrincipal/tempSecondary);
}
else
{
matrix[fixedRowPrincipal+1][j]=matrix[fixedRowPrincipal][j]+(1.0)*matrix[fixedRowPrincipal+1][j]*(tempPrincipal/tempSecondary);
}
}
else
{
if(tempSecondary<0)
{
matrix[fixedRowPrincipal+1][j]=matrix[fixedRowPrincipal][j]+(1.0)*matrix[fixedRowPrincipal+1][j]*(tempPrincipal/tempSecondary);
}
else
{
matrix[fixedRowPrincipal+1][j]=matrix[fixedRowPrincipal][j]-(matrix[fixedRowPrincipal+1][j]/tempSecondary)*tempPrincipal;
}
}
matrix[fixedRowPrincipal+1][j]=matrix[fixedRowPrincipal][j]+(-1)*matrix[fixedRowPrincipal+1][j]*(tempPrincipal/tempSecondary);
}
sortMatrix(matrix);
fixedRowPrincipal=0;
break;
}
else
{
break;
}
}
}
}
return matrix;
}发布于 2016-06-30 03:43:39
第一步
first归约是关于行操作的,所以最好先实现它们,为此我们只需要一个接受矩阵的函数,Ri和Rj,a,它将*Ri添加到Rj。
其他Row操作是:切换两行并将一行乘以一个数字,稍后我将解释为什么我们在这里不需要它们:)。
第二步
现在我们要确定“枢轴元素”,假设矩阵A的大小为n*m,在第一次迭代中,枢轴元素是A,在第二次迭代中是A1。
简而言之,它就是A[i][i]。
第三步
现在,我们希望更改i之后的每一行,以便每个i+1<=r<n的A[r][i]都为零
我们更改行r,以便A[r]+=(-A[r][i]/A[i][i])*A[i],我们可以调用步骤1中的函数来完成此操作。
第四步
我们对每个i<n-1 && i<m重复第二步和第三步,我们就完成了。
变量i应该小于n-1而不是n,因为我们希望更改每一行r为i+1或更多,行i=n-1后面没有任何行。
它应该小于m,因为我们还将它用作列号。
为什么只有一个行操作就足够了?
其他的用于制作pivot元素1,因为1对于人类来说更简单。
但是计算机对除1以外的数字没有问题,实际上试图将其设为1反而会损害性能,因为它需要额外的计算(特别是切换行)。
性能步骤
在步骤3中我们使用了A[r]+=(-A[r][i]/A[i][i])*A[i],即使当A[r][i]已经是零的时候也可以工作,但是因为前面的等式是通过具有O(n)复杂度的函数调用实现的,我们可以通过检查A[r][i]是否为零来避免调用它,如果它是我们在第四步中continue循环(我们也可以在执行此操作的函数内部进行检查,在我看来这更好)。
我将把实现留给你,我建议第一步的函数有这个签名:
void addRowToRow(double[][] a,int i,int j,int alpha)在步骤3中应该像这样调用它:
addRowToRow(a,i,r,-(a[r][i]/a[i][i]));备注
我假设你只需要高斯约化,并且你不需要方程求解器,不管怎样,前面的步骤可以是方程求解器可以调用的reduce(double[][] a)函数。
https://stackoverflow.com/questions/38107598
复制相似问题