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

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

Introduction

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

Prototype

int getColumnDimension();

Source Link

Document

Returns the number of columns in the matrix.

Usage

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).  
 * /*  w  w  w  .j a v  a2s  .c o  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.yahoo.egads.utilities.SpectralMethods.java

public static RealMatrix averageHankelMatrix(RealMatrix hankelMat, int windowSize) {

    int k = hankelMat.getRowDimension();
    int m = hankelMat.getColumnDimension() / windowSize;
    int n = k + windowSize - 1;

    RealMatrix result = MatrixUtils.createRealMatrix(n, m);

    for (int t = 0; t < n; ++t) {
        int i = (t < windowSize) ? 0 : (t - windowSize + 1);
        int j = (t < windowSize) ? t : (windowSize - 1);
        int counter = 0;

        for (; i < k && j >= 0; ++i, --j, ++counter) {
            for (int h = 0; h < m; ++h) {
                result.addToEntry(t, h, hankelMat.getEntry(i, j * m + h));
            }//from  ww  w  .  jav a  2 s. c o m
        }

        for (int h = 0; h < m; ++h) {
            result.setEntry(t, h, result.getEntry(t, h) / counter);
        }
    }

    return result;
}

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));
        }//  w  w  w  . j  a  v  a  2s .  com
        writer.newline();
    }
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns the sums of columns.//from   w w w.j  ava 2s. c o  m
 *
 * @param matrix The matrix of which the sums of columns to be computed
 * @return A double array of column sums
 */
public static double[] colSums(RealMatrix matrix) {
    // Declare and initialize the return value:
    double[] retval = new double[matrix.getColumnDimension()];

    // Iterate over columns and compute totals:
    for (int col = 0; col < matrix.getColumnDimension(); col++) {
        for (int row = 0; row < matrix.getRowDimension(); row++) {
            retval[col] += matrix.getEntry(row, col);
        }
    }

    // Done, return col sums:
    return retval;
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns the sums of rows./*  www  .j  a va 2 s. com*/
 *
 * @param matrix The matrix of which the sums of rows to be computed
 * @return A double array of row sums.
 */
public static double[] rowSums(RealMatrix matrix) {
    // Declare and initialize the return value:
    double[] retval = new double[matrix.getColumnDimension()];

    // Iterate over columns and compute totals:
    for (int row = 0; row < matrix.getRowDimension(); row++) {
        for (int col = 0; col < matrix.getColumnDimension(); col++) {
            retval[row] += matrix.getEntry(row, col);
        }
    }

    // Done, return col sums:
    return retval;
}

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//from w ww.  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:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns a new matrix by subtracting elements column by column.
 *
 * @param matrix The input matrix//from   w ww  .ja va 2s . co  m
 * @param vector The vector to be subtracted from columns
 * @return A new matrix of which the vector is subtracted column by column
 */
public static RealMatrix colSubtract(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int col = 0; col < retval.length; col++) {
        // Iterate over columns:
        for (int row = 0; row < retval[0].length; row++) {
            retval[row][col] = matrix.getEntry(row, col) - vector[row];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns a new matrix by subtracting elements row by row.
 *
 * @param matrix The input matrix/*from   w  ww  . j a v  a 2  s  .com*/
 * @param vector The vector to be subtracted from rows
 * @return A new matrix of which the vector is subtracted row by row
 */
public static RealMatrix rowSubtract(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int row = 0; row < retval.length; row++) {
        // Iterate over columns:
        for (int col = 0; col < retval[0].length; col++) {
            retval[row][col] = matrix.getEntry(row, col) - vector[col];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns a new matrix by adding elements row by row.
 *
 * @param matrix The input matrix//ww w . j ava 2  s .c  om
 * @param vector The vector to be added to rows
 * @return A new matrix of which the vector is added row by row
 */
public static RealMatrix columnAdd(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int col = 0; col < retval.length; col++) {
        // Iterate over columns:
        for (int row = 0; row < retval[0].length; row++) {
            retval[row][col] = matrix.getEntry(row, col) + vector[row];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}

From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java

/**
 * Returns a new matrix by adding elements row by row.
 *
 * @param matrix The input matrix/*from  w w w.j av a2s.  c om*/
 * @param vector The vector to be added to rows
 * @return A new matrix of which the vector is added row by row
 */
public static RealMatrix rowAdd(RealMatrix matrix, double[] vector) {
    // Declare and initialize the new matrix:
    double[][] retval = new double[matrix.getRowDimension()][matrix.getColumnDimension()];

    // Iterate over rows:
    for (int row = 0; row < retval.length; row++) {
        // Iterate over columns:
        for (int col = 0; col < retval[0].length; col++) {
            retval[row][col] = matrix.getEntry(row, col) + vector[col];
        }
    }

    // Done, return a new matrix:
    return MatrixUtils.createRealMatrix(retval);
}