Browse Source

Polynomial: IFormattable

ridge-regression
Christoph Ruegg 8 years ago
parent
commit
a1d234a97e
  1. 4
      src/Numerics.Tests/PolynomialTests.cs
  2. 242
      src/Numerics/Polynomial.cs

4
src/Numerics.Tests/PolynomialTests.cs

@ -60,10 +60,10 @@ namespace MathNet.Numerics.UnitTests
[TestCase(new double[] { 0, -4, 3 }, "3x^2 - 4x")] [TestCase(new double[] { 0, -4, 3 }, "3x^2 - 4x")]
[TestCase(new double[] { 0, -4, -3 }, "-3x^2 - 4x")] [TestCase(new double[] { 0, -4, -3 }, "-3x^2 - 4x")]
[TestCase(new double[] { 0, 4, -3 }, "-3x^2 + 4x")] [TestCase(new double[] { 0, 4, -3 }, "-3x^2 + 4x")]
public void ToStringTestReverse(double[] x, string expected) public void ToStringTestDescending(double[] x, string expected)
{ {
var p = new Polynomial(x); var p = new Polynomial(x);
Assert.AreEqual(expected, p.ToString(highestFirst:true)); Assert.AreEqual(expected, p.ToStringDescending());
} }
[TestCase(new double[] { 5, 4, 3, 0, 2 }, new double[] { 4*1, 3*2, 0*3, 2*4 })] [TestCase(new double[] { 5, 4, 3, 0, 2 }, new double[] { 4*1, 3*2, 0*3, 2*4 })]

242
src/Numerics/Polynomial.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Text; using System.Text;
@ -13,7 +14,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// A single-variable polynomial with real-valued coefficients and non-negative exponents. /// A single-variable polynomial with real-valued coefficients and non-negative exponents.
/// </summary> /// </summary>
public class Polynomial public class Polynomial : IFormattable
{ {
/// <summary> /// <summary>
/// The coefficients of the polynomial in a /// The coefficients of the polynomial in a
@ -97,38 +98,6 @@ namespace MathNet.Numerics
return -1; return -1;
} }
/// <summary>
/// remove all trailing zeros, e.G before: "3 + 2 x^1 + 0 x^2" after: "3 + 2 x^1"
/// </summary>
public void Trim()
{
if (Coefficients.Length == 1)
{
return;
}
int i = Coefficients.Length - 1;
while (i >= 0 && Coefficients[i] == 0.0)
{
i--;
}
if (i < 0)
{
Coefficients = new[] { 0.0 };
}
else if (i == 0)
{
Coefficients = new[] { Coefficients[0] };
}
else
{
var hold = new double[i+1];
Array.Copy(Coefficients, hold, i+1);
Coefficients = hold;
}
}
/// <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>
@ -773,19 +742,56 @@ namespace MathNet.Numerics
#region ToString #region ToString
/// <summary> /// <summary>
/// "0.00 x^3 + 0.00 x^2 + 0.00 x^1 + 0.00" like display of this Polynomial /// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3".
/// </summary> /// </summary>
/// <returns>string in displayed format</returns>
public override string ToString() public override string ToString()
{ {
return ToString(highestFirst: false); return ToString("G", CultureInfo.CurrentCulture);
}
/// <summary>
/// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3".
/// </summary>
public string ToStringDescending()
{
return ToStringDescending("G", CultureInfo.CurrentCulture);
}
/// <summary>
/// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3".
/// </summary>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3".
/// </summary>
public string ToStringDescending(string format)
{
return ToStringDescending(format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3".
/// </summary>
public string ToString(IFormatProvider formatProvider)
{
return ToString("G", formatProvider);
} }
/// <summary> /// <summary>
/// "0.00 x^3 + 0.00 x^2 + 0.00 x^1 + 0.00" like display of this Polynomial /// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3".
/// </summary> /// </summary>
/// <returns>string in displayed format</returns> public string ToStringDescending(IFormatProvider formatProvider)
public string ToString(bool highestFirst) {
return ToStringDescending("G", formatProvider);
}
/// <summary>
/// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3".
/// </summary>
public string ToString(string format, IFormatProvider formatProvider)
{ {
if (Degree < 0) if (Degree < 0)
{ {
@ -794,101 +800,111 @@ namespace MathNet.Numerics
var sb = new StringBuilder(); var sb = new StringBuilder();
bool first = true; bool first = true;
if (highestFirst) for (int i = 0; i < Coefficients.Length; i++)
{ {
for (int i = Coefficients.Length - 1; i >= 0; i--) double c = Coefficients[i];
if (c == 0.0)
{
continue;
}
if (first)
{ {
double c = Coefficients[i]; sb.Append(c.ToString(format, formatProvider));
if (c == 0.0) if (i > 0)
{ {
continue; sb.Append(VarName);
}
if (i > 1)
{
sb.Append("^");
sb.Append(i);
} }
if (first) first = false;
}
else
{
if (c < 0.0)
{ {
sb.Append(c); sb.Append(" - ");
if (i > 0) sb.Append((-c).ToString(format, formatProvider));
{
sb.Append(VarName);
}
if (i > 1)
{
sb.Append("^");
sb.Append(i);
}
first = false;
} }
else else
{ {
if (c < 0.0) sb.Append(" + ");
{ sb.Append(c.ToString(format, formatProvider));
sb.Append(" - "); }
sb.Append(-c); if (i > 0)
} {
else sb.Append(VarName);
{ }
sb.Append(" + "); if (i > 1)
sb.Append(c); {
} sb.Append("^");
if (i > 0) sb.Append(i);
{
sb.Append(VarName);
}
if (i > 1)
{
sb.Append("^");
sb.Append(i);
}
} }
} }
} }
else
return sb.ToString();
}
/// <summary>
/// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3".
/// </summary>
public string ToStringDescending(string format, IFormatProvider formatProvider)
{
if (Degree < 0)
{
return "0";
}
var sb = new StringBuilder();
bool first = true;
for (int i = Coefficients.Length - 1; i >= 0; i--)
{ {
for (int i = 0; i < Coefficients.Length; i++) double c = Coefficients[i];
if (c == 0.0)
{ {
double c = Coefficients[i]; continue;
if (c == 0.0) }
if (first)
{
sb.Append(c.ToString(format, formatProvider));
if (i > 0)
{
sb.Append(VarName);
}
if (i > 1)
{ {
continue; sb.Append("^");
sb.Append(i);
} }
if (first) first = false;
}
else
{
if (c < 0.0)
{ {
sb.Append(c); sb.Append(" - ");
if (i > 0) sb.Append((-c).ToString(format, formatProvider));
{
sb.Append(VarName);
}
if (i > 1)
{
sb.Append("^");
sb.Append(i);
}
first = false;
} }
else else
{ {
if (c < 0.0) sb.Append(" + ");
{ sb.Append(c.ToString(format, formatProvider));
sb.Append(" - "); }
sb.Append(-c); if (i > 0)
} {
else sb.Append(VarName);
{ }
sb.Append(" + "); if (i > 1)
sb.Append(c); {
} sb.Append("^");
if (i > 0) sb.Append(i);
{
sb.Append(VarName);
}
if (i > 1)
{
sb.Append("^");
sb.Append(i);
}
} }
} }
} }

Loading…
Cancel
Save