List of usage examples for org.apache.commons.math3.linear RealMatrix copy
RealMatrix copy();
From source file:ch.zhaw.iamp.rct.weights.Weights.java
private static RealMatrix addNoise(final RealMatrix A) { RealMatrix buffer = A.copy(); RealMatrix noise = MatrixUtils.createRealMatrix(A.getRowDimension(), A.getColumnDimension()); for (int i = 0; i < A.getRowDimension(); ++i) { for (int j = 0; j < A.getColumnDimension(); ++j) { double noiseSign = Math.random() > 0.5 ? 1 : -1; double noiseAmplitude = 1; noise.setEntry(i, j, noiseSign * Math.random() * noiseAmplitude); }//from w w w.j a v a 2 s . com } buffer = buffer.add(noise); return buffer; }
From source file:com.github.tteofili.looseen.yay.RectifierFunction.java
@Override public RealMatrix applyMatrix(RealMatrix weights) { RealMatrix matrix = weights.copy(); matrix.walkInOptimizedOrder(new RealMatrixChangingVisitor() { @Override/*w w w . jav a 2 s .c o m*/ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } @Override public double visit(int row, int column, double value) { return Math.max(0, value); } @Override public double end() { return 0; } }); return matrix; }
From source file:com.github.tteofili.looseen.yay.SoftmaxActivationFunction.java
public RealMatrix applyMatrix(RealMatrix weights) { RealMatrix matrix = weights.copy(); final double finalD = expDen(matrix); matrix.walkInOptimizedOrder(new RealMatrixChangingVisitor() { @Override/*w w w .ja v a 2s. c o m*/ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } @Override public double visit(int row, int column, double value) { return Math.exp(value) / finalD; } @Override public double end() { return 0; } }); return matrix; }
From source file:io.github.malapert.jwcs.coordsystem.Utility.java
/** * Remove the elliptic component of annual aberration when this is included * in a catalogue fk4 position./*from w w w . j a v a 2s .c om*/ * * Notes: * ------ * Return a new position where the elliptic terms of aberration * are removed i.e. convert a apparent position from a catalog to * a mean place. * The effects of ecliptic aberration were included in the * catalog positions to facilitate telescope pointing. * See also notes at 'addEterms'. * * @param xyz vector xyz * @param eterm E-terms vector (as returned by getEterms()). If input a is * omitted (== *null*), the e-terms for 1950 will be substituted. * @return **Mean place** */ public final static RealMatrix removeEterms(final RealMatrix xyz, RealMatrix eterm) { RealMatrix xyzeterm = xyz.copy(); if (eterm == null) { eterm = FK4.getEterms(1950); } double x = xyz.getEntry(0, 0); double y = xyz.getEntry(1, 0); double z = xyz.getEntry(2, 0); x -= eterm.getEntry(0, 0); y -= eterm.getEntry(0, 1); z -= eterm.getEntry(0, 2); xyzeterm.setEntry(0, 0, x); xyzeterm.setEntry(1, 0, y); xyzeterm.setEntry(2, 0, z); return xyzeterm; }
From source file:io.github.malapert.jwcs.coordsystem.Utility.java
/** * Add the elliptic component of annual aberration when the rsult must be a * catalogue fk4 position.//from w ww . ja v a2s . c o m * * Reference: * ---------- * * Seidelman, P.K., 1992. Explanatory Supplement to the Astronomical * Almanac. University Science Books, Mill Valley. * * Yallop et al, Transformation of mean star places, * AJ, 1989, vol 97, page 274 * * Stumpff, On the relation between Classical and Relativistic * Theory of Stellar Aberration, Astron, Astrophys, 84, 257-259 (1980) * * Notes: * ------ * There is a so called ecliptic component in the stellar aberration. * This vector depends on the epoch at which we want to process * these terms. It corresponds to the component of the earth's velocity * perpendicular to the major axis of the ellipse in the ecliptic. * The E-term corrections are as follows. A catalog FK4 position * include corrections for elliptic terms of aberration. * These positions are apparent places. For precession and/or * rotations to other sky systems, one processes only mean places. * So to get a mean place, one has to remove the E-terms vector. * The ES suggests for the removal to use a decompositions of the * E-term vector along the unit circle to get the approximate * new vector, which has almost the correct angle and has almost * length 1. The advantage is that when we add the E-term vector * to this new vector, we obtain a new vector with the original * angle, but with a length unequal to 1, which makes it suitable * for closure tests. * However, the procedure can be made more rigorous: * For the subtraction we subtract the E-term vector from the * start vector and normalize it afterwards. Then we have an * exact new angle (opposed to the approximation in the ES). * The procedure to go from a vector in the mean place system to * a vector in the system of apparent places is a bit more * complicated: * Find a value for lambda so that the current vector is * adjusted in length so that adding the e-term vector gives a new * vector with length 1. This is by definition the new vector * with the right angle. * * @param xyz Cartesian position(s) converted from lonlat * @param eterm E-terms vector (as returned by getEterms()). If input *a* * is omitted (i.e. *a == null*), the e-terms for 1950 will be substituted. * @return **Apparent place**, */ public final static RealMatrix addEterms(final RealMatrix xyz, RealMatrix eterm) { RealMatrix xyzeterm = xyz.copy(); if (eterm == null) { eterm = FK4.getEterms(1950); } double x = xyz.getEntry(0, 0); double y = xyz.getEntry(1, 0); double z = xyz.getEntry(2, 0); // Normalize to get a vector of length 1. Our algorithm is based on that fact. double d = Math.sqrt(x * x + y * y + z * z); x /= d; y /= d; z /= d; // Find the lambda to stretch the vector double w = 2.0d * (eterm.getEntry(0, 0) * x + eterm.getEntry(0, 1) * y + eterm.getEntry(0, 2) * z); double p = eterm.getEntry(0, 0) * eterm.getEntry(0, 0) + eterm.getEntry(0, 1) * eterm.getEntry(0, 1) + eterm.getEntry(0, 2) * eterm.getEntry(0, 2) - 1.0d; double lambda1 = (-1 * w + Math.sqrt(w * w - 4.0d * p)) / 2.0d; //Vector a is small. We want only the positive lambda x = lambda1 * x + eterm.getEntry(0, 0); y = lambda1 * y + eterm.getEntry(0, 1); z = lambda1 * z + eterm.getEntry(0, 2); xyzeterm.setEntry(0, 0, x); xyzeterm.setEntry(1, 0, y); xyzeterm.setEntry(2, 0, z); return xyzeterm; }
From source file:mase.app.allocation.AllocationProblem.java
private DescriptiveStatistics pairDistances(RealMatrix distMatrix) { DescriptiveStatistics ds = new DescriptiveStatistics(); distMatrix = distMatrix.copy(); for (int k = 0; k < numAgents; k++) { // find closest pair double min = Double.POSITIVE_INFINITY; int minI = -1, minJ = -1; for (int i = 0; i < numAgents; i++) { for (int j = 0; j < numAgents; j++) { double d = distMatrix.getEntry(i, j); if (!Double.isNaN(d) && d < min) { min = d;//from ww w . j a v a2 s .c om minI = i; minJ = j; } } } ds.addValue(min); distMatrix.setRow(minI, nulify); distMatrix.setColumn(minJ, nulify); } return ds; }
From source file:com.joptimizer.optimizers.OptimizationRequestHandler.java
/** * Find a solution of the linear (equalities) system A.x = b. * @see "S.Boyd and L.Vandenberghe, Convex Optimization, p. 682" *//* w w w . j a v a 2 s . c o m*/ protected double[] findEqFeasiblePoint(double[][] A, double[] b) throws Exception { RealMatrix AT = new Array2DRowRealMatrix(A).transpose(); int p = A.length; SingularValueDecomposition dFact1 = new SingularValueDecomposition(AT); int rangoAT = dFact1.getRank(); if (rangoAT != p) { throw new RuntimeException("Equalities matrix A must have full rank"); } QRDecomposition dFact = new QRDecomposition(AT); //A = QR RealMatrix Q1Q2 = dFact.getQ(); RealMatrix R0 = dFact.getR(); RealMatrix Q1 = Q1Q2.getSubMatrix(0, AT.getRowDimension() - 1, 0, p - 1); RealMatrix R = R0.getSubMatrix(0, p - 1, 0, p - 1); double[][] rData = R.copy().getData(); //inversion double[][] rInvData = Utils.upperTriangularMatrixUnverse(rData); RealMatrix RInv = new Array2DRowRealMatrix(rInvData); //w = Q1 * Inv([R]T) . b double[] w = Q1.operate(RInv.transpose().operate(new ArrayRealVector(b))).toArray(); return w; }
From source file:com.itemanalysis.psychometrics.factoranalysis.VarimaxCriteria.java
/** * Computes the function value for varimax rotation. * * @param L matrix of factor loadings./* w ww . ja va 2s.co m*/ */ public void computeValues(RealMatrix L) { //initialize dimensions and column mean array int nrow = L.getRowDimension(); int ncol = L.getColumnDimension(); Mean[] colMean = new Mean[ncol]; for (int i = 0; i < ncol; i++) { colMean[i] = new Mean(); } //square each element in matrix RealMatrix L2 = L.copy(); double value = 0.0; for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { value = L.getEntry(i, j); value *= value; L2.setEntry(i, j, value); colMean[j].increment(value); } } double dif = 0.0; RealMatrix QL = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { dif = L2.getEntry(i, j) - colMean[j].getResult(); QL.setEntry(i, j, dif); } } //compute gradientAt gradient = new Array2DRowRealMatrix(nrow, ncol); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { value = -L.getEntry(i, j) * QL.getEntry(i, j); gradient.setEntry(i, j, value); } } //compute function value RealMatrix B = QL.transpose().multiply(QL); double sum = B.getTrace(); functionValue = -sum / 4.0; }
From source file:edu.stanford.cfuller.imageanalysistools.fitting.NelderMeadMinimizer.java
/** * Runs the minimization of the specified function starting from an initial simplex. * @param f The ObjectiveFunction to be minimized. * @param initialSimplex The initial simplex to use to start optimization, as might be returned from {@link #generateInitialSimplex} * @return The parameters at the function minimum in the same order as specified for each point on the simplex. *//*from w ww .j a v a 2 s. c o m*/ public RealVector optimize(ObjectiveFunction f, RealMatrix initialSimplex) { RealMatrix currentSimplex = initialSimplex.copy(); double currTolVal = 1.0e6; RealVector values = new ArrayRealVector(initialSimplex.getRowDimension(), 0.0); RealVector centerOfMass = new ArrayRealVector(initialSimplex.getColumnDimension(), 0.0); boolean shouldEvaluate = false; long iterCounter = 0; while (Math.abs(currTolVal) > this.tol) { int maxIndex = 0; int minIndex = 0; double maxValue = -1.0 * Double.MAX_VALUE; double minValue = Double.MAX_VALUE; double secondMaxValue = -1.0 * Double.MAX_VALUE; centerOfMass.mapMultiplyToSelf(0.0); if (shouldEvaluate) { for (int i = 0; i < currentSimplex.getRowDimension(); i++) { RealVector currRow = currentSimplex.getRowVector(i); values.setEntry(i, f.evaluate(currRow)); } } for (int i = 0; i < currentSimplex.getRowDimension(); i++) { double currValue = values.getEntry(i); if (currValue < minValue) { minValue = currValue; minIndex = i; } if (currValue > maxValue) { secondMaxValue = maxValue; maxValue = currValue; maxIndex = i; } else if (currValue > secondMaxValue) { secondMaxValue = currValue; } } for (int i = 0; i < currentSimplex.getRowDimension(); i++) { if (i == maxIndex) continue; centerOfMass = centerOfMass.add(currentSimplex.getRowVector(i)); } centerOfMass.mapDivideToSelf(currentSimplex.getRowDimension() - 1); RealVector oldPoint = currentSimplex.getRowVector(maxIndex); RealVector newPoint = centerOfMass.subtract(oldPoint).mapMultiplyToSelf(a).add(centerOfMass); // newpoint = COM + a*(COM-oldpoint) double newValue = f.evaluate(newPoint); if (newValue < secondMaxValue) { // success if (newValue < minValue) { // best found so far //expansion RealVector expPoint = centerOfMass.subtract(oldPoint).mapMultiplyToSelf(g).add(centerOfMass); double expValue = f.evaluate(expPoint); if (expValue < newValue) { currentSimplex.setRowVector(maxIndex, expPoint); currTolVal = 2.0 * (expValue - maxValue) / (1.0e-20 + expValue + maxValue); values.setEntry(maxIndex, expValue); shouldEvaluate = false; continue; } } //reflection currentSimplex.setRowVector(maxIndex, newPoint); currTolVal = 2.0 * (newValue - maxValue) / (1.0e-20 + newValue + maxValue); values.setEntry(maxIndex, newValue); shouldEvaluate = false; continue; } //contraction RealVector conPoint = centerOfMass.subtract(oldPoint).mapMultiplyToSelf(r).add(centerOfMass); double conValue = f.evaluate(conPoint); if (conValue < maxValue) { currentSimplex.setRowVector(maxIndex, conPoint); currTolVal = 2.0 * (conValue - maxValue) / (1.0e-20 + conValue + maxValue); values.setEntry(maxIndex, conValue); shouldEvaluate = false; continue; } //reduction for (int i = 0; i < currentSimplex.getRowDimension(); i++) { if (i == minIndex) continue; currentSimplex.setRowVector(i, currentSimplex.getRowVector(i).subtract(currentSimplex.getRowVector(minIndex)) .mapMultiplyToSelf(s).add(currentSimplex.getRowVector(minIndex))); } double redValue = f.evaluate(currentSimplex.getRowVector(maxIndex)); currTolVal = 2.0 * (redValue - maxValue) / (1.0e-20 + redValue + maxValue); shouldEvaluate = true; if (iterCounter++ > 100000) { System.out.println("stalled? tol: " + currTolVal + " minValue: " + minValue); } } double minValue = Double.MAX_VALUE; RealVector minVector = null; for (int i = 0; i < currentSimplex.getRowDimension(); i++) { values.setEntry(i, f.evaluate(currentSimplex.getRowVector(i))); if (values.getEntry(i) < minValue) { minValue = values.getEntry(i); minVector = currentSimplex.getRowVector(i); } } return minVector; }
From source file:com.itemanalysis.psychometrics.factoranalysis.WeightedLeastSquaresMethod.java
public WeightedLeastSquaresMethod(RealMatrix R, int nFactors, RotationMethod rotationMethod) { this.nVariables = R.getColumnDimension(); this.nParam = nVariables; this.nFactors = nFactors; this.rotationMethod = rotationMethod; this.R = R;//from w w w . j a v a2 s. c o m this.R2 = R.copy(); }