Esempio n. 1
0
  public static void main(String[] args) {
    double[][] arg1 = {{1, 1, 1}, {2, 3, 2}, {4, 7, 3}};
    DenseDoubleMatrix2D obj1 = new DenseDoubleMatrix2D(arg1); // rot

    double[][] arg2 = {{5, 5, 5}, {8, 8, 8}, {2, 2, 2}};
    DenseDoubleMatrix2D obj2 = new DenseDoubleMatrix2D(arg2); // grŸn

    double[][] arg3 = {{4, 5, 6}, {3, 2, 1}, {1, 2, 1}};
    DenseDoubleMatrix2D obj3 = new DenseDoubleMatrix2D(arg3); // schwarz

    System.out.println("obj1\n" + obj1);
    System.out.println("obj2\n" + obj2);
    System.out.println("obj3\n" + obj3);

    double[][][] pair21 = {arg1, arg2};
    Transformation trans21 = Kabsch.calculateTransformation(pair21); // grŸn -> rot
    DenseDoubleMatrix2D rot21 = trans21.peekRotation();
    DenseDoubleMatrix1D trl21 = trans21.peekTranslation();
    System.out.println("rot21\n" + rot21);
    System.out.println("trl21\n" + trl21);

    double[][][] pair12 = {arg2, arg1};
    Transformation trans12 = Kabsch.calculateTransformation(pair12); // grŸn -> rot
    DenseDoubleMatrix2D rot12 = trans12.peekRotation();
    DenseDoubleMatrix1D trl12 = trans12.peekTranslation();
    System.out.println("rot12\n" + rot12);
    System.out.println("trl12\n" + trl12);

    double[][][] pair32 = {arg2, arg3};
    Transformation trans32 = Kabsch.calculateTransformation(pair32); // schwarz -> grŸn
    DenseDoubleMatrix2D rot32 = trans32.peekRotation();
    DenseDoubleMatrix1D trl32 = trans32.peekTranslation();
    System.out.println("rot32\n" + rot32);
    System.out.println("trl32\n" + trl32.toString());

    DenseDoubleMatrix2D arg2onarg1 =
        new DenseDoubleMatrix2D(trans21.transform(arg2)); // grŸn -> rot
    System.out.println(arg2onarg1.toString());
    DenseDoubleMatrix2D uni21 = createUniMatrix(rot21, trl21);
    DenseDoubleMatrix2D uni32 = createUniMatrix(rot32, trl32);
    System.out.println("uni21\n" + uni21.toString());
    System.out.println("uni32\n" + uni32.toString());

    Algebra alg = new Algebra();
    DenseDoubleMatrix2D result =
        (DenseDoubleMatrix2D)
            alg.mult(
                DoubleFactory2D.dense.identity(4),
                computeTransitiveTransformation(
                    (DenseDoubleMatrix2D) DoubleFactory2D.dense.identity(4), uni32, uni21));

    System.out.println("action results\n" + result);
    System.out.println("moved points\n" + movePoints(result, obj3).toString());
  }
  /** Creates a new instance of testmatrix */
  public GLSsolver(double[][] p_MatrixgleichNull) throws IllegalArgumentException {

    // --------------------------------------
    // Kontrolle, ob Eingabematrix rechteckig
    // --------------------------------------

    int nplus1 = p_MatrixgleichNull[0].length;
    for (int i = 1; i < p_MatrixgleichNull.length; i++) { // Zeilen i
      if (p_MatrixgleichNull[i].length != nplus1) {
        System.err.println(
            "Programmfehler: Matrix des GLS ist nicht rechteckig! (im solver entdeckt)");
        throw new IllegalArgumentException();
      }
    }
    if (nplus1 <= 1) throw new IllegalArgumentException("keine Unbekannte"); // keine Unbekannte!!!

    // Umgeht einen Fehler in der colt-Bibliothek // TODO wenn behoben, Workaround entfernen
    // ------
    int anzGl = p_MatrixgleichNull.length;
    if (anzGl < nplus1 - 1) { // anzGleichungen < anz Unbekannte
      if (debug) System.out.println("WorkAround fuer Fehler in colt: 0 = 0 Gleichungen anhaengen");
      anzGl = nplus1 - 1; // = Anzahl Unbek, 0 0 0 ... 0 = 0 Zeile angehängt
    }

    // -------------------------
    // Daten in A und b einlesen
    // -------------------------

    // so dass A*x = b
    A = new DenseDoubleMatrix2D(anzGl, (nplus1 - 1));
    DenseDoubleMatrix2D b = new DenseDoubleMatrix2D(anzGl, 1);

    for (int i = 0; i < p_MatrixgleichNull.length; i++) { // Zeilen i
      for (int j = 0; j < nplus1 - 1; j++) { // Spalten
        A.set(i, j, p_MatrixgleichNull[i][j]);
      }
      b.set(i, 0, -p_MatrixgleichNull[i][nplus1 - 1]);
    }

    if (debug) {
      System.out.println(" A = " + A.toString());
      System.out.println(" b = " + b.toString());
      System.out.println("");
    }

    // --------------
    // LR - Zerlegung
    // --------------

    LUDecomposition ALU = new LUDecomposition(A);
    if (debug) System.out.println(ALU.toString());

    DoubleMatrix2D L = ALU.getL();
    R = ALU.getU();
    int[] piv = ALU.getPivot();

    Algebra alg = new Algebra();
    //        if (debug) System.out.println("L = " + L.toString());
    //        if (debug) System.out.println("Kontrolle L*R = " + alg.mult(L,R).toString());
    //        if (debug) System.out.println("Kontrolle P*b = " + alg.permute(b, piv, null) );
    //
    //        if (debug) System.out.println("Rx = c: R = " + R.toString());
    //        if (debug) System.out.println("alg.permute(b, piv, null) = " + alg.permute(b, piv,
    // null).toString());

    c = alg.solve(L, alg.permute(b, piv, null)); // TODO: kann zu Problemen führen,
    // wenn weniger Gleichungen als Unbek --> s.Workaround oben

    if (debug) System.out.println("Lc = Pb:  c = " + c.toString());

    if (debug) {
      System.out.println("Rang A: " + alg.rank(A));
      System.out.println("Rang R: " + alg.rank(R));
    }

    assert (alg.rank(A) == alg.rank(R)) : "Rang von A ungleich Rang von R --> Programmfehler";
    anzUnbestParam = A.columns() - alg.rank(A);
    if (debug) System.out.println("Anz unbest Parameter: " + anzUnbestParam);
  }
