Browse Source

add JsonInclude for Polynomial and DescriptiveStatistics

pull/772/head
Joseph Petersen 5 years ago
parent
commit
382a2acd5d
No known key found for this signature in database GPG Key ID: 5756914983DA9E9
  1. 17
      src/Numerics.Tests/PolynomialTests.cs
  2. 46
      src/Numerics.Tests/StatisticsTests/DescriptiveStatisticsTests.cs
  3. 6
      src/Numerics/Polynomial.cs
  4. 39
      src/Numerics/Statistics/DescriptiveStatistics.cs

17
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<Polynomial>(json);
Assert.NotNull(deserialize);
Assert.AreEqual(polynomial.Coefficients.Length, deserialize.Coefficients.Length);
Assert.IsTrue(polynomial.Coefficients.SequenceEqual(deserialize.Coefficients));
}
#endif
}

46
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
/// <summary>
/// <c>IEnumerable</c> Double.
/// </summary>
/// <param name="dataSet">Dataset name.</param>
/// <param name="digits">Digits count.</param>
/// <param name="skewness">Skewness value.</param>
/// <param name="kurtosis">Kurtosis value.</param>
/// <param name="median">Median value.</param>
/// <param name="min">Min value.</param>
/// <param name="max">Max value.</param>
/// <param name="count">Count value.</param>
[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<DescriptiveStatistics>(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
}
}

6
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
/// </summary>
[DataMember(Order = 1)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double[] Coefficients { get; private set; }
/// <summary>

39
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
/// <summary>
/// Initializes a new instance of the <see cref="DescriptiveStatistics"/> class.
/// </summary>
/// <remarks>
/// Used for Json serialization
/// </remarks>
public DescriptiveStatistics()
{
}
#endif
/// <summary>
/// Gets the size of the sample.
/// </summary>
/// <value>The size of the sample.</value>
[DataMember(Order = 1)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public long Count { get; private set; }
/// <summary>
@ -119,6 +137,9 @@ namespace MathNet.Numerics.Statistics
/// </summary>
/// <value>The sample mean.</value>
[DataMember(Order = 2)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double Mean { get; private set; }
/// <summary>
@ -126,6 +147,9 @@ namespace MathNet.Numerics.Statistics
/// </summary>
/// <value>The sample variance.</value>
[DataMember(Order = 3)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double Variance { get; private set; }
/// <summary>
@ -133,6 +157,9 @@ namespace MathNet.Numerics.Statistics
/// </summary>
/// <value>The sample standard deviation.</value>
[DataMember(Order = 4)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double StandardDeviation { get; private set; }
/// <summary>
@ -141,6 +168,9 @@ namespace MathNet.Numerics.Statistics
/// <value>The sample skewness.</value>
/// <remarks>Returns zero if <see cref="Count"/> is less than three. </remarks>
[DataMember(Order = 5)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double Skewness { get; private set; }
/// <summary>
@ -149,6 +179,9 @@ namespace MathNet.Numerics.Statistics
/// <value>The sample kurtosis.</value>
/// <remarks>Returns zero if <see cref="Count"/> is less than four. </remarks>
[DataMember(Order = 6)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double Kurtosis { get; private set; }
/// <summary>
@ -156,6 +189,9 @@ namespace MathNet.Numerics.Statistics
/// </summary>
/// <value>The maximum sample value.</value>
[DataMember(Order = 7)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double Maximum { get; private set; }
/// <summary>
@ -163,6 +199,9 @@ namespace MathNet.Numerics.Statistics
/// </summary>
/// <value>The minimum sample value.</value>
[DataMember(Order = 8)]
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public double Minimum { get; private set; }
/// <summary>

Loading…
Cancel
Save