Android Open Source - AnkiStats Math Function Atom






From Project

Back to project page AnkiStats.

License

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

package com.wildplot.android.parsing.AtomTypes;
//  w  w w. j a v a  2 s  .c  om
import com.wildplot.android.parsing.*;

/**
 * @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);
        if(!isValid){
            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;
            if(isValidExpression){
                if(funcName.equals("sin")){
                    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;
    }

    @Override
    public double getValue() throws ExpressionFormatException{
        if(hasSavedValue)
            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:
            default:
                throw new ExpressionFormatException("Number is Invalid, cannot parse");
        }
    }

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

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

    public MathType getMathType() {
        return mathType;
    }
}




Java Source Code List

com.wildplot.android.ankistats.AnkiDb.java
com.wildplot.android.ankistats.AnkiStatsActivity.java
com.wildplot.android.ankistats.AnkiStatsApplication.java
com.wildplot.android.ankistats.AnswerButton.java
com.wildplot.android.ankistats.ApplicationTest.java
com.wildplot.android.ankistats.CardsTypes.java
com.wildplot.android.ankistats.CollectionData.java
com.wildplot.android.ankistats.Forecast.java
com.wildplot.android.ankistats.HourlyBreakdown.java
com.wildplot.android.ankistats.Intervals.java
com.wildplot.android.ankistats.ReviewCount.java
com.wildplot.android.ankistats.Utils.java
com.wildplot.android.ankistats.WeeklyBreakdown.java
com.wildplot.android.parsing.Atom.java
com.wildplot.android.parsing.ExpressionFormatException.java
com.wildplot.android.parsing.Expression.java
com.wildplot.android.parsing.Factor.java
com.wildplot.android.parsing.Pow.java
com.wildplot.android.parsing.Term.java
com.wildplot.android.parsing.TopLevelParser.java
com.wildplot.android.parsing.TreeElement.java
com.wildplot.android.parsing.AtomTypes.FunctionXAtom.java
com.wildplot.android.parsing.AtomTypes.FunctionXYAtom.java
com.wildplot.android.parsing.AtomTypes.MathFunctionAtom.java
com.wildplot.android.parsing.AtomTypes.NumberAtom.java
com.wildplot.android.parsing.AtomTypes.VariableAtom.java
com.wildplot.android.parsing.AtomTypes.XVariableAtom.java
com.wildplot.android.parsing.AtomTypes.YVariableAtom.java
com.wildplot.android.rendering.AdvancedPlotSheet.java
com.wildplot.android.rendering.BarGraph.java
com.wildplot.android.rendering.DrawableContainer.java
com.wildplot.android.rendering.FunctionDrawer.java
com.wildplot.android.rendering.FunctionDrawer_y.java
com.wildplot.android.rendering.Integral.java
com.wildplot.android.rendering.LegendDrawable.java
com.wildplot.android.rendering.LinesPoints.java
com.wildplot.android.rendering.Lines.java
com.wildplot.android.rendering.MultiScreenPart.java
com.wildplot.android.rendering.PieChart.java
com.wildplot.android.rendering.PlotSheet.java
com.wildplot.android.rendering.PointDrawer2D.java
com.wildplot.android.rendering.RelativeColorGradient.java
com.wildplot.android.rendering.ReliefDrawer.java
com.wildplot.android.rendering.XAxisBarGraph.java
com.wildplot.android.rendering.XAxisHistoGram.java
com.wildplot.android.rendering.XAxis.java
com.wildplot.android.rendering.XGrid.java
com.wildplot.android.rendering.YAxisBarGraph.java
com.wildplot.android.rendering.YAxisHistoGram.java
com.wildplot.android.rendering.YAxis.java
com.wildplot.android.rendering.YGrid.java
com.wildplot.android.rendering.graphics.wrapper.BasicStroke.java
com.wildplot.android.rendering.graphics.wrapper.BufferedImage.java
com.wildplot.android.rendering.graphics.wrapper.Color.java
com.wildplot.android.rendering.graphics.wrapper.FontMetrics.java
com.wildplot.android.rendering.graphics.wrapper.Graphics2D.java
com.wildplot.android.rendering.graphics.wrapper.Graphics.java
com.wildplot.android.rendering.graphics.wrapper.Rectangle.java
com.wildplot.android.rendering.graphics.wrapper.Stroke.java
com.wildplot.android.rendering.interfaces.Drawable.java
com.wildplot.android.rendering.interfaces.Function2D.java
com.wildplot.android.rendering.interfaces.Function3D.java
com.wildplot.android.rendering.interfaces.Legendable.java
com.wildplot.android.rendering.interfaces.StepFunction2D.java