diff --git a/src/Numerics.Tests/PolynomialTests.cs b/src/Numerics.Tests/PolynomialTests.cs index 323b2396..dfaa3797 100644 --- a/src/Numerics.Tests/PolynomialTests.cs +++ b/src/Numerics.Tests/PolynomialTests.cs @@ -29,8 +29,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Numerics; +using System.Runtime.Serialization; using NUnit.Framework; namespace MathNet.Numerics.UnitTests @@ -318,6 +320,25 @@ namespace MathNet.Numerics.UnitTests TestEqual(x_4, expected_4); } + [Test] + public void DataContractSerializationTest() + { + Polynomial expected = new Polynomial(new [] { 1.0d, 2.0d, 0.0d, 3.0d }); + expected.VariableName = "z"; + + var serializer = new DataContractSerializer(typeof(Polynomial)); + var stream = new MemoryStream(); + serializer.WriteObject(stream, expected); + stream.Position = 0; + + var actual = (Polynomial)serializer.ReadObject(stream); + + Assert.That(actual.Degree, Is.EqualTo(expected.Degree)); + Assert.That(actual.VariableName, Is.EqualTo(expected.VariableName)); + Assert.That(actual, Is.EqualTo(expected)); + Assert.That(actual, Is.Not.SameAs(expected)); + } + static void TestEqual(double[] x, List eIn) { var tol = 1e-10; diff --git a/src/Numerics/Polynomial.cs b/src/Numerics/Polynomial.cs index 8e4981d7..a7d3c054 100644 --- a/src/Numerics/Polynomial.cs +++ b/src/Numerics/Polynomial.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; using System.Linq; using System.Numerics; using System.Text; @@ -9,22 +11,30 @@ using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearRegression; using MathNet.Numerics.LinearAlgebra.Factorization; +#if !NETSTANDARD1_3 +using System.Runtime; +#endif + namespace MathNet.Numerics { /// /// A single-variable polynomial with real-valued coefficients and non-negative exponents. /// + [Serializable] + [DataContract(Namespace = "urn:MathNet/Numerics")] public class Polynomial : IFormattable, IEquatable { /// /// The coefficients of the polynomial in a /// + [DataMember(Order = 1)] public double[] Coefficients { get; private set; } /// /// Only needed for the ToString method /// - public string VarName = "x"; + [DataMember(Order = 2)] + public string VariableName = "x"; /// /// Degree of the polynomial, i.e. the largest monomial exponent. For example, the degree of y=x^2+x^5 is 5, for y=3 it is 0. @@ -813,7 +823,7 @@ namespace MathNet.Numerics sb.Append(c.ToString(format, formatProvider)); if (i > 0) { - sb.Append(VarName); + sb.Append(VariableName); } if (i > 1) { @@ -837,7 +847,7 @@ namespace MathNet.Numerics } if (i > 0) { - sb.Append(VarName); + sb.Append(VariableName); } if (i > 1) { @@ -875,7 +885,7 @@ namespace MathNet.Numerics sb.Append(c.ToString(format, formatProvider)); if (i > 0) { - sb.Append(VarName); + sb.Append(VariableName); } if (i > 1) { @@ -899,7 +909,7 @@ namespace MathNet.Numerics } if (i > 0) { - sb.Append(VarName); + sb.Append(VariableName); } if (i > 1) {