/** * Negate the instance. * * @return a new polynomial with all coefficients negated */ public PolynomialFunction negate() { double[] newCoefficients = new double[coefficients.length]; for (int i = 0; i < coefficients.length; ++i) { newCoefficients[i] = -coefficients[i]; } return new PolynomialFunction(newCoefficients); }
/** Create a polynomial from its coefficients. * @param c polynomials coefficients * @return polynomial */ private PolynomialFunction polynomial(double ... c) { return new PolynomialFunction(c); }
/** * Returns the derivative as a {@link PolynomialFunction}. * * @return the derivative polynomial. */ public PolynomialFunction polynomialDerivative() { return new PolynomialFunction(differentiate(coefficients)); }
/** * Multiply the instance by a polynomial. * * @param p Polynomial to multiply by. * @return a new polynomial equal to this times {@code p} */ public PolynomialFunction multiply(final PolynomialFunction p) { double[] newCoefficients = new double[coefficients.length + p.coefficients.length - 1]; for (int i = 0; i < newCoefficients.length; ++i) { newCoefficients[i] = 0.0; for (int j = FastMath.max(0, i + 1 - p.coefficients.length); j < FastMath.min(coefficients.length, i + 1); ++j) { newCoefficients[i] += coefficients[j] * p.coefficients[i-j]; } } return new PolynomialFunction(newCoefficients); }
/** * Add a polynomial to the instance. * * @param p Polynomial to add. * @return a new polynomial which is the sum of the instance and {@code p}. */ public PolynomialFunction add(final PolynomialFunction p) { // identify the lowest degree polynomial final int lowLength = FastMath.min(coefficients.length, p.coefficients.length); final int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] + p.coefficients[i]; } System.arraycopy((coefficients.length < p.coefficients.length) ? p.coefficients : coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); return new PolynomialFunction(newCoefficients); }
/** * Subtract a polynomial from the instance. * * @param p Polynomial to subtract. * @return a new polynomial which is the instance minus {@code p}. */ public PolynomialFunction subtract(final PolynomialFunction p) { // identify the lowest degree polynomial int lowLength = FastMath.min(coefficients.length, p.coefficients.length); int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] - p.coefficients[i]; } if (coefficients.length < p.coefficients.length) { for (int i = lowLength; i < highLength; ++i) { newCoefficients[i] = -p.coefficients[i]; } } else { System.arraycopy(coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); } return new PolynomialFunction(newCoefficients); }
return new PolynomialFunction(a);
/** * Find all complex roots for the polynomial with the given * coefficients, starting from the given initial value. * <p> * Note: This method is not part of the API of {@link BaseUnivariateSolver}.</p> * * @param coefficients polynomial coefficients * @param initial start value * @param maxEval maximum number of evaluations * @return the full set of complex roots of the polynomial * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum number of evaluations is exceeded when solving for one of the roots * @throws NullArgumentException if the {@code coefficients} is * {@code null} * @throws NoDataException if the {@code coefficients} array is empty * @since 3.5 */ public Complex[] solveAllComplex(double[] coefficients, double initial, int maxEval) throws NullArgumentException, NoDataException, TooManyEvaluationsException { setup(maxEval, new PolynomialFunction(coefficients), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, initial); return complexSolver.solveAll(ComplexUtils.convertToComplex(coefficients), new Complex(initial, 0d)); }
/** * Find a complex root for the polynomial with the given coefficients, * starting from the given initial value. * <p> * Note: This method is not part of the API of {@link BaseUnivariateSolver}.</p> * * @param coefficients polynomial coefficients * @param initial start value * @param maxEval maximum number of evaluations * @return a complex root of the polynomial * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum number of evaluations is exceeded * @throws NullArgumentException if the {@code coefficients} is * {@code null} * @throws NoDataException if the {@code coefficients} array is empty * @since 3.1 */ public Complex solveComplex(double[] coefficients, double initial, int maxEval) throws NullArgumentException, NoDataException, TooManyEvaluationsException { setup(maxEval, new PolynomialFunction(coefficients), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, initial); return complexSolver.solve(ComplexUtils.convertToComplex(coefficients), new Complex(initial, 0d)); }
coefficients[0] = y[i]; coefficients[1] = m[i]; polynomials[i] = new PolynomialFunction(coefficients);
coefficients[2] = (3 * (yvP - yv) / w - 2 * fd - fdP) / w; coefficients[3] = (2 * (yv - yvP) / w + fd + fdP) / w2; polynomials[i] = new PolynomialFunction(coefficients);
coefficients[2] = c[i]; coefficients[3] = d[i]; polynomials[i] = new PolynomialFunction(coefficients);
public DACChannel(String name, double[] span, int channum) { this.name = name; this.range = span; this.channum = channum; this.slope = span[1] - span[0]; this.intercept = span[0]; this.VToCode = new PolynomialFunction(new double[]{-4095. * intercept / slope, 4095. / slope}); this.CodeToV = new PolynomialFunction(new double[]{intercept, slope / 4095.}); this.calibrationEnabled = "false"; this.slope = 1; this.offset = 0; }
public void regenerateCalibration() { double A, B, intercept, slope; B = range[1]; A = range[0]; if (gain >= 0 && gain<=8) { gain = gainValues[(int) gain]; B /= gain; A /= gain; } slope = 2 * (B - A); intercept = 2 * A; if (!calibrationReady || gain == 8) { calPoly10 = new PolynomialFunction(new double[]{intercept, slope / 1023, 0.}); calPoly12 = new PolynomialFunction(new double[]{intercept, slope / 4095, 0.}); }//else cases need to be worked on!!! voltToCode10 = new PolynomialFunction(new double[]{-1023 * intercept / slope, 1023. / slope, 0.}); voltToCode12 = new PolynomialFunction(new double[]{-4095 * intercept / slope, 4095., 0.}); }
yPolyX[j] = new PolynomialFunction(xFitter.fit(new double[xDegree + 1])); xPolyY[i] = new PolynomialFunction(yFitter.fit(new double[yDegree + 1]));
public void loadPolynomials(ArrayList<Double[]> polys) { for (int i = 0; i < polys.size(); i++) { double[] temp = ArrayUtils.toPrimitive(polys.get(i)); ArrayUtils.reverse(temp); polynomials.add(new PolynomialFunction(temp)); } }
public AnalogInputSource(String channelName) { AnalogConstants analogConstants = new AnalogConstants(); this.channelName = channelName; range = analogConstants.inputRanges.get(channelName); gainValues = analogConstants.gains; this.CHOSA = analogConstants.picADCMultiplex.get(channelName); calPoly10 = new PolynomialFunction(new double[]{0., 3.3 / 1023, 0.}); calPoly12 = new PolynomialFunction(new double[]{0., 3.3 / 4095, 0.}); if (range[1] - range[0] < 0) { inverted = true; inversion = -1; } if (channelName.equals("CH1")) { gainEnabled = true; gainPGA = 1; gain = 0; } else if (channelName.equals("CH2")) { gainEnabled = true; gainPGA = 2; gain = 0; } gain = 0; regenerateCalibration(); }
private boolean fetchData(String chan) throws IOException, InterruptedException { while (true) { int stat = readRegister(STATUS); if ((stat & 0x80) == 1) { float data = readData(); data = convertUniPolar(data); List channelList = Collections.singletonList(chan); if ((int) channelList.get(3) > 4) { data = (data - 3.3f / 2) * 4; } PolynomialFunction function = new PolynomialFunction(caldata.get(chan)); return function.value(data) == 0; } else { Thread.sleep(100); Log.d(TAG, "Increase Delay"); } } }
private double[] rootsToPolynomial(Complex[] x){ /* Returns a double array of coefficients of the polynomial, assuming the each complex root has it's conjugate in the same array. */ PolynomialFunction[] polynomialFunctionArray = new PolynomialFunction[x.length/2]; PolynomialFunction product = new PolynomialFunction(new double[]{1}); for(int i = 0; i < x.length / 2; i++){ PolynomialFunction complexRoot = new PolynomialFunction(new double[]{-x[i].getReal(), 1}); complexRoot = complexRoot.multiply(complexRoot); complexRoot = complexRoot.subtract(new PolynomialFunction(new double[]{-1 * x[i].getImaginary() * x[i].getImaginary()})); System.out.println((complexRoot)); polynomialFunctionArray[i] = complexRoot; } for (PolynomialFunction aPolynomialFunctionArray : polynomialFunctionArray) product = aPolynomialFunctionArray.multiply(product); return product.getCoefficients(); }
public AD7718(SPI spi) throws IOException { this.spi = spi; this.cs = "CS1"; // Populate Calibrations populateCalibrationMap(); // Set SPI Parameters spi.setParameters(2, 1, 0, 1, 1); writeRegister(FILTER, 20); writeRegister(MODE, MODE_SINGLE | MODE_CHCON | MODE_REFSEL); for (String key : calibs.keySet()) { double[] convertedList = new PolynomialFunction(calibs.get(key)).getCoefficients(); ArrayUtils.reverse(convertedList); caldata.put(key, convertedList); } }