List of usage examples for org.apache.commons.math.analysis.polynomials PolynomialFunction add
public PolynomialFunction add(final PolynomialFunction p)
From source file:geogebra.kernel.AlgoIntersectPolynomialConic.java
@Override protected void compute() { double[] A = c.matrix; if (h.isPolynomialFunction(false)) { PolyFunction pf = h.getFunction().getNumericPolynomialDerivative(0); PolynomialFunction y = new PolynomialFunction(pf.getCoeffs()); PolynomialFunction r = new PolynomialFunction(new double[] { A[2], 2 * A[4], A[0] }); r = r.add(y.multiply(new PolynomialFunction(new double[] { 2 * A[5], 2 * A[3] }))); r = r.add(y.multiply(y.multiply(new PolynomialFunction(new double[] { A[1] })))); //Application.debug("r = "+r.toString()); setRootsPolynomial(r);//from www.j av a 2 s .c om } else { Kernel ker = cons.getKernel(); GeoFunction substituteFunctionX = null; ker.setSilentMode(true); /*try { substituteFunctionX = (GeoFunction) ker.getAlgebraProcessor().processValidExpression( ker.getParser().parseGeoGebraExpression("x"))[0]; } catch (MyError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ GeoImplicitPoly iPoly = new GeoImplicitPoly(cons); c.toGeoImplicitPoly(iPoly); GeoFunction paramEquation = new GeoFunction(cons, iPoly, null, h); double nroots = 0; double res[] = new double[2]; if (c.getType() == GeoConicND.CONIC_CIRCLE || c.getType() == GeoConicND.CONIC_ELLIPSE) { nroots = kernel.getEquationSolver().solveQuadratic(new double[] { -A[5] * A[5] + A[1] * A[2], 2 * (A[1] * A[4] - A[3] * A[5]), A[0] * A[1] - A[3] * A[3] }, res); } AlgoRoots algo = null; if (nroots == 2) { //assume res[0]>=res[1] if (res[1] > res[0]) { double temp = res[0]; res[0] = res[1]; res[1] = temp; } algo = new AlgoRoots(cons, paramEquation, new GeoNumeric(cons, Math.max(res[1] - Kernel.MIN_PRECISION, h.getMinParameter())), new GeoNumeric(cons, Math.min(res[0] + Kernel.MIN_PRECISION, h.getMaxParameter()))); } else { algo = new AlgoRoots(cons, paramEquation, new GeoNumeric(cons, h.getMinParameter()), new GeoNumeric(cons, h.getMaxParameter())); } GeoPoint[] points = algo.getRootPoints(); List<double[]> valPairs = new ArrayList<double[]>(); for (int i = 0; i < points.length; i++) { double t = points[i].getX(); valPairs.add(new double[] { t, h.evaluate(t) }); } ker.setSilentMode(false); setPoints(valPairs); return; } }
From source file:geogebra.common.kernel.algos.AlgoIntersectPolynomialConic.java
@Override public void compute() { double[] A = c.getFlatMatrix(); if (h.isPolynomialFunction(false)) { PolyFunction pf = h.getFunction().getNumericPolynomialDerivative(0, false); PolynomialFunction y = new PolynomialFunction(pf.getCoeffs()); PolynomialFunction r = new PolynomialFunction(new double[] { A[2], 2 * A[4], A[0] }); r = r.add(y.multiply(new PolynomialFunction(new double[] { 2 * A[5], 2 * A[3] }))); r = r.add(y.multiply(y.multiply(new PolynomialFunction(new double[] { A[1] })))); // Application.debug("r = "+r.toString()); setRootsPolynomial(r);/*from w w w. j ava 2 s . c o m*/ } else { Kernel ker = cons.getKernel(); ker.setSilentMode(true); /* * try { substituteFunctionX = (GeoFunction) * ker.getAlgebraProcessor().processValidExpression( * ker.getParser().parseGeoGebraExpression("x"))[0]; } catch * (MyError e) { // TODO Auto-generated catch block * e.printStackTrace(); } catch (ParseException e) { // TODO * Auto-generated catch block e.printStackTrace(); } catch * (Exception e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ GeoImplicitPoly iPoly = new GeoImplicitPoly(cons); c.toGeoImplicitPoly(iPoly); GeoFunction paramEquation = new GeoFunction(cons, iPoly, null, h); double nroots = 0; double res[] = new double[2]; if (c.getType() == GeoConicNDConstants.CONIC_CIRCLE || c.getType() == GeoConicNDConstants.CONIC_ELLIPSE) { nroots = kernel.getEquationSolver().solveQuadratic(new double[] { -A[5] * A[5] + A[1] * A[2], 2 * (A[1] * A[4] - A[3] * A[5]), A[0] * A[1] - A[3] * A[3] }, res, Kernel.STANDARD_PRECISION); } AlgoRoots algo = null; if (nroots == 2) { // assume res[0]>=res[1] if (res[1] > res[0]) { double temp = res[0]; res[0] = res[1]; res[1] = temp; } algo = new AlgoRoots(cons, paramEquation, new GeoNumeric(cons, Math.max(res[1] - Kernel.MIN_PRECISION, h.getMinParameter())), new GeoNumeric(cons, Math.min(res[0] + Kernel.MIN_PRECISION, h.getMaxParameter()))); } else { algo = new AlgoRoots(cons, paramEquation, new GeoNumeric(cons, h.getMinParameter()), new GeoNumeric(cons, h.getMaxParameter())); } GeoPoint[] points = algo.getRootPoints(); List<double[]> valPairs = new ArrayList<double[]>(); for (int i = 0; i < points.length; i++) { double t = points[i].getX(); valPairs.add(new double[] { t, h.evaluate(t) }); } ker.setSilentMode(false); setPoints(valPairs); return; } }