Browse Source

changed somne comments.

improved the Trim implementation
added a UnitTest for Trim
ridge-regression
Tobias Glaubach 8 years ago
parent
commit
e0f2378c22
  1. 13
      src/Numerics.Tests/PolynomialTests.cs
  2. 29
      src/Numerics/Polynomial.cs

13
src/Numerics.Tests/PolynomialTests.cs

@ -210,6 +210,19 @@ namespace MathNet.Numerics.UnitTests
} }
[TestCase(new double[] { 5, 4, 0 }, "5 + 4x^1")]
[TestCase(new double[] { 0, 0, 0 }, "0")]
[TestCase(new double[] { 5, 4, 3, 0, 2 }, "5 + 4x^1 + 3x^2 + 0x^3 + 2x^4")]
[TestCase(new double[] { 0, 0, 8, 0, 0 }, "0 + 0x^1 + 8x^2")]
[TestCase(new double[] { 0, 4, 3, 0, 0 }, "0 + 4x^1 + 3x^2")]
public void TrimTest(double[] x, string expected)
{
var p = new Polynomial(x);
p.Trim();
Assert.AreEqual(expected, p.ToString());
}
public void DivideLongTestScalar(Tuple<double[], double> inVals, Tuple<double[], double> expectedVals) public void DivideLongTestScalar(Tuple<double[], double> inVals, Tuple<double[], double> expectedVals)
{ {
var p1 = new Polynomial(1.0d); var p1 = new Polynomial(1.0d);

29
src/Numerics/Polynomial.cs

@ -93,8 +93,8 @@ namespace MathNet.Numerics
/// var xP1 = new Polynomial(x, isFlip:true); /// var xP1 = new Polynomial(x, isFlip:true);
/// var xP2 = new Polynomial(x, isFlip:false); /// var xP2 = new Polynomial(x, isFlip:false);
/// ///
/// xP1: 5 x^3 + 4 x^2 + 3 x^2 + 0 x^1 + 2 /// xP1: 2 + 0x^1 + 3x^2 + 4x^3 + 5x^4
/// xP2: 2 x^3 + 0 x^2 + 3 x^2 + 4 x^1 + 5 /// xP2: 5 + 4x^1 + 3x^2 + 0x^3 + 2x^4
/// ///
/// </summary> /// </summary>
/// <param name="coeffs"> Polynomial coefficiens as array</param> /// <param name="coeffs"> Polynomial coefficiens as array</param>
@ -120,22 +120,17 @@ namespace MathNet.Numerics
/// </summary> /// </summary>
public void Trim() public void Trim()
{ {
int count = 0; int i = Degree - 1;
for (int ii = Degree - 1; ii >= 0; ii--) while (i >= 0 && Coeffs[i] == 0.0)
i--;
if (i < 0)
Coeffs = new double[0];
else
{ {
if (Coeffs[ii] == 0.0) var hold = new double[i+1];
{ Array.Copy(Coeffs, hold, i+1);
count++; Coeffs = hold;
}
else
{
double[] CoeffsHold = new double[Coeffs.Length];
Coeffs.CopyTo(CoeffsHold, 0);
Array.Resize(ref CoeffsHold, Degree - count);
Coeffs = new double[Degree - count];
CoeffsHold.CopyTo(Coeffs, 0);
return;
}
} }
} }

Loading…
Cancel
Save