Solve the equation from the line to get C

I would like to know if anyone has information or experience on how to do something that sounds simple, but not like this when trying to program it. The idea is this: give a line containing the equation, for example: "2 * x = 10" (it is simple, but can be very complex, for example sqrt (54) * 35 = x ^ 2; on ....), and the program will return x = 5 and perhaps give a log of how it got there.

Is this doable? If so, who has the lead? For information, there is this site ( http://www.numberempire.com/equationsolver.php ) that does the same in PHP, but is not open source.

Thanks for any help!

+3
source share
8 answers

This is called "parsing", and although computer science has already solved this problem, it is not so simple until you fully understand it. There is all the discipline in the field of computer science, which describes how to solve this problem. In C, you should determine the grammar of your input (possibly with priority rules in it), then perform lexical analysis on your input, then analyze the result and finally evaluate the parse tree.

In languages โ€‹โ€‹like Ruby, however, since you have such solid support for string manipulation, and since you have such tremendous power at run time, you can solve your problem with a single line of code, for example:

puts(eval($_)) while gets 

Yes, this will cover more than you ask.

+3
source

First, you must correctly determine which equations you can use as input. Then you should create a good abstraction to represent the equation, for example. class of polynomials. If you want to use a more complex expression, go to the tree for numeric expressions. The analysis can be quite simple, if you have good rules for converting an expression into prefix notation, then evaluation is easily done using stacks. When you have arithmetic trees or polynomials, you can implement transformations to calculate the variable (s).

+2
source

If the equations are really complex, then of course there will be few lines of C / C ++ code.

For linear equations, you will need to simulate one of the methods described in books of linear algebra. The code for this is small enough.

+1
source

One correction: this is not linear algebra, which usually means matrices of multiple equations and unknowns.

Your example, of course, is not complicated.

You need a simple expression grammar and parser. Divide the equation into an abstract syntax tree and swipe the tree to evaluate it.

If you write Java, it may look like this . Another example is symja . Perhaps you will have enough inspiration so that you can come up with your own for C ++.

You can also look at Mathematica and Wolfram Alpha. Stephen Wolfram is one of the world's best mathematicians and computer scientists. He got a lot of things that you could use in your best interests and not write yourself.

You will need to determine what you mean by โ€œdecisionโ€ and what you expect to return.

There are symbolic solutions and numerical solutions. What do you have in mind? Both are equally important, but they are different. You will use different methods depending on your answer.

Another point: there are many methods for "solving" equations, which largely depend on the type of equation. If you give me something like f(x) = 0 , I am thinking of root search algorithms such as Newton's method. If you give me an ordinary differential equation, I can try the substitution method or numerical integration using Runge-Kutta. If you give me a partial differential equation, I can apply finite difference methods, finite elements, or boundary elements. (Do not start me with elliptical, parabolic and hyperbolic PDEs.)

The fact is that your question is very general, and the answer largely depends on what you are trying to do. More details may help.

+1
source

You can try associating C (or C ++) code in SymPy and use it to solve your equations.

IIRC, SymPy has this functionality. Also, it should be easier to manipulate the input string with the applicable equation inside Python, and then pass it to SymPy for the solution.

+1
source

Your problem will have two parts: parsing the equation (s) and solving them symbolically. I am not going to talk much about the first, since other answers have already covered this topic well; my personal recommendation would be to write a simple recursive descent parser for expressions in prefix notation.

The second part, analytically solving the equations, will be complex. Generally speaking, there are special classes of equations for which there are standard methods for finding an analytical solution:

  • Systems of linear equations: any direct linear solver. If you want to explicitly show the steps and the number of equations / unknowns is small, I would recommend something as simple as the unaccepted Gauss exception or the Cramer rule.
  • System of polynomial equations: Equivalent, after variable substitution, for finding the roots of single polynomials. If they have degree <= 4, there are formulas for exact solutions. NB: For 3rd and 4th degree, these formulas are not pleasant.
  • Rational solutions of a system of polynomial equations with a rational coefficient: we replace the variable, as indicated above. Then brute force using a rational zero test.
  • Other types of equations: Good luck. For more complex [systems] nonlinear equations, if you can count on numerical (nonanalytic) solutions, look at Newton's method.
+1
source

In general, you will have to parse the expression in some internal representation. Many books with linear algebra suggest using matrices (or std::vector ) to represent coefficients. The indicator of a member is determined by its position in the vector.

So, for example, the expression:

  2 + 3x + 5x^2 

It can be represented as an array or std::vector :

 std::vector<int> expression; expression[0] = 2; // 2 * x ^ 0 expression[1] = 3; expression[2] = 5; 

Writing an evaluation function becomes trivial and remains as an exercise for the reader.

The solution of several equations is complicated. There are existing libraries and algorithms for this. A google search should come up with something good. :-)

I suggest starting with simple terms and building a parser for this. Once this works, you can change the parser to accept function names.

If you are trying to simplify an expression that has terms on both sides of = , simply write the steps that you usually do when solving manually. Try a few different equations to lower some rules. Now we implement these rules in C ++.

+1
source

All Articles