Example usage for org.apache.commons.math.analysis.polynomials PolynomialFunction value

List of usage examples for org.apache.commons.math.analysis.polynomials PolynomialFunction value

Introduction

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

Prototype

public double value(double x) 

Source Link

Document

Compute the value of the function for the given argument.

Usage

From source file:geogebra.common.kernel.implicit.AlgoIntersectImplicitpolys.java

private static int getNearRoots(double[] roots, EquationSolverInterface solver, double epsilon) {
    PolynomialFunction poly = new PolynomialFunction(roots);
    double[] rootsDerivative = poly.polynomialDerivative().getCoefficients();

    int nrRoots = getRoots(roots, solver);
    int nrDeRoots = getRoots(rootsDerivative, solver);
    for (int i = 0; i < nrDeRoots; i++) {
        if (Kernel.isEqual(poly.value(rootsDerivative[i]), 0, epsilon)) {
            if (nrRoots < roots.length) {
                roots[nrRoots++] = rootsDerivative[i];
            }/*from  w  w  w . j  a va2  s.  co m*/
        }
    }
    if (nrRoots == 0) {
        //a wild guess, test if the root of the n-1 derivative is a root of the original poly as well
        //works in case of a polynomial with one root of really high multiplicity.
        double[] c = poly.getCoefficients();
        int n = c.length - 1;
        if (n > 0) {
            double x = -c[n - 1] / n / c[n];
            if (Kernel.isEqual(poly.value(x), 0)) {
                roots[0] = x;
                return 1;
            }
        }
    }
    if (nrRoots == 0) {
        PolynomialFunction derivative = poly.polynomialDerivative();
        double x = 0;
        double err = Math.abs(poly.value(x));
        double lastErr = err * 2;
        while (err < lastErr && err > Kernel.STANDARD_PRECISION) {
            double devVal = derivative.value(x);
            if (!Kernel.isZero(devVal))
                x = x - poly.value(x) / devVal;
            else
                break;
            lastErr = err;
            err = Math.abs(poly.value(x));
        }
        if (Kernel.isEqual(poly.value(x), 0, epsilon)) {
            roots[0] = x;
            return 1;
        }
    }
    Arrays.sort(roots, 0, nrRoots);
    return nrRoots;
}

From source file:io.github.msdk.features.ransacaligner.RANSAC.java

private void fittPolinomialFunction(List<AlignStructMol> data, boolean linear) {
    List<AlignStructMol> points = new ArrayList<AlignStructMol>();

    int degree = 3;
    if (linear) {
        degree = 1;//from   w  ww  . j a  v a 2  s  .c  o  m
    }

    PolynomialFitter fitter = new PolynomialFitter(degree, new GaussNewtonOptimizer(true));
    for (int i = 0; i < data.size(); i++) {
        AlignStructMol point = data.get(i);
        if (point.ransacMaybeInLiers) {
            points.add(point);
            fitter.addObservedPoint(1, point.RT, point.RT2);
        }
    }
    try {
        PolynomialFunction function = fitter.fit();
        for (AlignStructMol point : data) {
            double y = point.RT2;
            double bestY = function.value(point.RT);
            if (Math.abs(y - bestY) < t) {
                point.ransacAlsoInLiers = true;
                AlsoNumber++;
            } else {
                point.ransacAlsoInLiers = false;
            }
        }
    } catch (Exception ex) {
    }
}

From source file:guineu.modules.filter.Alignment.RANSACGCGC.RANSACGCGC.java

