List of usage examples for org.apache.commons.math.linear RealMatrix setColumn
void setColumn(int column, double[] array) throws MatrixIndexException, InvalidMatrixException;
column as a column matrix. From source file:geogebra.kernel.implicit.GeoImplicitPoly.java
/** * make curve through given points/*from w w w . j a v a 2s . c o m*/ * @param points ArrayList of points */ public void throughPoints(ArrayList<GeoPoint> points) { if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) { setUndefined(); return; } int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1; int realDegree = degree; RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1); RealMatrix matrix = new RealMatrixImpl(points.size(), points.size()); double[][] coeffMatrix = new double[degree + 1][degree + 1]; DecompositionSolver solver; double[] matrixRow = new double[points.size() + 1]; double[] results; for (int i = 0; i < points.size(); i++) { double x = points.get(i).x / points.get(i).z; double y = points.get(i).y / points.get(i).z; for (int j = 0, m = 0; j < degree + 1; j++) for (int k = 0; j + k != degree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } int solutionColumn = 0, noPoints = points.size(); do { if (solutionColumn > noPoints) { noPoints = noPoints - realDegree - 1; if (noPoints < 2) { setUndefined(); return; } extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1); realDegree -= 1; matrixRow = new double[noPoints + 1]; for (int i = 0; i < noPoints; i++) { double x = points.get(i).x; double y = points.get(i).y; for (int j = 0, m = 0; j < realDegree + 1; j++) for (int k = 0; j + k != realDegree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } matrix = new RealMatrixImpl(noPoints, noPoints); solutionColumn = 0; } results = extendMatrix.getColumn(solutionColumn); for (int i = 0, j = 0; i < noPoints + 1; i++) if (i == solutionColumn) continue; else matrix.setColumn(j++, extendMatrix.getColumn(i)); solutionColumn++; solver = new LUDecompositionImpl(matrix).getSolver(); } while (!solver.isNonSingular()); for (int i = 0; i < results.length; i++) results[i] *= -1; double[] partialSolution = solver.solve(results); double[] solution = new double[partialSolution.length + 1]; for (int i = 0, j = 0; i < solution.length; i++) if (i == solutionColumn - 1) solution[i] = 1; else { solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j]; j++; } for (int i = 0, k = 0; i < realDegree + 1; i++) for (int j = 0; i + j < realDegree + 1; j++) coeffMatrix[i][j] = solution[k++]; this.setCoeff(coeffMatrix, true); this.defined = true; for (int i = 0; i < points.size(); i++) if (!this.isOnPath(points.get(i), 1)) { this.setUndefined(); return; } }
From source file:geogebra.kernel.GeoImplicitPoly.java
/** * make curve through given points//from ww w . j a va 2 s .c o m * @param points ArrayList of points */ public void throughPoints(ArrayList<GeoPoint> points) { if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) { setUndefined(); return; } if (pointsOnCurve == null) pointsOnCurve = new Coords[points.size()]; for (int i = 0; i < points.size(); i++) { if (pointsOnCurve[i] == null) pointsOnCurve[i] = new Coords(points.get(i).x, points.get(i).y, points.get(i).z); else { pointsOnCurve[i].setX(points.get(i).x); pointsOnCurve[i].setY(points.get(i).y); pointsOnCurve[i].setZ(points.get(i).z); } } int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1; int realDegree = degree; RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1); RealMatrix matrix = new RealMatrixImpl(points.size(), points.size()); double[][] coeffMatrix = new double[degree + 1][degree + 1]; DecompositionSolver solver; double[] matrixRow = new double[points.size() + 1]; double[] results = new double[points.size()]; for (int i = 0; i < points.size(); i++) { double x = points.get(i).x / points.get(i).z; double y = points.get(i).y / points.get(i).z; for (int j = 0, m = 0; j < degree + 1; j++) for (int k = 0; j + k != degree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } int solutionColumn = 0, noPoints = points.size(); do { if (solutionColumn > noPoints) { noPoints = noPoints - realDegree - 1; if (noPoints < 2) { setUndefined(); return; } extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1); realDegree -= 1; matrixRow = new double[noPoints + 1]; for (int i = 0; i < noPoints; i++) { double x = points.get(i).x; double y = points.get(i).y; for (int j = 0, m = 0; j < realDegree + 1; j++) for (int k = 0; j + k != realDegree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } matrix = new RealMatrixImpl(noPoints, noPoints); solutionColumn = 0; } results = extendMatrix.getColumn(solutionColumn); for (int i = 0, j = 0; i < noPoints; i++) if (i == solutionColumn) continue; else matrix.setColumn(j++, extendMatrix.getColumn(i)); solutionColumn++; solver = new LUDecompositionImpl(matrix).getSolver(); } while (!solver.isNonSingular()); for (int i = 0; i < results.length; i++) results[i] *= -1; double[] partialSolution = solver.solve(results); for (int i = 0; i < partialSolution.length; i++) if (Kernel.isZero(partialSolution[i])) partialSolution[i] = 0; for (int i = 0; i < partialSolution.length; i++) if (Kernel.isZero(partialSolution[i])) partialSolution[i] = 0; for (int i = 0, k = 0; i < realDegree + 1; i++) for (int j = 0; j + i != realDegree + 1; j++) if (k == solutionColumn - 1) coeffMatrix[i][j] = 1; else coeffMatrix[i][j] = partialSolution[k++]; this.setCoeff(coeffMatrix); this.update(); this.defined = true; for (int i = 0; i < points.size(); i++) if (!this.isOnPath(points.get(i))) { this.setUndefined(); return; } this.type = IMPLICIT_POLY_THROUGH_POINTS; }
From source file:geogebra.common.kernel.implicit.GeoImplicitPoly.java
/** * make curve through given points//www. j a v a2 s .c om * @param points ArrayList of points */ public void throughPoints(ArrayList<GeoPoint> points) { if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) { setUndefined(); return; } int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1; int realDegree = degree; RealMatrix extendMatrix = new Array2DRowRealMatrix(points.size(), points.size() + 1); RealMatrix matrix = new Array2DRowRealMatrix(points.size(), points.size()); double[][] coeffMatrix = new double[degree + 1][degree + 1]; DecompositionSolver solver; double[] matrixRow = new double[points.size() + 1]; double[] results; for (int i = 0; i < points.size(); i++) { double x = points.get(i).x / points.get(i).z; double y = points.get(i).y / points.get(i).z; for (int j = 0, m = 0; j < degree + 1; j++) for (int k = 0; j + k != degree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } int solutionColumn = 0, noPoints = points.size(); do { if (solutionColumn > noPoints) { noPoints = noPoints - realDegree - 1; if (noPoints < 2) { setUndefined(); return; } extendMatrix = new Array2DRowRealMatrix(noPoints, noPoints + 1); realDegree -= 1; matrixRow = new double[noPoints + 1]; for (int i = 0; i < noPoints; i++) { double x = points.get(i).x; double y = points.get(i).y; for (int j = 0, m = 0; j < realDegree + 1; j++) for (int k = 0; j + k != realDegree + 1; k++) matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k); extendMatrix.setRow(i, matrixRow); } matrix = new Array2DRowRealMatrix(noPoints, noPoints); solutionColumn = 0; } results = extendMatrix.getColumn(solutionColumn); for (int i = 0, j = 0; i < noPoints + 1; i++) { if (i == solutionColumn) continue; matrix.setColumn(j++, extendMatrix.getColumn(i)); } solutionColumn++; solver = new LUDecompositionImpl(matrix).getSolver(); } while (!solver.isNonSingular()); for (int i = 0; i < results.length; i++) results[i] *= -1; double[] partialSolution = solver.solve(results); double[] solution = new double[partialSolution.length + 1]; for (int i = 0, j = 0; i < solution.length; i++) if (i == solutionColumn - 1) solution[i] = 1; else { solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j]; j++; } for (int i = 0, k = 0; i < realDegree + 1; i++) for (int j = 0; i + j < realDegree + 1; j++) coeffMatrix[i][j] = solution[k++]; this.setCoeff(coeffMatrix, true); setDefined(); for (int i = 0; i < points.size(); i++) if (!this.isOnPath(points.get(i), 1)) { this.setUndefined(); return; } }