当前位置: 首页>>代码示例>>Java>>正文


Java MatrixUtils.createRealIdentityMatrix方法代码示例

本文整理汇总了Java中org.apache.commons.math3.linear.MatrixUtils.createRealIdentityMatrix方法的典型用法代码示例。如果您正苦于以下问题:Java MatrixUtils.createRealIdentityMatrix方法的具体用法?Java MatrixUtils.createRealIdentityMatrix怎么用?Java MatrixUtils.createRealIdentityMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.math3.linear.MatrixUtils的用法示例。


在下文中一共展示了MatrixUtils.createRealIdentityMatrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: translateToCenter

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Translate the algebraic form of the ellipsoid to the center.
 * 
 * @param center
 *            vector containing the center of the ellipsoid.
 * @param a
 *            the algebraic form of the polynomial.
 * @return the center translated form of the algebraic ellipsoid.
 */
private RealMatrix translateToCenter(RealVector center, RealMatrix a)
{
	// Form the corresponding translation matrix.
	RealMatrix t = MatrixUtils.createRealIdentityMatrix(4);

	RealMatrix centerMatrix = new Array2DRowRealMatrix(1, 3);

	centerMatrix.setRowVector(0, center);

	t.setSubMatrix(centerMatrix.getData(), 3, 0);

	// Translate to the center.
	RealMatrix r = t.multiply(a).multiply(t.transpose());

	return r;
}
 
开发者ID:jiaminghan,项目名称:droidplanner-master,代码行数:26,代码来源:FitPoints.java

示例2: testGLSOLSConsistency

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Verifies that GLS with identity covariance matrix gives the same results
 * as OLS.
 */
