-1

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.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
TychoBrahe
  • 477
  • 5
  • 19
  • 4
    `Unfortunately I'm not good enough to implement this.` And if someone else does it for you, you will never be. – Manfred Radlwimmer Jan 29 '18 at 13:19
  • From the looks of your example, you don't want to find solutions to the polynomial, you just want to *evaluate* it. That doesn't require any kind of solver, just a parser. And you don't even need *that* if you store the polynomial as its constants instead. – Jeroen Mostert Jan 29 '18 at 13:21
  • @JeroenMostert I appreciate your answer, as well as the correction in the terms I'm using. What do you mean by "store the polynomial as its constants instead"? Are you describing a different database column for each coefficient plus one for the constant, and then just taking the values in those columns and evaluating them against the X in question? – TychoBrahe Jan 29 '18 at 13:26
  • 1
    @ManfredRadlwimmer Clearly sarcasm is lost on you. – TychoBrahe Jan 29 '18 at 13:28
  • @TychoBrahe: yes, exactly that. See also Micha's answer. In canonical form, a polynomial is just a list of numbers (with `0` for any missing coefficients). If you need to parse a string representation to get that list, that's a different problem from actually evaluating it. If you don't actually need a string because you're the one supplying the input, you can use the more computer-friendly form instead. – Jeroen Mostert Jan 29 '18 at 13:28
  • @JeroenMostert Yes, this seems to be the solution, once I extract all the coefficients from all my equations and get them lined up correctly. – TychoBrahe Jan 29 '18 at 13:32

3 Answers3

2

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)
Wyck
  • 10,311
  • 6
  • 39
  • 60
1

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);
}
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
0

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.

TychoBrahe
  • 477
  • 5
  • 19