List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix copy
@Override
public RealMatrix copy()
From source file:broadwick.math.Matrix.java
/** * Create a matrix from the internal representation of the matrix (copy construtor). This constructor * is intentionally private to hide the internal representation of the matrix. * @param data the data to be copied.//www.j a v a 2 s . co m */ private Matrix(final Array2DRowRealMatrix data) { this.numRows = data.getRowDimension(); this.numCols = data.getColumnDimension(); this.data = (Array2DRowRealMatrix) data.copy(); }
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 a2 s . c om*/ } } } } 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: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);//w w w.j av 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; } 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 w w w . ja v 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++) { 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 . jav a 2 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:com.clust4j.data.DataSet.java
public DataSet(Array2DRowRealMatrix data, int[] labels, String[] hdrz, MatrixFormatter formatter, boolean copyData) { /*// we should allow this behavior... if(null == labels)//from www . j av a 2 s.c o m throw new IllegalArgumentException("labels cannot be null"); */ if (null == data) throw new IllegalArgumentException("data cannot be null"); if (null == hdrz) this.headers = genHeaders(data.getColumnDimension()); else this.headers = VecUtils.copy(hdrz); // Check to make sure dims match up... if ((null != labels) && labels.length != data.getRowDimension()) throw new DimensionMismatchException(labels.length, data.getRowDimension()); if (this.headers.length != data.getColumnDimension()) throw new DimensionMismatchException(this.headers.length, data.getColumnDimension()); this.data = copyData ? (Array2DRowRealMatrix) data.copy() : data; this.labels = VecUtils.copy(labels); this.formatter = null == formatter ? DEF_FORMATTER : formatter; }
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);//from w ww . ja v 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); } }