@Test
public void testGLSOLSConsistency() {      
    RealMatrix identityCov = MatrixUtils.createRealIdentityMatrix(16);
    GLSMultipleLinearRegression glsModel = new GLSMultipleLinearRegression();
    OLSMultipleLinearRegression olsModel = new OLSMultipleLinearRegression();
    glsModel.newSampleData(longley, 16, 6);
    olsModel.newSampleData(longley, 16, 6);
    glsModel.newCovarianceData(identityCov.getData());
    double[] olsBeta = olsModel.calculateBeta().toArray();
    double[] glsBeta = glsModel.calculateBeta().toArray();
    // TODO:  Should have assertRelativelyEquals(double[], double[], eps) in TestUtils
    //        Should also add RealVector and RealMatrix versions
    for (int i = 0; i < olsBeta.length; i++) {
        TestUtils.assertRelativelyEquals(olsBeta[i], glsBeta[i], 10E-7);
    }
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:21,代码来源:GLSMultipleLinearRegressionTest.java

示例3: getPaddedTransitionMatrix

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Pad the transition matrix with extra hidden states. The old transition matrix will be embedded on the upper left
 * corner of an identity matrix. As a result, there will be no mixing between the existing states and the hidden
 * states.
 *
 * Note: padding is performed for convenience and the extra hidden states are meant to be inaccessible. It is the
 * user's responsibility to make sure that the prior probabilities prohibits the occupancy of these states.
 *
 * @param originalTransitionMatrix the original non-padded transition matrix
 * @param padding non-negative number of extra hidden states to pad
 * @return an instance of {@link IntegerCopyNumberTransitionMatrix}
 */
private static RealMatrix getPaddedTransitionMatrix(final RealMatrix originalTransitionMatrix, final int padding) {
    if (padding > 0) {
        final int maxCopyNumber = originalTransitionMatrix.getColumnDimension() - 1;
        final int newMaxCopyNumber = maxCopyNumber + padding;
        final RealMatrix paddedTransitionMatrix = MatrixUtils.createRealIdentityMatrix(newMaxCopyNumber + 1);
        for (int i = 0; i <= maxCopyNumber; i++) {
            for (int j = 0; j <= maxCopyNumber; j++) {
                paddedTransitionMatrix.setEntry(i, j, originalTransitionMatrix.getEntry(i, j));
            }
        }
        return paddedTransitionMatrix;
    } else {
        return originalTransitionMatrix;
    }
}
 
开发者ID:broadinstitute,项目名称:gatk-protected,代码行数:28,代码来源:IntegerCopyNumberTransitionMatrix.java

示例4: HybridLinUCB

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public HybridLinUCB(double alpha) {
	this.alpha = alpha;
	AMap = new HashMap<String, RealMatrix>();
	bMap = new HashMap<String, RealMatrix>();
	BMap = new HashMap<String, RealMatrix>();

	// Need to double check that it is 6 long
	double[] zeroArrayKLong = new double[36];
	Double zero = new Double(0);
	Arrays.fill(zeroArrayKLong, zero);
	A0 = MatrixUtils.createRealIdentityMatrix(36);
	b0 = MatrixUtils.createColumnRealMatrix(zeroArrayKLong);
	BetaHat = MatrixUtils.inverse(A0).multiply(b0);

}
 
开发者ID:tankle,项目名称:Bandit4J,代码行数:16,代码来源:HybridLinUCB.java

示例5: getScaleMatrix

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public RealMatrix getScaleMatrix() {
    RealMatrix matrix = MatrixUtils.createRealIdentityMatrix(4);

    matrix = matrix.scalarMultiply(scale);
    matrix.setEntry(3, 3, 1);

    return matrix;
}
 
开发者ID:plushmonkey,项目名称:modelviewer,代码行数:9,代码来源:Transform.java

示例6: getRotationMatrix

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public RealMatrix getRotationMatrix() {
    RealMatrix A = MatrixUtils.createRealMatrix(this.rotation.getMatrix());

    RealMatrix result = MatrixUtils.createRealIdentityMatrix(4);
    for (int i = 0; i < 3; ++i)
        result.setColumnVector(i, A.getColumnVector(i).append(0));
    return result;
}
 
开发者ID:plushmonkey,项目名称:modelviewer,代码行数:9,代码来源:Transform.java

示例7: correct

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z the measurement vector
 * @throws DimensionMismatchException if the dimension of the
 * measurement vector does not fit
 * @throws org.apache.commons.math3.linear.SingularMatrixException
 * if the covariance matrix could not be inverted
 */
public void correct(final RealVector z) {
    // sanity checks
    MathUtils.checkNotNull(z);
    if (z.getDimension() != measurementMatrix.getRowDimension()) {
        throw new DimensionMismatchException(z.getDimension(),
                                             measurementMatrix.getRowDimension());
    }

    // S = H * P(k) - * H' + R
    RealMatrix s = measurementMatrix.multiply(errorCovariance)
        .multiply(measurementMatrixT)
        .add(measurementModel.getMeasurementNoise());

    // invert S
    // as the error covariance matrix is a symmetric positive
    // semi-definite matrix, we can use the cholesky decomposition
    DecompositionSolver solver = new CholeskyDecomposition(s).getSolver();
    RealMatrix invertedS = solver.getInverse();

    // Inn = z(k) - H * xHat(k)-
    RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));

    // calculate gain matrix
    // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
    // K(k) = P(k)- * H' * S^-1
    RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS);

    // update estimate with measurement z(k)
    // xHat(k) = xHat(k)- + K * Inn
    stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));

    // update covariance of prediction error
    // P(k) = (I - K * H) * P(k)-
    RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
    errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
 
开发者ID:jiaminghan,项目名称:droidplanner-master,代码行数:46,代码来源:KalmanFilter.java

示例8: getDirectMatrixPower

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
private static RealMatrix getDirectMatrixPower(final RealMatrix mat, final int power) {
    if (power < 0) {
        throw new IllegalArgumentException("Can not calculate negative matrix powers");
    } else if (power == 0) {
        return MatrixUtils.createRealIdentityMatrix(mat.getColumnDimension());
    } else {
        return mat.power(power);
    }
}
 
开发者ID:broadinstitute,项目名称:gatk-protected,代码行数:10,代码来源:IntegerCopyNumberTransitionProbabilityCacheCollectionUnitTest.java

