Android Open Source - AnkiStats Math Function Atom

From Project

Back to project page AnkiStats.


The source code is released under:

GNU General Public License

If you think the Android project AnkiStats listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//  w  w w. j a v a  2 s  .c  om

 * @author Michael Goldbach
public class MathFunctionAtom implements TreeElement{
    private TopLevelParser parser;
    public static enum MathType {SIN, COS, TAN, SQRT, ACOS, ASIN, ATAN, SINH, COSH, LOG, LN, INVALID}
    private MathType mathType = MathType.INVALID;
    private Expression expression;
    private boolean hasSavedValue = false;
    private double savedValue = 0;

    public MathFunctionAtom(String funcString, TopLevelParser parser){
        this.parser = parser;
        boolean isValid = init(funcString);
            this.mathType = MathType.INVALID;
        if(isValid && !isVariable()){
            savedValue = getValue();
            hasSavedValue = true;

    private boolean init(String funcString){
        int leftBracket = funcString.indexOf("(");
        int rightBracket = funcString.lastIndexOf(")");
        if(leftBracket > 1 && rightBracket > leftBracket+1){
            String funcName = funcString.substring(0, leftBracket);
            String expressionString = funcString.substring(leftBracket+1, rightBracket);
            Expression expressionInBrackets = new Expression(expressionString, parser);
            boolean isValidExpression = expressionInBrackets.getExpressionType() != Expression.ExpressionType.INVALID;
                    this.mathType = MathType.SIN;
                }else if (funcName.equals("cos")){
                    this.mathType = MathType.COS;
                }else if (funcName.equals("tan")){
                    this.mathType = MathType.TAN;
                }else if (funcName.equals("sqrt")){
                    this.mathType = MathType.SQRT;
                }else if (funcName.equals("acos")){
                    this.mathType = MathType.ACOS;
                }else if (funcName.equals("asin")){
                    this.mathType = MathType.ASIN;
                }else if (funcName.equals("atan")){
                    this.mathType = MathType.ATAN;
                }else if (funcName.equals("sinh")){
                    this.mathType = MathType.SINH;
                }else if (funcName.equals("cosh")){
                    this.mathType = MathType.COSH;
                }else if (funcName.equals("log") || funcName.equals("lg")){
                    this.mathType = MathType.LOG;
                }else if (funcName.equals("ln")){
                    this.mathType = MathType.LN;
                }else {
                    this.mathType = MathType.INVALID;
                    return false;
                this.expression = expressionInBrackets;
                return true;



        return false;

    public double getValue() throws ExpressionFormatException{
            return savedValue;

        switch (mathType) {
            case SIN:
                return Math.sin(expression.getValue());
            case COS:
                return Math.cos(expression.getValue());
            case TAN:
                return Math.tan(expression.getValue());
            case SQRT:
                return Math.sqrt(expression.getValue());
            case ACOS:
                return Math.acos(expression.getValue());
            case ASIN:
                return Math.asin(expression.getValue());
            case ATAN:
                return Math.atan(expression.getValue());
            case SINH:
                return Math.sinh(expression.getValue());
            case COSH:
                return Math.cosh(expression.getValue());
            case LOG:
                return Math.log10(expression.getValue());
            case LN:
                return Math.log(expression.getValue());
            case INVALID:
                throw new ExpressionFormatException("Number is Invalid, cannot parse");

    public boolean isVariable() {
        if (mathType != MathType.INVALID){

            return expression.isVariable();
            throw new ExpressionFormatException("Number is Invalid, cannot parse");

    public MathType getMathType() {
        return mathType;

Java Source Code List