3

Imagine I have the following String: "10*x = 20"

How would I get x using Java? I am looking for something like:

String str = "10*x = 20";
int x = Integer.parseInt( str.solve("x") ) //==> 2;

It should also work for equations like:

10-x+y = 5 //x = 10+y-5
4x = 8y //y = 2x
...

Is there any function, or an easy way to automatically solve equations like these?

EDIT: I would also be happy with a way, to use the Python SymPy solve feature using Java.

d0n.key
  • 1,318
  • 2
  • 18
  • 39
  • 2
    Interesting problem! – rajuGT Nov 07 '15 at 19:33
  • why don't you create a function to solve? – Blip Nov 07 '15 at 19:34
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1431885/how-to-solve-equations-with-java – lrnzcig Nov 07 '15 at 19:39
  • Not a duplicate of that one. I have only one equation where variables are variables, and not solved to numbers directly. – d0n.key Nov 07 '15 at 19:41
  • @Blip >> Tell me how ^^ – d0n.key Nov 07 '15 at 19:42
  • str.solve()? what are you trying to do here? going to add a method to the Strig Class? good luck – Franmcod Nov 07 '15 at 20:03
  • @NoobCoder >> Not like this, but it just showed what I expect as Method. Of course it would make more sense if there was something like: EquationSolver.solve(str, "x"); – d0n.key Nov 07 '15 at 20:13
  • Well i think if it was easy to solve certain string equation in java, it would be already implemented. The only way that i think of right now is to split string by "=" , and then by its operations(+-/*), and then many `case` and `if` statements would be involved, maybe there is a better approach tho. – Sekula1991 Nov 07 '15 at 20:17
  • I suggest you first learn to generate a syntax tree from the string. Then you do some crazy transformations and reordering on that until you can solve the equation. Pretty much how you'd do it in Maths using your brain. – Clashsoft Nov 07 '15 at 20:28
  • @Sekula1991 >> Yea, I also thought of that idea. Would you be up to help me coding this? – d0n.key Nov 07 '15 at 20:42
  • @Clashsoft has a point, that would be a better approach, however if uwant to go with splitting, that would require a lot of situations handled (What is the operation, is the position of x inside equation relevant for that operation, are there multiple operations, if so - more splits, then there may be brackets...) You see a point - its pointless :) – Sekula1991 Nov 07 '15 at 20:50
  • I guess you're right. – d0n.key Nov 07 '15 at 20:56
  • 1
    The best way to solve problems like this is divide&conquer. Split your problem into several smaller problems and find a solution to each of them. Start with how you convert the String to something you can work with more easily. Then find out how to do the *solve* part with that. – Clashsoft Nov 08 '15 at 12:47
  • 2
    Example for using Symja's Solve() function: https://bitbucket.org/axelclk/symjaunittests/src/master/SymjaUnitTests/src/org/matheclipse/core/examples/SO_33586896_SolveExample.java for your problem. – axelclk Nov 10 '15 at 20:55

0 Answers0