示例9: correct

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z
 *            the measurement vector
 * @throws NullArgumentException
 *             if the measurement vector is {@code null}
 * @throws DimensionMismatchException
 *             if the dimension of the measurement vector does not fit
 * @throws SingularMatrixException
 *             if the covariance matrix could not be inverted
 */
public void correct(final RealVector z) throws NullArgumentException,
           DimensionMismatchException, SingularMatrixException
{

	// sanity checks
	MathUtils.checkNotNull(z);
	if (z.getDimension() != measurementMatrix.getRowDimension())
	{
		throw new DimensionMismatchException(z.getDimension(),
				measurementMatrix.getRowDimension());
	}

	// S = H * P(k) * H' + R
	RealMatrix s = measurementMatrix.multiply(errorCovariance)
			.multiply(measurementMatrixT)
			.add(measurementModel.getMeasurementNoise());

	// Inn = z(k) - H * xHat(k)-
	RealVector innovation = z.subtract(measurementMatrix
			.operate(stateEstimation));

	// calculate gain matrix
	// K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
	// K(k) = P(k)- * H' * S^-1

	// instead of calculating the inverse of S we can rearrange the formula,
	// and then solve the linear equation A x X = B with A = S', X = K' and
	// B = (H * P)'

	// K(k) * S = P(k)- * H'
	// S' * K(k)' = H * P(k)-'
	RealMatrix kalmanGain = new CholeskyDecomposition(s).getSolver()
			.solve(measurementMatrix.multiply(errorCovariance.transpose()))
			.transpose();

	// update estimate with measurement z(k)
	// xHat(k) = xHat(k)- + K * Inn
	stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));

	// update covariance of prediction error
	// P(k) = (I - K * H) * P(k)-
	RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain
			.getRowDimension());
	errorCovariance = identity.subtract(
			kalmanGain.multiply(measurementMatrix)).multiply(
			errorCovariance);
}
 
开发者ID:KalebKE,项目名称:FSensor,代码行数:60,代码来源:RotationKalmanFilter.java

示例10: chooseArm

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public Article chooseArm(User user, List<Article> articles) {
	Article bestA = null;
	double bestArmP = Double.MIN_VALUE;

	RealMatrix Aa;
	RealMatrix Ba;
	RealMatrix ba;

	for (Article a : articles) {
		String aId = a.getId();
		if (!AMap.containsKey(aId)) {
			Aa = MatrixUtils.createRealIdentityMatrix(6);
			AMap.put(aId, Aa); // set as identity for now and we will update
								// in reward

			double[] zeros = { 0, 0, 0, 0, 0, 0 };
			ba = MatrixUtils.createColumnRealMatrix(zeros);
			bMap.put(aId, ba);

			double[][] BMapZeros = new double[6][36];
			for (double[] row : BMapZeros) {
				Arrays.fill(row, 0.0);
			}
			Ba = MatrixUtils.createRealMatrix(BMapZeros);
			BMap.put(aId, Ba);
		} else {
			Aa = AMap.get(aId);
			ba = bMap.get(aId);
			Ba = BMap.get(aId);
		}

		// Make column vector out of features
		RealMatrix xta = MatrixUtils
				.createColumnRealMatrix(a.getFeatures());
		RealMatrix zta = makeZta(
				MatrixUtils.createColumnRealMatrix(user.getFeatures()), xta);

		// Set up common variables
		RealMatrix A0Inverse = MatrixUtils.inverse(A0);
		RealMatrix AaInverse = MatrixUtils.inverse(Aa);
		RealMatrix ztaTranspose = zta.transpose();
		RealMatrix BaTranspose = Ba.transpose();
		RealMatrix xtaTranspose = xta.transpose();

		// Find theta
		RealMatrix theta = AaInverse.multiply(ba.subtract(Ba
				.multiply(BetaHat)));
		// Find sta
		RealMatrix staMatrix = ztaTranspose.multiply(A0Inverse).multiply(
				zta);
		staMatrix = staMatrix.subtract(ztaTranspose.multiply(A0Inverse)
				.multiply(BaTranspose).multiply(AaInverse).multiply(xta)
				.scalarMultiply(2));
		staMatrix = staMatrix.add(xtaTranspose.multiply(AaInverse)
				.multiply(xta));
		staMatrix = staMatrix.add(xtaTranspose.multiply(AaInverse)
				.multiply(Ba).multiply(A0Inverse).multiply(BaTranspose)
				.multiply(AaInverse).multiply(xta));

		// Find pta for arm
		RealMatrix ptaMatrix = ztaTranspose.multiply(BetaHat);
		ptaMatrix = ptaMatrix.add(xtaTranspose.multiply(theta));
		double ptaVal = ptaMatrix.getData()[0][0];
		double staVal = staMatrix.getData()[0][0];
		ptaVal = ptaVal + alpha * Math.sqrt(staVal);

		// Update argmax
		if (ptaVal > bestArmP) {
			bestArmP = ptaVal;
			bestA = a;
		}
	}
	return bestA;
}
 
