Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable JAMA SVD for LSA #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/edu/ucla/sspace/matrix/SVD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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();
}
}