Example usage for org.apache.commons.math3.linear Array2DRowRealMatrix setColumn

List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix setColumn

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear Array2DRowRealMatrix setColumn.

Prototype

public void setColumn(final int column, final double[] array)
        throws OutOfRangeException, MatrixDimensionMismatchException 

Source Link

Usage

From source file:lanchester.MultiArena.java

public void step() {
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);//w w w . jav  a 2  s . co m
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    //        for(int i1=0;i1<eVals.length;i1++){
    //            System.out.println("eVals["+i1+"] = "+eVals[i1]);
    //        }
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            AthenaForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getForceSize() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    MultiTimeStep currentStep = new MultiTimeStep(numFoes);
    currentTime += timeStep;
    currentStep.setTime(currentTime);
    for (int i1 = 0; i1 < numFoes; i1++) {
        double updatedForce = 0.;
        for (int i2 = 0; i2 < numFoes; i2++) {
            updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
            //                updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep);
            //                updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep);
        }
        forces.get(i1).updateForce(updatedForce);
        currentStep.setForceNumber(updatedForce, i1);
    }
    history.add(currentStep);
    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (true) {
        //            System.out.println("time = " + time);
    }
}

From source file:MultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix./*from ww  w  .  j a  v a2  s .c  o  m*/
 * <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);
}

