Browse Source

Polynomial: adapt to local code style

ridge-regression
Christoph Ruegg 8 years ago
parent
commit
674a221956
  1. 14
      src/Numerics.Tests/PolynomialTests.cs
  2. 330
      src/Numerics/Polynomial.cs

14
src/Numerics.Tests/PolynomialTests.cs

@ -69,7 +69,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(expected.Length, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(expected.Length, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(expected[k], p_res.Coeffs[k], "idx: " + k + " mismatch"); Assert.AreEqual(expected[k], p_res.Coefficients[k], "idx: " + k + " mismatch");
} }
} }
} }
@ -92,7 +92,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(expected.Length, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(expected.Length, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(expected[k], p_res.Coeffs[k], "idx: " + k + " mismatch"); Assert.AreEqual(expected[k], p_res.Coefficients[k], "idx: " + k + " mismatch");
} }
} }
} }
@ -128,7 +128,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(p_tar.Coeffs[k], p_res.Coeffs[k], msg); Assert.AreEqual(p_tar.Coefficients[k], p_res.Coefficients[k], msg);
} }
} }
} }
@ -165,7 +165,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(p_tar.Coeffs[k], p_res.Coeffs[k], msg); Assert.AreEqual(p_tar.Coefficients[k], p_res.Coefficients[k], msg);
} }
} }
} }
@ -201,7 +201,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(p_tar.Coeffs[k], p_res.Coeffs[k], msg); Assert.AreEqual(p_tar.Coefficients[k], p_res.Coefficients[k], msg);
} }
} }
} }
@ -362,7 +362,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(p_tar.Length, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(p_tar.Length, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(p_tar[k], p_res.Coeffs[k], msg); Assert.AreEqual(p_tar[k], p_res.Coefficients[k], msg);
} }
} }
@ -371,7 +371,7 @@ namespace MathNet.Numerics.UnitTests
Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.CoefficientCount; k++) for (int k = 0; k < p_res.CoefficientCount; k++)
{ {
Assert.AreEqual(p_tar.Coeffs[k], p_res.Coeffs[k], msg); Assert.AreEqual(p_tar.Coefficients[k], p_res.Coefficients[k], msg);
} }
} }
} }

330
src/Numerics/Polynomial.cs

