简体   繁体   中英

Automatic function solving/writing from equation declaration (java ?)

I haven't found a free Java library on the web (lot of people recommend Jep but it is not free). I would like to precise that my need is not solving linear equation (and I have read the posts on it) but to save time when coding:

Java Need

What I would like to do is: rather than interacting with Function's objects, I would like to interact with Equation's ones meaning that I don't want to write twice the same equality or assertion.

Let's say that I'm dealing with the same equation over time: a+b=c. Well sometimes, I could have a and b, sometimes b and c, etc. If I want to get them all I would need to write a Function for each case: f(a,b) -> c, f(a,c) -> b, etc. Is there a library (except Jep so) or do you know an implementation that could do the following pseudo code (Equation class):

Equation eq = new Equation(a+b=c)
eq.a=1
eq.b=2
println(eq.c)  //return 3

Equation eq = new Equation(a+b=c)
eq.a=1
eq.c=3
println(eq.b)  //return 2

We could even imagine something like that:

Equation eq = new Equation()
eq.left.addVariable().plus().addVariable()
eq.right.addVariable()
eq.left.variables.set(0)=1
eq.left.variables.set(1)=2
println(eq.get.)  //return 3

General maths/logic

Just for my personal knowledge:

Is there for any linear equation, a program that could automatically interpret and generate every possible function making this equation true?

Is there for any linear function , a program that could automatically interpret and generate the equation that is true for this function ?

and let's be crazy:

Is there for any programming function (so a procedure taking n arguments and returning a result), a program that could automatically interpret and generate all programming functions that provide exactly the same tuples (args + result)

to precise my last question, let's imagine this pseudo code:

Class FunctionE extends Equationalized{
 apply(arg1,arg2,arg3){
   //do some stuff
 return arg4
}

Equation eq = FunctionE.Equationalize() //return Equation
eq.arg1=***
eq.arg2=***
eq.arg4=***
print(eq.arg3)

If one of the above questions has a bounded positive answer, I'm interesting to know which one and which bound is it !

thank you

So in all honesty this is only half an answer, as I have not seen anything like this in java.

Since you put (java ?) in your title I'm just gonna go ahead an say this: There is a very easy way to do this in python using the free sympy library.

Even more complex functions can be solved, derived, integrated, expanded and simplified, for instance this example piece of code:

 >>> solve(Eq(x**3 + 2*x**2 + 4*x + 8, 0), x)
 [-2*I, 2*I, -2]

Taken from the Quick Examples

I honestly think it would be less work to write a small python script and call it from java then it would be to write something like this for java.

Then again, there might be some library in java that I don't know about that is capable of this.

The older version of Jep is available, and it is free as in beer and open source. It's available on https://sourceforge.net/projects/jep/ and also a slightly changed git-hub repository https://github.com/nathanfunk/jep-java-gpl . It's pretty functional but misses some of the features of the later commercial release.

This version is not being actively developed, apart from critical bugs.

Looking at your question, it does not seem that Jep could do that without quite a bit of work.

(Disclaimer I'm one of the developers of Jep)


If you are strictly in the realm of linear equations. Then there is a whole domain of linear programming and there are a host of free numeric libraries out there. See http://www.ee.ucl.ac.uk/~mflanaga/java/OpenSourceNumeric.html

It is worth having a good look at Apache Commons Maths which is pretty powerful and can solve some nonlinear problems as well.

In the Symja - Java Computer Algebra Library you can use this snippet to program your example:

import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.SyntaxError;
import org.matheclipse.parser.client.math.MathException;

public class SolveSO39753012 {

    public static void main(String[] args) {
        try {
            ExprEvaluator util = new ExprEvaluator();
            IExpr eq = F.Equal(F.Plus(F.a, F.b), F.c);
            IExpr eq1 = eq.replaceAll(F.Rule(F.a, F.integer(1)));
            eq1 = eq1.replaceAll(F.Rule(F.b, F.integer(2)));

            // Solve(1+2==c, c)
            IExpr result = util.evaluate(F.Solve(eq1, F.c));
            // print: {{c->3}}
            System.out.println(result.toString());

            IExpr eq2 = eq.replaceAll(F.Rule(F.a, F.integer(1)));
            eq2 = eq2.replaceAll(F.Rule(F.c, F.integer(3)));
            // Solve(1+b==3, b)
            result = util.evaluate(F.Solve(eq2, F.b));
            // print: {{b->2}}
            System.out.println(result.toString());

        } catch (SyntaxError e) {
            // catch Symja parser errors here
            System.out.println(e.getMessage());
        } catch (MathException me) {
            // catch Symja math errors here
            System.out.println(me.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM