Example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunction getCoefficients

List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunction getCoefficients

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunction getCoefficients.

Prototype

public double[] getCoefficients() 

Source Link

Document

Returns a copy of the coefficients array.

Usage

From source file:dom.rootlocus.utils.Utils.java

public Complex[] getRoots(PolynomialFunction p) {
    double[] coefficients = p.getCoefficients();
    LaguerreSolver solver = new LaguerreSolver(1e-10);
    Complex[] r = solver.solveAllComplex(p.getCoefficients(), 0);
    return r;//from   w w  w  .  j av  a  2  s.co  m
    //        Complex[] roots = new Complex[r.length];
    //        for (int i = 0; i < r.length; i++) {
    //            double Re = new BigDecimal(r[i].getReal()).setScale(5, RoundingMode.HALF_UP).doubleValue();
    //            double Img = new BigDecimal(r[i].getImaginary()).setScale(5, RoundingMode.HALF_UP).doubleValue();
    //            roots[i] = new Complex(Re, Img);
    //        }
    //        return roots;
}

From source file:jurbano.melodyshape.comparison.bspline.Laguerre.java

protected Complex[] findAllComplexRoots(PolynomialFunction f) {
    // This call to super.setup is the change we need in the Laguerre
    // implementation from the Apache Commons Math library. Need to set
    // up the maximum number of iterations and the bounds 0 and 1.
    // The original implementation had the maximum number of iterations set
    // to Integer.MAX_VALUE
    super.setup(this.maxIterations, f, 0, 1, 0);
    return this.solver.solveAll(ComplexUtils.convertToComplex(f.getCoefficients()), new Complex(0, 0d));
}

From source file:ffx.utilities.BlockAverager.java

/**
 * Compute the statistical uncertainty of G in each histogram bin and
 * overall. Loop over increasing values of block size. For each, calculate
 * the block means and their standard deviation. Then limit(blockStdErr,
 * blockSize to entireTraj) == trajStdErr.
 *
 * @return aggregate standard error of the total free energy change
 *///w w  w.  j a va 2 s. c  o m
public double[] computeBinUncertainties() {
    double[][] sems = new double[numBins][maxBlockSize + 1];
    BinDev[][] binStDevs = new BinDev[numBins][maxBlockSize + 1];

    List<WeightedObservedPoint>[] obsDev = new ArrayList[numBins];
    List<WeightedObservedPoint>[] obsErr = new ArrayList[numBins];

    for (int binIndex = 0; binIndex < numBins; binIndex++) {
        logger.info(format(" Computing stdError for bin %d...", binIndex));
        obsDev[binIndex] = new ArrayList<>();
        obsErr[binIndex] = new ArrayList<>();
        for (int blockSize = 1; blockSize <= maxBlockSize; blockSize += blockSizeStep) {
            int numBlocks = (int) Math.floor(numObs / blockSize);
            binStDevs[binIndex][blockSize] = new BinDev(binIndex, blockSize);
            sems[binIndex][blockSize] = binStDevs[binIndex][blockSize].stdev / Math.sqrt(numBlocks - 1);
            obsDev[binIndex]
                    .add(new WeightedObservedPoint(1.0, blockSize, binStDevs[binIndex][blockSize].stdev));
            obsErr[binIndex].add(new WeightedObservedPoint(1.0, blockSize, sems[binIndex][blockSize]));
            if (TEST) {
                logger.info(format("  bin,blockSize,stdev,sem: %d %d %.6g %.6g", binIndex, blockSize,
                        binStDevs[binIndex][blockSize].stdev, sems[binIndex][blockSize]));
            }
        }
    }

    // Fit a function to (blockSize v. stdError) and extrapolate to blockSize == entire trajectory.
    // This is our correlation-corrected estimate of the std error for this lambda bin.
    stdError = new double[numBins];
    for (int binIndex = 0; binIndex < numBins; binIndex++) {
        logger.info(format("\n Bin %d : fitting & extrapolating blockSize v. stdError", binIndex));
        if (fitter == FITTER.POLYNOMIAL) {
            // Fit a polynomial (shitty).
            double[] coeffsPoly = PolynomialCurveFitter.create(polyDegree).fit(obsErr[binIndex]);
            PolynomialFunction poly = new PolynomialFunction(coeffsPoly);
            logger.info(format("    Poly %d:   %12.10g     %s", polyDegree, poly.value(numObs),
                    Arrays.toString(poly.getCoefficients())));
        } else if (fitter == FITTER.POWER) {
            // Fit a power function (better).
            double[] coeffsPow = powerFit(obsErr[binIndex]);
            double powerExtrapolated = coeffsPow[0] * pow(numObs, coeffsPow[1]);
            logger.info(format("    Power:     %12.10g     %s", powerExtrapolated, Arrays.toString(coeffsPow)));
        }
        // Fit a log function (best).
        double[] logCoeffs = logFit(obsErr[binIndex]);
        double logExtrap = logCoeffs[0] + logCoeffs[1] * log(numObs);
        logger.info(format("    Log sem:   %12.10g     Residual: %12.10g     Coeffs: %6.4f, %6.4f \n",
                logExtrap, logCoeffs[0], logCoeffs[1]));

        // Also try fitting a linear function for the case of uncorrelated or extremely well-converged data.
        double[] linearCoef = linearFit(obsErr[binIndex]);
        double linearExtrap = linearCoef[0] + linearCoef[1] * numObs;
        logger.info(format("    Lin. sem:  %12.10g     Residual: %12.10g     Coeffs: %6.4f, %6.4f \n",
                linearExtrap, linearCoef[0], linearCoef[1]));

        stdError[binIndex] = logExtrap;
    }
    return stdError;
}

From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.PolynomialInterpolator.java

/**
 * Creates the polynomial function AST that encodes the give polynomial function.
 * /*from  w ww. j av  a 2s  .c  o m*/
 * @param pf     polynomial function. The starting knot corresponds to zero for this function.
 * @param knot   starting knot for this function
 * 
 * @return AST node representing the function
 */
private static ASTNode createPolynomialFunctionAST(PolynomialFunction pf, double knot) {
    double[] coefficients = pf.getCoefficients();

    ASTNode sum = new ASTNode(ASTNode.Type.PLUS);

    // Coefficients start with the constant then x^1, x^2 and so on
    sum.addChild(new ASTNode(coefficients[0]));
    for (int i = 1; i < coefficients.length; ++i) {
        sum.addChild(
                ASTNode.times(new ASTNode(coefficients[i]), ASTNode.pow(createTimeMinusOffsetAST(knot), i)));
    }
    return sum;
}