本文整理汇总了Java中org.apache.commons.math3.linear.Array2DRowRealMatrix.transpose方法的典型用法代码示例。如果您正苦于以下问题:Java Array2DRowRealMatrix.transpose方法的具体用法?Java Array2DRowRealMatrix.transpose怎么用?Java Array2DRowRealMatrix.transpose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.Array2DRowRealMatrix
的用法示例。
在下文中一共展示了Array2DRowRealMatrix.transpose方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MultivariateNormalDistribution
import org.apache.commons.math3.linear.Array2DRowRealMatrix; //导入方法依赖的package包/类
/**
* Creates a multivariate normal distribution with the given mean vector and
* covariance matrix.
* <br/>
* The number of dimensions is equal to the length of the mean vector
* and to the number of rows and columns of the covariance matrix.
* It is frequently written as "p" in formulae.
*
* @param rng Random Number Generator.
* @param means Vector of means.
* @param covariances Covariance matrix.
* @throws DimensionMismatchException if the arrays length are
* inconsistent.
* @throws SingularMatrixException if the eigenvalue decomposition cannot
* be performed on the provided covariance matrix.
* @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
* negative.
*/
public MultivariateNormalDistribution(RandomGenerator rng,
final double[] means,
final double[][] covariances)
throws SingularMatrixException,
DimensionMismatchException,
NonPositiveDefiniteMatrixException {
super(rng, means.length);
final int dim = means.length;
if (covariances.length != dim) {
throw new DimensionMismatchException(covariances.length, dim);
}
for (int i = 0; i < dim; i++) {
if (dim != covariances[i].length) {
throw new DimensionMismatchException(covariances[i].length, dim);
}
}
this.means = MathArrays.copyOf(means);
covarianceMatrix = new Array2DRowRealMatrix(covariances);
// Covariance matrix eigen decomposition.
final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);
// Compute and store the inverse.
covarianceMatrixInverse = covMatDec.getSolver().getInverse();
// Compute and store the determinant.
covarianceMatrixDeterminant = covMatDec.getDeterminant();
// Eigenvalues of the covariance matrix.
final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();
for (int i = 0; i < covMatEigenvalues.length; i++) {
if (covMatEigenvalues[i] < 0) {
throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
}
}
// Matrix where each column is an eigenvector of the covariance matrix.
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();
// Scale each eigenvector by the square root of its eigenvalue.
for (int row = 0; row < dim; row++) {
final double factor = FastMath.sqrt(covMatEigenvalues[row]);
for (int col = 0; col < dim; col++) {
tmpMatrix.multiplyEntry(row, col, factor);
}
}
samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}