Question

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 to put each element in an HashSet because it doesnt allow double entries. But it does not work. I clear the HashSet after each row. Is there any logical mistake?

Edit: Whole 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]; //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(")");

        }
    }
}

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!");
                }
            }
        }
    }
}
Was it helpful?

Solution

You are rounding your results at 2 decimal places. Which makes you probably think they are exactly 0.0, but they are not.


I think you should have it the other way around: if it contains 0.0 and there is only one element, it is OK. And if it contains more then one element it is not OK, so stop! Besides from that and as far as I can see, your code should work. Maybe you are confused about what your application is actually doing. Try to check if it works by printing. I also think that it should return a boolean instead.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top