@ -17,7 +17,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// The coefficients of the polynomial in a /// The coefficients of the polynomial in a
/// </summary> /// </summary>
public double[] Coeffs { get; set; } public double[] Coefficients { get; set; }
/// <summary> /// <summary>
/// Only needed for the ToString method /// Only needed for the ToString method
@ -31,7 +31,7 @@ namespace MathNet.Numerics
{ {
get get
{ {
return (Coeffs == null ? 0 : Coeffs.Length); return (Coefficients == null ? 0 : Coefficients.Length);
} }
} }
@ -43,14 +43,14 @@ namespace MathNet.Numerics
{ {
get get
{ {
if (Coeffs == null) if (Coefficients == null)
{ {
return -1; return -1;
} }
for (int i = Coeffs.Length - 1; i >= 0; i--) for (int i = Coefficients.Length - 1; i >= 0; i--)
{ {
if (Coeffs[i] != 0.0) if (Coefficients[i] != 0.0)
{ {
return i; return i;
} }
@ -70,45 +70,44 @@ namespace MathNet.Numerics
{ {
throw new ArgumentOutOfRangeException("n must be postive"); throw new ArgumentOutOfRangeException("n must be postive");
} }
Coeffs = new double[n]; Coefficients = new double[n];
} }
/// <summary> /// <summary>
/// make Polynomial: e.G 3.0 = 3.0 + 0 x^1 + 0 x^2 /// make Polynomial: e.G 3.0 = 3.0 + 0 x^1 + 0 x^2
/// </summary> /// </summary>
/// <param name="coeff">just the "x^0" part</param> /// <param name="coefficient">just the "x^0" part</param>
public Polynomial(double coeff) public Polynomial(double coefficient)
{ {
this.Coeffs = new double[1]; Coefficients = new double[1];
Coeffs[0] = coeff; Coefficients[0] = coefficient;
} }
/// <summary> /// <summary>
/// make Polynomial: e.G new double[] {5, 0, 2} = "5 + 0 x^1 + 2 x^2" /// make Polynomial: e.G new double[] {5, 0, 2} = "5 + 0 x^1 + 2 x^2"
/// </summary> /// </summary>
/// <param name="coeffs"> Polynomial coefficiens as enumerable</param> /// <param name="coefficients">Polynomial coefficients as enumerable</param>
public Polynomial(IEnumerable<double> coeffs) public Polynomial(IEnumerable<double> coefficients)
{ {
if (coeffs == null) if (coefficients == null)
{ {
throw new ArgumentNullException("coeffs"); throw new ArgumentNullException(nameof(coefficients));
} }
this.Coeffs = coeffs.ToArray(); Coefficients = coefficients.ToArray();
} }
/// <summary> /// <summary>
/// make Polynomial: e.G new double[] {5, 0, 2} = "5 + 0 x^1 + 2 x^2" /// make Polynomial: e.G new double[] {5, 0, 2} = "5 + 0 x^1 + 2 x^2"
/// </summary> /// </summary>
/// <param name="coeffs"> Polynomial coefficiens as array</param> /// <param name="coefficients">Polynomial coefficients as array</param>
public Polynomial(double[] coeffs) public Polynomial(double[] coefficients)
{ {
if (coeffs == null) if (coefficients == null)
{ {
throw new ArgumentNullException("coeffs"); throw new ArgumentNullException(nameof(coefficients));
} }
this.Coeffs = new double[coeffs.Length]; Coefficients = new double[coefficients.Length];
Array.Copy(coeffs, this.Coeffs, coeffs.Length); Array.Copy(coefficients, Coefficients, coefficients.Length);
} }
/// <summary> /// <summary>
@ -120,30 +119,27 @@ namespace MathNet.Numerics
return; return;
int i = CoefficientCount - 1; int i = CoefficientCount - 1;
while (i >= 0 && Coeffs[i] == 0.0) while (i >= 0 && Coefficients[i] == 0.0)
i--; i--;
if (i < 0) if (i < 0)
Coeffs = new double[1] { 0.0 }; Coefficients = new[] { 0.0 };
else if (i == 0) else if (i == 0)
Coeffs = new double[1] { Coeffs[0] }; Coefficients = new[] { Coefficients[0] };
else else
{ {
var hold = new double[i+1]; var hold = new double[i+1];
Array.Copy(Coeffs, hold, i+1); Array.Copy(Coefficients, hold, i+1);
Coeffs = hold; Coefficients = hold;
} }
} }
#region Data Interaction
/// <summary> /// <summary>
/// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k
/// </summary> /// </summary>
public static Polynomial Fit(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR) public static Polynomial Fit(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
{ {
var pArr = MathNet.Numerics.Fit.Polynomial(x, y, order, method); var pArr = Numerics.Fit.Polynomial(x, y, order, method);
return new Polynomial(pArr); return new Polynomial(pArr);
} }
@ -153,7 +149,7 @@ namespace MathNet.Numerics
/// <param name="z">The location where to evaluate the polynomial at.</param> /// <param name="z">The location where to evaluate the polynomial at.</param>
public double Evaluate(double z) public double Evaluate(double z)
{ {
return MathNet.Numerics.Evaluate.Polynomial(z, Coeffs); return Numerics.Evaluate.Polynomial(z, Coefficients);
} }
/// <summary> /// <summary>
@ -162,32 +158,30 @@ namespace MathNet.Numerics
/// <param name="z">The locations where to evaluate the polynomial at.</param> /// <param name="z">The locations where to evaluate the polynomial at.</param>
public IEnumerable<double> Evaluate(IEnumerable<double> z) public IEnumerable<double> Evaluate(IEnumerable<double> z)
{ {
var Lst = new List<double>(); var result = new List<double>();
foreach (var item in z) foreach (var item in z)
{ {
Lst.Add(Evaluate(item)); result.Add(Evaluate(item));
} }
return Lst;
}
#endregion return result;
}
#region diff/int
public Polynomial Differentiate() public Polynomial Differentiate()
{ {
if (Coefficients.Length == 0)
if (Coeffs.Length == 0)
{ {
return null; return null;
} }
var t = this.Clone() as Polynomial; var t = Clone() as Polynomial;
t.Trim(); t.Trim();
var cNew = new double[t.Coeffs.Length - 1]; var cNew = new double[t.Coefficients.Length - 1];
for (int i = 1; i < t.Coeffs.Length; i++) for (int i = 1; i < t.Coefficients.Length; i++)
{ {
cNew[i-1] = t.Coeffs[i] * i; cNew[i-1] = t.Coefficients[i] * i;
} }
var p = new Polynomial(cNew); var p = new Polynomial(cNew);
p.Trim(); p.Trim();
return p; return p;
@ -195,30 +189,26 @@ namespace MathNet.Numerics
public Polynomial Integrate() public Polynomial Integrate()
{ {
var t = this.Clone() as Polynomial; var t = Clone() as Polynomial;
t.Trim(); t.Trim();
var cNew = new double[t.Coeffs.Length + 1]; var cNew = new double[t.Coefficients.Length + 1];
for (int i = 1; i < cNew.Length; i++) for (int i = 1; i < cNew.Length; i++)
{ {
cNew[i] = t.Coeffs[i-1] / i; cNew[i] = t.Coefficients[i - 1] / i;
} }
var p = new Polynomial(cNew); var p = new Polynomial(cNew);
p.Trim(); p.Trim();
return p; return p;
} }
#endregion
#region Operators
/// <summary> /// <summary>
/// multiplies a Polynomial by a Polynomial using convolution [ASINCO.libs.subfun.conv(a.Coeffs, b.Coeffs)] /// multiplies a Polynomial by a Polynomial using convolution [ASINCO.libs.subfun.conv(a.Coeffs, b.Coeffs)]
/// </summary> /// </summary>
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator *( Polynomial a, Polynomial b) public static Polynomial operator *(Polynomial a, Polynomial b)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
var bb = b.Clone() as Polynomial; var bb = b.Clone() as Polynomial;
@ -226,12 +216,12 @@ namespace MathNet.Numerics
//a.Trim(); //a.Trim();
//b.Trim(); //b.Trim();
double[] ret = conv(aa.Coeffs, bb.Coeffs); double[] ret = Convolution(aa.Coefficients, bb.Coefficients);
Polynomial ret_p = new Polynomial(ret); Polynomial result = new Polynomial(ret);
//ret_p.Trim(); //ret_p.Trim();
return (ret_p); return result;
} }
@ -241,13 +231,12 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="k">scalar value</param> /// <param name="k">scalar value</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator *( Polynomial a, double k) public static Polynomial operator *(Polynomial a, double k)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
for (int ii = 0; ii < aa.Coefficients.Length; ii++)
for (int ii = 0; ii < aa.Coeffs.Length; ii++) aa.Coefficients[ii] *= k;
aa.Coeffs[ii] *= k;
return aa; return aa;
} }
@ -258,11 +247,11 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="k">scalar value</param> /// <param name="k">scalar value</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator +( Polynomial a, double k) public static Polynomial operator +(Polynomial a, double k)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
aa.Coeffs[0] += k; aa.Coefficients[0] += k;
return aa; return aa;
} }
@ -272,11 +261,11 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="k">scalar value</param> /// <param name="k">scalar value</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator -( Polynomial a, double k) public static Polynomial operator -(Polynomial a, double k)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
a.Coeffs[0] -= k; a.Coefficients[0] -= k;
return aa; return aa;
} }
@ -286,12 +275,12 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="k">scalar value</param> /// <param name="k">scalar value</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator /( Polynomial a, double k) public static Polynomial operator /(Polynomial a, double k)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
for (int ii = 0; ii < aa.Coeffs.Length; ii++) for (int ii = 0; ii < aa.Coefficients.Length; ii++)
aa.Coeffs[ii] /= k; aa.Coefficients[ii] /= k;
return aa; return aa;
} }
@ -302,7 +291,7 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator +( Polynomial a, Polynomial b) public static Polynomial operator +(Polynomial a, Polynomial b)
{ {
return Add(a, b); return Add(a, b);
} }
@ -313,7 +302,7 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial operator -( Polynomial a, Polynomial b) public static Polynomial operator -(Polynomial a, Polynomial b)
{ {
return Substract(a, b); return Substract(a, b);
} }
@ -324,25 +313,26 @@ namespace MathNet.Numerics
/// <returns>a vector of complex numbers with the roots</returns> /// <returns>a vector of complex numbers with the roots</returns>
public Complex[] GetRoots() public Complex[] GetRoots()
{ {
DenseMatrix A = this.GetEigValMatrix(); DenseMatrix A = GetEigValMatrix();
Complex[] c_vec; Complex[] roots;
if (A == null) if (A == null)
{ {
if (Coeffs.Length < 2) if (Coefficients.Length < 2)
{ {
var val = Coeffs.Length == 1 ? Coeffs[0] : Double.NaN; var val = Coefficients.Length == 1 ? Coefficients[0] : Double.NaN;
c_vec = new Complex[1] { val }; roots = new Complex[] { val };
} }
else else
c_vec = new Complex[1] { new Complex(-Coeffs[0] / Coeffs[1], 0) }; roots = new[] { new Complex(-Coefficients[0] / Coefficients[1], 0) };
} }
else else
{ {
Evd<double> eigen = A.Evd(Symmetricity.Asymmetric); Evd<double> eigen = A.Evd(Symmetricity.Asymmetric);
c_vec = eigen.EigenValues.ToArray(); roots = eigen.EigenValues.ToArray();
} }
return c_vec;
return roots;
} }
/// <summary> /// <summary>
@ -352,26 +342,28 @@ namespace MathNet.Numerics
/// <note>this matrix is similar to the companion matrix of this polynomial, in such a way, that it's transpose is the columnflip of the companion matrix</note> /// <note>this matrix is similar to the companion matrix of this polynomial, in such a way, that it's transpose is the columnflip of the companion matrix</note>
public DenseMatrix GetEigValMatrix() public DenseMatrix GetEigValMatrix()
{ {
Polynomial pLoc = new Polynomial(this.Coeffs); Polynomial pLoc = new Polynomial(Coefficients);
pLoc.Trim(); pLoc.Trim();
int n = pLoc.Coeffs.Length - 1; int n = pLoc.Coefficients.Length - 1;
if (n < 2) if (n < 2)
{
return null; return null;
}
double a0 = pLoc.Coefficients[n];
double[] p = new double[n]; double[] p = new double[n];
double a0 = pLoc.Coeffs[n];
for (int ii = n - 1; ii >= 0; ii--) for (int ii = n - 1; ii >= 0; ii--)
p[ii] = -pLoc.Coeffs[ii] / a0; {
p[ii] = -pLoc.Coefficients[ii] / a0;
}
DenseMatrix A0 = DenseMatrix.CreateDiagonal(n - 1, n - 1, 1.0); DenseMatrix A0 = DenseMatrix.CreateDiagonal(n - 1, n - 1, 1.0);
DenseMatrix A = new DenseMatrix(n); DenseMatrix A = new DenseMatrix(n);
A.SetSubMatrix(1, 0, A0); A.SetSubMatrix(1, 0, A0);
A.SetRow(0, p.Reverse().ToArray()); A.SetRow(0, p.Reverse().ToArray());
return A; return A;
} }
@ -381,24 +373,24 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial DividePointwise( Polynomial a, Polynomial b) public static Polynomial DividePointwise(Polynomial a, Polynomial b)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
var bb = b.Clone() as Polynomial; var bb = b.Clone() as Polynomial;
if (aa.Coeffs.Length != bb.Coeffs.Length) if (aa.Coefficients.Length != bb.Coefficients.Length)
mkSameLength(ref aa, ref bb); {
MakeSameLength(ref aa, ref bb);
int n = aa.Coeffs.Length; }
double[] res = new double[aa.Coeffs.Length];
int n = aa.Coefficients.Length;
double[] res = new double[aa.Coefficients.Length];
for (int ii = 0; ii < n; ii++) for (int ii = 0; ii < n; ii++)
{ {
res[ii] = aa.Coeffs[ii] / bb.Coeffs[ii]; res[ii] = aa.Coefficients[ii] / bb.Coefficients[ii];
} }
Polynomial res_poly = new Polynomial(res);
return (res_poly); return new Polynomial(res);
} }
/// <summary> /// <summary>
@ -407,24 +399,24 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial MultiplyPointwise( Polynomial a, Polynomial b) public static Polynomial MultiplyPointwise(Polynomial a, Polynomial b)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
var bb = b.Clone() as Polynomial; var bb = b.Clone() as Polynomial;
if (aa.Coeffs.Length != bb.Coeffs.Length) if (aa.Coefficients.Length != bb.Coefficients.Length)
mkSameLength(ref aa, ref bb); {
MakeSameLength(ref aa, ref bb);
int n = aa.Coeffs.Length; }
double[] res = new double[aa.Coeffs.Length];
int n = aa.Coefficients.Length;
double[] res = new double[aa.Coefficients.Length];
for (int ii = 0; ii < n; ii++) for (int ii = 0; ii < n; ii++)
{ {
res[ii] = aa.Coeffs[ii] * bb.Coeffs[ii]; res[ii] = aa.Coefficients[ii] * bb.Coefficients[ii];
} }
Polynomial res_poly = new Polynomial(res);
return (res_poly); return new Polynomial(res);
} }
/// <summary> /// <summary>
@ -433,24 +425,24 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial Add( Polynomial a, Polynomial b) public static Polynomial Add(Polynomial a, Polynomial b)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
var bb = b.Clone() as Polynomial; var bb = b.Clone() as Polynomial;
if (aa.CoefficientCount != bb.CoefficientCount) if (aa.CoefficientCount != bb.CoefficientCount)
mkSameLength(ref aa, ref bb); {
MakeSameLength(ref aa, ref bb);
}
int n = aa.CoefficientCount; int n = aa.CoefficientCount;
double[] res = new double[n]; double[] res = new double[n];
for (int ii = 0; ii < n; ii++) for (int ii = 0; ii < n; ii++)
{ {
res[ii] = aa.Coeffs[ii] + bb.Coeffs[ii]; res[ii] = aa.Coefficients[ii] + bb.Coefficients[ii];
} }
Polynomial res_poly = new Polynomial(res);
return (res_poly); return new Polynomial(res);
} }
/// <summary> /// <summary>
@ -459,24 +451,24 @@ namespace MathNet.Numerics
/// <param name="a">left Polynomial</param> /// <param name="a">left Polynomial</param>
/// <param name="b">right Polynomial</param> /// <param name="b">right Polynomial</param>
/// <returns>resulting Polynomial</returns> /// <returns>resulting Polynomial</returns>
public static Polynomial Substract( Polynomial a, Polynomial b) public static Polynomial Substract(Polynomial a, Polynomial b)
{ {
var aa = a.Clone() as Polynomial; var aa = a.Clone() as Polynomial;
var bb = b.Clone() as Polynomial; var bb = b.Clone() as Polynomial;
if (aa.CoefficientCount != bb.CoefficientCount) if (aa.CoefficientCount != bb.CoefficientCount)
mkSameLength(ref aa, ref bb); {
MakeSameLength(ref aa, ref bb);
}
int n = aa.CoefficientCount; int n = aa.CoefficientCount;
double[] res = new double[n]; double[] res = new double[n];
for (int ii = 0; ii < n; ii++) for (int ii = 0; ii < n; ii++)
{ {
res[ii] = aa.Coeffs[ii] - bb.Coeffs[ii]; res[ii] = aa.Coefficients[ii] - bb.Coefficients[ii];
} }
Polynomial res_poly = new Polynomial(res);
return (res_poly); return new Polynomial(res);
} }
/// <summary> /// <summary>
@ -497,11 +489,11 @@ namespace MathNet.Numerics
if (b.CoefficientCount <= 0) if (b.CoefficientCount <= 0)
throw new ArgumentOutOfRangeException("b Degree must be greater than zero"); throw new ArgumentOutOfRangeException("b Degree must be greater than zero");
if (b.Coeffs[b.CoefficientCount-1] == 0) if (b.Coefficients[b.CoefficientCount-1] == 0)
throw new DivideByZeroException("b polynomial ends with zero"); throw new DivideByZeroException("b polynomial ends with zero");
var c1 = a.Coeffs.ToArray(); var c1 = a.Coefficients.ToArray();
var c2 = b.Coeffs.ToArray(); var c2 = b.Coefficients.ToArray();
var n1 = c1.Length; var n1 = c1.Length;
var n2 = c2.Length; var n2 = c2.Length;
@ -529,14 +521,15 @@ namespace MathNet.Numerics
var scl = c2[n2 - 1]; var scl = c2[n2 - 1];
var c22 = new double[n2 - 1]; var c22 = new double[n2 - 1];
for (int ii = 0; ii < c22.Length; ii++) for (int ii = 0; ii < c22.Length; ii++)
{
c22[ii] = c2[ii] / scl; c22[ii] = c2[ii] / scl;
}
int i = dn; int i = dn;
int j = n1 - 1; int j = n1 - 1;
while (i >= 0) while (i >= 0)
{ {
var v = c1[j]; var v = c1[j];
var vals = new double[j - i];
for (int k = i; k < j; k++) for (int k = i; k < j; k++)
c1[k] -= c22[k-i] * v; c1[k] -= c22[k-i] * v;
i--; i--;
@ -550,10 +543,14 @@ namespace MathNet.Numerics
quo = new double[l1]; quo = new double[l1];
for (int k = 0; k < l1; k++) for (int k = 0; k < l1; k++)
{
quo[k] = c1[k + j1] / scl; quo[k] = c1[k + j1] / scl;
}
for (int k = 0; k < j1; k++) for (int k = 0; k < j1; k++)
{
rem[k] = c1[k]; rem[k] = c1[k];
}
} }
@ -563,7 +560,6 @@ namespace MathNet.Numerics
if (quo == null) if (quo == null)
throw new NullReferenceException("resulting quotient was null"); throw new NullReferenceException("resulting quotient was null");
// output mapping // output mapping
var pQuo = new Polynomial(quo); var pQuo = new Polynomial(quo);
var pRem = new Polynomial(rem); var pRem = new Polynomial(rem);
@ -573,7 +569,6 @@ namespace MathNet.Numerics
return new Tuple<Polynomial, Polynomial>(pQuo, pRem); return new Tuple<Polynomial, Polynomial>(pQuo, pRem);
} }
/// <summary> /// <summary>
/// Division of two polynomials returning the quotient-with-remainder of the two polynomials given /// Division of two polynomials returning the quotient-with-remainder of the two polynomials given
/// </summary> /// </summary>
@ -584,10 +579,6 @@ namespace MathNet.Numerics
return DivideLong(this, b); return DivideLong(this, b);
} }
#endregion
#region Displaying
/// <summary> /// <summary>
/// "0.00 x^3 + 0.00 x^2 + 0.00 x^1 + 0.00" like display of this Polynomial /// "0.00 x^3 + 0.00 x^2 + 0.00 x^1 + 0.00" like display of this Polynomial
/// </summary> /// </summary>
@ -603,95 +594,84 @@ namespace MathNet.Numerics
/// <returns>string in displayed format</returns> /// <returns>string in displayed format</returns>
public string ToString(bool highestFirst) public string ToString(bool highestFirst)
{ {
string strLoc = ""; if (Coefficients == null)
if (this.Coeffs == null)
{ {
return "null"; return "null";
} }
if (this.Coeffs.Length == 0) if (Coefficients.Length == 0)
{ {
return ""; return "";
} }
string result = "";
if (!highestFirst) if (!highestFirst)
{ {
for (int ii = 0; ii < Coeffs.Length; ii++) for (int ii = 0; ii < Coefficients.Length; ii++)
{ {
if (ii == 0 && Coeffs.Length == 1) if (ii == 0 && Coefficients.Length == 1)
strLoc += String.Format("{0}", this.Coeffs[ii], VarName, ii); result += String.Format("{0}", Coefficients[ii], VarName, ii);
else if(ii == 0) else if(ii == 0)
strLoc += String.Format("{0} + ", this.Coeffs[ii], VarName, ii); result += String.Format("{0} + ", Coefficients[ii], VarName, ii);
else if (ii == Coeffs.Length - 1) else if (ii == Coefficients.Length - 1)
strLoc += String.Format("{0}{1}{2}", this.Coeffs[ii], VarName, ii); result += String.Format("{0}{1}{2}", Coefficients[ii], VarName, ii);
else else
strLoc += String.Format("{0}{1}{2} + ", this.Coeffs[ii], VarName, ii); result += String.Format("{0}{1}{2} + ", Coefficients[ii], VarName, ii);
} }
} }
else else
{ {
for (int ii = Coeffs.Length - 1; ii >= 0; ii--) for (int ii = Coefficients.Length - 1; ii >= 0; ii--)
{ {
if (ii == 0) if (ii == 0)
strLoc += this.Coeffs[ii].ToString(); result += Coefficients[ii].ToString();
else else
strLoc += String.Format("{0}{1}{2} + ", this.Coeffs[ii], VarName, ii); result += String.Format("{0}{1}{2} + ", Coefficients[ii], VarName, ii);
} }
} }
return strLoc; return result;
} }
#endregion
#region Interfacing
/// <summary> /// <summary>
/// This method returns the coefficcients of the Polynomial as an array the "IsFlipped" property, /// This method returns the coefficients of the Polynomial as an array the "IsFlipped" property,
/// which is set during construction is taken into account automatically. /// which is set during construction is taken into account automatically.
/// </summary> /// </summary>
/// <returns>the coefficcients of the Polynomial as an array</returns> /// <returns>the coefficients of the Polynomial as an array</returns>
public double[] ToArray() public double[] ToArray()
{ {
return (Coeffs.ToArray()); return Coefficients.ToArray();
} }
#endregion static void MakeSameLength(ref Polynomial a, ref Polynomial b)
#region Helpers
private static void mkSameLength(ref Polynomial a, ref Polynomial b)
{ {
double[] aHold = new double[a.Coeffs.Length]; double[] aHold = new double[a.Coefficients.Length];
double[] bHold = new double[b.Coeffs.Length]; double[] bHold = new double[b.Coefficients.Length];
Array.Copy(a.Coeffs, aHold, a.Coeffs.Length); Array.Copy(a.Coefficients, aHold, a.Coefficients.Length);
Array.Copy(b.Coeffs, bHold, b.Coeffs.Length); Array.Copy(b.Coefficients, bHold, b.Coefficients.Length);
if (a.Coeffs.Length < b.Coeffs.Length) if (a.Coefficients.Length < b.Coefficients.Length)
{ {
a.Coeffs = new double[b.Coeffs.Length]; a.Coefficients = new double[b.Coefficients.Length];
b.Coeffs = new double[b.Coeffs.Length]; b.Coefficients = new double[b.Coefficients.Length];
Array.Copy(aHold, a.Coeffs, aHold.Length); Array.Copy(aHold, a.Coefficients, aHold.Length);
Array.Copy(bHold, b.Coeffs, bHold.Length); Array.Copy(bHold, b.Coefficients, bHold.Length);
} }
else else
{ {
a.Coeffs = new double[a.Coeffs.Length]; a.Coefficients = new double[a.Coefficients.Length];
b.Coeffs = new double[a.Coeffs.Length]; b.Coefficients = new double[a.Coefficients.Length];
Array.Copy(aHold, a.Coeffs, aHold.Length); Array.Copy(aHold, a.Coefficients, aHold.Length);
Array.Copy(bHold, b.Coeffs, bHold.Length); Array.Copy(bHold, b.Coefficients, bHold.Length);
} }
} }
/// <summary> /// <summary>
/// (full) convolution of two arrays /// Full convolution of two arrays
/// </summary> /// </summary>
/// <param name="a">left vector</param>
/// <param name="b">right vector</param>
/// <returns>convolution of a and b as vector</returns> /// <returns>convolution of a and b as vector</returns>
private static double[] conv(double[] a, double[] b) static double[] Convolution(double[] a, double[] b)
{ {
double[] ret = new double[a.Length + b.Length]; double[] ret = new double[a.Length + b.Length];
@ -707,10 +687,8 @@ namespace MathNet.Numerics
public object Clone() public object Clone()
{ {
return new Polynomial(this.Coeffs); // TODO: this assumes the constructor does a copy
return new Polynomial(Coefficients);
} }
#endregion
} }
} }

Loading…
Cancel
Save