Example usage for org.apache.commons.math3.util FastMath round

List of usage examples for org.apache.commons.math3.util FastMath round

Introduction

In this page you can find the example usage for org.apache.commons.math3.util FastMath round.

Prototype

public static int round(final float x) 

Source Link

Document

Get the closest int to x.

Usage

From source file:jmb.jcortex.strategies.batchingstrategies.BatchedDataSet.java

public BatchedDataSet(DataSet dataSet, int instancesPerBatch) {
    DataSet shuffledDataSet = dataSet.shuffleRows();
    int numBatches = (int) FastMath.round((double) shuffledDataSet.numRows() / (double) instancesPerBatch);
    int numPerBatch = (int) FastMath.ceil(shuffledDataSet.numRows() / numBatches);
    for (int i = 0; i < numBatches; i++) {
        int start = i * numPerBatch;
        int end = start + numPerBatch;
        if (end > shuffledDataSet.numRows()) {
            end = shuffledDataSet.numRows();
        }//from  w w  w  .  ja  va  2 s.c o m
        batches.add(shuffledDataSet.sliceRows(start, end));
    }
    batchIterator = batches.iterator();
}

From source file:jmb.jcortex.data.DataSetSplitter.java

public DataSet[] split(DataSet src, double... setSizes) {
    int numRows = src.numRows();
    DataSet[] subsets = new DataSet[setSizes.length];
    int previousEnd = 0;
    for (int i = 0; i < setSizes.length; i++) {
        // if we've consumed the entire DataSet and still have another subset, there is something very wrong
        if (previousEnd >= numRows) {
            throw new IllegalArgumentException("Error, setSizes sum to > 1.0");
        }/*from  ww  w  .  j ava  2 s. co m*/
        int numRowsInSubset = (int) FastMath.round(setSizes[i] * numRows);
        int newEnd = previousEnd + numRowsInSubset;
        // if we're over on this subset, just truncate it. This can happen due to rounding issues, so don't fail.
        if (newEnd > numRows) {
            newEnd = numRows;
        }
        subsets[i] = src.sliceRows(previousEnd, newEnd);
        previousEnd = newEnd;
    }
    return subsets;
}

From source file:Math.Entropy.java

/**
 * Check if the values for the entropy calculations are summed to 1
 *
 * @param values values represent the occurrences number of each class
 * @return true if the the entropy calculations are summed to 1
 *///  ww w . j  a  v  a 2 s.c o  m
private static boolean AreEntropyValuesSumOne(ArrayList<Double> values) {
    double values_sum = 0;
    for (Double value : values) {
        values_sum += value;
    }

    if (FastMath.round(values_sum) == 1) {
        return true;
    } else {
        Console.PrintException("Error: Get_Entropy() was provided with value that do not sum to 1", null);
        return false;
    }
}

From source file:it.unibo.alchemist.model.implementations.conditions.BiomolPresentInEnv.java

@Override
public double getPropensityConditioning() {
    final double totalQuantity = getTotalQuantity();
    if (totalQuantity < getQuantity()) {
        return 0;
    }//from www. j  av a 2  s.com
    return CombinatoricsUtils.binomialCoefficientDouble((int) FastMath.round(totalQuantity),
            (int) FastMath.round(getQuantity()));
}

From source file:net.myrrix.online.eval.ParameterRange.java

/**
 * @param numSteps must be at least 2. The number of value steps to construct
 * @return value steps to try for this parameter value. {@code min} and {@code max} are the first and last
 *  elements. The values are not necessarily distributed uniformly in the range.
 *///from w w  w . j  av  a 2  s  .co  m
public Number[] buildSteps(int numSteps) {
    Preconditions.checkArgument(numSteps >= 2, "numSteps must be at least 2: {}", numSteps);
    if (min == max) {
        return new Number[] { maybeRound(min) };
    }
    if (integerOnly) {
        int roundedMin = (int) FastMath.round(min);
        int roundedMax = (int) FastMath.round(max);
        int maxResonableSteps = roundedMax - roundedMin + 1;
        if (numSteps >= maxResonableSteps) {
            Number[] sequence = new Number[maxResonableSteps];
            for (int i = 0; i < sequence.length; i++) {
                sequence[i] = roundedMin + i;
            }
            return sequence;
        }
    }

    Number[] stepValues = new Number[numSteps];
    stepValues[0] = maybeRound(min);
    stepValues[stepValues.length - 1] = maybeRound(max);
    double range = max - min;
    for (int i = 1; i < stepValues.length - 1; i++) {
        double fraction = (double) i / (numSteps - 1);
        stepValues[i] = maybeRound(min + range * fraction * fraction);
    }
    return stepValues;
}