private void fittPolinomialFunction(List<AlignGCGCStructMol> data) {
    List<AlignGCGCStructMol> points = new ArrayList<AlignGCGCStructMol>();

    PolynomialFitter fitter = new PolynomialFitter(3, new GaussNewtonOptimizer(true));
    for (int i = 0; i < data.size(); i++) {
        AlignGCGCStructMol point = data.get(i);
        if (point.ransacMaybeInLiers) {
            points.add(point);/*from   w ww .j ava  2  s.  c  o m*/
            fitter.addObservedPoint(1, point.RT, point.RT2);
        }
    }
    try {
        PolynomialFunction function = fitter.fit();
        for (AlignGCGCStructMol point : data) {
            double y = point.RT2;
            double bestY = function.value(point.RT);
            if (Math.abs(y - bestY) < t) {
                point.ransacAlsoInLiers = true;
                AlsoNumber++;
            } else {
                point.ransacAlsoInLiers = false;
            }
        }
    } catch (Exception ex) {
    }
}

From source file:guineu.modules.filter.Alignment.RANSAC.RANSAC.java

private void fittPolinomialFunction(List<AlignStructMol> data) {
    List<AlignStructMol> points = new ArrayList<AlignStructMol>();

    PolynomialFitter fitter = new PolynomialFitter(3, new GaussNewtonOptimizer(true));
    for (int i = 0; i < data.size(); i++) {
        AlignStructMol point = data.get(i);
        if (point.ransacMaybeInLiers) {
            points.add(point);/*from w  ww .j av a 2s . co  m*/
            fitter.addObservedPoint(1, point.RT, point.RT2);
        }
    }
    try {
        PolynomialFunction function = fitter.fit();
        for (AlignStructMol point : data) {
            double y = point.RT2;
            double bestY = function.value(point.RT);
            if (Math.abs(y - bestY) < t) {
                point.ransacAlsoInLiers = true;
                AlsoNumber++;
            } else {
                point.ransacAlsoInLiers = false;
            }
        }
    } catch (Exception ex) {
    }
}

From source file:net.sf.mzmine.modules.peaklistmethods.alignment.ransac.AlignmentRansacPlot.java

/**
 * Add new serie.//from  w ww.  jav  a  2 s  .  c  om
 * @param v Vector with the alignments
 * @param Name Name of the type of lipids in this alignment
 */
public void addSeries(Vector<AlignStructMol> data, String title, boolean linear) {
    try {
        chart.setTitle(title);
        XYSeries s1 = new XYSeries("Aligned pairs");
        XYSeries s2 = new XYSeries("Non-aligned pairs");
        XYSeries s3 = new XYSeries("Model");

        PolynomialFunction function = getPolynomialFunction(data, linear);

        for (AlignStructMol point : data) {

            if (point.Aligned) {
                s1.add(point.row1.getPeaks()[0].getRT(), point.row2.getPeaks()[0].getRT());
            } else {
                s2.add(point.row1.getPeaks()[0].getRT(), point.row2.getPeaks()[0].getRT());
            }
            try {
                s3.add(function.value(point.row2.getPeaks()[0].getRT()), point.row2.getPeaks()[0].getRT());
            } catch (Exception e) {
            }
        }

        this.dataset.addSeries(s1);
        this.dataset.addSeries(s2);
        this.dataset.addSeries(s3);

    } catch (Exception e) {
    }
}

From source file:guineu.modules.filter.Alignment.RANSAC.AlignmentRansacPlot.java

/**
 * Add new serie.//from   w  w  w.java2 s  . c  o  m
 * @param v Vector with the alignments
 * @param Name Name of the type of lipids in this alignment
 */
public void addSeries(List<AlignStructMol> data, String title, boolean linear) {
    try {
        chart.setTitle(title);
        XYSeries s1 = new XYSeries("Aligned pairs");
        XYSeries s2 = new XYSeries("Non-aligned pairs");
        XYSeries s3 = new XYSeries("Model");

        PolynomialFunction function = getPolynomialFunction(data, linear);

        for (AlignStructMol point : data) {

            if (point.Aligned) {
                s1.add((Double) point.row1.getVar("getRT"), (Double) point.row2.getVar("getRT"));
            } else {
                s2.add((Double) point.row1.getVar("getRT"), (Double) point.row2.getVar("getRT"));
            }
            try {
                s3.add(function.value((Double) point.row2.getVar("getRT")),
                        (Double) point.row2.getVar("getRT"));
            } catch (Exception e) {
            }
        }

        this.dataset.addSeries(s1);
        this.dataset.addSeries(s2);
        this.dataset.addSeries(s3);

    } catch (Exception e) {
    }
}