From source file:lanchester.MultiArena2.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);//www  . j a v a  2  s  . c  o  m
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    //        for(int i1=0;i1<eVals.length;i1++){
    //            System.out.println("eVals["+i1+"] = "+eVals[i1]);
    //        }
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            AthenaForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getForceSize() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    int cntr = 0;
    int numGone;
    do {
        MultiTimeStep currentStep = new MultiTimeStep(numFoes);
        currentTime += timeStep;
        currentCycle += timeStep;
        currentStep.setTime(currentTime);
        numGone = 0;
        for (int i1 = 0; i1 < numFoes; i1++) {
            double updatedForce = 0.;
            if (forces.get(i1).getForceSize() > lb) {
                for (int i2 = 0; i2 < numFoes; i2++) {
                    //                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);
                    if (updatedForce < 1.) {
                        updatedForce = 0.;
                        numGone++;
                    }
                    //                updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep);
                    //                updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep);
                }
            } else {
                updatedForce = lb / 2.;
                numGone++;
            }
            forces.get(i1).updateForce(updatedForce);
            currentStep.setForceNumber(updatedForce, i1);
        }
        history.add(currentStep);
        aboveFloor = checkAboveFloors();
        cntr++;
    } while (aboveFloor && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setFloor(i1, i2);
            }
        }
    }

    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:lanchester.MultiArena3.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        currentCeiling = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                    currentCeiling[i1][i2] = 100.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);//from www .ja  v a  2s  .c o m
                    setCeiling(i1, i2);
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            MultiForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getNumber() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    boolean belowCeiling = true;
    int cntr = 0;
    int numGone;
    do {
        timeStep = determineTimeStep();
        MultiTimeStep currentStep = new MultiTimeStep(numFoes);
        currentTime += timeStep;
        currentCycle += timeStep;
        currentStep.setTime(currentTime);
        numGone = 0;
        for (int i1 = 0; i1 < numFoes; i1++) {
            double updatedForce = 0.;
            if (stillAlive[i1]) {
                for (int i2 = 0; i2 < numFoes; i2++) {
                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);
                }
                if (updatedForce < 1.) {
                    updatedForce = lb;
                    stillAlive[i1] = false;
                    numGone++;
                }
            } else {
                numGone++;
                updatedForce = lb;
            }
            forces.get(i1).updateForce(updatedForce);
            currentStep.setForceNumber(updatedForce, i1);
        }
        history.add(currentStep);
        aboveFloor = checkAboveFloors();
        belowCeiling = checkBelowCeilings();
        cntr++;
    } while (aboveFloor && belowCeiling && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setFloor(i1, i2);
            }
        }
    }

    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:lanchester.MultiArena4.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        currentCeiling = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            //                int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                //                    int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                    currentCeiling[i1][i2] = 1000000.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    //                        setStances(i1,i2);
                    setFloor(i1, i2);//from   w  w w .ja  v a2 s  .  co m
                    setCeiling(i1, i2);
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            MultiForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getNumber() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    boolean belowCeiling = true;
    int cntr = 0;
    int numGone;
    do {
        timeStep = determineTimeStep();
        MultiTimeStep currentStep = new MultiTimeStep(numFoes);
        currentTime += timeStep;
        currentCycle += timeStep;
        currentStep.setTime(currentTime);
        numGone = 0;
        for (int i1 = 0; i1 < numFoes; i1++) {
            double updatedForce = 0.;
            //                if (forces.get(i1).getForceSize() > lb) {
            if (stillAlive[i1]) {
                for (int i2 = 0; i2 < numFoes; i2++) {
                    //                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);

                    //                updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep);
                    //                updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep);
                }
                if (updatedForce < 1.) {
                    updatedForce = lb;
                    stillAlive[i1] = false;
                    numGone++;
                }
            } else {
                updatedForce = lb;
                numGone++;
            }
            forces.get(i1).updateForce(updatedForce);
            currentStep.setForceNumber(updatedForce, i1);
            //                for (int i2 = 0; i2 < numFoes; i1++) {
            //                    if (i1 != i2) {
            //
            //                    }
            //                }
        }
        history.add(currentStep);
        aboveFloor = checkAboveFloors();
        belowCeiling = checkBelowCeilings();
        cntr++;
    } while (aboveFloor && belowCeiling && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                //                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setStances(i1, i2);
                setFloor(i1, i2);
                setCeiling(i1, i2);
            }
        }
    }

    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:net2.N2MultiArena.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    System.out.println("Num foes = " + numFoes);
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        currentCeiling = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            //                int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                //                    int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                    currentCeiling[i1][i2] = 1000000.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);/*w w w  .jav  a 2  s  .  c om*/
                    setCeiling(i1, i2);
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            N2ForceUnit f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getNumber() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    boolean belowCeiling = true;
    //        int cntr = 0;
    int numGone;
    //        do {
    timeStep = determineTimeStep();
    MultiTimeStep currentStep = new MultiTimeStep(numFoes);
    currentTime += timeStep;
    currentCycle += timeStep;
    currentStep.setTime(currentTime);
    numGone = 0;
    for (int i1 = 0; i1 < numFoes; i1++) {
        double updatedForce = 0.;
        //                if (forces.get(i1).getForceSize() > lb) {
        if (stillAlive[i1]) {
            for (int i2 = 0; i2 < numFoes; i2++) {
                //                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
                updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);

            }
            if (updatedForce < 1.) {
                updatedForce = lb;
                stillAlive[i1] = false;
                numGone++;
            }
        } else {
            updatedForce = lb;
            numGone++;
        }
        forces.get(i1).updateForce(updatedForce);
        currentStep.setForceNumber(updatedForce, i1);
    }
    history.add(currentStep);
    aboveFloor = checkAboveFloors();
    belowCeiling = checkBelowCeilings();
    //            cntr++;
    //        } while (aboveFloor && belowCeiling && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                //                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setStances(i1, i2);
                setFloor(i1, i2);
                setCeiling(i1, i2);
            }
        }
    }

    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:org.orekit.files.ccsds.OEMParserTest.java