From source file:net.myrrix.online.eval.ParameterRange.java

/**
 * @return {@link Long} or {@link Double}
 *///  www .  j a v  a 2  s .c om
private Number maybeRound(double value) {
    if (integerOnly) {
        return FastMath.round(value);
    }
    return value;
}

From source file:lambertmrev.Lambert.java

/** Constructs and solves a Lambert problem.
 *
 * \param[in] R1 first cartesian position
 * \param[in] R2 second cartesian position
 * \param[in] tof time of flight//from   www .ja v a 2  s .c o  m
 * \param[in] mu gravity parameter
 * \param[in] cw when 1 a retrograde orbit is assumed
 * \param[in] multi_revs maximum number of multirevolutions to compute
 */

public void lambert_problem(Vector3D r1, Vector3D r2, double tof, double mu, Boolean cw, int multi_revs) {
    // sanity checks
    if (tof <= 0) {
        System.out.println("ToF is negative! \n");
    }
    if (mu <= 0) {
        System.out.println("mu is below zero");
    }

    // 1 - getting lambda and T
    double m_c = FastMath.sqrt((r2.getX() - r1.getX()) * (r2.getX() - r1.getX())
            + (r2.getY() - r1.getY()) * (r2.getY() - r1.getY())
            + (r2.getZ() - r1.getZ()) * (r2.getZ() - r1.getZ()));
    double R1 = r1.getNorm();
    double R2 = r2.getNorm();
    double m_s = (m_c + R1 + R2) / 2.0;

    Vector3D ir1 = r1.normalize();
    Vector3D ir2 = r2.normalize();
    Vector3D ih = Vector3D.crossProduct(ir1, ir2);
    ih = ih.normalize();

    if (ih.getZ() == 0) {
        System.out.println("angular momentum vector has no z component \n");
    }
    double lambda2 = 1.0 - m_c / m_s;
    double m_lambda = FastMath.sqrt(lambda2);

    Vector3D it1 = new Vector3D(0.0, 0.0, 0.0);
    Vector3D it2 = new Vector3D(0.0, 0.0, 0.0);

    if (ih.getZ() < 0.0) { // Transfer angle is larger than 180 degrees as seen from abive the z axis
        m_lambda = -m_lambda;
        it1 = Vector3D.crossProduct(ir1, ih);
        it2 = Vector3D.crossProduct(ir2, ih);
    } else {
        it1 = Vector3D.crossProduct(ih, ir1);
        it2 = Vector3D.crossProduct(ih, ir2);
    }
    it1.normalize();
    it2.normalize();

    if (cw) { // Retrograde motion
        m_lambda = -m_lambda;
        it1.negate();
        it2.negate();
    }
    double lambda3 = m_lambda * lambda2;
    double T = FastMath.sqrt(2.0 * mu / m_s / m_s / m_s) * tof;

    // 2 - We now hava lambda, T and we will find all x
    // 2.1 - let us first detect the maximum number of revolutions for which there exists a solution
    int m_Nmax = FastMath.toIntExact(FastMath.round(T / FastMath.PI));
    double T00 = FastMath.acos(m_lambda) + m_lambda * FastMath.sqrt(1.0 - lambda2);
    double T0 = (T00 + m_Nmax * FastMath.PI);
    double T1 = 2.0 / 3.0 * (1.0 - lambda3);
    double DT = 0.0;
    double DDT = 0.0;
    double DDDT = 0.0;

    if (m_Nmax > 0) {
        if (T < T0) { // We use Halley iterations to find xM and TM
            int it = 0;
            double err = 1.0;
            double T_min = T0;
            double x_old = 0.0, x_new = 0.0;
            while (true) {
                ArrayRealVector deriv = dTdx(x_old, T_min, m_lambda);
                DT = deriv.getEntry(0);
                DDT = deriv.getEntry(1);
                DDDT = deriv.getEntry(2);
                if (DT != 0.0) {
                    x_new = x_old - DT * DDT / (DDT * DDT - DT * DDDT / 2.0);
                }
                err = FastMath.abs(x_old - x_new);
                if ((err < 1e-13) || (it > 12)) {
                    break;
                }
                tof = x2tof(x_new, m_Nmax, m_lambda);
                x_old = x_new;
                it++;
            }
            if (T_min > T) {
                m_Nmax -= 1;
            }
        }
    }
    // We exit this if clause with Mmax being the maximum number of revolutions
    // for which there exists a solution. We crop it to multi_revs
    m_Nmax = FastMath.min(multi_revs, m_Nmax);

    // 2.2 We now allocate the memory for the output variables
    m_v1 = MatrixUtils.createRealMatrix(m_Nmax * 2 + 1, 3);
    RealMatrix m_v2 = MatrixUtils.createRealMatrix(m_Nmax * 2 + 1, 3);
    RealMatrix m_iters = MatrixUtils.createRealMatrix(m_Nmax * 2 + 1, 3);
    //RealMatrix m_x = MatrixUtils.createRealMatrix(m_Nmax*2+1, 3);
    ArrayRealVector m_x = new ArrayRealVector(m_Nmax * 2 + 1);

    // 3 - We may now find all solution in x,y
    // 3.1 0 rev solution
    // 3.1.1 initial guess
    if (T >= T00) {
        m_x.setEntry(0, -(T - T00) / (T - T00 + 4));
    } else if (T <= T1) {
        m_x.setEntry(0, T1 * (T1 - T) / (2.0 / 5.0 * (1 - lambda2 * lambda3) * T) + 1);
    } else {
        m_x.setEntry(0, FastMath.pow((T / T00), 0.69314718055994529 / FastMath.log(T1 / T00)) - 1.0);
    }
    // 3.1.2 Householder iterations
    //m_iters.setEntry(0, 0, housOutTmp.getEntry(0));
    m_x.setEntry(0, householder(T, m_x.getEntry(0), 0, 1e-5, 15, m_lambda));

    // 3.2 multi rev solutions
    double tmp;
    double x0;

    for (int i = 1; i < m_Nmax + 1; i++) {
        // 3.2.1 left householder iterations
        tmp = FastMath.pow((i * FastMath.PI + FastMath.PI) / (8.0 * T), 2.0 / 3.0);
        m_x.setEntry(2 * i - 1, (tmp - 1) / (tmp + 1));
        x0 = householder(T, m_x.getEntry(2 * i - 1), i, 1e-8, 15, m_lambda);
        m_x.setEntry(2 * i - 1, x0);
        //m_iters.setEntry(2*i-1, 0, housOutTmp.getEntry(0));

        //3.2.1 right Householder iterations
        tmp = FastMath.pow((8.0 * T) / (i * FastMath.PI), 2.0 / 3.0);
        m_x.setEntry(2 * i, (tmp - 1) / (tmp + 1));
        x0 = householder(T, m_x.getEntry(2 * i), i, 1e-8, 15, m_lambda);
        m_x.setEntry(2 * i, x0);
        //m_iters.setEntry(2*i, 0, housOutTmp.getEntry(0));
    }

    // 4 - For each found x value we recontruct the terminal velocities
    double gamma = FastMath.sqrt(mu * m_s / 2.0);
    double rho = (R1 - R2) / m_c;

    double sigma = FastMath.sqrt(1 - rho * rho);
    double vr1, vt1, vr2, vt2, y;

    ArrayRealVector ir1_vec = new ArrayRealVector(3);
    ArrayRealVector ir2_vec = new ArrayRealVector(3);
    ArrayRealVector it1_vec = new ArrayRealVector(3);
    ArrayRealVector it2_vec = new ArrayRealVector(3);

    // set Vector3D values to a mutable type
    ir1_vec.setEntry(0, ir1.getX());
    ir1_vec.setEntry(1, ir1.getY());
    ir1_vec.setEntry(2, ir1.getZ());
    ir2_vec.setEntry(0, ir2.getX());
    ir2_vec.setEntry(1, ir2.getY());
    ir2_vec.setEntry(2, ir2.getZ());
    it1_vec.setEntry(0, it1.getX());
    it1_vec.setEntry(1, it1.getY());
    it1_vec.setEntry(2, it1.getZ());
    it2_vec.setEntry(0, it2.getX());
    it2_vec.setEntry(1, it2.getY());
    it2_vec.setEntry(2, it2.getZ());

    for (int i = 0; i < m_x.getDimension(); i++) {
        y = FastMath.sqrt(1.0 - lambda2 + lambda2 * m_x.getEntry(i) * m_x.getEntry(i));
        vr1 = gamma * ((m_lambda * y - m_x.getEntry(i)) - rho * (m_lambda * y + m_x.getEntry(i))) / R1;
        vr2 = -gamma * ((m_lambda * y - m_x.getEntry(i)) + rho * (m_lambda * y + m_x.getEntry(i))) / R2;

        double vt = gamma * sigma * (y + m_lambda * m_x.getEntry(i));

        vt1 = vt / R1;
        vt2 = vt / R2;

        for (int j = 0; j < 3; ++j)
            m_v1.setEntry(i, j, vr1 * ir1_vec.getEntry(j) + vt1 * it1_vec.getEntry(j));
        for (int j = 0; j < 3; ++j)
            m_v2.setEntry(i, j, vr2 * ir2_vec.getEntry(j) + vt2 * it2_vec.getEntry(j));
    }

}

