Example usage for org.apache.commons.math3.linear RealMatrix getEntry

List of usage examples for org.apache.commons.math3.linear RealMatrix getEntry

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix getEntry.

Prototype

double getEntry(int row, int column) throws OutOfRangeException;

Source Link

Document

Get the entry in the specified row and column.

Usage

From source file:lsafunctions.LSA.java

private static RealMatrix normalizeMatrix(RealMatrix M) {
    double sumColumn = 0;
    //        Matrix row = new Matrix(1, M.getColumnDimension());
    RealMatrix row = MatrixUtils.createRealMatrix(1, M.getColumnDimension());
    for (int j = 0; j < M.getColumnDimension(); j++) {
        sumColumn = 0;//from   www .j a  va  2  s .c  o  m
        for (int k = 0; k < M.getRowDimension(); k++) {
            sumColumn += Math.pow(M.getEntry(k, j), 2);
        }
        sumColumn = Math.sqrt(sumColumn);
        row.setEntry(0, j, sumColumn);
    }
    for (int j = 0; j < M.getColumnDimension(); j++) {
        for (int k = 0; k < M.getRowDimension(); k++) {
            M.setEntry(k, j, M.getEntry(k, j) / row.getEntry(0, j));
        }
    }
    return M;
}

From source file:edu.oregonstate.eecs.mcplan.util.Csv.java

public static void write(final PrintStream out, final RealMatrix m) {
    final Writer writer = new Writer(out);
    for (int i = 0; i < m.getRowDimension(); ++i) {
        for (int j = 0; j < m.getColumnDimension(); ++j) {
            writer.cell(m.getEntry(i, j));
        }/*from   www.  ja  va2  s .  com*/
        writer.newline();
    }
}

From source file:cooccurrence.Omer_Levy.java

/**
 * Method that will extract top D singular values from CoVariance Matrix 
 * It will then identify the corresponding columns from U and V and add it to new matrices 
 * @param U/*from  ww w . j a  va2 s . co  m*/
 * @param V
 * @param coVariance
 * @param matrixUd
 * @param matrixVd
 * @param coVarD
 * @param indicesD 
 */
private static void getTopD(RealMatrix U, RealMatrix V, RealMatrix coVariance, RealMatrix matrixUd,
        RealMatrix matrixVd, RealMatrix coVarD, ArrayList<Integer> indicesD) {
    TreeMap<Double, Set<Integer>> tmap = new TreeMap<>();
    for (int i = 0; i < coVariance.getRowDimension(); i++) {
        double val = coVariance.getEntry(i, i);
        if (tmap.containsKey(val)) {
            Set<Integer> temp = tmap.get(val);
            temp.add(i);
        } else {
            Set<Integer> temp = new HashSet<>();
            temp.add(i);
            tmap.put(val, temp);
        }
    }
    Iterator iter = tmap.keySet().iterator();
    while (iter.hasNext()) {
        Double val = (Double) iter.next();
        Set<Integer> indices = tmap.get(val);
        for (int i = 0; i < indices.size(); i++) {
            Iterator iterIndices = indices.iterator();
            while (iterIndices.hasNext()) {
                int index = (Integer) iterIndices.next();
                indicesD.add(index);
                coVarD.addToEntry(index, index, val);
                matrixUd.setColumnVector(index, U.getColumnVector(index));
                matrixVd.setColumnVector(index, V.getColumnVector(index));
            }
        }
    }

}

From source file:com.yahoo.egads.utilities.SpectralMethods.java

public static TimeSeries.DataSequence mFilter(TimeSeries.DataSequence data, int windowSize,
        FilteringMethod method, double methodParameter) {

    TimeSeries.DataSequence result = new TimeSeries.DataSequence();
    RealMatrix dataMat = MatrixUtils.createRealMatrix(data.size(), 1);

    int i = 0;//from w w w.  jav a2s . c o m
    for (TimeSeries.Entry e : data) {
        dataMat.setEntry(i, 0, e.value);
        i++;
        TimeSeries.Entry eCopy = new TimeSeries.Entry(e);
        result.add(eCopy);
    }

    RealMatrix resultMat = SpectralMethods.mFilter(dataMat, windowSize, method, methodParameter);

    i = 0;
    for (TimeSeries.Entry e : result) {
        e.value = (float) resultMat.getEntry(i, 0);
        i++;
    }

    return result;
}

From source file:cooccurrence.Omer_Levy.java

/**
 * Generic Method to take squareRoot of individual Values.
 * @param coVariance/*from ww  w. j a  v a  2 s.c  o  m*/
 * @return 
 */
private static RealMatrix squareRoot(RealMatrix coVariance) {
    RealMatrix squareRoot = MatrixUtils.createRealMatrix(coVariance.getRowDimension(),
            coVariance.getColumnDimension());
    for (int i = 0; i < coVariance.getRowDimension(); i++) {
        for (int j = 0; j < coVariance.getColumnDimension(); j++) {
            double val = coVariance.getEntry(i, j);
            val = Math.sqrt(val);
            squareRoot.addToEntry(i, j, val);
        }
    }
    return squareRoot;
}

From source file:IO.java

public static void display(RealMatrix mat, String name) {

    int row = mat.getRowDimension();
    int col = mat.getColumnDimension();

    System.out.println(name + ":" + row + "x" + col);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            System.out.print(mat.getEntry(i, j) + ",");
        }//from w w  w .  j a  va  2s  . c om
        System.out.println();
    }
}