From source file:guineu.modules.filter.Alignment.RANSAC.RansacAlignerTask.java

/**
 *
 * @param peakList//w ww  .  ja  va2  s .c  o m
 * @return
 */
private HashMap<PeakListRow, PeakListRow> getAlignmentMap(Dataset peakList) {

    // Create a table of mappings for best scores
    HashMap<PeakListRow, PeakListRow> alignmentMapping = new HashMap<PeakListRow, PeakListRow>();

    if (alignedPeakList.getNumberRows() < 1) {
        return alignmentMapping;
    }

    // Create a sorted set of scores matching
    TreeSet<RowVsRowScore> scoreSet = new TreeSet<RowVsRowScore>();

    // RANSAC algorithm
    List<AlignStructMol> list = ransacPeakLists(alignedPeakList, peakList);
    PolynomialFunction function = this.getPolynomialFunction(list,
            ((SimpleLCMSDataset) alignedPeakList).getRowsRTRange());

    PeakListRow allRows[] = peakList.getRows().toArray(new PeakListRow[0]);

    for (PeakListRow row : allRows) {
        double rt = 0.0;
        try {
            rt = function.value(((SimplePeakListRowLCMS) row).getRT());
        } catch (NullPointerException e) {
            rt = ((SimplePeakListRowLCMS) row).getRT();
        }

        if (Double.isNaN(rt) || rt == -1) {
            rt = ((SimplePeakListRowLCMS) row).getRT();
        }

        Range mzRange = this.mzTolerance.getToleranceRange(((SimplePeakListRowLCMS) row).getMZ());
        Range rtRange = this.rtToleranceAfterRTcorrection.getToleranceRange(rt);
        // Get all rows of the aligned peaklist within parameter limits
        PeakListRow candidateRows[] = ((SimpleLCMSDataset) alignedPeakList).getRowsInsideRTAndMZRange(rtRange,
                mzRange);

        for (PeakListRow candidate : candidateRows) {
            RowVsRowScore score;
            try {
                score = new RowVsRowScore(row, candidate, mzTolerance.getTolerance(),
                        rtToleranceAfterRTcorrection.getTolerance(), rt);

                scoreSet.add(score);
                errorMessage = score.getErrorMessage();

            } catch (Exception e) {
                e.printStackTrace();
                setStatus(TaskStatus.ERROR);
                return null;
            }
        }
        progress = (double) processedRows++ / (double) totalRows;
    }

    // Iterate scores by descending order
    Iterator<RowVsRowScore> scoreIterator = scoreSet.iterator();
    while (scoreIterator.hasNext()) {

        RowVsRowScore score = scoreIterator.next();

        // Check if the row is already mapped
        if (alignmentMapping.containsKey(score.getPeakListRow())) {
            continue;
        }

        // Check if the aligned row is already filled
        if (alignmentMapping.containsValue(score.getAlignedRow())) {
            continue;
        }

        alignmentMapping.put(score.getPeakListRow(), score.getAlignedRow());

    }

    return alignmentMapping;
}

From source file:guineu.modules.filter.Alignment.RANSACGCGC.AlignmentGCGCRansacPlot.java

/**
 * Add new serie.//from ww  w.j  a v  a 2s .  c om
 *
 * @param v Vector with the alignments
 * @param Name Name of the type of lipids in this alignment
 */
