Search in sources :

Example 1 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class BasicDistributiveProperty method generateExpression.

@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
    Node[] n = new Node[2];
    if (difficulty == ProblemGenerator.EASY) {
        double bigNum = ExUtil.randomInt(2, 10, true) * 10, smallNum = ExUtil.randomInt(1, 5, true), multiplier = ExUtil.randomInt(2, 9, true);
        if (ExUtil.randomBoolean()) {
            n[0] = new Expression(new Operator.Addition(), new Number(bigNum), new Number(smallNum));
        } else {
            n[0] = new Expression(new Operator.Subtraction(), new Number(bigNum), new Number(smallNum));
        }
        n[0].setDisplayParentheses(true);
        n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
    } else if (difficulty == ProblemGenerator.MEDIUM) {
        double multiplier = ExUtil.randomInt(2, 9, true);
        n[0] = ExUtil.randomLinearExpression(ExUtil.randomVarName(), 1, 8);
        n[0].setDisplayParentheses(true);
        n[0].setDisplayParentheses(true);
        n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
    } else {
        double multiplier = ExUtil.randomInt(2, 14, true);
        String varName = ExUtil.randomVarName();
        n[0] = ExUtil.randomLinearExpression(varName, 1, 12);
        Node newNode = null;
        if (ExUtil.randomBoolean())
            newNode = new Number(ExUtil.randomInt(2, 15, true));
        else
            newNode = ExUtil.randomTerm(1, varName, 2, 9);
        if (ExUtil.randomBoolean()) {
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), n[0], newNode);
        } else {
            n[0] = ExUtil.randomlyStaggerOperation(new Operator.Subtraction(), n[0], newNode);
        }
        n[0].setDisplayParentheses(true);
        n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
    }
    n[1] = n[0].cloneNode().smartNumericSimplify().standardFormat();
    return n;
}
Also used : Operator(expression.Operator) Node(expression.Node) Number(expression.Number) Expression(expression.Expression)

Example 2 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method addNodeOnRandomSide.

public static Node addNodeOnRandomSide(Node oldNode, Node newNode, Operator o) {
    Expression ex = new Expression(o);
    if (randomBoolean()) {
        addChild(ex, newNode);
        addChild(ex, oldNode);
    } else {
        addChild(ex, oldNode);
        addChild(ex, newNode);
    }
    return ex;
}
Also used : Expression(expression.Expression)

Example 3 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method randomlyAddParenthesis.

/**
	 * Randomly adds parenthesis to an expression composed of additions or multiplications.
	 * The result of the operation does not change, due to the Associative property of both of
	 * the operators, but this method is useful for creating exercises to teach those concepts.
	 * 
	 * @param ex - the expression
	 * @param minNumParens - the minimum number of parenthesis to add
	 * @param maxNumParens - the maximum number of parenthesis to add
	 * @return - the modified expression
	 */
public static Node randomlyAddParenthesis(Expression ex, int minNumParens, int maxNumParens) {
    Vector<Node> terms = new Vector<>();
    if (ex.getOperator() instanceof Operator.Addition) {
        terms = ex.splitOnAddition();
        int numParensToAdd = randomInt(minNumParens, maxNumParens, true);
        int unusedTermsLeft = terms.size();
        int tempIndex;
        Expression tempEx;
        while (unusedTermsLeft > 2 && numParensToAdd > 0) {
            // use size - 2 because a term and it's successive term will be used to
            // create a new expression with parenthesis
            tempIndex = randomInt(0, terms.size() - 2, false);
            // remove from the same index twice, because everything is shifted with the
            // first move
            tempEx = new Expression(new Operator.Addition(), terms.remove(tempIndex), terms.remove(tempIndex));
            tempEx.setDisplayParentheses(true);
            terms.add(tempEx);
            unusedTermsLeft--;
            numParensToAdd--;
        }
        return Expression.staggerAddition(terms);
    } else if (ex.getOperator() instanceof Operator.Multiplication) {
        terms = ex.splitOnMultiplication();
        int numParensToAdd = randomInt(minNumParens, maxNumParens, true);
        int unusedTermsLeft = terms.size();
        int tempIndex;
        Expression tempEx;
        while (unusedTermsLeft > 2 && numParensToAdd > 0) {
            tempIndex = randomInt(0, terms.size() - 2, false);
            tempEx = new Expression(new Operator.Multiplication(), terms.remove(tempIndex), terms.remove(tempIndex));
            tempEx.setDisplayParentheses(true);
            terms.add(tempEx);
            unusedTermsLeft--;
            numParensToAdd--;
        }
        return Expression.staggerMultiplication(terms);
    }
    return ex;
}
Also used : Operator(expression.Operator) Expression(expression.Expression) Node(expression.Node) Vector(java.util.Vector)

Example 4 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method addRandomOp.

