Search in sources :

Example 11 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class AssociativePropertiesWithVariables method generateExpression.

@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
    Node[] n = new Node[2];
    if (ExUtil.randomBoolean()) {
        if (difficulty == ProblemGenerator.EASY) {
            double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(numPair[0]), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(numPair[1]));
        } else if (difficulty == ProblemGenerator.MEDIUM) {
            double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
            double[] numPair2 = ExUtil.pairOfCleanAddingNumbers(200);
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(numPair[0]), new Number(numPair2[1]), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(numPair[1]), new Number(numPair2[0]));
        } else if (difficulty == ProblemGenerator.HARD) {
            double[] numPair = ExUtil.pairOfCleanAddingNumbers(400);
            double[] numPair2 = ExUtil.pairOfCleanAddingNumbers(200);
            Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(numPair[0]), ExUtil.randomTerm(1, varNames.get(0), 2, 12), new Number(numPair2[1]), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair[1]), new Number(numPair2[0]));
        }
    } else {
        // otherwise create a problem testing the associative property of multiplication
        if (difficulty == ProblemGenerator.EASY) {
            double[] numPair = ExUtil.pairOfCleanFactors(20);
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), new Number(numPair[0]), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(numPair[1]));
        } else if (difficulty == ProblemGenerator.MEDIUM) {
            double[] numPair = ExUtil.pairOfCleanFactors(30);
            Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), new Number(numPair[0]), ExUtil.randomTerm(1, varNames.get(0), 3, 9), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair[1]));
        } else if (difficulty == ProblemGenerator.HARD) {
            double[] numPair = ExUtil.pairOfCleanFactors(40);
            Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), new Number(numPair[0]), ExUtil.randomTerm(1, varNames.get(0), 3, 9), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair[1]));
            ;
        }
    }
    n[0] = ExUtil.randomlyAddParenthesis((Expression) n[0], 0, 3);
    n[1] = n[0].smartNumericSimplify().standardFormat();
    return n;
}
Also used : Operator(expression.Operator) Node(expression.Node) Number(expression.Number) Expression(expression.Expression) Vector(java.util.Vector)

Example 12 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method randomAdditionOrSubtraction.

public static Expression randomAdditionOrSubtraction(int minVal, int maxVal) {
    Number num1 = new Number(randomInt(minVal, maxVal, true)), num2 = new Number(randomInt(minVal, maxVal, true));
    Expression sumNode = null;
    if (ExUtil.randomBoolean()) {
        sumNode = new Expression(new Operator.Addition(), num1, num2);
    } else {
        sumNode = new Expression(new Operator.Subtraction(), num1, num2);
    }
    return sumNode;
}
Also used : Number(expression.Number) Expression(expression.Expression)

Example 13 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method randomTerm.

