Math.Net Polynomial Class Proposal

(Tobias Glaubach) #1

I am currently working on a few features for MathNet.Numerics.

For these features I require a Polynomial class implementations for numeric values (basically just an array of double values stored within a class with a bunch of methods and operators for Polynomial operations).

I browsed through MathNet.Numerics and could not find anything mathcing what I look for. From
this post I can see that this is a still requested feature.

I have a pretty well imlemented class in my own code that I would like to migrate to MathNet Numerics for that. Where would you propose to put that? I would propose MathNet.Numerics.Polynomial as a location. What do you think?

Features the class would have:

  • Only real values (double[]) are supported (as a first implementation)
  • Polynomial operations + - * /
  • Method: Complex[] Roots()
  • Method: Polynomial Differentiate()
  • Method: Polynomial Integrate()
  • Static Method: Polynomial FitFromData(double x)

usage example:

var x = new double[] {5, 4, 3, 0, 2};
var p = new Polynomial(x);
p.ToString() // "2 x^3 + 0 x^2 + 3 x^2 + 4 x^1 + 5"

var x = new double[] {5, 4, 3, 0, 2};
var p2 = new Polynomial(x, isFlip:true);
p2.ToString() // 5 x^3 + 4 x^2 + 3 x^2 + 0 x^1 + 2

(Christoph Rüegg) #2

That would be great, thanks. We can put it either to MathNet.Numerics.Polynomial or directly to MathNet.Numerics.

We already have some polynomial related functionality in Evaluate, FindRoots, Fit and Interpolate, there may thus be some chance for some consolidation.

(Christoph Rüegg) #3

Typically I’d tend to implement aspects like interpolation and curve fitting in their contexts, not directly in the Polynomial type. But the polynomial type could (should) provide shortcuts to use them, so we could still have a static Polyomial.Fit etc for usability.

(Peter Vanderwaart) #4

From a mathematical (algebraic) perspective, polynomials form a ring, which is to say, a set with two operations defined on it: addition and multiplication. So it seem reasonable to include:

var p3 = Polynomial.Add(p1,p2);
var p3 = Polynomial.Mult(p1,p2);

Two other functions that could be included are multiplication by a scalar and degree:;

var p2 = Polynomial.Mult(s,p1);
var d = Polynomial.Degree(p);

Complex versions of everything should be implemented as overrides.

(Tobias Glaubach) #5

I will try to reuse as many of the existing features as possible by calling them from within the Polynomial class.

  • p.Evaluate(double x) with p as instance of Polynomial will simply map to Evaluate.Polynomial(x, p.Coeffs)

  • Polynomial.Fit(double[] x double[] y) will simply map to Fit.Polynomial(x, y)

for the Complex[] r = p.Roots() method I did not find any implementation that matches my needs. You can take a look at my implementation, when I send a pull Request.

(Christoph Rüegg) #6

Great, thanks.

Of course, if your implementation is superior or more specific we should use your existing implementation instead. For example, our Fit.Polynomial is not particularly robust at this point.