From source file:dataGen.DataGen.java

/**
 * Do the yet created data stored in the matrix fulfill the Anti-Correlation condition?
 * Returns true if more than the half of all correlation coefficients are below the supremum coefficient.
 * @param corrMatrix/*  w w  w .j  a v  a2  s.c o  m*/
 *       The Data-Matrix
 * @param corrCoeff
 *       the Supremum correlation coefficient
 * @param dimensions
 *       how many dimensions does the test file have?
 * @return
 *       true if more than the half of all correlation coefficients are below the supremum coefficient.
 */
public static boolean isCorrMatrixValid(RealMatrix corrMatrix, double corrCoeff, int dimensions) {
    int count = 0;
    int maxVariance = sum(dimensions - 1) / 2;
    for (int i = 1; i < corrMatrix.getColumnDimension(); i++) {
        for (int j = 0; j < i; j++) {
            if (corrMatrix.getEntry(i, j) > corrCoeff) {
                if (count < maxVariance)
                    count++;
                else
                    return false;
            }
        }
    }
    return true;
}

From source file:edu.mit.genecircuits.GcUtils.java

/** Convert dense Apache Commons to Colt matrix */
static public DoubleMatrix2D apache2colt(RealMatrix apache) {

    // Convert apache commons matrix to colt
    DoubleMatrix2D colt = new DenseDoubleMatrix2D(apache.getRowDimension(), apache.getColumnDimension());
    for (int i = 0; i < apache.getRowDimension(); i++)
        for (int j = 0; j < apache.getColumnDimension(); j++)
            colt.set(i, j, apache.getEntry(i, j));

    return colt;/* www.java  2s  . c  o  m*/
}

From source file:edu.cudenver.bios.matrix.OrthogonalPolynomials.java

/**
 * Computes orthogonal polynomial contrasts for the specified data values.  Currently only
 * supports fitting (not prediction contrasts).  
 * //from ww w  . j av  a 2 s. co  m
 *    @param x the points at which the polynomials will be evaluated
 * @param maxDegree contrasts will be computed for degrees 1 to maxDegree
 * @return matrix containing 0th,1st, 2nd,...maxDegree-th degree contrasts in each column
 * @throws IllegalArgumentException
 */
public static RealMatrix orthogonalPolynomialCoefficients(double[] x, int maxDegree)
        throws IllegalArgumentException {
    if (x == null)
        throw new IllegalArgumentException("no data specified");
    if (maxDegree < 1)
        throw new IllegalArgumentException("max polynomial degree must be greater than 1");
    // count number of unique values
    HashSet<Double> s = new HashSet<Double>();
    for (double i : x)
        s.add(i);
    int uniqueCount = s.size();
    if (maxDegree >= uniqueCount)
        throw new IllegalArgumentException(
                "max polynomial degree must be less than the number of unique points");

    // center the data
    double xBar = StatUtils.mean(x);
    double[] xCentered = new double[x.length];
    for (int i = 0; i < x.length; i++)
        xCentered[i] = x[i] - xBar;
    // compute an "outer product" of the centered x vector and a vector 
    // containing the sequence 0 to maxDegree-1, but raise the x values
    // to the power in the sequence array
    double[][] xOuter = new double[x.length][maxDegree + 1];
    int row = 0;
    for (double xValue : xCentered) {
        for (int col = 0; col <= maxDegree; col++) {
            xOuter[row][col] = Math.pow(xValue, col);
        }
        row++;
    }
    // do some mysterious QR decomposition stuff.  See Emerson (1968)
    RealMatrix outerVector = new Array2DRowRealMatrix(xOuter);
    QRDecomposition qrDecomp = new QRDecomposition(outerVector);

    RealMatrix z = MatrixUtils.getDiagonalMatrix(qrDecomp.getR());
    RealMatrix raw = qrDecomp.getQ().multiply(z);

    // column sum of squared elements in raw
    double[] normalizingConstants = new double[raw.getColumnDimension()];
    for (int col = 0; col < raw.getColumnDimension(); col++) {
        normalizingConstants[col] = 0;
        for (row = 0; row < raw.getRowDimension(); row++) {
            double value = raw.getEntry(row, col);
            normalizingConstants[col] += value * value;
        }
    }

    // now normalize the raw values
    for (int col = 0; col < raw.getColumnDimension(); col++) {
        double normalConstantSqrt = Math.sqrt(normalizingConstants[col]);
        for (row = 0; row < raw.getRowDimension(); row++) {
            raw.setEntry(row, col, raw.getEntry(row, col) / normalConstantSqrt);
        }
    }

    return raw;
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java

/**
 * Elementwise multiplication of two matrices.
 *
 * @param A a matrix that is multiplied by the elements of B
 * @param B another matrix//from  w w  w.  j a va 2  s.com
 * @throws DimensionMismatchException
 */
public static void multiplyElementsBy(RealMatrix A, RealMatrix B) throws DimensionMismatchException {
    int nrow = A.getRowDimension();
    int ncol = A.getColumnDimension();
    if (nrow != B.getRowDimension()) {
        throw new DimensionMismatchException(nrow, B.getRowDimension());
    }
    if (ncol != B.getColumnDimension()) {
        throw new DimensionMismatchException(ncol, B.getColumnDimension());
    }

    RealMatrix M = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            A.multiplyEntry(i, j, B.getEntry(i, j));
        }
    }
}