diff --git a/src/Numerics.Tests/PolynomialTests.cs b/src/Numerics.Tests/PolynomialTests.cs index 0c696f66..323b2396 100644 --- a/src/Numerics.Tests/PolynomialTests.cs +++ b/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")] - public void ToStringTestReverse(double[] x, string expected) + public void ToStringTestDescending(double[] x, string expected) { 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 })] diff --git a/src/Numerics/Polynomial.cs b/src/Numerics/Polynomial.cs index d3f0952b..e846603d 100644 --- a/src/Numerics/Polynomial.cs +++ b/src/Numerics/Polynomial.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Numerics; using System.Text; @@ -13,7 +14,7 @@ namespace MathNet.Numerics /// /// A single-variable polynomial with real-valued coefficients and non-negative exponents. /// - public class Polynomial + public class Polynomial : IFormattable { /// /// The coefficients of the polynomial in a @@ -97,38 +98,6 @@ namespace MathNet.Numerics return -1; } - /// - /// remove all trailing zeros, e.G before: "3 + 2 x^1 + 0 x^2" after: "3 + 2 x^1" - /// - 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; - } - } - /// /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k /// @@ -773,19 +742,56 @@ namespace MathNet.Numerics #region ToString /// - /// "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". /// - /// string in displayed format public override string ToString() { - return ToString(highestFirst: false); + return ToString("G", CultureInfo.CurrentCulture); + } + + /// + /// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3". + /// + public string ToStringDescending() + { + return ToStringDescending("G", CultureInfo.CurrentCulture); + } + + /// + /// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3". + /// + public string ToString(string format) + { + return ToString(format, CultureInfo.CurrentCulture); + } + + /// + /// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3". + /// + public string ToStringDescending(string format) + { + return ToStringDescending(format, CultureInfo.CurrentCulture); + } + /// + /// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3". + /// + public string ToString(IFormatProvider formatProvider) + { + return ToString("G", formatProvider); } /// - /// "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". /// - /// string in displayed format - public string ToString(bool highestFirst) + public string ToStringDescending(IFormatProvider formatProvider) + { + return ToStringDescending("G", formatProvider); + } + + /// + /// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3". + /// + public string ToString(string format, IFormatProvider formatProvider) { if (Degree < 0) { @@ -794,101 +800,111 @@ namespace MathNet.Numerics var sb = new StringBuilder(); 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]; - if (c == 0.0) + sb.Append(c.ToString(format, formatProvider)); + 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); - if (i > 0) - { - sb.Append(VarName); - } - if (i > 1) - { - sb.Append("^"); - sb.Append(i); - } - - first = false; + sb.Append(" - "); + sb.Append((-c).ToString(format, formatProvider)); } else { - if (c < 0.0) - { - sb.Append(" - "); - sb.Append(-c); - } - else - { - sb.Append(" + "); - sb.Append(c); - } - if (i > 0) - { - sb.Append(VarName); - } - if (i > 1) - { - sb.Append("^"); - sb.Append(i); - } + sb.Append(" + "); + sb.Append(c.ToString(format, formatProvider)); + } + if (i > 0) + { + sb.Append(VarName); + } + if (i > 1) + { + sb.Append("^"); + sb.Append(i); } } } - else + + return sb.ToString(); + } + + /// + /// Format the polynomial in descending order, e.g. "x^3 + 2.0x^2 - 4.3". + /// + 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]; - if (c == 0.0) + continue; + } + + 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); - if (i > 0) - { - sb.Append(VarName); - } - if (i > 1) - { - sb.Append("^"); - sb.Append(i); - } - - first = false; + sb.Append(" - "); + sb.Append((-c).ToString(format, formatProvider)); } else { - if (c < 0.0) - { - sb.Append(" - "); - sb.Append(-c); - } - else - { - sb.Append(" + "); - sb.Append(c); - } - if (i > 0) - { - sb.Append(VarName); - } - if (i > 1) - { - sb.Append("^"); - sb.Append(i); - } + sb.Append(" + "); + sb.Append(c.ToString(format, formatProvider)); + } + if (i > 0) + { + sb.Append(VarName); + } + if (i > 1) + { + sb.Append("^"); + sb.Append(i); } } }