Search in sources :

Example 36 with DMatrixRMaj

use of org.ejml.data.DMatrixRMaj in project clusterMaker2 by RBVI.

the class PrincipalComponentAnalysis method eigenToSampleSpace.

/**
 * Converts a vector from eigen space into sample space.
 *
 * @param eigenData Eigen space data.
 * @return Sample space projection.
 */
public double[] eigenToSampleSpace(double[] eigenData) {
    if (eigenData.length != numComponents)
        throw new IllegalArgumentException("Unexpected sample length");
    DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1);
    DMatrixRMaj r = DMatrixRMaj.wrap(numComponents, 1, eigenData);
    CommonOps_DDRM.multTransA(V_t, r, s);
    DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);
    CommonOps_DDRM.add(s, mean, s);
    return s.data;
}
Also used : DMatrixRMaj(org.ejml.data.DMatrixRMaj)

Example 37 with DMatrixRMaj

use of org.ejml.data.DMatrixRMaj in project clusterMaker2 by RBVI.

the class PrincipalComponentAnalysis method computeBasis.

/**
 * Computes a basis (the principal components) from the most dominant eigenvectors.
 *
 * @param numComponents Number of vectors it will use to describe the data.  Typically much
 * smaller than the number of elements in the input vector.
 */
public void computeBasis(int numComponents) {
    if (numComponents > A.getNumCols())
        throw new IllegalArgumentException("More components requested that the data's length.");
    if (sampleIndex != A.getNumRows())
        throw new IllegalArgumentException("Not all the data has been added");
    if (numComponents > sampleIndex)
        throw new IllegalArgumentException("More data needed to compute the desired number of components");
    this.numComponents = numComponents;
    // compute the mean of all the samples
    for (int i = 0; i < A.getNumRows(); i++) {
        for (int j = 0; j < mean.length; j++) {
            mean[j] += A.get(i, j);
        }
    }
    for (int j = 0; j < mean.length; j++) {
        mean[j] /= A.getNumRows();
    }
    // subtract the mean from the original data
    for (int i = 0; i < A.getNumRows(); i++) {
        for (int j = 0; j < mean.length; j++) {
            A.set(i, j, A.get(i, j) - mean[j]);
        }
    }
    // Compute SVD and save time by not computing U
    SingularValueDecomposition_F64<DMatrixRMaj> svd = DecompositionFactory_DDRM.svd(A.numRows, A.numCols, false, true, false);
    if (!svd.decompose(A))
        throw new RuntimeException("SVD failed");
    V_t = svd.getV(null, true);
    DMatrixRMaj W = svd.getW(null);
    // Singular values are in an arbitrary order initially
    SingularOps_DDRM.descendingOrder(null, false, W, V_t, true);
    // strip off unneeded components and find the basis
    V_t.reshape(numComponents, mean.length, true);
}
Also used : DMatrixRMaj(org.ejml.data.DMatrixRMaj)

Example 38 with DMatrixRMaj

use of org.ejml.data.DMatrixRMaj in project clusterMaker2 by RBVI.

the class PrincipalComponentAnalysis method sampleToEigenSpace.

/**
 * Converts a vector from sample space into eigen space.
 *
 * @param sampleData Sample space data.
 * @return Eigen space projection.
 */
public double[] sampleToEigenSpace(double[] sampleData, int row) {
    if (sampleData.length != A.getNumCols())
        throw new IllegalArgumentException("Unexpected sample length");
    DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);
    DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1, true, sampleData);
    DMatrixRMaj r = new DMatrixRMaj(numComponents, 1);
    CommonOps_DDRM.subtract(s, mean, s);
    // FastTSne.writeMatrix("s-"+row,s);
    CommonOps_DDRM.mult(V_t, s, r);
    return r.data;
}
Also used : DMatrixRMaj(org.ejml.data.DMatrixRMaj)

Example 39 with DMatrixRMaj

use of org.ejml.data.DMatrixRMaj in project narchy by automenta.

