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");
}
}
}
I want to check if the elements of each row are the same (in this example, 0). I came up with the idea of ββputting every element in a HashSet because it does not allow duplicate entries. But that does not work. I clear the HashSet after each line. Is there any kind of logical error?
Edit: All code:
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];
while (pivot == 0)
{
int secLine = line + counter;
if (secLine < matrix.length)
{
swapLines(line, secLine, matrix, vector);
counter++;
}else if (!(secLine < matrix.length))
{
checkSolution(matrix, vector);
}
pivot = matrix[line][column];
}
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(")");
}
}
}
Solution: Thanks to 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!");
}
}
}
}
}
source
share