@Test
public void testParseOEM1() throws OrekitException, IOException {
    //// ww w .j  a va  2s.  c om
    final String ex = "/ccsds/OEMExample.txt";
    final InputStream inEntry = getClass().getResourceAsStream(ex);
    final OEMParser parser = new OEMParser().withMu(CelestialBodyFactory.getEarth().getGM());
    final OEMFile file = parser.parse(inEntry, "OEMExample.txt");
    Assert.assertEquals(TimeSystem.UTC, file.getTimeSystem());
    Assert.assertEquals("MARS GLOBAL SURVEYOR",
            file.getEphemeridesBlocks().get(0).getMetaData().getObjectName());
    Assert.assertEquals("1996-062A", file.getEphemeridesBlocks().get(0).getMetaData().getObjectID());
    Assert.assertEquals("MARS BARYCENTER", file.getEphemeridesBlocks().get(0).getMetaData().getCenterName());
    Assert.assertEquals(1996, file.getEphemeridesBlocks().get(0).getMetaData().getLaunchYear());
    Assert.assertEquals(62, file.getEphemeridesBlocks().get(0).getMetaData().getLaunchNumber());
    Assert.assertEquals("A", file.getEphemeridesBlocks().get(0).getMetaData().getLaunchPiece());
    Assert.assertFalse(file.getEphemeridesBlocks().get(0).getMetaData().getHasCreatableBody());
    Assert.assertNull(file.getEphemeridesBlocks().get(0).getMetaData().getCenterBody());
    Assert.assertEquals(new AbsoluteDate(1996, 12, 18, 12, 00, 0.331, TimeScalesFactory.getUTC()),
            file.getEphemeridesBlocks().get(0).getStartTime());
    Assert.assertEquals(new AbsoluteDate(1996, 12, 28, 21, 28, 0.331, TimeScalesFactory.getUTC()),
            file.getEphemeridesBlocks().get(0).getStopTime());
    Assert.assertEquals(new AbsoluteDate(1996, 12, 18, 12, 10, 0.331, TimeScalesFactory.getUTC()),
            file.getEphemeridesBlocks().get(0).getUseableStartTime());
    Assert.assertEquals(new AbsoluteDate(1996, 12, 28, 21, 23, 0.331, TimeScalesFactory.getUTC()),
            file.getEphemeridesBlocks().get(0).getUseableStopTime());
    Assert.assertEquals("HERMITE", file.getEphemeridesBlocks().get(0).getInterpolationMethod());
    Assert.assertEquals(7, file.getEphemeridesBlocks().get(0).getInterpolationDegree());
    ArrayList<String> ephemeridesDataLinesComment = new ArrayList<String>();
    ephemeridesDataLinesComment.add("This file was produced by M.R. Somebody, MSOO NAV/JPL, 1996NOV 04. It is");
    ephemeridesDataLinesComment.add("to be used for DSN scheduling purposes only.");
    Assert.assertEquals(ephemeridesDataLinesComment,
            file.getEphemeridesBlocks().get(0).getEphemeridesDataLinesComment());
    CartesianOrbit orbit = new CartesianOrbit(
            new PVCoordinates(new Vector3D(2789.619 * 1000, -280.045 * 1000, -1746.755 * 1000),
                    new Vector3D(4.73372 * 1000, -2.49586 * 1000, -1.04195 * 1000)),
            FramesFactory.getEME2000(), new AbsoluteDate("1996-12-18T12:00:00.331", TimeScalesFactory.getUTC()),
            CelestialBodyFactory.getEarth().getGM());
    Assert.assertArrayEquals(
            orbit.getPVCoordinates().getPosition().toArray(), file.getEphemeridesBlocks().get(0)
                    .getEphemeridesDataLines().get(0).getOrbit().getPVCoordinates().getPosition().toArray(),
            1e-10);
    Assert.assertArrayEquals(
            orbit.getPVCoordinates().getVelocity().toArray(), file.getEphemeridesBlocks().get(0)
                    .getEphemeridesDataLines().get(0).getOrbit().getPVCoordinates().getVelocity().toArray(),
            1e-10);
    Assert.assertArrayEquals((new Vector3D(1, 1, 1)).toArray(),
            file.getEphemeridesBlocks().get(1).getEphemeridesDataLines().get(0).getAcceleration().toArray(),
            1e-10);
    final Array2DRowRealMatrix covMatrix = new Array2DRowRealMatrix(6, 6);
    final double[] column1 = { 3.331349476038534e-04, 4.618927349220216e-04, -3.070007847730449e-04,
            -3.349365033922630e-07, -2.211832501084875e-07, -3.041346050686871e-07 };
    final double[] column2 = { 4.618927349220216e-04, 6.782421679971363e-04, -4.221234189514228e-04,
            -4.686084221046758e-07, -2.864186892102733e-07, -4.989496988610662e-07 };
    final double[] column3 = { -3.070007847730449e-04, -4.221234189514228e-04, 3.231931992380369e-04,
            2.484949578400095e-07, 1.798098699846038e-07, 3.540310904497689e-07 };
    final double[] column4 = { -3.349365033922630e-07, -4.686084221046758e-07, 2.484949578400095e-07,
            4.296022805587290e-10, 2.608899201686016e-10, 1.869263192954590e-10 };
    final double[] column5 = { -2.211832501084875e-07, -2.864186892102733e-07, 1.798098699846038e-07,
            2.608899201686016e-10, 1.767514756338532e-10, 1.008862586240695e-10 };
    final double[] column6 = { -3.041346050686871e-07, -4.989496988610662e-07, 3.540310904497689e-07,
            1.869263192954590e-10, 1.008862586240695e-10, 6.224444338635500e-10 };
    covMatrix.setColumn(0, column1);
    covMatrix.setColumn(1, column2);
    covMatrix.setColumn(2, column3);
    covMatrix.setColumn(3, column4);
    covMatrix.setColumn(4, column5);
    covMatrix.setColumn(5, column6);
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 6; j++) {
            Assert.assertEquals(covMatrix.getEntry(i, j), file.getEphemeridesBlocks().get(2)
                    .getCovarianceMatrices().get(0).getMatrix().getEntry(i, j), 1e-10);
        }
    }
    Assert.assertEquals(new AbsoluteDate("1996-12-28T21:29:07.267", TimeScalesFactory.getUTC()),
            file.getEphemeridesBlocks().get(2).getCovarianceMatrices().get(0).getEpoch());
    Assert.assertEquals(FramesFactory.getEME2000(),
            file.getEphemeridesBlocks().get(2).getCovarianceMatrices().get(1).getFrame());
}

