/** * Get the height of the tableau. * @return height of the tableau */ protected final int getHeight() { return tableau.getRowDimension(); }
/** * Get a fresh copy of the underlying data array. * * @return a copy of the underlying data array. */ private double[][] copyOut() { final int nRows = this.getRowDimension(); final double[][] out = new double[nRows][this.getColumnDimension()]; // can't copy 2-d array in one shot, otherwise get row references for (int i = 0; i < nRows; i++) { System.arraycopy(data[i], 0, out[i], 0, data[i].length); } return out; }
void printMatrix(Array2DRowRealMatrix a) { for (int i = 0; i < a.getRowDimension(); i++) { for (int j = 0; j < a.getColumnDimension(); j++) System.out.print(a.getEntry(i, j) + " "); System.out.println(); } } }
/** * * @param features * Matrix with feature vectors as rows * @return A list with all changing points detected in the file */ private LinkedList<Integer> getAllChangingPoints(Array2DRowRealMatrix features) { LinkedList<Integer> ret = new LinkedList<Integer>(); ret.add(0); int framesCount = features.getRowDimension(), step = 500; int start = 0, end = step, cp; while (end < framesCount) { cp = getPoint(start, end - start + 1, step / 10, features); if (cp > 0) { start = cp; end = start + step; ret.add(cp); } else end += step; } ret.add(framesCount); return ret; }
/** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += data[i][col] * v[i]; } out[col] = sum; } return out; }
/** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; row++) { final double[] dataRow = data[row]; double sum = 0; for (int i = 0; i < nCols; i++) { sum += dataRow[i] * v[i]; } out[row] = sum; } return out; }
/** * * @param bicValue * The bicValue of the model represented by only one Gaussian. * This parameter it's useful when this function is called * repeatedly for different frame values and the same features * parameter * @param frame * the frame which is tested for being a change point * @param features * the feature vectors matrix * @return the likelihood ratio */ double getLikelihoodRatio(double bicValue, int frame, Array2DRowRealMatrix features) { double bicValue1, bicValue2; int d = Segment.FEATURES_SIZE; double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(features.getRowDimension()) * 2; int nrows = features.getRowDimension(), ncols = features.getColumnDimension(); Array2DRowRealMatrix sub1, sub2; sub1 = (Array2DRowRealMatrix) features.getSubMatrix(0, frame - 1, 0, ncols - 1); sub2 = (Array2DRowRealMatrix) features.getSubMatrix(frame, nrows - 1, 0, ncols - 1); bicValue1 = getBICValue(sub1); bicValue2 = getBICValue(sub2); return (bicValue - bicValue1 - bicValue2 - penalty); }
double computeDistance(SpeakerCluster c1, SpeakerCluster c2) { int rowDim = c1.getFeatureMatrix().getRowDimension() + c2.getFeatureMatrix().getRowDimension(); int colDim = c1.getFeatureMatrix().getColumnDimension(); Array2DRowRealMatrix combinedFeatures = new Array2DRowRealMatrix(rowDim, colDim); combinedFeatures.setSubMatrix(c1.getFeatureMatrix().getData(), 0, 0); combinedFeatures.setSubMatrix(c2.getFeatureMatrix().getData(), c1.getFeatureMatrix() .getRowDimension(), 0); double bicValue = getBICValue(combinedFeatures); double d = Segment.FEATURES_SIZE; double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(combinedFeatures.getRowDimension()) * 2; return bicValue - c1.getBicValue() - c2.getBicValue() - penalty; }
public void mergeWith(SpeakerCluster target) throws NullPointerException { if (target == null) throw new NullPointerException(); Iterator<Segment> it = target.segmentSet.iterator(); while (it.hasNext()) { if (!this.addSegment(it.next())) System.out.println("Something doesn't work in mergeWith method, Cluster class"); } int rowDim = featureMatrix.getRowDimension() + target.getFeatureMatrix().getRowDimension(); int colDim = featureMatrix.getColumnDimension(); Array2DRowRealMatrix combinedFeatures = new Array2DRowRealMatrix(rowDim, colDim); combinedFeatures.setSubMatrix(featureMatrix.getData(), 0, 0); combinedFeatures .setSubMatrix(target.getFeatureMatrix().getData(), featureMatrix.getRowDimension(), 0); bicValue = SpeakerIdentification.getBICValue(combinedFeatures); featureMatrix = new Array2DRowRealMatrix(combinedFeatures.getData()); } }
/** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { visitor.visit(i, j, data[i][j]); } } return visitor.end(); }
/** * @param mat * A matrix with feature vectors as rows. * @return Returns the BICValue of the Gaussian model that approximates the * the feature vectors data samples */ public static double getBICValue(Array2DRowRealMatrix mat) { double ret = 0; EigenDecomposition ed = new EigenDecomposition(new Covariance(mat).getCovarianceMatrix()); double[] re = ed.getRealEigenvalues(); for (int i = 0; i < re.length; i++) ret += Math.log(re[i]); return ret * (mat.getRowDimension() / 2); }
/** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int i = 0; i < rows; ++i) { final double[] rowI = data[i]; for (int j = 0; j < columns; ++j) { rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); }
/** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { final double[] rowI = data[i]; rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); }
/** * Compute the sum of {@code this} and {@code m}. * * @param m Matrix to be added. * @return {@code this + m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public Array2DRowRealMatrix add(final Array2DRowRealMatrix m) throws MatrixDimensionMismatchException { // Safety check. MatrixUtils.checkAdditionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final double[][] outData = new double[rowCount][columnCount]; for (int row = 0; row < rowCount; row++) { final double[] dataRow = data[row]; final double[] mRow = m.data[row]; final double[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col] + mRow[col]; } } return new Array2DRowRealMatrix(outData, false); }
/** * Returns {@code this} minus {@code m}. * * @param m Matrix to be subtracted. * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public Array2DRowRealMatrix subtract(final Array2DRowRealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final double[][] outData = new double[rowCount][columnCount]; for (int row = 0; row < rowCount; row++) { final double[] dataRow = data[row]; final double[] mRow = m.data[row]; final double[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col] - mRow[col]; } } return new Array2DRowRealMatrix(outData, false); }
/** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int i = 0; i < rows; ++i) { final double[] rowI = data[i]; for (int j = 0; j < columns; ++j) { visitor.visit(i, j, rowI[j]); } } return visitor.end(); }
/** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int j = startColumn; j <= endColumn; ++j) { for (int i = startRow; i <= endRow; ++i) { visitor.visit(i, j, data[i][j]); } } return visitor.end(); }
/** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int i = startRow; i <= endRow; ++i) { final double[] rowI = data[i]; for (int j = startColumn; j <= endColumn; ++j) { visitor.visit(i, j, rowI[j]); } } return visitor.end(); }
/** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int j = startColumn; j <= endColumn; ++j) { for (int i = startRow; i <= endRow; ++i) { final double[] rowI = data[i]; rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); }
/** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int i = startRow; i <= endRow; ++i) { final double[] rowI = data[i]; for (int j = startColumn; j <= endColumn; ++j) { rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); }