Browse Source

Polynomial: rename Degree to CoefficientCount and add new Degree that matches the mathematical definition

ridge-regression
Christoph Ruegg 8 years ago
parent
commit
8bfb3535ef
  1. 44
      src/Numerics.Tests/PolynomialTests.cs
  2. 52
      src/Numerics/Polynomial.cs

44
src/Numerics.Tests/PolynomialTests.cs

@ -29,12 +29,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using MathNet.Numerics;
using MathNet.Numerics.LinearRegression;
using MathNet.Numerics.Statistics;
using NUnit.Framework; using NUnit.Framework;
namespace MathNet.Numerics.UnitTests namespace MathNet.Numerics.UnitTests
@ -46,7 +42,6 @@ namespace MathNet.Numerics.UnitTests
[TestFixture, Category("Calculus")] [TestFixture, Category("Calculus")]
public class PolynomialTests public class PolynomialTests
{ {
[TestCase(new double[] { 5, 4, 3, 0, 2 }, "5 + 4x^1 + 3x^2 + 0x^3 + 2x^4")] [TestCase(new double[] { 5, 4, 3, 0, 2 }, "5 + 4x^1 + 3x^2 + 0x^3 + 2x^4")]
[TestCase(new double[0], "")] [TestCase(new double[0], "")]
[TestCase(new double[] { 0, 4, 3, 0, 0 }, "0 + 4x^1 + 3x^2 + 0x^3 + 0x^4")] [TestCase(new double[] { 0, 4, 3, 0, 0 }, "0 + 4x^1 + 3x^2 + 0x^3 + 0x^4")]
@ -71,13 +66,12 @@ namespace MathNet.Numerics.UnitTests
} }
else else
{ {
Assert.AreEqual(expected.Length, p_res.Degree, "length mismatch"); Assert.AreEqual(expected.Length, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], "idx: " + k + " mismatch");
} }
} }
} }
[TestCase(new double[] { 5, 4, 3, 0, 2 }, new double[] { 0, 5.0/1.0, 4.0/2.0, 3.0/3.0, 0.0/4.0, 2.0/5.0 })] [TestCase(new double[] { 5, 4, 3, 0, 2 }, new double[] { 0, 5.0/1.0, 4.0/2.0, 3.0/3.0, 0.0/4.0, 2.0/5.0 })]
@ -95,8 +89,8 @@ namespace MathNet.Numerics.UnitTests
} }
else else
{ {
Assert.AreEqual(expected.Length, p_res.Degree, "length mismatch"); Assert.AreEqual(expected.Length, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], "idx: " + k + " mismatch");
} }
@ -131,8 +125,8 @@ namespace MathNet.Numerics.UnitTests
p_res.Trim(); p_res.Trim();
p_tar.Trim(); p_tar.Trim();
Assert.AreEqual(p_tar.Degree, p_res.Degree, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], p_res.Coeffs[k], msg);
} }
@ -168,8 +162,8 @@ namespace MathNet.Numerics.UnitTests
p_res.Trim(); p_res.Trim();
p_tar.Trim(); p_tar.Trim();
Assert.AreEqual(p_tar.Degree, p_res.Degree, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], p_res.Coeffs[k], msg);
} }
@ -204,8 +198,8 @@ namespace MathNet.Numerics.UnitTests
p_res.Trim(); p_res.Trim();
p_tar.Trim(); p_tar.Trim();
Assert.AreEqual(p_tar.Degree, p_res.Degree, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], p_res.Coeffs[k], msg);
} }
@ -227,15 +221,6 @@ namespace MathNet.Numerics.UnitTests
} }
public void DivideLongTestScalar(Tuple<double[], double> inVals, Tuple<double[], double> expectedVals)
{
var p1 = new Polynomial(1.0d);
var p2 = new Polynomial(new double[0]);
var tpl = Polynomial.DivideLong(p1, p2);
}
[Test] [Test]
public void DivideLongTestWrongInputs() public void DivideLongTestWrongInputs()
{ {
@ -268,7 +253,6 @@ namespace MathNet.Numerics.UnitTests
[Test] [Test]
public void DivideLongTest() public void DivideLongTest()
{ {
var p11 = new Polynomial(2.0d); var p11 = new Polynomial(2.0d);
var p21 = new Polynomial(2.0d); var p21 = new Polynomial(2.0d);
var tpl1 = Polynomial.DivideLong(p11, p21); var tpl1 = Polynomial.DivideLong(p11, p21);
@ -375,8 +359,8 @@ namespace MathNet.Numerics.UnitTests
private void testEqual(double[] p_tar, Polynomial p_res, string msg = null) private void testEqual(double[] p_tar, Polynomial p_res, string msg = null)
{ {
Assert.AreEqual(p_tar.Length, p_res.Degree, "length mismatch"); Assert.AreEqual(p_tar.Length, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], msg);
} }
@ -384,8 +368,8 @@ namespace MathNet.Numerics.UnitTests
} }
private void testEqual(Polynomial p_tar, Polynomial p_res, string msg = null) private void testEqual(Polynomial p_tar, Polynomial p_res, string msg = null)
{ {
Assert.AreEqual(p_tar.Degree, p_res.Degree, "length mismatch"); Assert.AreEqual(p_tar.CoefficientCount, p_res.CoefficientCount, "length mismatch");
for (int k = 0; k < p_res.Degree; 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.Coeffs[k], p_res.Coeffs[k], msg);
} }

52
src/Numerics/Polynomial.cs

@ -1,21 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics; using System.Numerics;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Statistics;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.LinearRegression; using MathNet.Numerics.LinearRegression;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
namespace MathNet.Numerics namespace MathNet.Numerics
{ {
/// <summary> /// <summary>
/// a class handling REAL VALUED Polynomials, complex coefficients can not be handled (yet) /// A single-variable polynomial with real-valued coefficients.
/// </summary> /// </summary>
public class Polynomial public class Polynomial
{ {
@ -32,7 +27,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Length of Polynomial (max element + 1) e.G x^5 highest element, will give Length = 6 /// Length of Polynomial (max element + 1) e.G x^5 highest element, will give Length = 6
/// </summary> /// </summary>
public int Degree public int CoefficientCount
{ {
get get
{ {
@ -40,6 +35,31 @@ namespace MathNet.Numerics
} }
} }
/// <summary>
/// Degree of the polynomial, i.e. the largest monomial exponent. For example, the degree of x^2+x^5 is 5.
/// The null-polynomial returns degree -1 because the correct degree, negative infinity, cannot be represented by integers.
/// </summary>
public int Degree
{
get
{
if (Coeffs == null)
{
return -1;
}
for (int i = Coeffs.Length - 1; i >= 0; i--)
{
if (Coeffs[i] != 0.0)
{
return i;
}
}
return -1;
}
}
/// <summary> /// <summary>
/// constructor setting a Polynomial of size n containing only zeros /// constructor setting a Polynomial of size n containing only zeros
/// </summary> /// </summary>
@ -96,10 +116,10 @@ namespace MathNet.Numerics
/// </summary> /// </summary>
public void Trim() public void Trim()
{ {
if (Degree == 1) if (CoefficientCount == 1)
return; return;
int i = Degree - 1; int i = CoefficientCount - 1;
while (i >= 0 && Coeffs[i] == 0.0) while (i >= 0 && Coeffs[i] == 0.0)
i--; i--;
@ -418,10 +438,10 @@ namespace MathNet.Numerics
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.Degree != bb.Degree) if (aa.CoefficientCount != bb.CoefficientCount)
mkSameLength(ref aa, ref bb); mkSameLength(ref aa, ref bb);
int n = aa.Degree; int n = aa.CoefficientCount;
double[] res = new double[n]; double[] res = new double[n];
@ -444,10 +464,10 @@ namespace MathNet.Numerics
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.Degree != bb.Degree) if (aa.CoefficientCount != bb.CoefficientCount)
mkSameLength(ref aa, ref bb); mkSameLength(ref aa, ref bb);
int n = aa.Degree; int n = aa.CoefficientCount;
double[] res = new double[n]; double[] res = new double[n];
@ -472,12 +492,12 @@ namespace MathNet.Numerics
if (b == null) if (b == null)
throw new ArgumentNullException("b"); throw new ArgumentNullException("b");
if (a.Degree <= 0) if (a.CoefficientCount <= 0)
throw new ArgumentOutOfRangeException("a Degree must be greater than zero"); throw new ArgumentOutOfRangeException("a Degree must be greater than zero");
if (b.Degree <= 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.Degree-1] == 0) if (b.Coeffs[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.Coeffs.ToArray();

Loading…
Cancel
Save