From source file:org.orekit.files.ccsds.OPMParserTest.java

@Test
public void testParseOPM3() throws OrekitException, URISyntaxException {
    // simple test for OPM file, contains all mandatory information plus
    // Spacecraft parameters and the position/velocity Covariance Matrix.
    final String name = getClass().getResource("/ccsds/OPMExample3.txt").toURI().getPath();
    OPMParser parser = new OPMParser().withConventions(IERSConventions.IERS_2010);
    final OPMFile file = parser.parse(name);
    Assert.assertEquals(//  w  ww  . j av  a  2s.c  o m
            new AbsoluteDate(1998, 12, 18, 14, 28, 15.1172,
                    TimeScalesFactory.getGMST(IERSConventions.IERS_2010, false)),
            file.getMetaData().getFrameEpoch());
    // Check Data Covariance matrix Block
    ArrayList<String> dataCovMatrixComment = new ArrayList<String>();
    dataCovMatrixComment.add("toto");
    dataCovMatrixComment.add("tata");
    Assert.assertEquals(dataCovMatrixComment, file.getCovarianceComment());
    Assert.assertTrue(file.hasCovarianceMatrix());
    Assert.assertEquals(file.getCovRefFrame(), FramesFactory.getTEME());

    Array2DRowRealMatrix covMatrix = new Array2DRowRealMatrix(6, 6);
    double[] column1 = { 3.331349476038534e-04, 4.618927349220216e-04, -3.070007847730449e-04,
            -3.349365033922630e-07, -2.211832501084875e-07, -3.041346050686871e-07 };
    double[] column2 = { 4.618927349220216e-04, 6.782421679971363e-04, -4.221234189514228e-04,
            -4.686084221046758e-07, -2.864186892102733e-07, -4.989496988610662e-07 };
    double[] column3 = { -3.070007847730449e-04, -4.221234189514228e-04, 3.231931992380369e-04,
            2.484949578400095e-07, 1.798098699846038e-07, 3.540310904497689e-07 };
    double[] column4 = { -3.349365033922630e-07, -4.686084221046758e-07, 2.484949578400095e-07,
            4.296022805587290e-10, 2.608899201686016e-10, 1.869263192954590e-10 };
    double[] column5 = { -2.211832501084875e-07, -2.864186892102733e-07, 1.798098699846038e-07,
            2.608899201686016e-10, 1.767514756338532e-10, 1.008862586240695e-10 };
    double[] column6 = { -3.041346050686871e-07, -4.989496988610662e-07, 3.540310904497689e-07,
            1.869263192954590e-10, 1.008862586240695e-10, 6.224444338635500e-10 };
    covMatrix.setColumn(0, column1);
    covMatrix.setColumn(1, column2);
    covMatrix.setColumn(2, column3);
    covMatrix.setColumn(3, column4);
    covMatrix.setColumn(4, column5);
    covMatrix.setColumn(5, column6);
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 6; j++) {
            Assert.assertEquals(covMatrix.getEntry(i, j), file.getCovarianceMatrix().getEntry(i, j), 1e-15);
        }
    }

    // Check User defined Parameters Block
    HashMap<String, String> userDefinedParameters = new HashMap<String, String>();
    userDefinedParameters.put("USER_DEFINED_EARTH_MODEL", "WGS-84");
    userDefinedParameters.put("USER_DEFINED_TOTO", "TITI");
    Assert.assertEquals(userDefinedParameters, file.getUserDefinedParameters());

}

