diff --git a/src/main/java/edu/ucla/sspace/matrix/SVD.java b/src/main/java/edu/ucla/sspace/matrix/SVD.java index 6b007096..67d08107 100644 --- a/src/main/java/edu/ucla/sspace/matrix/SVD.java +++ b/src/main/java/edu/ucla/sspace/matrix/SVD.java @@ -25,6 +25,7 @@ import edu.ucla.sspace.matrix.Matrix.Type; import edu.ucla.sspace.matrix.factorization.SingularValueDecomposition; +import edu.ucla.sspace.matrix.factorization.SingularValueDecompositionJAMA; import edu.ucla.sspace.matrix.factorization.SingularValueDecompositionLibC; import edu.ucla.sspace.matrix.factorization.SingularValueDecompositionMatlab; import edu.ucla.sspace.matrix.factorization.SingularValueDecompositionOctave; @@ -153,6 +154,8 @@ private SVD() { } return new SingularValueDecompositionMatlab(); if (isOctaveAvailable()) return new SingularValueDecompositionOctave(); + if (isJAMAAvailable()) + return new SingularValueDecompositionJAMA(); throw new UnsupportedOperationException("Cannot find a valid SVD implementation"); } @@ -162,6 +165,7 @@ private SVD() { } */ public static SingularValueDecomposition getFactorization(Algorithm alg) { switch (alg) { + case JAMA: return new SingularValueDecompositionJAMA(); case MATLAB: return new SingularValueDecompositionMatlab(); case OCTAVE: return new SingularValueDecompositionOctave(); case SVDLIBC: return new SingularValueDecompositionLibC(); @@ -189,7 +193,7 @@ else if (isMatlabAvailable()) return Algorithm.MATLAB; else if (isOctaveAvailable()) return Algorithm.OCTAVE; - else if (isJAMAavailable()) + else if (isJAMAAvailable()) return Algorithm.JAMA; else if (isColtAvailable()) return Algorithm.COLT; @@ -440,7 +444,7 @@ public static Matrix[] svd(File matrix, Algorithm alg, /** * Returns {@code true} if the JAMA library is available */ - private static boolean isJAMAavailable() { + private static boolean isJAMAAvailable() { try { Class clazz = loadJamaMatrixClass(); } catch (ClassNotFoundException cnfe) { @@ -653,7 +657,7 @@ static Matrix[] jamaSVD(double[][] inputMatrix, int dimensions) { // package. try { SVD_LOGGER.fine("attempting JAMA"); - isJAMAavailable(); + isJAMAAvailable(); int rows = inputMatrix.length; int cols = inputMatrix[0].length; // assume at least one row diff --git a/src/main/java/edu/ucla/sspace/matrix/factorization/SingularValueDecompositionJAMA.java b/src/main/java/edu/ucla/sspace/matrix/factorization/SingularValueDecompositionJAMA.java new file mode 100644 index 00000000..ac8d2b00 --- /dev/null +++ b/src/main/java/edu/ucla/sspace/matrix/factorization/SingularValueDecompositionJAMA.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2013. Earl J. Wagner + * + * This file is part of the S-Space package and is covered under the terms and + * conditions therein. + * + * The S-Space package is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation and distributed hereunder to you. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND NO REPRESENTATIONS OR WARRANTIES, + * EXPRESS OR IMPLIED ARE MADE. BY WAY OF EXAMPLE, BUT NOT LIMITATION, WE MAKE + * NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY + * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE OR DOCUMENTATION + * WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER + * RIGHTS. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package edu.ucla.sspace.matrix.factorization; + +import edu.ucla.sspace.matrix.Matrix; +import edu.ucla.sspace.matrix.MatrixBuilder; +import edu.ucla.sspace.matrix.MatrixFactorization; +import edu.ucla.sspace.matrix.MatrixFile; +import edu.ucla.sspace.matrix.SparseMatrix; +import edu.ucla.sspace.matrix.MatlabSparseMatrixBuilder; + +import edu.ucla.sspace.matrix.SVD; +import edu.ucla.sspace.matrix.SVD.Algorithm; + +import java.io.IOError; +import java.io.IOException; + +import java.util.logging.Logger; +import java.util.logging.Level; + + +/** + * A wrapper around the JAMA + * + * @author Earl J. Wagner + */ +public class SingularValueDecompositionJAMA extends AbstractSvd { + + private static final Logger LOG = + Logger.getLogger(SingularValueDecompositionJAMA.class.getName()); + + /** + * {@inheritDoc} + */ + public void factorize(SparseMatrix matrix, int dimensions) { + Matrix[] svd = SVD.svd(matrix, Algorithm.JAMA, dimensions); + + dataClasses = svd[0]; + U = dataClasses; + scaledDataClasses = false; + + classFeatures = svd[2]; + V = classFeatures; + scaledClassFeatures = false; + + singularValues = new double[dimensions]; + for (int k = 0; k < dimensions; ++k) + singularValues[k] = svd[1].get(k, k); + } + + /** + * {@inheritDoc} + */ + public void factorize(MatrixFile mFile, int dimensions) { + Matrix[] svd = SVD.svd(mFile.getFile(), Algorithm.JAMA, + mFile.getFormat(), dimensions); + + dataClasses = svd[0]; + U = dataClasses; + scaledDataClasses = false; + + classFeatures = svd[2]; + V = classFeatures; + scaledClassFeatures = false; + + singularValues = new double[dimensions]; + for (int k = 0; k < dimensions; ++k) + singularValues[k] = svd[1].get(k, k); + } + + /** + * {@inheritDoc} + */ + public MatrixBuilder getBuilder() { + return new MatlabSparseMatrixBuilder(); + } +}