From source file:edu.mit.fss.examples.gui.ExecutionControlPanel.java

/**
 * Instantiates a new execution control panel.
 *///from   ww w .  j av a2  s  .com
public ExecutionControlPanel() {
    initializeAction.putValue(Action.SHORT_DESCRIPTION, "Initialize the execution.");
    initializeAction.setEnabled(false);
    runAction.putValue(Action.SHORT_DESCRIPTION, "Run the execution.");
    runAction.setEnabled(false);
    stopAction.putValue(Action.SHORT_DESCRIPTION, "Stop the execution.");
    stopAction.setEnabled(false);
    terminateAction.putValue(Action.SHORT_DESCRIPTION, "Terminate the execution.");
    terminateAction.setEnabled(false);

    setLayout(new BorderLayout());
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());

    buttonPanel.add(new JButton(initializeAction));
    buttonPanel.add(new JButton(runAction));
    buttonPanel.add(new JButton(stopAction));
    buttonPanel.add(new JButton(terminateAction));

    timeStepField = new JFormattedTextField(NumberFormat.getNumberInstance());
    timeStepField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
            checkUpdate();
        }

        private void checkUpdate() {
            long unitConversion = getTimeStepUnits();
            long timeStep = FastMath.round(unitConversion * ((Number) timeStepField.getValue()).doubleValue());
            setTimeStepAction.setEnabled(timeStep != federate.getTimeStep());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            checkUpdate();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            checkUpdate();
        }
    });
    timeStepField.setColumns(4);
    buttonPanel.add(new JLabel("Time Step: "));
    buttonPanel.add(timeStepField);
    timeStepUnits.setSelectedItem(TimeUnit.SECONDS);
    buttonPanel.add(timeStepUnits);
    timeStepUnits.addActionListener(new ActionListener() {
        private long lastUnitConversion = getTimeStepUnits();

        @Override
        public void actionPerformed(ActionEvent e) {
            long unitConversion = getTimeStepUnits();
            // update field value using new units
            timeStepField.setValue(((Double) timeStepField.getValue()) * lastUnitConversion / unitConversion);
            // update stored value
            lastUnitConversion = unitConversion;
        }
    });
    buttonPanel.add(new JButton(setTimeStepAction));

    buttonPanel.add(new JLabel("Min. Step Duration"));
    stepDurationField = new JFormattedTextField(NumberFormat.getNumberInstance());
    stepDurationField.setColumns(4);
    stepDurationField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
            checkUpdate();
        }

        private void checkUpdate() {
            long unitConversion = getStepDurationUnits();
            long minStepDuration = FastMath
                    .round(unitConversion * ((Number) stepDurationField.getValue()).doubleValue());
            setMinStepDurationAction.setEnabled(minStepDuration != federate.getMinimumStepDuration());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            checkUpdate();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            checkUpdate();
        }
    });
    buttonPanel.add(stepDurationField);
    stepDurationUnits.setSelectedItem(TimeUnit.MILLISECONDS);
    buttonPanel.add(stepDurationUnits);
    stepDurationUnits.addActionListener(new ActionListener() {
        private long lastUnitConversion = getStepDurationUnits();

        @Override
        public void actionPerformed(ActionEvent e) {
            long unitConversion = getStepDurationUnits();
            // update field value using new units
            stepDurationField
                    .setValue(((Double) stepDurationField.getValue()) * lastUnitConversion / unitConversion);
            // update stored value
            lastUnitConversion = unitConversion;
        }
    });
    buttonPanel.add(new JButton(setMinStepDurationAction));

    add(buttonPanel, BorderLayout.CENTER);
}

From source file:cloudnet.core.Vm.java

public long getRequestedRam(long timestamp) {
    return FastMath.round(getRamWorkloadModel().getWorkload(timestamp) * getSpec().getRam());
}

From source file:cloudnet.core.Vm.java

public long getRequestedSize(long timestamp) {
    return FastMath.round(getSizeWorkloadModel().getWorkload(timestamp) * getSpec().getSize());
}