Browse Source

Merge branch 'serialization'

netstandard
Christoph Ruegg 11 years ago
parent
commit
0fbbc4b3eb
  1. 1
      src/Numerics/Numerics-Net35.csproj
  2. 1
      src/Numerics/Numerics.csproj
  3. 20
      src/Numerics/Statistics/DescriptiveStatistics.cs
  4. 24
      src/Numerics/Statistics/Histogram.cs
  5. 27
      src/Numerics/Statistics/RunningStatistics.cs
  6. 81
      src/UnitTests/StatisticsTests/StatisticsSerializationTests.cs
  7. 2
      src/UnitTests/UnitTests-Net35.csproj
  8. 2
      src/UnitTests/UnitTests-Portable259.csproj
  9. 2
      src/UnitTests/UnitTests-Portable328.csproj
  10. 2
      src/UnitTests/UnitTests-Portable47.csproj
  11. 2
      src/UnitTests/UnitTests-Portable7.csproj
  12. 2
      src/UnitTests/UnitTests-Portable78.csproj
  13. 3
      src/UnitTests/UnitTests.csproj

1
src/Numerics/Numerics-Net35.csproj

@ -80,6 +80,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx"> <EmbeddedResource Include="Properties\Resources.resx">

1
src/Numerics/Numerics.csproj

@ -81,6 +81,7 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Differentiate.cs" /> <Compile Include="Differentiate.cs" />