开发者ID:tankle,项目名称:Bandit4J,代码行数:75,代码来源:HybridLinUCB.java

示例11: chooseArm

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public Article chooseArm(User user, List<Article> articles) {
	Article bestA = null;
	double bestArmP = Double.MIN_VALUE;
	RealMatrix Aa;
	RealMatrix ba;
	for (Article a : articles) {
		
		String aId = a.getId();
		double[] articleFeatureV = a.getFeatures();

		// If not contained, then make new identity matrix and zero vector
		if (!AMap.containsKey(aId)) {
			Aa = MatrixUtils
					.createRealIdentityMatrix(articleFeatureV.length);

			double[] zeros = new double[articleFeatureV.length];
			AMap.put(aId, Aa); //set as identity for now and we will update in reward
			ba = MatrixUtils.createColumnRealMatrix(zeros);
			bMap.put(aId, ba);
		} else {
			Aa = AMap.get(aId);
			ba = bMap.get(aId);
		}
		// Make column vector out of features
		RealMatrix xta = MatrixUtils
				.createColumnRealMatrix(articleFeatureV);
		RealMatrix theta = MatrixUtils.inverse(Aa).multiply(ba);
		// Will have to index into matrix of one value after multiplication
		double newP = theta.transpose().multiply(xta).getData()[0][0]
				+ alpha
				* Math.sqrt(xta.transpose()
						.multiply(MatrixUtils.inverse(Aa)).multiply(xta)
						.getData()[0][0]);
		// Update argmax
		if (newP > bestArmP) {
			bestArmP = newP;
			bestA = a;
		}
	}
	return bestA;
}
 
开发者ID:tankle,项目名称:Bandit4J,代码行数:42,代码来源:LinUCB.java

示例12: Transform

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public Transform() {
    this.scale = 1.0;
    this.rotation = Rotation.IDENTITY;
    this.translation = Vector3D.ZERO;
    this.matrix = MatrixUtils.createRealIdentityMatrix(4);
}
 
开发者ID:plushmonkey,项目名称:modelviewer,代码行数:7,代码来源:Transform.java

示例13: correct

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z
 *            the measurement vector
 * @throws NullArgumentException
 *             if the measurement vector is {@code null}
 * @throws DimensionMismatchException
 *             if the dimension of the measurement vector does not fit
 * @throws SingularMatrixException
 *             if the covariance matrix could not be inverted
 */
public void correct(final RealVector z)
        throws NullArgumentException, DimensionMismatchException, SingularMatrixException {

    // sanity checks
    MathUtils.checkNotNull(z);
    if (z.getDimension() != measurementMatrix.getRowDimension()) {
        throw new DimensionMismatchException(z.getDimension(),
                                             measurementMatrix.getRowDimension());
    }

    // S = H * P(k) * H' + R
    RealMatrix s = measurementMatrix.multiply(errorCovariance)
        .multiply(measurementMatrixT)
        .add(measurementModel.getMeasurementNoise());

    // Inn = z(k) - H * xHat(k)-
    RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));

    // calculate gain matrix
    // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
    // K(k) = P(k)- * H' * S^-1

    // instead of calculating the inverse of S we can rearrange the formula,
    // and then solve the linear equation A x X = B with A = S', X = K' and B = (H * P)'

    // K(k) * S = P(k)- * H'
    // S' * K(k)' = H * P(k)-'
    RealMatrix kalmanGain = new CholeskyDecomposition(s).getSolver()
            .solve(measurementMatrix.multiply(errorCovariance.transpose()))
            .transpose();

    // update estimate with measurement z(k)
    // xHat(k) = xHat(k)- + K * Inn
    stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));

    // update covariance of prediction error
    // P(k) = (I - K * H) * P(k)-
    RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
    errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
 