Esempio n. 3
0
  public static void main(String[] args) {
    try {
      //            Matrix m = new Matrix(matrix);
      //            System.out.println("Before divide: " + m.toString());
      //            m.avg();
      //            m.divideByAvg();
      //            System.out.println("After  divide: " + m.toString());
      //            double[][] scalar = m.scalar();
      //            double[][] cos = m.cos();
      //            Matrix scal = new Matrix(scalar);
      //            Matrix co = new Matrix(cos);
      //            System.out.println("Scalar");
      //            System.out.println(scal.toString());
      //            System.out.println("Cos");
      //            System.out.println(co.toString());
      SVD svd = new SVD(matrix);

      if (svd.setSVD()) {
        DenseDoubleMatrix2D u = new DenseDoubleMatrix2D(svd.getU());
        DenseDoubleMatrix2D v = new DenseDoubleMatrix2D(svd.getV());
        DenseDoubleMatrix2D s = new DenseDoubleMatrix2D(svd.getS());

        System.out.println("U: \n" + u.toString());
        System.out.println("S: \n" + s.toString());
        System.out.println("V: \n" + v.toString());
        v = new DenseDoubleMatrix2D(v.viewDice().toArray());
        System.out.println("V: \n" + v.toString());

        u = new DenseDoubleMatrix2D(u.viewPart(0, 0, 12, 2).toArray());
        //                u = new DenseDoubleMatrix2D(u.viewPart(0, 0, 2, 9).toArray());
        //                v = new DenseDoubleMatrix2D(v.viewDice().toArray());
        v = new DenseDoubleMatrix2D(v.viewPart(0, 0, 2, 9).toArray());
        //                v = new DenseDoubleMatrix2D(v.viewDice().toArray());
        //                v = new DenseDoubleMatrix2D(v.viewPart(0, 0, 9, 2).toArray());
        s = new DenseDoubleMatrix2D(s.viewPart(0, 0, 2, 2).toArray());

        System.out.println("U: \n" + u.toString());
        System.out.println("S: \n" + s.toString());
        System.out.println("V: \n" + v.toString());

        double[][] u1 = u.toArray();
        double[][] s1 = s.toArray();
        double[][] v1 = v.toArray();
        double[][] res = new double[u1.length][s1.length];
        for (int i = 0; i < u1.length; i++) {
          for (int j = 0; j < u1[i].length; j++) {
            for (int k = 0; k < s1[j].length; k++) {
              res[i][j] += u1[i][k] * s1[j][k];
            }
          }
        }

        System.out.println("Res Length: " + res.length);

        System.out.println("V1 Length: " + v1.length);

        System.out.println("");

        double[][] res2 = new double[res.length][v1[0].length];
        //                for(int i = 0; i < res.length; i++)
        //                {
        //                    for(int j = 0; j < res[i].length; j++)
        //                    {
        //                        for(int k = 0; k < v1[j].length; k++)
        //                        {
        //                            res2[i][k] += res[i][j] * v1[j][k];
        //                        }
        //                    }
        //                }

        for (int i = 0; i < 12; i++) {
          for (int j = 0; j < 9; j++) {
            for (int k = 0; k < 2; k++) {
              res2[i][j] += res[i][k] * v1[k][j];
            }
          }
        }

        Matrix res3 = new Matrix(res2);
        System.out.println("Final res: \n" + res3.toString());
        res3.trans();
        Matrix res4 = new Matrix(res3.cos());
        System.out.println("Final res2: \n" + res4.toString());

      } else {
        System.out.println("False");
      }
    } catch (Exception ex) {
      System.out.println("Exception in main: " + ex.toString());
    }
  }