20
src/Numerics/Statistics/DescriptiveStatistics.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2013 Math.NET // Copyright (c) 2009-2015 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -30,6 +30,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization;
namespace MathNet.Numerics.Statistics namespace MathNet.Numerics.Statistics
{ {
@ -38,8 +39,15 @@ namespace MathNet.Numerics.Statistics
/// NIST standard of accuracy for mean, variance, and standard deviation /// NIST standard of accuracy for mean, variance, and standard deviation
/// (the only statistics they provide exact values for) and exceeds them /// (the only statistics they provide exact values for) and exceeds them
/// in increased accuracy mode. /// in increased accuracy mode.
/// Recommendation: consider to use RunningStatistics instead.
/// </summary> /// </summary>
/// <remarks>Consider to use RunningStatistics instead.</remarks> /// <remarks>
/// This type declares a DataContract for out of the box ephemeral serialization
/// with engines like DataContractSerializer, Protocol Buffers and FsPickler,
/// but does not guarantee any compatibility between versions.
/// It is not recommended to rely on this mechanism for durable persistance.
/// </remarks>
[DataContract(Namespace = "urn:MathNet/Numerics")]
public class DescriptiveStatistics public class DescriptiveStatistics
{ {
/// <summary> /// <summary>
@ -104,24 +112,28 @@ namespace MathNet.Numerics.Statistics
/// Gets the size of the sample. /// Gets the size of the sample.
/// </summary> /// </summary>
/// <value>The size of the sample.</value> /// <value>The size of the sample.</value>
[DataMember(Order = 1)]
public long Count { get; private set; } public long Count { get; private set; }
/// <summary> /// <summary>
/// Gets the sample mean. /// Gets the sample mean.
/// </summary> /// </summary>
/// <value>The sample mean.</value> /// <value>The sample mean.</value>
[DataMember(Order = 2)]
public double Mean { get; private set; } public double Mean { get; private set; }
/// <summary> /// <summary>
/// Gets the unbiased population variance estimator (on a dataset of size N will use an N-1 normalizer). /// Gets the unbiased population variance estimator (on a dataset of size N will use an N-1 normalizer).
/// </summary> /// </summary>
/// <value>The sample variance.</value> /// <value>The sample variance.</value>
[DataMember(Order = 3)]
public double Variance { get; private set; } public double Variance { get; private set; }
/// <summary> /// <summary>
/// Gets the unbiased population standard deviation (on a dataset of size N will use an N-1 normalizer). /// Gets the unbiased population standard deviation (on a dataset of size N will use an N-1 normalizer).
/// </summary> /// </summary>
/// <value>The sample standard deviation.</value> /// <value>The sample standard deviation.</value>
[DataMember(Order = 4)]
public double StandardDeviation { get; private set; } public double StandardDeviation { get; private set; }
/// <summary> /// <summary>
@ -129,6 +141,7 @@ namespace MathNet.Numerics.Statistics
/// </summary> /// </summary>
/// <value>The sample skewness.</value> /// <value>The sample skewness.</value>
/// <remarks>Returns zero if <see cref="Count"/> is less than three. </remarks> /// <remarks>Returns zero if <see cref="Count"/> is less than three. </remarks>
[DataMember(Order = 5)]
public double Skewness { get; private set; } public double Skewness { get; private set; }
/// <summary> /// <summary>
@ -136,18 +149,21 @@ namespace MathNet.Numerics.Statistics
/// </summary> /// </summary>
/// <value>The sample kurtosis.</value> /// <value>The sample kurtosis.</value>
/// <remarks>Returns zero if <see cref="Count"/> is less than four. </remarks> /// <remarks>Returns zero if <see cref="Count"/> is less than four. </remarks>
[DataMember(Order = 6)]
public double Kurtosis { get; private set; } public double Kurtosis { get; private set; }
/// <summary> /// <summary>
/// Gets the maximum sample value. /// Gets the maximum sample value.
/// </summary> /// </summary>
/// <value>The maximum sample value.</value> /// <value>The maximum sample value.</value>
[DataMember(Order = 7)]
public double Maximum { get; private set; } public double Maximum { get; private set; }
/// <summary> /// <summary>
/// Gets the minimum sample value. /// Gets the minimum sample value.
/// </summary> /// </summary>
/// <value>The minimum sample value.</value> /// <value>The minimum sample value.</value>
[DataMember(Order = 8)]
public double Minimum { get; private set; } public double Minimum { get; private set; }
/// <summary> /// <summary>

24
src/Numerics/Statistics/Histogram.cs

@ -28,18 +28,26 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Statistics namespace MathNet.Numerics.Statistics
{ {
using System;
using System.Collections.Generic;
using System.Text;
using Properties;
/// <summary> /// <summary>
/// A <see cref="Histogram"/> consists of a series of <see cref="Bucket"/>s, /// A <see cref="Histogram"/> consists of a series of <see cref="Bucket"/>s,
/// each representing a region limited by a lower bound (exclusive) and an upper bound (inclusive). /// each representing a region limited by a lower bound (exclusive) and an upper bound (inclusive).
/// </summary> /// </summary>
/// <remarks>
/// This type declares a DataContract for out of the box ephemeral serialization
/// with engines like DataContractSerializer, Protocol Buffers and FsPickler,
/// but does not guarantee any compatibility between versions.
/// It is not recommended to rely on this mechanism for durable persistance.
/// </remarks>
[Serializable] [Serializable]
[DataContract(Namespace = "urn:MathNet/Numerics")]
public class Bucket : public class Bucket :
#if PORTABLE #if PORTABLE
IComparable<Bucket> IComparable<Bucket>
@ -71,11 +79,13 @@ namespace MathNet.Numerics.Statistics
/// <summary> /// <summary>
/// Lower Bound of the Bucket. /// Lower Bound of the Bucket.
/// </summary> /// </summary>
[DataMember(Order = 1)]
public double LowerBound { get; set; } public double LowerBound { get; set; }
/// <summary> /// <summary>
/// Upper Bound of the Bucket. /// Upper Bound of the Bucket.
/// </summary> /// </summary>
[DataMember(Order = 2)]
public double UpperBound { get; set; } public double UpperBound { get; set; }
/// <summary> /// <summary>
@ -84,6 +94,7 @@ namespace MathNet.Numerics.Statistics
/// <remarks> /// <remarks>
/// Value may be NaN if this was constructed as a <see cref="IComparer{Bucket}"/> argument. /// Value may be NaN if this was constructed as a <see cref="IComparer{Bucket}"/> argument.
/// </remarks> /// </remarks>
[DataMember(Order = 3)]
public double Count { get; set; } public double Count { get; set; }
/// <summary> /// <summary>
@ -245,16 +256,19 @@ namespace MathNet.Numerics.Statistics
/// A class which computes histograms of data. /// A class which computes histograms of data.
/// </summary> /// </summary>
[Serializable] [Serializable]
[DataContract(Namespace = "urn:MathNet/Numerics")]
public class Histogram public class Histogram
{ {
/// <summary> /// <summary>
/// Contains all the <c>Bucket</c>s of the <c>Histogram</c>. /// Contains all the <c>Bucket</c>s of the <c>Histogram</c>.
/// </summary> /// </summary>
[DataMember(Order = 1)]
private readonly List<Bucket> _buckets; private readonly List<Bucket> _buckets;
/// <summary> /// <summary>
/// Indicates whether the elements of <c>buckets</c> are currently sorted. /// Indicates whether the elements of <c>buckets</c> are currently sorted.
/// </summary> /// </summary>
[DataMember(Order = 2)]
private bool _areBucketsSorted; private bool _areBucketsSorted;
/// <summary> /// <summary>

27
src/Numerics/Statistics/RunningStatistics.cs

@ -1,4 +1,4 @@
// <copyright file="DescriptiveStatistics.cs" company="Math.NET"> // <copyright file="RunningStatistics.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project // Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
@ -33,6 +33,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization;
namespace MathNet.Numerics.Statistics namespace MathNet.Numerics.Statistics
{ {
@ -40,15 +41,35 @@ namespace MathNet.Numerics.Statistics
/// Running statistics accumulator, allows updating by adding values /// Running statistics accumulator, allows updating by adding values
/// or by combining two accumulators. /// or by combining two accumulators.
/// </summary> /// </summary>
/// <remarks>
/// This type declares a DataContract for out of the box ephemeral serialization
/// with engines like DataContractSerializer, Protocol Buffers and FsPickler,
/// but does not guarantee any compatibility between versions.
/// It is not recommended to rely on this mechanism for durable persistance.
/// </remarks>
[DataContract(Namespace = "urn:MathNet/Numerics")]
public class RunningStatistics public class RunningStatistics
{ {
[DataMember(Order = 1)]
long _n; long _n;
[DataMember(Order = 2)]
double _min = double.PositiveInfinity;
[DataMember(Order = 3)]
double _max = double.NegativeInfinity;
[DataMember(Order = 4)]
double _m1; double _m1;
[DataMember(Order = 5)]
double _m2; double _m2;
[DataMember(Order = 6)]
double _m3; double _m3;
[DataMember(Order = 7)]
double _m4; double _m4;
double _min = double.PositiveInfinity;
double _max = double.NegativeInfinity;
public RunningStatistics() public RunningStatistics()
{ {

81
src/UnitTests/StatisticsTests/StatisticsSerializationTests.cs

@ -0,0 +1,81 @@
using System.IO;
using System.Runtime.Serialization;
using MathNet.Numerics.Statistics;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests
{
[TestFixture]
public class StatisticsSerializationTests
{
[Test]
public void RunningStatisticsDataContractSerializationTest()
{
var expected = new RunningStatistics(new[] { 1.0, 2.0, 3.0 });
var serializer = new DataContractSerializer(typeof(RunningStatistics));
var stream = new MemoryStream();
serializer.WriteObject(stream, expected);
stream.Position = 0;
var actual = (RunningStatistics)serializer.ReadObject(stream);
Assert.That(actual.Count, Is.EqualTo(expected.Count));
Assert.That(actual.Maximum, Is.EqualTo(expected.Maximum));
Assert.That(actual.Mean, Is.EqualTo(expected.Mean));
}
[Test]
public void RunningStatisticsWithInfinityNaNDataContractSerializationTest()
{
var expected = new RunningStatistics(new[] { 1.0, 2.0, 3.0, double.PositiveInfinity, double.NaN });
var serializer = new DataContractSerializer(typeof(RunningStatistics));
var stream = new MemoryStream();
serializer.WriteObject(stream, expected);
stream.Position = 0;
var actual = (RunningStatistics)serializer.ReadObject(stream);
Assert.That(actual.Count, Is.EqualTo(expected.Count));
Assert.That(actual.Maximum, Is.EqualTo(expected.Maximum));
Assert.That(actual.Mean, Is.EqualTo(expected.Mean));
}
[Test]
public void DescriptiveStatisticsDataContractSerializationTest()
{
var expected = new DescriptiveStatistics(new[] { 1.0, 2.0, 3.0 });
var serializer = new DataContractSerializer(typeof(DescriptiveStatistics));
var stream = new MemoryStream();
serializer.WriteObject(stream, expected);
stream.Position = 0;
var actual = (DescriptiveStatistics)serializer.ReadObject(stream);
Assert.That(actual.Count, Is.EqualTo(expected.Count));
Assert.That(actual.Maximum, Is.EqualTo(expected.Maximum));
Assert.That(actual.Mean, Is.EqualTo(expected.Mean));
}
[Test]
public void HistogramDataContractSerializationTest()
{
var expected = new Histogram(new[] { 1.0, 2.0, 3.0, 4.0 }, 2);
var serializer = new DataContractSerializer(typeof(Histogram));
var stream = new MemoryStream();
serializer.WriteObject(stream, expected);
stream.Position = 0;
var actual = (Histogram)serializer.ReadObject(stream);
Assert.That(actual.BucketCount, Is.EqualTo(expected.BucketCount));
Assert.That(actual.DataCount, Is.EqualTo(expected.DataCount));
Assert.That(actual.LowerBound, Is.EqualTo(expected.LowerBound));
Assert.That(actual[0].Width, Is.EqualTo(expected[0].Width));
Assert.That(actual[0].UpperBound, Is.EqualTo(expected[0].UpperBound));
}
}
}

2
src/UnitTests/UnitTests-Net35.csproj

@ -59,6 +59,8 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs"> <Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs">

2
src/UnitTests/UnitTests-Portable259.csproj

@ -44,6 +44,8 @@
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs"> <Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs">

2
src/UnitTests/UnitTests-Portable328.csproj

@ -44,6 +44,8 @@
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs"> <Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs">

2
src/UnitTests/UnitTests-Portable47.csproj

@ -52,6 +52,8 @@
<Project>{49205185-621E-FFB9-2104-887C9F1BBD13}</Project> <Project>{49205185-621E-FFB9-2104-887C9F1BBD13}</Project>
<Name>Numerics-Portable47</Name> <Name>Numerics-Portable47</Name>
</ProjectReference> </ProjectReference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs"> <Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs">

2
src/UnitTests/UnitTests-Portable7.csproj

@ -52,6 +52,8 @@
<Project>{25E6E93B-56A2-4F14-8458-03665F122B3F}</Project> <Project>{25E6E93B-56A2-4F14-8458-03665F122B3F}</Project>
<Name>Numerics-Portable7</Name> <Name>Numerics-Portable7</Name>
</ProjectReference> </ProjectReference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs"> <Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs">

2
src/UnitTests/UnitTests-Portable78.csproj

@ -52,6 +52,8 @@
<Project>{2EBD6F22-9BD6-4AF2-A21B-7198C9C5571F}</Project> <Project>{2EBD6F22-9BD6-4AF2-A21B-7198C9C5571F}</Project>
<Name>Numerics-Portable78</Name> <Name>Numerics-Portable78</Name>
</ProjectReference> </ProjectReference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs"> <Compile Include="**\*.cs" Exclude="Properties\Settings.Designer.cs">

3
src/UnitTests/UnitTests.csproj

@ -75,6 +75,8 @@
<Project>{B7CAE5F4-A23F-4438-B5BE-41226618B695}</Project> <Project>{B7CAE5F4-A23F-4438-B5BE-41226618B695}</Project>
<Name>Numerics</Name> <Name>Numerics</Name>
</ProjectReference> </ProjectReference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.XML" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ArrayHelpers.cs" /> <Compile Include="ArrayHelpers.cs" />
@ -398,6 +400,7 @@
<Compile Include="StatisticsTests\MCMCTests\UnivariateHybridMCTest.cs" /> <Compile Include="StatisticsTests\MCMCTests\UnivariateHybridMCTest.cs" />
<Compile Include="StatisticsTests\MCMCTests\UnivariateSliceSamplerTests.cs" /> <Compile Include="StatisticsTests\MCMCTests\UnivariateSliceSamplerTests.cs" />
<Compile Include="StatisticsTests\PercentileTests.cs" /> <Compile Include="StatisticsTests\PercentileTests.cs" />
<Compile Include="StatisticsTests\StatisticsSerializationTests.cs" />
<Compile Include="StatisticsTests\StatisticsTests.cs" /> <Compile Include="StatisticsTests\StatisticsTests.cs" />
<Compile Include="StatisticsTests\StatTestData.cs" /> <Compile Include="StatisticsTests\StatTestData.cs" />
<Compile Include="TrigonometryTest.cs" /> <Compile Include="TrigonometryTest.cs" />

Loading…
Cancel
Save