Example usage for org.apache.commons.math3.fraction Fraction multiply

List of usage examples for org.apache.commons.math3.fraction Fraction multiply

Introduction

In this page you can find the example usage for org.apache.commons.math3.fraction Fraction multiply.

Prototype

public Fraction multiply(final int i) 

Source Link

Document

Multiply the fraction by an integer.

Usage

From source file:bayesGame.BayesGame.java

public static int getNewHeight(int height) {
    Fraction multiplier = Fraction.ONE;
    if (gameResolution.equals("1600x900")) {
        multiplier = new Fraction(900, 1080);
    }// w  w  w  .  ja va 2  s .c  o m
    if (gameResolution.equals("1280x720")) {
        multiplier = new Fraction(720, 1080);
    }
    Fraction newValue = multiplier.multiply(height);
    return newValue.intValue();
}

From source file:bayesGame.BayesGame.java

public static int getNewWidth(int width) {
    Fraction multiplier = Fraction.ONE;
    if (gameResolution.equals("1600x900")) {
        multiplier = new Fraction(1600, 1920);
    }/*from  w  w  w  . j a  v  a 2s . co  m*/
    if (gameResolution.equals("1280x720")) {
        multiplier = new Fraction(1280, 1920);
    }
    Fraction newValue = multiplier.multiply(width);
    return newValue.intValue();
}

From source file:bayesGame.ui.painter.OrNodePainter.java

public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node,
        Fraction parentNode1Probability, Fraction parentNode2Probability) {

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();

    NodePainter.graphicBackgroundPainter(g, 0, 0, size, size);

    // get non-zero truth table entries from the node
    List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities();

    // get the identities of its parents by taking the first map and querying it 
    // for KeySet, subtracting the object representing the node itself
    Set<Object> nodeParents = nonZeroEntries.get(0).keySet();
    nodeParents.remove(node.type);/*from   www  .j  a va2 s  .  co  m*/

    if (nodeParents.size() > 2) {
        throw new IllegalArgumentException("OR node with more than 2 parents not yet implemented");
    }

    Object[] nodeParentsArray = nodeParents.toArray();
    Object parent1 = nodeParentsArray[0];
    Object parent2 = nodeParentsArray[1];

    // for each map, check the truth table entry it corresponds to and color
    // those appropriately
    boolean p1true_p2true = false;
    boolean p1true_p2false = false;
    boolean p1false_p2true = false;
    boolean p1false_p2false = false;

    for (Map<Object, Boolean> map : nonZeroEntries) {
        Boolean parent1truth = map.get(parent1);
        Boolean parent2truth = map.get(parent2);

        if (parent1truth && parent2truth) {
            p1true_p2true = true;
        } else if (parent1truth && !parent2truth) {
            p1true_p2false = true;
        } else if (!parent1truth && parent2truth) {
            p1false_p2true = true;
        } else if (!parent1truth && !parent2truth) {
            p1false_p2false = true;
        }
    }

    int XSize = parentNode1Probability.multiply(size).intValue();
    int X_Size = size - XSize;
    int YSize = parentNode2Probability.multiply(size).intValue();
    int Y_Size = size - YSize;

    if (p1true_p2true) {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, gridColor, Color.BLACK);
    NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, gridColor, Color.BLACK);

    if (p1false_p2false) {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    return img;
}

From source file:bayesGame.ui.painter.AndNodePainter.java

public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node,
        Fraction parentNode1Probability, Fraction parentNode2Probability) {

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();

    NodePainter.graphicBackgroundPainter(g, 0, 0, size, size);

    // get non-zero truth table entries from the node
    List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities();

    // get the identities of its parents by taking the first map and querying it 
    // for KeySet, subtracting the object representing the node itself
    Set<Object> nodeParents = nonZeroEntries.get(0).keySet();
    nodeParents.remove(node.type);/*from  w ww  .ja  v a 2s  . com*/

    if (nodeParents.size() > 2) {
        throw new IllegalArgumentException("AND node with more than 2 parents not yet implemented");
    }

    Object[] nodeParentsArray = nodeParents.toArray();
    Object parent1 = nodeParentsArray[0];
    Object parent2 = nodeParentsArray[1];

    // for each map, check the truth table entry it corresponds to and color
    // those appropriately
    boolean p1true_p2true = false;
    boolean p1true_p2false = false;
    boolean p1false_p2true = false;
    boolean p1false_p2false = false;

    for (Map<Object, Boolean> map : nonZeroEntries) {
        Boolean parent1truth = map.get(parent1);
        Boolean parent2truth = map.get(parent2);

        if (parent1truth && parent2truth) {
            p1true_p2true = true;
        } else if (parent1truth && !parent2truth) {
            p1true_p2false = true;
        } else if (!parent1truth && parent2truth) {
            p1false_p2true = true;
        } else if (!parent1truth && !parent2truth) {
            p1false_p2false = true;
        }
    }

    Color whiteColor = Color.WHITE;

    int XSize = parentNode1Probability.multiply(size).intValue();
    int X_Size = size - XSize;
    int YSize = parentNode2Probability.multiply(size).intValue();
    int Y_Size = size - YSize;

    if (p1true_p2true) {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, falseColor, Color.BLACK);
    NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, falseColor, Color.BLACK);

    if (p1false_p2false) {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    return img;
}

From source file:unikl.disco.misc.NumberObj.java

public static NumberObj mult(NumberObj num1, NumberObj num2) {
    if (performTypeChecks) {
        TypeCheck(num1, num2);/* www  .  j a  v  a2s .c  o m*/
    }

    if (num1.equals(NaN) || num2.equals(NaN)) {
        return NaN;
    }
    // prevent overflow exception when adding integer based number representations like Fraction
    if (num1.equals(POSITIVE_INFINITY) || num2.equals(POSITIVE_INFINITY)) {
        return POSITIVE_INFINITY;
    }
    if (num1.equals(NEGATIVE_INFINITY) || num2.equals(NEGATIVE_INFINITY)) {
        return NEGATIVE_INFINITY;
    }

    switch (num1.getType()) {
    case DOUBLE:
        Double double1 = (Double) num1.getValue();
        Double double2 = (Double) num2.getValue();
        return new NumberObj(double1.doubleValue() * double2.doubleValue());
    case RATIONAL:
        Fraction frac1 = (Fraction) num1.getValue();
        Fraction frac2 = (Fraction) num2.getValue();
        // may throw MathArithmeticException due to integer overflow
        return new NumberObj(frac1.multiply(frac2));
    default:
        return null;
    }
}