public void addSeries(List<AlignGCGCStructMol> data, String title, boolean linear) {
    try {
        chart.setTitle(title);
        XYSeries s1 = new XYSeries("Aligned pairs");
        XYSeries s2 = new XYSeries("Non-aligned pairs");
        XYSeries s3 = new XYSeries("Model");

        PolynomialFunction function = getPolynomialFunction(data, linear);

        for (AlignGCGCStructMol point : data) {
            try {
                if (point.Aligned) {

                    s1.add((Double) point.row1.getVar("getRT1"), (Double) point.row2.getVar("getRT1"));
                } else {
                    s2.add((Double) point.row1.getVar("getRT1"), (Double) point.row2.getVar("getRT1"));
                }
                try {
                    s3.add(function.value((Double) point.row2.getVar("getRT1")),
                            (Double) point.row2.getVar("getRT1"));
                } catch (Exception e) {
                }
            } catch (Exception ex) {
                if (point.Aligned) {

                    s1.add((Double) point.row1.getVar("getRTI"), (Double) point.row2.getVar("getRTI"));
                } else {
                    s2.add((Double) point.row1.getVar("getRTI"), (Double) point.row2.getVar("getRTI"));
                }
                try {
                    s3.add(function.value((Double) point.row2.getVar("getRTI")),
                            (Double) point.row2.getVar("getRTI"));
                } catch (Exception e) {
                }

            }
        }

        this.dataset.addSeries(s1);
        this.dataset.addSeries(s2);
        this.dataset.addSeries(s3);

    } catch (Exception e) {
    }
}

From source file:net.sf.mzmine.modules.peaklistmethods.alignment.ransac.RansacAlignerTask.java

/**
 * //  w  w  w .j  ava 2  s. c o m
 * @param peakList
 * @return
 */
private HashMap<PeakListRow, PeakListRow> getAlignmentMap(PeakList peakList) {

    // Create a table of mappings for best scores
    HashMap<PeakListRow, PeakListRow> alignmentMapping = new HashMap<PeakListRow, PeakListRow>();

    if (alignedPeakList.getNumberOfRows() < 1) {
        return alignmentMapping;
    }

    // Create a sorted set of scores matching
    TreeSet<RowVsRowScore> scoreSet = new TreeSet<RowVsRowScore>();

    // RANSAC algorithm
    List<AlignStructMol> list = ransacPeakLists(alignedPeakList, peakList);
    PolynomialFunction function = this.getPolynomialFunction(list);

    PeakListRow allRows[] = peakList.getRows();

    for (PeakListRow row : allRows) {
        // Calculate limits for a row with which the row can be aligned
        Range mzRange = mzTolerance.getToleranceRange(row.getAverageMZ());

        double rt;
        try {
            rt = function.value(row.getAverageRT());
        } catch (NullPointerException e) {
            rt = row.getAverageRT();
        }
        if (Double.isNaN(rt) || rt == -1) {
            rt = row.getAverageRT();
        }

        Range rtRange = rtToleranceAfter.getToleranceRange(rt);

        // Get all rows of the aligned peaklist within parameter limits
        PeakListRow candidateRows[] = alignedPeakList.getRowsInsideScanAndMZRange(rtRange, mzRange);

        for (PeakListRow candidate : candidateRows) {
            RowVsRowScore score;
            if (sameChargeRequired && (!PeakUtils.compareChargeState(row, candidate))) {
                continue;
            }

            try {
                score = new RowVsRowScore(row, candidate, mzRange.getSize() / 2, rtRange.getSize() / 2, rt);

                scoreSet.add(score);
                errorMessage = score.getErrorMessage();

            } catch (Exception e) {
                e.printStackTrace();
                setStatus(TaskStatus.ERROR);
                return null;
            }
        }
        processedRows++;
    }

    // Iterate scores by descending order
    Iterator<RowVsRowScore> scoreIterator = scoreSet.iterator();
    while (scoreIterator.hasNext()) {

        RowVsRowScore score = scoreIterator.next();

        // Check if the row is already mapped
        if (alignmentMapping.containsKey(score.getPeakListRow())) {
            continue;
        }

        // Check if the aligned row is already filled
        if (alignmentMapping.containsValue(score.getAlignedRow())) {
            continue;
        }

        alignmentMapping.put(score.getPeakListRow(), score.getAlignedRow());

    }

    return alignmentMapping;
}

