當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。