diff --git a/src/Numerics.Tests/PolynomialTests.cs b/src/Numerics.Tests/PolynomialTests.cs index 8b82dcec..57874e5e 100644 --- a/src/Numerics.Tests/PolynomialTests.cs +++ b/src/Numerics.Tests/PolynomialTests.cs @@ -444,7 +444,7 @@ namespace MathNet.Numerics.UnitTests } // static Complex Evaluate(Complex, double[]) - { + { var actual = Polynomial.Evaluate(zComplex, c); Assert.AreEqual(expectedComplex, actual); } @@ -466,8 +466,21 @@ namespace MathNet.Numerics.UnitTests var actual = p.Evaluate(zComplex); Assert.AreEqual(expectedComplex, actual); } - + + } + +#if NET5_0_OR_GREATER + [Test] + public void JsonDeserializationTest() + { + var polynomial = new Polynomial(0, 1, 2); + var json = System.Text.Json.JsonSerializer.Serialize(polynomial); + var deserialize = System.Text.Json.JsonSerializer.Deserialize(json); + Assert.NotNull(deserialize); + Assert.AreEqual(polynomial.Coefficients.Length, deserialize.Coefficients.Length); + Assert.IsTrue(polynomial.Coefficients.SequenceEqual(deserialize.Coefficients)); } +#endif } diff --git a/src/Numerics.Tests/StatisticsTests/DescriptiveStatisticsTests.cs b/src/Numerics.Tests/StatisticsTests/DescriptiveStatisticsTests.cs index 1d9b728f..b3e2492d 100644 --- a/src/Numerics.Tests/StatisticsTests/DescriptiveStatisticsTests.cs +++ b/src/Numerics.Tests/StatisticsTests/DescriptiveStatisticsTests.cs @@ -29,6 +29,10 @@ using System; using System.Collections.Generic; +#if NET5_0_OR_GREATER +using System.Text.Json; +using System.Text.Json.Serialization; +#endif using NUnit.Framework; using MathNet.Numerics.Statistics; @@ -301,5 +305,47 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests Assert.That(stats.Skewness, Is.NaN); Assert.That(stats.Kurtosis, Is.NaN); } + +#if NET5_0_OR_GREATER + /// + /// IEnumerable Double. + /// + /// Dataset name. + /// Digits count. + /// Skewness value. + /// Kurtosis value. + /// Median value. + /// Min value. + /// Max value. + /// Count value. + [TestCase("lottery", 12, -0.09333165310779, -1.19256091074856, 522.5, 4, 999, 218)] + [TestCase("lew", 12, -0.050606638756334, -1.49604979214447, -162, -579, 300, 200)] + [TestCase("mavro", 11, 0.64492948110824, -0.82052379677456, 2.0018, 2.0013, 2.0027, 50)] + [TestCase("michelso", 11, -0.0185388637725746, 0.33968459842539, 299.85, 299.62, 300.07, 100)] + [TestCase("numacc1", 15, 0, double.NaN, 10000002, 10000001, 10000003, 3)] + [TestCase("numacc2", 13, 0, -2.003003003003, 1.2, 1.1, 1.3, 1001)] + [TestCase("numacc3", 9, 0, -2.003003003003, 1000000.2, 1000000.1, 1000000.3, 1001)] + [TestCase("numacc4", 7, 0, -2.00300300299913, 10000000.2, 10000000.1, 10000000.3, 1001)] + [TestCase("meixner", 8, -0.016649617280859657, 0.8171318629552635, -0.002042931016531602, -4.825626912281697, 5.3018298664184913, 10000)] + public void JsonDeserializationTest(string dataSet, int digits, double skewness, double kurtosis, double median, double min, double max, int count) + { + var data = _data[dataSet]; + var serialize = new DescriptiveStatistics(data.Data, false); + var jsonSerializerOptions = new JsonSerializerOptions + { + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, + }; + var json = JsonSerializer.Serialize(serialize, jsonSerializerOptions); + var stats = JsonSerializer.Deserialize(json, jsonSerializerOptions); + Assert.NotNull(stats); + AssertHelpers.AlmostEqualRelative(data.Mean, stats.Mean, 10); + AssertHelpers.AlmostEqualRelative(data.StandardDeviation, stats.StandardDeviation, digits); + AssertHelpers.AlmostEqualRelative(skewness, stats.Skewness, 8); + AssertHelpers.AlmostEqualRelative(kurtosis, stats.Kurtosis, 8); + Assert.AreEqual(stats.Minimum, min); + Assert.AreEqual(stats.Maximum, max); + Assert.AreEqual(stats.Count, count); + } +#endif } } diff --git a/src/Numerics/Polynomial.cs b/src/Numerics/Polynomial.cs index 44bf3280..54a1d7fb 100644 --- a/src/Numerics/Polynomial.cs +++ b/src/Numerics/Polynomial.cs @@ -5,6 +5,9 @@ using System.Runtime.Serialization; using System.Linq; using Complex = System.Numerics.Complex; using System.Text; +#if NET5_0_OR_GREATER +using System.Text.Json.Serialization; +#endif using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearRegression; @@ -24,6 +27,9 @@ namespace MathNet.Numerics /// The coefficients of the polynomial in a /// [DataMember(Order = 1)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double[] Coefficients { get; private set; } /// diff --git a/src/Numerics/Statistics/DescriptiveStatistics.cs b/src/Numerics/Statistics/DescriptiveStatistics.cs index e1991f5c..e6452f0d 100644 --- a/src/Numerics/Statistics/DescriptiveStatistics.cs +++ b/src/Numerics/Statistics/DescriptiveStatistics.cs @@ -30,6 +30,9 @@ using System; using System.Collections.Generic; using System.Runtime.Serialization; +#if NET5_0_OR_GREATER +using System.Text.Json.Serialization; +#endif namespace MathNet.Numerics.Statistics { @@ -107,11 +110,26 @@ namespace MathNet.Numerics.Statistics } } +#if NET5_0_OR_GREATER + /// + /// Initializes a new instance of the class. + /// + /// + /// Used for Json serialization + /// + public DescriptiveStatistics() + { + } +#endif + /// /// Gets the size of the sample. /// /// The size of the sample. [DataMember(Order = 1)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public long Count { get; private set; } /// @@ -119,6 +137,9 @@ namespace MathNet.Numerics.Statistics /// /// The sample mean. [DataMember(Order = 2)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double Mean { get; private set; } /// @@ -126,6 +147,9 @@ namespace MathNet.Numerics.Statistics /// /// The sample variance. [DataMember(Order = 3)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double Variance { get; private set; } /// @@ -133,6 +157,9 @@ namespace MathNet.Numerics.Statistics /// /// The sample standard deviation. [DataMember(Order = 4)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double StandardDeviation { get; private set; } /// @@ -141,6 +168,9 @@ namespace MathNet.Numerics.Statistics /// The sample skewness. /// Returns zero if is less than three. [DataMember(Order = 5)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double Skewness { get; private set; } /// @@ -149,6 +179,9 @@ namespace MathNet.Numerics.Statistics /// The sample kurtosis. /// Returns zero if is less than four. [DataMember(Order = 6)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double Kurtosis { get; private set; } /// @@ -156,6 +189,9 @@ namespace MathNet.Numerics.Statistics /// /// The maximum sample value. [DataMember(Order = 7)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double Maximum { get; private set; } /// @@ -163,6 +199,9 @@ namespace MathNet.Numerics.Statistics /// /// The minimum sample value. [DataMember(Order = 8)] +#if NET5_0_OR_GREATER + [JsonInclude] +#endif public double Minimum { get; private set; } ///