gpt4 book ai didi

Java:数组:检查行元素

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:54 25 4
gpt4 key购买 nike

public static void checkSolution(double[][] matrix, double[] vector)
{
HashSet<Double> arraySet = new HashSet<Double>();

for (int line = 0; line < matrix.length; line++)
{
arraySet.clear();
for (int column = 0; column < matrix[line].length; column++)
{
arraySet.add(matrix[line][column]);
}
if ((arraySet.size() == 1) && (arraySet.contains(0.0)))
{
throw new RuntimeException("Oups");
}
}
}

我想检查每一行的元素是否相同(在本例中为 0)。我想出了将每个元素放入 HashSet 的想法,因为它不允许重复输入。但它不起作用。我在每一行之后清除 HashSet。有没有逻辑错误?

编辑:整个代码:

class Main
{
public static void main(String [] args)
{

double[][] A = new double[3][3];
double[] b = new double[3];

A[0][0] = 3; A[0][1] = -1; A[0][2] = 2; b[0] = 1;
A[1][0] = 7; A[1][1] = -4; A[1][2] = -1; b[1] = -2;
A[2][0] = -1; A[2][1] = -3; A[2][2] = -12; b[2] = -5;


solveEquation(A, b);

}

public static double[] solveEquation(double[][] matrix, double [] vector)
{
int counter = 1;
double pivot;

System.out.println("LGS: ");
System.out.println("--------------------------------");
printMatrix(matrix);
printVector(vector);

for (int line = 0; line < matrix.length; line++)
{
for (int column = 0; column < matrix[line].length; column++)
{
if (line == column)
{
pivot = matrix[line][column]; //setting pivot element

while (pivot == 0) //testing if pivot is equal to zero; if thats the case we have to swap lines
{
int secLine = line + counter;

if (secLine < matrix.length)
{
swapLines(line, secLine, matrix, vector);
counter++;
}else if (!(secLine < matrix.length)) //necessary?
{
checkSolution(matrix, vector);
}

pivot = matrix[line][column]; //override pivot with the new element because lines have switched
}

//normalizing the pivot row
for (int elementAdjustment = 0; elementAdjustment < matrix[line].length; elementAdjustment++)
{
matrix[line][elementAdjustment] = matrix[line][elementAdjustment] / pivot;
}

vector[line] = vector[line] / pivot;


for (int i = 0; i < matrix[line].length; i++)
{
if (i != line)
{
double factor = matrix[i][line];

for (int k = 0; k < matrix[line].length; k++)
{
matrix[i][k] = matrix[i][k] - factor * matrix[line][k];
}

vector[i] = vector[i] - factor * vector[line];
}
}
System.out.println();
System.out.println("Step: " + (column + 1));

printMatrix(matrix);
printVector(vector);

}

}

checkSolution(matrix, vector);
}

return vector;
}

public static void swapLines(int lineOne, int lineTwo, double[][] matrix, double[] vector)
{
double holderArr [];
double holderVar;

holderArr = matrix[lineOne];
holderVar = vector[lineOne];

matrix[lineOne] = matrix[lineTwo];
vector[lineOne] = vector[lineTwo];

matrix[lineTwo] = holderArr;
vector[lineTwo] = holderVar;
}

public static void checkSolution(double[][] matrix, double[] vector)
{
HashSet<Double> arraySet = new HashSet<Double>();

for (int line = 0; line < matrix.length; line++)
{
arraySet.clear();
for (int column = 0; column < matrix[line].length; column++)
{
arraySet.add(matrix[line][column]);
}
if ((arraySet.size() == 1) && (arraySet.contains(0.0)))
{
throw new RuntimeException("Oups");
}
}


}

public static void printVector(double vector[])
{
double temp;

if (vector == null)
{
return;
}
System.out.println();
System.out.println("Vector: ");

for (int line = 0; line < vector.length; line++)
{
temp = vector[line];
temp = temp * 100;
temp = Math.round(temp);
temp = temp / 100;
System.out.print("(");
System.out.print(temp);
System.out.print(")");
System.out.println();
}
}

public static void printMatrix(double [][] matrix)
{
double temp;

if (matrix == null)
{
return;
}

System.out.println("Matrix: ");

for (int line = 0; line < matrix.length; line++)
{
System.out.print("(");

for (int column = 0; column < matrix[line].length; column++)
{
temp = matrix[line][column];
temp = temp * 100;
temp = Math.round(temp);
temp = temp / 100;

if (column != 0)
{
System.out.print(" , ");
}

System.out.print(temp);
}

System.out.println(")");

}
}
}

解决方案:感谢 Martijn Courteaux。

public static void checkSolution(double[][] matrix, double[] vector)
{
for (int line = 0; line < vector.length; line++)
{
double temp = 0.0;
int counter = 0;
for (int column = 0; column < matrix[line].length; column++)
{
temp = matrix[line][column];
temp = temp * Integer.MAX_VALUE;
temp = Math.round(temp);
temp = temp / Integer.MAX_VALUE;
if ((temp == 0) && (vector[line] != 0))
{
counter = counter + 1;
if (counter == matrix[line].length)
{
throw new RuntimeException("Contradiction! Equation system is not uniquely solvable!");
}
}
}
}
}

最佳答案

您将结果四舍五入到小数点后两位。这让您可能认为它们正好是 0.0,但它们不是。


我认为您应该反过来:如果它包含 0.0 并且只有一个元素,则可以。如果它包含不止一个元素,那就不行了,所以停止!除此之外,据我所知,您的代码应该可以工作。也许您对您的应用程序实际上在做什么感到困惑。尝试通过打印检查它是否有效。我还认为它应该返回一个 boolean

public static boolean checkSolution(double[][] matrix, double[] vector)
{
HashSet<Double> arraySet = new HashSet<Double>();

for (int line = 0; line < matrix.length; line++)
{
arraySet.clear();
for (int column = 0; column < matrix[line].length; column++)
{
arraySet.add(matrix[line][column]);
}
if (!(arraySet.size() == 1) || !(arraySet.contains(0.0)))
{
return false;
}
}
return true;
}

关于Java:数组:检查行元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com