From source file:org.pmad.gmm.MyMND.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.//from   w w  w  .j  a  va  2  s.co  m
 * <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 MyMND(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);
    double msum = 0;
    for (int i = 0; i < covariances.length; i++) {
        for (int j = 0; j < covariances.length; j++) {
            msum += covariances[i][j];
        }
    }
    msum /= covariances.length * covariances.length;
    //        System.out.print("in");
    MyEDC covMatDec = null;
    double a = -1;
    while (true) {
        try {

            covarianceMatrix = new Array2DRowRealMatrix(covariances);

            covMatDec = new MyEDC(covarianceMatrix);

            // Compute and store the inverse.
            covarianceMatrixInverse = covMatDec.getSolver().getInverse();
            a *= -1;
            break;
        } catch (NoDataException e) {
            e.printStackTrace();
        } catch (NullArgumentException e) {
            e.printStackTrace();
        } catch (MathArithmeticException e) {
            e.printStackTrace();
        } catch (SingularMatrixException e) {
            //            System.out.print("S");
            for (int i = 0; i < covariances.length; i++) {
                double add = covariances[i][i] == 0 ? msum : covariances[i][i];
                covariances[i][i] += new Random().nextDouble() * add * 0.01;
            }
        }
        //         catch (MaxCountExceededException e) {
        ////            e.printStackTrace();
        ////            System.out.print("M"+msum);
        //            for (int i = 0; i < covariances.length; i++) {
        //               for (int j = i; j < covariances.length; j++) {
        //                  double add = covariances[i][j] == 0?msum:covariances[i][j];
        //                  add = new Random().nextDouble()*add*0.1*a;
        //                  covariances[i][j] += add;
        //                  covariances[j][i] += add;
        //               }
        //            }
        ////            break;
        //         }
    }
    // 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);
}

From source file:xyz.lejon.sampling.FastMultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix./*  w w w.  ja  v a  2 s. c  o  m*/
 * <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 FastMultivariateNormalDistribution(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);
    final Array2DRowRealMatrix tmpMatrix = new Array2DRowRealMatrix(dim, dim);
    for (int v = 0; v < dim; v++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[v]);
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
        tmpMatrix.setRow(v, evec);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(v, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}