From source file:guineu.modules.filter.Alignment.RANSACGCGC.RansacGCGCAlignerTask.java

/**
 *
 * @param peakList/*  ww  w  .  j a v  a 2  s  .co m*/
 * @return
 */
private HashMap<PeakListRow, PeakListRow> getAlignmentMap(Dataset peakList) {

    // Create a table of mappings for best scores
    HashMap<PeakListRow, PeakListRow> alignmentMapping = new HashMap<PeakListRow, PeakListRow>();

    if (alignedPeakList.getNumberRows() < 1) {
        return alignmentMapping;
    }

    // Create a sorted set of scores matching
    TreeSet<RowVsRowGCGCScore> scoreSet = new TreeSet<RowVsRowGCGCScore>();

    // RANSAC algorithm
    List<AlignGCGCStructMol> list = ransacPeakLists(alignedPeakList, peakList);
    PolynomialFunction function = this.getPolynomialFunction(list,
            ((SimpleGCGCDataset) alignedPeakList).getRowsRTRange());

    PeakListRow allRows[] = peakList.getRows().toArray(new PeakListRow[0]);

    for (PeakListRow row : allRows) {
        double rt = 0;
        if (!this.useOnlyRTI) {
            try {
                rt = function.value(((SimplePeakListRowGCGC) row).getRT1());
                if (Double.isNaN(rt) || rt == -1) {
                    rt = ((SimplePeakListRowGCGC) row).getRT1();
                }
            } catch (Exception ee) {
            }
        } else {
            try {
                rt = function.value(((SimplePeakListRowGCGC) row).getRTI());
                if (Double.isNaN(rt) || rt == -1) {
                    rt = ((SimplePeakListRowGCGC) row).getRTI();
                }
            } catch (Exception ee) {
            }
        }
        PeakListRow candidateRows[] = null;
        if (!this.useOnlyRTI) {
            Range RTIRange = this.rtiTolerance.getToleranceRange(((SimplePeakListRowGCGC) row).getRTI());
            Range RT1Range = this.rtToleranceAfterRTcorrection.getToleranceRange(rt);
            Range RT2Range = this.rt2Tolerance.getToleranceRange(((SimplePeakListRowGCGC) row).getRT2());
            // Get all rows of the aligned peaklist within parameter limits
            candidateRows = ((SimpleGCGCDataset) alignedPeakList).getRowsInsideRT1RT2RTIRange(RT1Range,
                    RT2Range, RTIRange);
        } else {
            Range RTIRange = this.rtiTolerance.getToleranceRange(((SimplePeakListRowGCGC) row).getRTI());
            candidateRows = ((SimpleGCGCDataset) alignedPeakList).getRowsInsideRT1RT2RTIRange(RTIRange);
        }
        for (PeakListRow candidate : candidateRows) {
            RowVsRowGCGCScore score;
            try {
                score = new RowVsRowGCGCScore(row, candidate, rtiTolerance.getTolerance(),
                        rtToleranceAfterRTcorrection.getTolerance(), rt);

                scoreSet.add(score);
                errorMessage = score.getErrorMessage();

            } catch (Exception e) {
                e.printStackTrace();
                setStatus(TaskStatus.ERROR);
                return null;
            }
        }
        progress = (double) processedRows++ / (double) totalRows;
    }

    // Iterate scores by descending order
    Iterator<RowVsRowGCGCScore> scoreIterator = scoreSet.iterator();
    while (scoreIterator.hasNext()) {

        RowVsRowGCGCScore score = scoreIterator.next();

        // Check if the row is already mapped
        if (alignmentMapping.containsKey(score.getPeakListRow())) {
            continue;
        }

        // Check if the spectra score is unacceptable
        if (score.score == -10) {
            continue;
        }

        // Check if the aligned row is already filled
        if (alignmentMapping.containsValue(score.getAlignedRow())) {
            continue;
        }

        alignmentMapping.put(score.getPeakListRow(), score.getAlignedRow());

    }

    return alignmentMapping;
}