Java:Array:检查行元素

时间:2014-02-09 19:23:06

标签: java arrays hashset

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!");
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您将结果四舍五入到小数位。这使得你可能认为它们正是0.0,但它们不是。{/ p>


我认为你应该反过来说:如果它包含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;
}