开发者ID:biocompibens,项目名称:SME,代码行数:53,代码来源:KalmanFilter.java

示例14: translateToCenter

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Translate the algebraic form of the ellipsoid to the center.
 *
 * @param center vector containing the center of the ellipsoid.
 * @param a      the algebraic form of the polynomial.
 * @return the center translated form of the algebraic ellipsoid.
 */
private RealMatrix translateToCenter(RealVector center, RealMatrix a) {
    // Form the corresponding translation matrix.
    RealMatrix t = MatrixUtils.createRealIdentityMatrix(4);

    RealMatrix centerMatrix = new Array2DRowRealMatrix(1, 3);

    centerMatrix.setRowVector(0, center);

    t.setSubMatrix(centerMatrix.getData(), 3, 0);

    // Translate to the center.
    RealMatrix r = t.multiply(a).multiply(t.transpose());

    return r;
}
 
开发者ID:MarcProe,项目名称:lp2go,代码行数:23,代码来源:FitPoints.java

示例15: testHat

import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
 * Test hat matrix computation
 *
 */
@Test
public void testHat() {

    /*
     * This example is from "The Hat Matrix in Regression and ANOVA",
     * David C. Hoaglin and Roy E. Welsch,
     * The American Statistician, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
     *
     */
    double[] design = new double[] {
            11.14, .499, 11.1,
            12.74, .558, 8.9,
            13.13, .604, 8.8,
            11.51, .441, 8.9,
            12.38, .550, 8.8,
            12.60, .528, 9.9,
            11.13, .418, 10.7,
            11.7, .480, 10.5,
            11.02, .406, 10.5,
            11.41, .467, 10.7
    };

    int nobs = 10;
    int nvars = 2;

    // Estimate the model
    OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
    model.newSampleData(design, nobs, nvars);

    RealMatrix hat = model.calculateHat();

    // Reference data is upper half of symmetric hat matrix
    double[] referenceData = new double[] {
            .418, -.002,  .079, -.274, -.046,  .181,  .128,  .222,  .050,  .242,
                   .242,  .292,  .136,  .243,  .128, -.041,  .033, -.035,  .004,
                          .417, -.019,  .273,  .187, -.126,  .044, -.153,  .004,
                                 .604,  .197, -.038,  .168, -.022,  .275, -.028,
                                        .252,  .111, -.030,  .019, -.010, -.010,
                                               .148,  .042,  .117,  .012,  .111,
                                                      .262,  .145,  .277,  .174,
                                                             .154,  .120,  .168,
                                                                    .315,  .148,
                                                                           .187
    };

    // Check against reference data and verify symmetry
    int k = 0;
    for (int i = 0; i < 10; i++) {
        for (int j = i; j < 10; j++) {
            Assert.assertEquals(referenceData[k], hat.getEntry(i, j), 10e-3);
            Assert.assertEquals(hat.getEntry(i, j), hat.getEntry(j, i), 10e-12);
            k++;
        }
    }

    /*
     * Verify that residuals computed using the hat matrix are close to
     * what we get from direct computation, i.e. r = (I - H) y
     */
    double[] residuals = model.estimateResiduals();
    RealMatrix I = MatrixUtils.createRealIdentityMatrix(10);
    double[] hatResiduals = I.subtract(hat).operate(model.getY()).toArray();
    TestUtils.assertEquals(residuals, hatResiduals, 10e-12);
}
 
开发者ID:Quanticol,项目名称:CARMA,代码行数:69,代码来源:OLSMultipleLinearRegressionTest.java


注:本文中的org.apache.commons.math3.linear.MatrixUtils.createRealIdentityMatrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。