Examples of SingularValueDecomposition


Examples of org.apache.commons.math.linear.SingularValueDecomposition

        checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare));
        checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose());
    }

    public void checkAEqualUSVt(final RealMatrix matrix) {
        SingularValueDecomposition svd = new SingularValueDecompositionImpl(matrix);
        RealMatrix u = svd.getU();
        RealMatrix s = svd.getS();
        RealMatrix v = svd.getV();
        double norm = u.multiply(s).multiply(v.transpose()).subtract(matrix).getNorm();
        assertEquals(0, norm, normTolerance);

    }
View Full Code Here

Examples of org.apache.commons.math.linear.SingularValueDecomposition

        assertEquals(0, mTm.subtract(id).getNorm(), normTolerance);
    }

    /** test matrices values */
    public void testMatricesValues1() {
       SingularValueDecomposition svd =
            new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
        RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] {
                { 3.0 / 5.0, -4.0 / 5.0 },
                { 4.0 / 5.03.0 / 5.0 }
        });
        RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] {
                { 3.0, 0.0 },
                { 0.0, 1.0 }
        });
        RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] {
                { 4.0 / 5.03.0 / 5.0 },
                { 3.0 / 5.0, -4.0 / 5.0 }
        });

        // check values against known references
        RealMatrix u = svd.getU();
        assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
        RealMatrix s = svd.getS();
        assertEquals(0, s.subtract(sRef).getNorm(), normTolerance);
        RealMatrix v = svd.getV();
        assertEquals(0, v.subtract(vRef).getNorm(), normTolerance);

        // check the same cached instance is returned the second time
        assertTrue(u == svd.getU());
        assertTrue(s == svd.getS());
        assertTrue(v == svd.getV());
       
    }
View Full Code Here

Examples of org.apache.commons.math.linear.SingularValueDecomposition

            24.0 / 125.0107.0 / 125.0, 60.0 / 125.0 },
            { -93.0 / 125.0,  -24.0 / 125.0, 80.0 / 125.0 }
        });

        // check values against known references
        SingularValueDecomposition svd =
            new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testNonSquare));
        RealMatrix u = svd.getU();
        assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
        RealMatrix s = svd.getS();
        assertEquals(0, s.subtract(sRef).getNorm(), normTolerance);
        RealMatrix v = svd.getV();
        assertEquals(0, v.subtract(vRef).getNorm(), normTolerance);

        // check the same cached instance is returned the second time
        assertTrue(u == svd.getU());
        assertTrue(s == svd.getS());
        assertTrue(v == svd.getV());

    }
View Full Code Here

Examples of org.apache.commons.math.linear.SingularValueDecomposition

  @Override
  public double getCondition(final Matrix<?> m) {
    Validate.notNull(m, "m");
    if (m instanceof DoubleMatrix2D) {
      final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m);
      final SingularValueDecomposition svd = new SingularValueDecompositionImpl(temp);
      return svd.getConditionNumber();
    }
    throw new IllegalArgumentException("Can only find condition number of DoubleMatrix2D; have " + m.getClass());
  }
View Full Code Here

Examples of org.apache.mahout.math.SingularValueDecomposition

      throw new CardinalityException(m.numRows(), m.numCols());
    }
    // See http://www.mlahanas.de/Math/svd.htm for details,
    // which specifically details the case of covariance matrix inversion
    // Complexity: O(min(nm2,mn2))
    SingularValueDecomposition svd = new SingularValueDecomposition(m);
    Matrix sInv = svd.getS();
    // Inverse Diagonal Elems
    for (int i = 0; i < sInv.numRows(); i++) {
      double diagElem = sInv.get(i,i);
      if (diagElem > 0.0) {
        sInv.set(i, i, 1 / diagElem);
      } else {
        throw new IllegalStateException("Eigen Value equals to 0 found.");
      }
    }
    inverseCovarianceMatrix = svd.getU().times(sInv.times(svd.getU().transpose()));
  }
View Full Code Here

Examples of org.apache.mahout.math.SingularValueDecomposition

      throw new CardinalityException(m.numRows(), m.numCols());
    }
    // See http://www.mlahanas.de/Math/svd.htm for details,
    // which specifically details the case of covariance matrix inversion
    // Complexity: O(min(nm2,mn2))
    SingularValueDecomposition svd = new SingularValueDecomposition(m);
    Matrix sInv = svd.getS();
    // Inverse Diagonal Elems
    for (int i = 0; i < sInv.numRows(); i++) {
      double diagElem = sInv.get(i, i);
      if (diagElem > 0.0) {
        sInv.set(i, i, 1 / diagElem);
      } else {
        throw new IllegalStateException("Eigen Value equals to 0 found.");
      }
    }
    inverseCovarianceMatrix = svd.getU().times(sInv.times(svd.getU().transpose()));
    Preconditions.checkArgument(inverseCovarianceMatrix != null, "inverseCovarianceMatrix not initialized");
  }
View Full Code Here

Examples of org.apache.mahout.math.SingularValueDecomposition

    mx.setQuick(1, 2, 6);
    mx.setQuick(2, 0, 7);
    mx.setQuick(2, 1, 8);
    mx.setQuick(2, 2, 9);

    SingularValueDecomposition svd2 = new SingularValueDecomposition(mx);
    double[] svaluesControl = svd2.getSingularValues();

    for (int i = 0; i < kp; i++) {
      System.out.printf("%.3e ", svaluesControl[i]);
    }
    System.out.println();
View Full Code Here

Examples of org.apache.mahout.math.SingularValueDecomposition

    mx.setQuick(1, 2, 6);
    mx.setQuick(2, 0, 7);
    mx.setQuick(2, 1, 8);
    mx.setQuick(2, 2, 9);

    SingularValueDecomposition svd2 = new SingularValueDecomposition(mx);
    double[] svaluesControl = svd2.getSingularValues();

    for (int i = 0; i < kp; i++) {
      System.out.printf("%e ", svaluesControl[i]);
    }
    System.out.println();
View Full Code Here

Examples of org.apache.mahout.math.SingularValueDecomposition

    // try to run the same thing without stochastic algo
    double[][] a = SSVDSolver.loadDistributedRowMatrix(fs, aPath, conf);

    // SingularValueDecompositionImpl svd=new SingularValueDecompositionImpl(new
    // Array2DRowRealMatrix(a));
    SingularValueDecomposition svd2 =
      new SingularValueDecomposition(new DenseMatrix(a));

    double[] svalues2 = svd2.getSingularValues();
    dumpSv(svalues2);

    for (int i = 0; i < k + p; i++) {
      assertTrue(Math.abs(svalues2[i] - stochasticSValues[i]) <= s_epsilon);
    }
View Full Code Here

Examples of org.apache.mahout.math.SingularValueDecomposition

    LSMR r = new LSMR();
    Vector x1 = r.solve(m, b);

//    assertEquals(0, m.times(x1).minus(b).norm(2), 1.0e-2);
    double norm = new SingularValueDecomposition(m).getS().viewDiagonal().norm(2);
    double actual = m.transpose().times(m).times(x1).minus(m.transpose().times(b)).norm(2);
    System.out.printf("%.4f\n", actual / norm * 1.0e6);
    assertEquals(0, actual, norm * 1.0e-5);

    // and we need to check that the error estimates are pretty good.
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.