2

I have a problem with my Android Program. I'm using mxparser as my Math Parser. I added it to my Lib. No errors are showing after I typed my program using this but the program gets an error when i click a certain button when running. I tried debugging it many times and I'm sure the error comes from the parser. Any Ideas?

  private OnClickListener ButtonClicked= new OnClickListener()
    {
    @Override
    public void onClick(View v)
    {
        Function f = new Function("f(x) = "+funcText.getText().toString());
        Expression xlexp = new Expression("f("+xlText.getText().toString()+")",f);
        Expression xuexp = new Expression("f("+xuText.getText().toString()+")",f);
        double c = xlexp.calculate();
        double d = xuexp.calculate();
        String xlString = String.valueOf(c);
        String xuString = String.valueOf(d);
        fxlText.setText(xlString);
        fxuText.setText(xuString);

    }

    };

Update:

These are my inputs

This is the Error Message (I think xD)

Thank you LutzL for keeping up with me :D

Update2:

I edited the code:

private OnClickListener ButtonClicked= new OnClickListener()
{
@Override
public void onClick(View v)
{
    xlText.getText();
    xuText.getText();

    String firstString=xlText.getText().toString();
    String secondString=xuText.getText().toString();
    double xl = Double.parseDouble(firstString);
    double xu = Double.parseDouble(secondString);

double f=2*xl+1;
double f2=2*xu+1;
String xlstring = String.valueOf(f);
String xustring = String.valueOf(f2);

    fxlText.setText(xlstring);
    fxuText.setText(xustring);

}

};

This is working. In this code, I get the values of xl and xu then I inserted it in the function I made(2*x+1). But what I need is a code that also gets the function from the user. That's why I used mxparser.

Update3:

I started debugging it and this shows(Source not Found). Don't know what it means. any idea? :(

  • post your stacktrace – Shriram Dec 04 '16 at 11:02
  • Remove redundant conversions. `.getText()` already is a string, no need to convert is to string. You can also use `fxlText.setText(c)` as the compiler automatically inserts the call of `Double.toString(c)` to get the expected `String` argument. This is the same mechanism that allows you to use, as example, `println("c= "+c)`. – Lutz Lehmann Dec 04 '16 at 11:10
  • I'm sorry. I just started learning android today. I kinda don't get what you said and do this. It still get errors :( @LutzL – HITACHIINMAE Dec 04 '16 at 11:21
  • What exactly do you use as input strings? What is the error message, please paste it into your question. -- You can ignore my style tips as they do not influence the error. Please do not post updates to your question in comments, directly edit your question, either replacing your code if it does not change the error situation or add a section with the new code if it changed the error. – Lutz Lehmann Dec 04 '16 at 11:26
  • It would also be helpful for you directly and for us if you could provide a minimal complete working code, something like ` ... main() { String funcTextStr =" ... ", xlTextStr = " ... "; ...` and output to console and report the resulting output incl. error message if any. -- Conversely, cut out the `mxmath` calls from the `onClick` method and just paste the strings together in the output text fields without evaluating any math, this should show if the error is in mxmath or the GUI construction. – Lutz Lehmann Dec 04 '16 at 11:38
  • Please read this post https://stackoverflow.com/a/13794154/3088138 and see if you can open the "LogCat" window and navigate to the actual error message. Do you run your app inside a debugger? If not, do so to get more meaningful information on errors. – Lutz Lehmann Dec 04 '16 at 11:55
  • Did you also include the `mxparser.jar` in the classpath for the execution? It should be possible to find more information on exception, on the console the error for the minimal example reads as `Exception in thread "main" java.lang.NoClassDefFoundError: org/mariuszgromada/math/mxparser/Function`. – Lutz Lehmann Dec 04 '16 at 12:34
  • See if this post answers your question: https://stackoverflow.com/questions/2247998/noclassdeffounderror-eclipse-and-android – Lutz Lehmann Dec 04 '16 at 12:44

1 Answers1

0

Its three years since this post was made. You could use the open-source ParserNG.

It supports arithmetic operations, equation solving, differential calculus, integral calculus, basic statistics, function/formula definition, can be used for graphing among others.

Evaluating an expression is as simple as:

    MathExpression expr = new MathExpression("(34+32)-44/(8+9(3+2))-22"); 
    System.out.println("result: " + expr.solve());

    result: 43.16981132075472

Or using variables and calculating simple expressions:

 MathExpression expr = new MathExpression("r=3;P=2*pi*r;"); 
System.out.println("result: " + expr.getValue("P"));

Or using functions:

MathExpression expr = new MathExpression("f(x)=39*sin(x^2)+x^3*cos(x);f(3)"); 
System.out.println("result: " + expr.solve());

result: -10.65717648378352

Or to evaluate the derivative at a given point(Note it does symbolic differentiation(not numerical) behind the scenes, so the accuracy is not limited by the errors of numerical approximations):

MathExpression expr = new MathExpression("f(x)=x^3*ln(x); diff(f,3,1)"); 
System.out.println("result: " + expr.solve());

 result: 38.66253179403897

Which differentiates x^3 * ln(x) once at x=3. The number of times you can differentiate is 1 for now.

or for Numerical Integration:

MathExpression expr = new MathExpression("f(x)=2*x; intg(f,1,3)"); 
System.out.println("result: " + expr.solve());

result: 7.999999999998261... approx: 8

This parser is decently fast and has lots of other functionality.

Work has been concluded on porting it to Swift via bindings to Objective C.

UPDATE

Due to requests in the comments, I have created the Android port as a simple dependency for your projects.

You can get that here

Or just add it in your root build.gradle at the end of repositories:

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Also add to your app's build.gradle file, under your dependencies, add:

dependencies {
            implementation 'com.github.gbenroscience:parserng-android:0.1.1'

    }

DISCLAIMER: ParserNG is authored by me.

gbenroscience
  • 994
  • 2
  • 10
  • 33