问题
In java i am trying to find the coefficients of a linear equation to find solution of linear equation in my calculator application for example :
3x +2*(6x-3) = 2 -4x
what i am dying to get is the coefficients of x and the constant in the form ax+b =0
,
in this particular example
coefficient = 19
constant = -8
Please suggest a generalized idea
回答1:
As already suggested by my comment: This may be arbitrarily complicated, depending on what exactly this parser should support. There are several potentially very complex and challenging tasks involved here.
The first one is parsing itself. Although it's well understood and there are supporting tools for writing parsers and all, it would be tedious (and would involve some effort) to write a robust, reliable parser for these expressions from scratch.
The second one is simplifying the expression. While one could write a simple parser (or use an existing one), one might (at the first glance) think that it is necessary to perform manipulations on the generated AST in order to find the actual constant and coefficient - e.g. one could think that it would be necessary to apply the laws of distribution, find common factors, shift partial expression from one side of the equation to the other and so on.
Fortunately, all this is not necessary :-)
You can use an arbitrary parser to parse the expressions that are involved in the equation. One of the most well-known parsers is JEP, the Java Expression Parser (this is not a recommendation - I just knew it, and it seems to work well). As the name suggests, it's only an Expression parser, and not an Equation parser. But the equation can simply be split at the =
in order to obtain two expressions that can be parsed individually.
The two expressions would not be sufficient in order to find the coefficient and constant. But here, a small (dirty?) trick comes into play: You can derive the coefficient and constants by evaluating these expressions. Particularly, you can once set x=0
to determine the constant part of the left and right side, respectively. Then, you can set x=1
, evaluate the resulting expression, and subtract the constant in order to obtain the coefficient.
From the coefficients and constants of both sides, you can compute the coefficient and constant of the whole equation. This is implemented here, as a MCVE:
import org.nfunk.jep.JEP;
public class LinearEquationParser
{
private double coefficient;
private double constant;
public static void main(String[] args)
{
runTest("3x = 5");
runTest("3x +2*(6x-3) = 2 -4x");
runTest("3x + 2*(6x -sin(3))=cos(2)-4*x*log(tan(43))");
}
private static void runTest(String s)
{
System.out.println("Input: "+s);
LinearEquationParser p = new LinearEquationParser();
p.process(s);
System.out.println("Coefficient: "+p.getCoefficient());
System.out.println("Constant : "+p.getConstant());
System.out.println();
}
public void process(String s)
{
JEP jep = new JEP();
jep.setImplicitMul(true);
jep.addStandardFunctions();
jep.addStandardConstants();
jep.addVariable("x", 0.0);
String s0 = s.substring(0, s.indexOf("="));
String s1 = s.substring(s.indexOf("=")+1, s.length());
jep.parseExpression(s0);
if (jep.hasError())
{
throw new IllegalArgumentException(jep.getErrorInfo());
}
jep.addVariable("x", 0.0);
double constant0 = jep.getValue();
jep.addVariable("x", 1.0);
double value0 = jep.getValue();
jep.parseExpression(s1);
if (jep.hasError())
{
throw new IllegalArgumentException(jep.getErrorInfo());
}
jep.addVariable("x", 0.0);
double constant1 = jep.getValue();
jep.addVariable("x", 1.0);
double value1 = jep.getValue();
constant = constant0 - constant1;
coefficient = (value0 - constant0) - (value1-constant1);
}
public double getCoefficient()
{
return coefficient;
}
public double getConstant()
{
return constant;
}
}
The output is, as desired:
Input: 3x = 5
Coefficient: 3.0
Constant : -5.0
Input: 3x +2*(6x-3) = 2 -4x
Coefficient: 19.0
Constant : -8.0
Input: 3x + 2*(6x -sin(3))=cos(2)-4*x*log(tan(43))
Coefficient: 15.7024963786418
Constant : 0.13390682042740798
来源:https://stackoverflow.com/questions/32093264/parse-coefficient-of-an-linear-equation