15 lines
765 B
C#
15 lines
765 B
C#
public static double[] Polyfit(double[] x, double[] y, int degree)
|
|
{
|
|
// Vandermonde matrix
|
|
var v = new DenseMatrix(x.Length, degree + 1);
|
|
for (int i = 0; i < v.RowCount; i++)
|
|
for (int j = 0; j <= degree; j++) v[i, j] = Math.Pow(x[i], j);
|
|
var yv = new DenseVector(y).ToColumnMatrix();
|
|
QR<double> qr = v.QR();
|
|
// Math.Net doesn't have an "economy" QR, so:
|
|
// cut R short to square upper triangle, then recompute Q
|
|
var r = qr.R.SubMatrix(0, degree + 1, 0, degree + 1);
|
|
var q = v.Multiply(r.Inverse());
|
|
var p = r.Inverse().Multiply(q.TransposeThisAndMultiply(yv));
|
|
return p.Column(0).ToArray();
|
|
}
|