public static Node addRandomOp(Node n, String[] ops, String[] vars, int min, int max, double maxAbsVal, boolean excludeZero, boolean subtractNegatives, boolean addNegatives) {
    int opIndex = rand.nextInt(ops.length);
    String opCode = ops[opIndex];
    Node newChild = new Number(randomInt(min, max, excludeZero));
    Operator op = null;
    Number newNum;
    Expression newEx;
    double expressionVal = 0;
    boolean numberTooBig = false;
    try {
        //			System.out.println(n.toStringRepresentation());
        expressionVal = ((Number) n.numericSimplify()).getValue();
    } catch (NodeException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (Math.abs(expressionVal) > maxAbsVal) {
        numberTooBig = true;
    }
    if (opCode.equals(DIVISION)) {
        op = new Operator.Division();
        newEx = new Expression(op);
        if (!(newChild instanceof Number)) {
            // to keep the answer clean
            return addNodeOnRandomSide(n, newChild, op);
        } else if (expressionVal == 0) {
            newNum = (Number) newChild;
            do {
                newChild = new Number(randomInt(min, max, excludeZero));
            } while (newNum.getValue() == 0);
            return new Expression(op, n, newChild);
        } else if (isPrime(expressionVal) || (rand.nextBoolean() && !numberTooBig)) {
            // or the expression was randomly selected to be the divisor when it wasn't prime
            return addRandomOp(n, ops, vars, min, max, maxAbsVal, excludeZero, subtractNegatives, addNegatives);
        // had problem with these next two lines, gave up for now and just adding another operation
        //				newChild = new Number( expressionVal * randomInt(min, max, excludeZero));
        //				return new Expression(op, newChild, n);
        } else {
            // need to find a divisor
            if (numberTooBig) {
                return addRandomOp(n, ops, vars, min, max, maxAbsVal, excludeZero, subtractNegatives, addNegatives);
            }
            double[] factors = getFactors(expressionVal);
            int factorIndex = rand.nextInt(factors.length);
            newChild = new Number(factors[factorIndex]);
            return new Expression(op, n, newChild);
        }
    } else if (opCode.equals(ADDITION)) {
        op = new Operator.Addition();
        if (!(newChild instanceof Number)) {
            // to keep the answer clean
            return addNodeOnRandomSide(n, newChild, op);
        }
        if (!addNegatives) {
            if (max < 0) {
            // the settings contradict one another, the user wanted only negative numbers generated
            //do nothing
            } else {
                // the minimum is below zero, so exclude those values between 0 and the minimum while finding a new number
                int tempMin = 0;
                newNum = (Number) newChild;
                if (min > 0)
                    tempMin = min;
                while (newNum.getValue() < 0) {
                    newNum = new Number(randomInt(tempMin, max, excludeZero));
                }
            }
        }
    } else if (opCode.equals(SUBTRACTION)) {
        op = new Operator.Subtraction();
        if (!(newChild instanceof Number)) {
            // to keep the answer clean
            if (n instanceof Expression && ((Expression) n).getOperator() instanceof Operator.Negation) {
                // MIGHT CAUSE INFINITE RECURSION, BE CAREFUL
                return addRandomOp(n, ops, vars, min, max, maxAbsVal, excludeZero, subtractNegatives, addNegatives);
            }
            return addNodeOnRandomSide(n, newChild, op);
        }
        if (!subtractNegatives) {
            // negative numbers are not supposed to be subtracted, generate new positive number
            if (max < 0) {
            // the settings contradict one another, the user wanted only negative numbers generated
            //do nothing, ignore the value of subtractNegatives
            } else {
                // the minimum is below zero, so exclude those values between 0 and the minimum while finding a new number
                int tempMin = 0;
                newNum = (Number) newChild;
                if (min > 0)
                    tempMin = min;
                while (newNum.getValue() < 0) {
                    newNum = new Number(randomInt(tempMin, max, excludeZero));
                }
            }
        }
    } else if (opCode.equals(MULTIPLICATION)) {
        op = new Operator.Multiplication();
        newEx = new Expression(op);
        if (!(newChild instanceof Number)) {
            // to keep the answer clean
            return addNodeOnRandomSide(n, newChild, op);
        }
    } else if (opCode.equals(NEGATION)) {
        if (n instanceof Expression && ((Expression) n).getOperator() instanceof Operator.Negation || (n instanceof Number && ((Number) n).getValue() < 0)) {
            // THIS WILL CAUSE INFINITE RECURSION IF THE ONLY OPERATOR IS NEGATION!!
            return addRandomOp(n, ops, vars, min, max, maxAbsVal, excludeZero, subtractNegatives, addNegatives);
        }
        return new Expression(new Operator.Negation(), true, n);
    } else {
        throw new RuntimeException("unknown op");
    }
    return addNodeOnRandomSide(n, newChild, op);
}
Also used : Operator(expression.Operator) Node(expression.Node) NodeException(expression.NodeException) Number(expression.Number) Expression(expression.Expression)

Example 5 with Expression

use of expression.Expression in project OpenNotebook by jaltekruse.

the class PropertyOfOppositiesWithVariables method generateExpression.

@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
    Node[] n = new Node[2];
    if (difficulty == ProblemGenerator.EASY) {
        double num = ExUtil.randomInt(2, 9, true);
        n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(num), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(-1 * num), new Number(ExUtil.randomInt(2, 9, true)));
    } else if (difficulty == ProblemGenerator.MEDIUM) {
        double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
        Node var = ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12);
        n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(ExUtil.randomInt(2, 9, true)), var, new Number(ExUtil.randomInt(2, 9, true)), new Expression(new Operator.Negation(), var));
    } else if (difficulty == ProblemGenerator.HARD) {
        double[] numPair2 = ExUtil.pairOfCleanAddingNumbers(200);
        Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
        Node var = ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12);
        n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(ExUtil.randomInt(2, 9, true)), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair2[0]), var, new Number(numPair2[0]), new Expression(new Operator.Negation(), var));
    }
    n[0] = ExUtil.randomlyAddParenthesis((Expression) n[0], 0, 3);
    n[1] = n[0].collectLikeTerms().smartNumericSimplify().standardFormat();
    return n;
}
Also used : Operator(expression.Operator) Node(expression.Node) Number(expression.Number) Expression(expression.Expression)

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