public static Node randomTerm(int degree, String var, int minCoefficient, int maxCoefficient) {
    Number num = new Number(randomInt(minCoefficient, maxCoefficient, true));
    if (degree == 0) {
        return num;
    }
    Expression ex = new Expression(new Operator.Multiplication());
    ex.getChildren().add(num);
    try {
        if (degree == 1) {
            ex.getChildren().add(new Identifier(var));
            return ex;
        } else {
            ex.getChildren().add(new Expression(new Operator.Exponent(), new Identifier(var), new Number(degree)));
            return ex;
        }
    } catch (NodeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Operator(expression.Operator) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) NodeException(expression.NodeException)

Example 14 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class GraphedCartFunction method draw.

@Override
public void draw(Graphics g) {
    //used to temporarily store the value stored in the independent and dependent vars,
    //this will allow it to be restored after graphing, so that if in the terminal a
    //value was assigned to x, it will not be overriden by the action of graphing
    //		Number xVal = getIndependentVar().getValue();
    //		Number yVal = getDependentVar().getValue();
    super.clearPts();
    Graphics2D g2d = ((Graphics2D) g);
    g.setColor(getColor());
    if (hasFocus()) {
        graph.LINE_SIZE = 3;
    } else {
        graph.LINE_SIZE = 2;
    }
    int tempLineSize = graph.LINE_SIZE;
    double lastX = 0, lastY = 0, currX = 0, currY = 0;
    Node origionalEx = null;
    Node ex;
    try {
        lastX = graph.X_MIN;
        origionalEx = Node.parseNode(getFuncEqtn());
        ex = origionalEx.cloneNode();
        ex = ex.replace(new Identifier(getIndependentVar()), new Number(lastX));
        ex = ex.replace("π", new Number(Math.PI));
        ex = ex.replace("e", new Number(Math.E));
        ex = ex.numericSimplify();
        if (ex instanceof Expression) {
            if (((Expression) ex).getOperator() instanceof Operator.Equals) {
                if (((Expression) ex).getChild(1) instanceof Number) {
                    lastY = ((Number) ((Expression) ex).getChild(1)).getValue();
                }
            }
        }
        if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= +graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= +graph.Y_PIC_ORIGIN) {
            //if the current point is on the screen, add it to the list of points
            addPt(gridxToScreen(lastX), gridyToScreen(lastY));
        }
    } catch (Exception e1) {
        currX = graph.X_MIN;
        lastY = graph.Y_MIN;
        ;
    }
    currX = graph.X_MIN;
    boolean validExpression;
    for (int i = 1; i < graph.X_SIZE; i += 1) {
        validExpression = false;
        try {
            currX = currX + graph.X_PIXEL;
            ex = origionalEx.cloneNode();
            ex = ex.replace(getIndependentVar(), new Number(currX));
            ex = ex.replace("π", new Number(Math.PI));
            ex = ex.replace("e", new Number(Math.E));
            ex = ex.numericSimplify();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            lastX = currX;
            lastY = graph.Y_MIN;
            continue;
        }
        if (ex instanceof Expression) {
            if (((Expression) ex).getOperator() instanceof Operator.Equals) {
                if (((Expression) ex).getChild(1) instanceof Number) {
                    currY = ((Number) ((Expression) ex).getChild(1)).getValue();
                    validExpression = true;
                }
            }
        } else if (!validExpression) {
            lastX = currX;
            lastY = graph.Y_MIN;
            continue;
        }
        if (gridxToScreen(currX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(currX) >= graph.X_PIC_ORIGIN && gridyToScreen(currY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(currY) >= graph.Y_PIC_ORIGIN) {
            //if the current point is on the screen, add it to the list of points
            if (lastY <= graph.Y_MIN) {
                addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
            }
            if (lastY >= graph.Y_MAX) {
                addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
            }
            addPt(gridxToScreen(currX), gridyToScreen(currY));
        } else if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= graph.Y_PIC_ORIGIN) {
            //if the last point is on the screen, add the correct boundary for this point to the list
            addPt(gridxToScreen(lastX), gridyToScreen(lastY));
            if (currY <= graph.Y_MIN) {
                addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
            }
            if (currY >= graph.Y_MAX) {
                addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
            }
        } else if (lastY >= graph.Y_MAX && currY <= graph.Y_MIN) {
            //if the last point was off the the top of the screen, and this one is off
            //the bottom, add the two to the list of points
            addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
            addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
        } else if (currY >= graph.Y_MAX && lastY <= graph.Y_MIN) {
            //if the last point was off the the bottom of the screen, and this one is off
            //the top, add the two to the list of points
            addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
            addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
        }
        if (isConnected()) {
            drawLineSeg(lastX, lastY, currX, currY, getColor(), g);
        }
        lastX = currX;
        lastY = currY;
    }
    //		g2d.setStroke(new BasicStroke(graph.LINE_SIZE * graph.DOC_ZOOM_LEVEL,
    //				BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    //		if ( this.getxVals().size() > 0){
    //			GeneralPath polyline = 
    //					new GeneralPath(GeneralPath.WIND_EVEN_ODD, this.getxVals().size());
    //			polyline.moveTo (this.getxVals().get(0), this.getyVals().get(0));
    //			for (int i = 1; i < this.getxVals().size(); i++) {
    //				polyline.lineTo( getxVals().get(i), getyVals().get(i));
    //			};
    //			g2d.draw(polyline);
    //		}
    graph.LINE_SIZE = 2;
    g2d.setStroke(new BasicStroke(1));
}
Also used : BasicStroke(java.awt.BasicStroke) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) Node(expression.Node) NodeException(expression.NodeException) Graphics2D(java.awt.Graphics2D)

Example 15 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class NegationGraphic method requestSize.

@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
    g.setFont(f);
    setFont(f);
    space = (int) (1 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL);
    Node tempChild = ((Expression) getValue()).getChild(0);
    NodeGraphic childValGraphic = null;
    int[] childSize = { 0, 0 };
    int[] symbolSize = { 0, 0 };
    int[] totalSize = { 0, 0 };
    symbolSize[0] = (int) Math.round(super.getRootNodeGraphic().getStringWidth("-", f)) + 2 * space;
    symbolSize[1] = super.getRootNodeGraphic().getFontHeight(f);
    childValGraphic = makeNodeGraphic(tempChild);
    setChildGraphic(childValGraphic);
    super.getRootNodeGraphic().getComponents().add(childValGraphic);
    childSize = childValGraphic.requestSize(g, f, x1 + symbolSize[0], y1);
    //set the west and east fields for inside an outside of the expression
    setMostInnerWest(this);
    setEast(childValGraphic);
    childValGraphic.setWest(this);
    setMostInnerEast(childValGraphic);
    symbolY1 = y1 + childValGraphic.getUpperHeight() - (int) Math.round(symbolSize[1] / 2.0);
    symbolY2 = symbolY1 + symbolSize[1];
    symbolX1 = x1;
    symbolX2 = x1 + symbolSize[0];
    setUpperHeight(childValGraphic.getUpperHeight());
    setLowerHeight(childValGraphic.getLowerHeight());
    totalSize[0] = symbolSize[0] + childSize[0];
    totalSize[1] = childSize[1];
    super.setX1(x1);
    super.setY1(y1);
    super.setX2(x1 + totalSize[0]);
    super.setY2(y1 + totalSize[1]);
    return totalSize;
}
Also used : Expression(expression.Expression) Node(expression.Node)

Aggregations

Expression (expression.Expression)13 Node (expression.Node)10 Number (expression.Number)9 Expression (org.h2.expression.Expression)9 Operator (expression.Operator)8 ValueExpression (org.h2.expression.ValueExpression)7 ExpressionColumn (org.h2.expression.ExpressionColumn)6 NodeException (expression.NodeException)5 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)5 Column (org.h2.table.Column)5 IndexColumn (org.h2.table.IndexColumn)5 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 GridSqlType.fromColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 Identifier (expression.Identifier)3 StringAttribute (doc.attributes.StringAttribute)2 IgniteException (org.apache.ignite.IgniteException)2 CreateTable (org.h2.command.ddl.CreateTable)2 DropTable (org.h2.command.ddl.DropTable)2