How to evaluate a polynomial given as a string?

1.9k views Asked by At

I have dozens of polynomial equations, some 2nd order, some 3rd, and others 4th. Each equation has different coefficients and constants, but none of them are overly complex since they all contain only one input variable. The input variable x will be different for each equation.

e.g. 
x = 11
y = -0.00006x^4 + 0.0272x^3 - 1.4546x^2 - 17.743x + 8137.3
y = 7801.44514

I have these stored in a database. I want to pull these out as a string type, and evaluate them. I will always have the X in question. What is a good way of handling this problem? I am trying to use the Microsoft Foundation Solver, because I think I would be able to convert my string equation into a format that the solver understands, supply my x, and solve for y. Unfortunately I'm not good enough to implement this.

3

There are 3 answers

0
Micha Wiedenmann On

As a starting point, the following code evaluates a polynomial given by its coefficients:

public static double Evaluate(double x, IEnumerable<double> coefficients)
    => coefficients.Select((a, i) => a * Math.Pow(x, i)).Sum();

public static void Main(string[] args)
{
    var coeff = new List<double> { 8137.3, -17.743, -1.4546, 0.0272, -0.00006, };

    var value = Evaluate(11, coeff);
}
0
Wyck On

Assuming it's in a predictable format, then this regex should find the terms, provided there's no scientific notation or negative exponents or other more complex stuff going on like parentheses or multivariables etc.

[-+](\s*\d+\.\d+)(x(\^\d+)?)?

Use regex tester to see how it groups it down to get the signed coefficient and exponent for each term.

Then you need an array of terms, each term having a coefficient and power. Recall that convention says that a missing coefficient is 1, a missing exponent is 1, and a constant term (missing x entirely) is a term where the exponent is 0.

Then evaluate the polynomial like this:

terms.Sum((Term term) => term.Evaluate(x));

Term.Evaluate would of course be just:

coefficient * Math.Power(x, exponent)
0
dev30207 On

Thanks to @MichaWiedenmann, @JeroenMostert, and @wyck I was able to come up with a solution using the Microsoft Solver Foundation (MSF).

I started with

y = 0.0002x^4 + 0.0031x^3 - 0.3972x^2 - 27.394x + 6879.2

After some regex massaging I came up with

y == 0.0002 * Power[x,4] + 0.0031 * Power[x,3] - 0.3972 * Power[x,2] - 27.394 * x + 6879.2

This is a format that is solvable by the MSF

SolverContext context = SolverContext.GetContext();
var model = context.CreateModel();
Decision y = new Decision(Domain.Real, "y");
model.AddDecisions(y);
int x = GetValueFromDatabase();
string formula = GetFormulaFromDatabase().Replace("x",x.ToString());
model.AddConstraint("myFormula", formula);
var solution = context.Solve();
var report = solution.GetReport();
Console.WriteLine("Load: {0}", y);

Using this code I'm able to pull my string equation from the database in a MSF format, load in my x value, and evaluate the equation.

Thanks for the help everyone.