Refine search
/** * @param m Input matrix. * @return Matrix representing the element-wise square root of m. */ private static RealMatrix sqrt(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = FastMath.sqrt(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); }
protected double[][] evaluate(double[][] d1) throws EvaluationException { Array2DRowRealMatrix temp = new Array2DRowRealMatrix(d1); return temp.transpose().getData(); } };
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()); } }
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(); } } }
/** * 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; }
lambda = 4 + (int) (3 * FastMath.log(dimension)); final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false); logMu2 = FastMath.log(mu + 0.5); weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2); double sumw = 0; double sumwq = 0; for (int i = 0; i < mu; i++) { double w = weights.getEntry(i, 0); sumw += w; sumwq += w * w; (dimension + 4 + 2 * mueff / dimension); cs = (mueff + 2) / (dimension + mueff + 3.); damps = (1 + 2 * FastMath.max(0, FastMath.sqrt((mueff - 1) / (dimension + 1)) - 1)) * FastMath.max(0.3,
ztest = FastMath.max(ztest, FastMath.abs(zMatrix.getEntry(k, j))); final double d1 = zMatrix.getEntry(knew, j); if (FastMath.abs(d1) > ztest) { final double d2 = zMatrix.getEntry(knew, 0); final double d3 = zMatrix.getEntry(knew, j); final double d4 = FastMath.sqrt(d2 * d2 + d3 * d3); final double d5 = zMatrix.getEntry(knew, 0) / d4; final double d6 = zMatrix.getEntry(knew, j) / d4; for (int i = 0; i < npt; i++) { final double d7 = d5 * zMatrix.getEntry(i, 0) + d6 * zMatrix.getEntry(i, j); zMatrix.setEntry(i, j, d5 * zMatrix.getEntry(i, j) - d6 * zMatrix.getEntry(i, 0)); zMatrix.setEntry(i, 0, d7); zMatrix.setEntry(knew, j, ZERO); work.setEntry(i, zMatrix.getEntry(knew, 0) * zMatrix.getEntry(i, 0)); final double d2 = zMatrix.getEntry(knew, 0) / sqrtDenom; for (int i = 0; i < npt; i++) { zMatrix.setEntry(i, 0, d1 * zMatrix.getEntry(i, 0) - d2 * lagrangeValuesAtNewPoint.getEntry(i)); work.setEntry(jp, bMatrix.getEntry(knew, j)); for (int i = 0; i <= jp; i++) { bMatrix.setEntry(i, j,
@Override public double[][] hessian(double[] X) { RealVector x = new ArrayRealVector(X); RealMatrix ret = new Array2DRowRealMatrix(dim, dim); for(int i=0; i<socpConstraintParametersList.size(); i++){ SOCPConstraintParameters param = socpConstraintParametersList.get(i); double t = this.buildT(param, x); RealVector u = this.buildU(param, x); double t2uu = t*t - u.dotProduct(u); RealVector t2u = u.mapMultiply(-2*t); RealMatrix Jacob = this.buildJ(param, x); int k = u.getDimension(); RealMatrix H = new Array2DRowRealMatrix(k+1, k+1); RealMatrix ID = MatrixUtils.createRealIdentityMatrix(k); H.setSubMatrix(ID.scalarMultiply(t2uu).add(u.outerProduct(u).scalarMultiply(2)).getData(), 0, 0); H.setSubMatrix(new double[][]{t2u.toArray()}, k, 0); for(int j=0; j<k; j++){ H.setEntry(j, k, t2u.getEntry(j)); } H.setEntry(k, k, t*t+u.dotProduct(u)); RealMatrix ret_i = Jacob.multiply(H).multiply(Jacob.transpose()).scalarMultiply(2./Math.pow(t2uu, 2)); ret = ret.add(ret_i); } return ret.getData(); }
transitionMatrixT = transitionMatrix.transpose(); controlMatrix = new Array2DRowRealMatrix(); } else { controlMatrix = processModel.getControlMatrix(); measurementMatrixT = measurementMatrix.transpose(); stateEstimation = new ArrayRealVector(transitionMatrix.getColumnDimension()); } else { stateEstimation = processModel.getInitialStateEstimate(); if (transitionMatrix.getColumnDimension() != stateEstimation.getDimension()) { throw new DimensionMismatchException(transitionMatrix.getColumnDimension(), stateEstimation.getDimension());
/** * Used for computing the actual transformations (A and B matrices). These * are stored in As and Bs. */ private void computeMllrTransforms(double[][][][][] regLs, double[][][][] regRs) { int len; DecompositionSolver solver; RealMatrix coef; RealVector vect, ABloc; for (int c = 0; c < nrOfClusters; c++) { this.As[c] = new float[loader.getNumStreams()][][]; this.Bs[c] = new float[loader.getNumStreams()][]; for (int i = 0; i < loader.getNumStreams(); i++) { len = loader.getVectorLength()[i]; this.As[c][i] = new float[len][len]; this.Bs[c][i] = new float[len]; for (int j = 0; j < len; ++j) { coef = new Array2DRowRealMatrix(regLs[c][i][j], false); solver = new LUDecomposition(coef).getSolver(); vect = new ArrayRealVector(regRs[c][i][j], false); ABloc = solver.solve(vect); for (int k = 0; k < len; ++k) { this.As[c][i][j][k] = (float) ABloc.getEntry(k); } this.Bs[c][i][j] = (float) ABloc.getEntry(len); } } } }
FastMath.abs(previousLogLikelihood - logLikelihood) > threshold) { previousLogLikelihood = logLikelihood; double sumLogLikelihood = 0d; sumLogLikelihood += FastMath.log(rowDensity); newCovMats[j] = new Array2DRowRealMatrix(numCols, numCols); = new Array2DRowRealMatrix(MathArrays.ebeSubtract(data[i], newMeans[j])); final RealMatrix dataCov = vec.multiply(vec.transpose()).scalarMultiply(gamma[i][j]); newCovMats[j] = newCovMats[j].add(dataCov); if (FastMath.abs(previousLogLikelihood - logLikelihood) > threshold) {
/** * Compute the outer product. * * @param v Vector with which outer product should be computed. * @return the matrix outer product between this instance and {@code v}. */ public RealMatrix outerProduct(RealVector v) { final int m = this.getDimension(); final int n = v.getDimension(); final RealMatrix product; if (v instanceof SparseRealVector || this instanceof SparseRealVector) { product = new OpenMapRealMatrix(m, n); } else { product = new Array2DRowRealMatrix(m, n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product.setEntry(i, j, this.getEntry(i) * v.getEntry(j)); } } return product; }
final QRDecomposition decomposition = new QRDecomposition(new Array2DRowRealMatrix(a, false)); final RealMatrix x = decomposition.getSolver().solve(new Array2DRowRealMatrix(b, false)); final Array2DRowRealMatrix truncatedX = new Array2DRowRealMatrix(x.getRowDimension() - 1, x.getColumnDimension()); for (int i = 0; i < truncatedX.getRowDimension(); ++i) { for (int j = 0; j < truncatedX.getColumnDimension(); ++j) { truncatedX.setEntry(i, j, x.getEntry(i, j));
if (b.getRowDimension() != m) { throw new DimensionMismatchException(b.getRowDimension(), m); final int nColB = b.getColumnDimension(); final double[][] bp = new double[m][nColB]; final double[] tmpCol = new double[m]; final double[] vData = v.getDataRef(); double s = 0; for (int j = 0; j < m; ++j) { s += v.getEntry(j) * tmpCol[j]; return new Array2DRowRealMatrix(bp, false);
private RealMatrix computeCov(int k, RealVector mean, double sumGamma){ RealMatrix res = new Array2DRowRealMatrix(data.getColumnDimension(),data.getColumnDimension()); for (int i=0;i<data.getRowDimension();i++){ res = res.add(data.getRowVector(i).outerProduct(data.getRowVector(i)).scalarMultiply(gammas[i][k])); } return res.scalarMultiply(1/sumGamma).subtract(mean.outerProduct(mean)); }
final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int ndim = bMatrix.getRowDimension(); originShift.setEntry(j, currentBest.getEntry(j)); for (int k = 0; k < npt; k++) { interpolationPoints.setEntry(k, j, ZERO); bMatrix.setEntry(i, j, ZERO); zMatrix.setEntry(k, j, ZERO); interpolationPoints.setEntry(nfm, nfmm, stepa); } else if (nfm > n) { stepa = interpolationPoints.getEntry(nfx, nfxm); interpolationPoints.setEntry(nfm, nfxm, stepb); interpolationPoints.setEntry(nfm, iptMinus1, interpolationPoints.getEntry(ipt, iptMinus1)); interpolationPoints.setEntry(nfm, jptMinus1, interpolationPoints.getEntry(jpt, jptMinus1)); currentBest.setEntry(j, FastMath.min(FastMath.max(lowerBound[j], originShift.getEntry(j) + interpolationPoints.getEntry(nfm, j)), upperBound[j])); if (interpolationPoints.getEntry(nfm, j) == lowerDifference.getEntry(j)) { if (interpolationPoints.getEntry(nfm, j) == upperDifference.getEntry(j)) { if (npt < numEval + n) {
/** * @param m Input matrix. * @param cols Columns to select. * @return Matrix representing the selected columns. */ private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) { final double[][] d = new double[m.getRowDimension()][cols.length]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < cols.length; c++) { d[r][c] = m.getEntry(r, cols[c]); } } return new Array2DRowRealMatrix(d, false); }
numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS int height = constraints.size() + getNumObjectiveFunctions(); Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width); matrix.setEntry(0, 0, -1); matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1); RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients(); copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]); matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm()); matrix.setEntry(zIndex, getSlackVariableOffset() - 1, getInvertedCoefficientSum(objectiveCoefficients)); copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]); matrix.setEntry(row, getSlackVariableOffset() - 1, getInvertedCoefficientSum(constraint.getCoefficients())); matrix.setEntry(row, width - 1, constraint.getValue()); matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1); // slack } else if (constraint.getRelationship() == Relationship.GEQ) { matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1); matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1); matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
covarianceMatrix = new Array2DRowRealMatrix(covariances); final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim); for (int v = 0; v < dim; v++) { final double[] evec = covMatDec.getEigenvector(v).toArray(); covMatEigenvectors.setColumn(v, evec); final RealMatrix tmpMatrix = covMatEigenvectors.transpose(); final double factor = FastMath.sqrt(covMatEigenvalues[row]); for (int col = 0; col < dim; col++) { tmpMatrix.multiplyEntry(row, col, factor); samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
throws NullArgumentException, NoDataException, DimensionMismatchException { this(new Array2DRowRealMatrix(stateTransition), new Array2DRowRealMatrix(control), new Array2DRowRealMatrix(processNoise), new ArrayRealVector(initialStateEstimate), new Array2DRowRealMatrix(initialErrorCovariance));