the class PrincipalComponentAnalysis method sampleToEigenSpace.

/**
 * Converts a vector from sample space into eigen space.
 *
 * @param sampleData Sample space data.
 * @return Eigen space projection.
 */
private double[] sampleToEigenSpace(double[] sampleData) {
    if (sampleData.length != A.getNumCols())
        throw new IllegalArgumentException("Unexpected sample length");
    DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);
    DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1, true, sampleData);
    DMatrixRMaj r = new DMatrixRMaj(numComponents, 1);
    subtract(s, mean, s);
    mult(V_t, s, r);
    return r.data;
}
Also used : DMatrixRMaj(org.ejml.data.DMatrixRMaj)

Example 40 with DMatrixRMaj

use of org.ejml.data.DMatrixRMaj in project narchy by automenta.

the class PrincipalComponentAnalysis method computeBasis.

/**
 * Computes a basis (the principal components) from the most dominant eigenvectors.
 *
 * @param numComponents Number of vectors it will use to describe the data.  Typically much
 * smaller than the number of elements in the input vector.
 */
private void computeBasis(int numComponents) {
    if (numComponents > A.getNumCols())
        throw new IllegalArgumentException("More components requested that the data's length.");
    int rows = A.getNumRows();
    if (sampleIndex != rows)
        throw new IllegalArgumentException("Not all the data has been added");
    if (numComponents > sampleIndex)
        throw new IllegalArgumentException("More data needed to compute the desired number of components");
    this.numComponents = numComponents;
    // compute the mean of all the samples
    for (int i = 0; i < rows; i++) for (int j = 0; j < mean.length; j++) mean[j] += A.get(i, j);
    for (int j = 0; j < mean.length; j++) mean[j] /= rows;
    // subtract the mean from the original data
    for (int i = 0; i < rows; i++) for (int j = 0; j < mean.length; j++) A.add(i, j, -mean[j]);
    // Compute SVD and save time by not computing U
    SingularValueDecomposition<DMatrixRMaj> svd = DecompositionFactory_DDRM.svd(A.numRows, A.numCols, false, true, false);
    if (!svd.decompose(A))
        throw new RuntimeException("SVD failed");
    V_t = svd.getV(null, true);
    DMatrixRMaj W = svd.getW(null);
    // Singular values are in an arbitrary order initially
    descendingOrder(null, false, W, V_t, true);
    // strip off unneeded components and find the basis
    V_t.reshape(numComponents, mean.length, true);
}
Also used : DMatrixRMaj(org.ejml.data.DMatrixRMaj)

Aggregations

DMatrixRMaj (org.ejml.data.DMatrixRMaj)454 Test (org.junit.jupiter.api.Test)210 Se3_F64 (georegression.struct.se.Se3_F64)107 Point2D_F64 (georegression.struct.point.Point2D_F64)87 Point3D_F64 (georegression.struct.point.Point3D_F64)68 ArrayList (java.util.ArrayList)55 Vector3D_F64 (georegression.struct.point.Vector3D_F64)54 AssociatedPair (boofcv.struct.geo.AssociatedPair)38 CameraPinhole (boofcv.struct.calib.CameraPinhole)32 Equation (org.ejml.equation.Equation)29 UtilPoint3D_F64 (georegression.geometry.UtilPoint3D_F64)25 Point4D_F64 (georegression.struct.point.Point4D_F64)19 StringReader (java.io.StringReader)16 Test (org.junit.Test)15 AssociatedTriple (boofcv.struct.geo.AssociatedTriple)12 TrifocalTensor (boofcv.struct.geo.TrifocalTensor)11 RectifyCalibrated (boofcv.alg.geo.rectify.RectifyCalibrated)10 CameraPinholeBrown (boofcv.struct.calib.CameraPinholeBrown)10 BufferedImage (java.awt.image.BufferedImage)10 SceneStructureProjective (boofcv.abst.geo.bundle.SceneStructureProjective)9