41 changed files with 0 additions and 6899 deletions
@ -1,72 +0,0 @@ |
|||
// <copyright file="DelimitedReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Complex.IO |
|||
{ |
|||
using System; |
|||
using System.Numerics; |
|||
using Generic; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates a <see cref="Matrix{T}"/> from a delimited text file. If the user does not
|
|||
/// specify a delimiter, then any whitespace is used.
|
|||
/// </summary>
|
|||
/// <typeparam name="TMatrix">The type of the matrix to return.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class DelimitedReader<TMatrix> : DelimitedReader<TMatrix, Complex> |
|||
where TMatrix : Matrix<Complex> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class using
|
|||
/// any whitespace as the delimiter.
|
|||
/// </summary>
|
|||
public DelimitedReader() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">The delimiter to use.</param>
|
|||
public DelimitedReader(char delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// The delimiter to use.
|
|||
/// </param>
|
|||
/// <exception cref="ArgumentNullException">
|
|||
/// If <paramref name="delimiter"/> is <see langword="null"/>.
|
|||
/// </exception>
|
|||
public DelimitedReader(string delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
// <copyright file="MatlabReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Complex.IO |
|||
{ |
|||
using System.IO; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates matrices from Matlab files.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class MatlabMatrixReader : MatlabMatrixReader<System.Numerics.Complex> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="filename">Name of the file to read matrices from.</param>
|
|||
public MatlabMatrixReader(string filename) : base(filename) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to reader matrices from.</param>
|
|||
public MatlabMatrixReader(Stream stream) : base(stream) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,72 +0,0 @@ |
|||
// <copyright file="DelimitedReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Complex32.IO |
|||
{ |
|||
using System; |
|||
using Generic; |
|||
using LinearAlgebra.IO; |
|||
using Numerics; |
|||
|
|||
/// <summary>
|
|||
/// Creates a <see cref="Matrix{T}"/> from a delimited text file. If the user does not
|
|||
/// specify a delimiter, then any whitespace is used.
|
|||
/// </summary>
|
|||
/// <typeparam name="TMatrix">The type of the matrix to return.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class DelimitedReader<TMatrix> : DelimitedReader<TMatrix, Complex32> |
|||
where TMatrix : Matrix<Complex32> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class using
|
|||
/// any whitespace as the delimiter.
|
|||
/// </summary>
|
|||
public DelimitedReader() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">The delimiter to use.</param>
|
|||
public DelimitedReader(char delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// The delimiter to use.
|
|||
/// </param>
|
|||
/// <exception cref="ArgumentNullException">
|
|||
/// If <paramref name="delimiter"/> is <see langword="null"/>.
|
|||
/// </exception>
|
|||
public DelimitedReader(string delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
// <copyright file="MatlabReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Complex32.IO |
|||
{ |
|||
using System.IO; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates matrices from Matlab files.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class MatlabMatrixReader : MatlabMatrixReader<Numerics.Complex32> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="filename">Name of the file to read matrices from.</param>
|
|||
public MatlabMatrixReader(string filename) : base(filename) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to reader matrices from.</param>
|
|||
public MatlabMatrixReader(Stream stream) : base(stream) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,71 +0,0 @@ |
|||
// <copyright file="DelimitedReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double.IO |
|||
{ |
|||
using System; |
|||
using Generic; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates a <see cref="Matrix{T}"/> from a delimited text file. If the user does not
|
|||
/// specify a delimiter, then any whitespace is used.
|
|||
/// </summary>
|
|||
/// <typeparam name="TMatrix">The type of the matrix to return.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class DelimitedReader<TMatrix> : DelimitedReader<TMatrix, double> |
|||
where TMatrix : Matrix<double> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class using
|
|||
/// any whitespace as the delimiter.
|
|||
/// </summary>
|
|||
public DelimitedReader() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">The delimiter to use.</param>
|
|||
public DelimitedReader(char delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// The delimiter to use.
|
|||
/// </param>
|
|||
/// <exception cref="ArgumentNullException">
|
|||
/// If <paramref name="delimiter"/> is <see langword="null"/>.
|
|||
/// </exception>
|
|||
public DelimitedReader(string delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
// <copyright file="MatlabReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double.IO |
|||
{ |
|||
using System.IO; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates matrices from Matlab files.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class MatlabMatrixReader : MatlabMatrixReader<double> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="filename">Name of the file to read matrices from.</param>
|
|||
public MatlabMatrixReader(string filename) : base(filename) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to reader matrices from.</param>
|
|||
public MatlabMatrixReader(Stream stream) : base(stream) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,291 +0,0 @@ |
|||
// <copyright file="DelimitedReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Numerics; |
|||
using System.Reflection; |
|||
using System.Text.RegularExpressions; |
|||
using Generic; |
|||
using Numerics; |
|||
|
|||
/// <summary>
|
|||
/// Creates a <see cref="Matrix{T}"/> from a delimited text file. If the user does not
|
|||
/// specify a delimiter, then any whitespace is used.
|
|||
/// </summary>
|
|||
/// <typeparam name="TMatrix">The type of the matrix to return.</typeparam>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class DelimitedReader<TMatrix, TDataType> : MatrixReader<TMatrix, TDataType> |
|||
where TMatrix : Matrix<TDataType> |
|||
where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
/// <summary>
|
|||
/// Converts a string into the given data type.
|
|||
/// </summary>
|
|||
/// <param name="number">
|
|||
/// The number as a string to convert.
|
|||
/// </param>
|
|||
/// <returns>The converted number.</returns>
|
|||
private delegate object ParseNumber(string number); |
|||
|
|||
/// <summary>
|
|||
/// The function that will do the conversion for a given type.
|
|||
/// </summary>
|
|||
private ParseNumber _parseFunction; |
|||
|
|||
/// <summary>
|
|||
/// Initializes static members of the <see cref="DelimitedReader{TMatrix, TDataType}"/> class.
|
|||
/// </summary>
|
|||
private void SetParser() |
|||
{ |
|||
var type = typeof(TDataType); |
|||
if (type == typeof(double)) |
|||
{ |
|||
_parseFunction = ConvertDouble; |
|||
} |
|||
else if (type == typeof(float)) |
|||
{ |
|||
_parseFunction = ConvertFloat; |
|||
} |
|||
else if (type == typeof(Complex)) |
|||
{ |
|||
_parseFunction = ConvertComplex; |
|||
} |
|||
else if (type == typeof(Complex32)) |
|||
{ |
|||
_parseFunction = ConvertComplex32; |
|||
} |
|||
else |
|||
{ |
|||
throw new NotSupportedException(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the string into a Complex32.
|
|||
/// </summary>
|
|||
/// <param name="number">The number to convert.</param>
|
|||
/// <returns>The converted number.</returns>
|
|||
private object ConvertComplex32(string number) |
|||
{ |
|||
return number.ToComplex32(_cultureInfo); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the string into a Complex.
|
|||
/// </summary>
|
|||
/// <param name="number">The number to convert.</param>
|
|||
/// <returns>The converted number.</returns>
|
|||
private object ConvertComplex(string number) |
|||
{ |
|||
return number.ToComplex(_cultureInfo); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the string into a double.
|
|||
/// </summary>
|
|||
/// <param name="number">The number to convert.</param>
|
|||
/// <returns>The converted number.</returns>
|
|||
private object ConvertDouble(string number) |
|||
{ |
|||
return double.Parse(number, NumberStyles.Any, _cultureInfo); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the string into a float.
|
|||
/// </summary>
|
|||
/// <param name="number">The number to convert.</param>
|
|||
/// <returns>The converted number.</returns>
|
|||
private object ConvertFloat(string number) |
|||
{ |
|||
return float.Parse(number, NumberStyles.Any, _cultureInfo); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Constructor to create matrix instance.
|
|||
/// </summary>
|
|||
private static readonly ConstructorInfo Constructor = typeof(TMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); |
|||
|
|||
/// <summary>
|
|||
/// The base regular expression.
|
|||
/// </summary>
|
|||
private static readonly string Base = "\\([^\\)]*\\)|'[^']*'|\"[^\"]*\"|[^{0}]*"; |
|||
|
|||
/// <summary>
|
|||
/// The regular expression to use.
|
|||
/// </summary>
|
|||
private readonly Regex _regex; |
|||
|
|||
/// <summary>
|
|||
/// The <see cref="CultureInfo"/> to use.
|
|||
/// </summary>
|
|||
private CultureInfo _cultureInfo = CultureInfo.CurrentCulture; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix, TDataType}"/> class using
|
|||
/// any whitespace as the delimiter.
|
|||
/// </summary>
|
|||
public DelimitedReader() |
|||
{ |
|||
_regex = new Regex(string.Format(Base, @"\s"), RegexOptions.Compiled); |
|||
SetParser(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix, TDataType}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">The delimiter to use.</param>
|
|||
public DelimitedReader(char delimiter) |
|||
{ |
|||
_regex = new Regex(string.Format(Base, delimiter), RegexOptions.Compiled); |
|||
SetParser(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix, TDataType}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// The delimiter to use.
|
|||
/// </param>
|
|||
/// <exception cref="ArgumentNullException">
|
|||
/// If <paramref name="delimiter"/> is <see langword="null"/>.
|
|||
/// </exception>
|
|||
public DelimitedReader(string delimiter) |
|||
{ |
|||
if (delimiter == null) |
|||
{ |
|||
throw new ArgumentNullException("delimiter"); |
|||
} |
|||
|
|||
_regex = new Regex(string.Format(Base, delimiter), RegexOptions.Compiled); |
|||
SetParser(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the <see cref="CultureInfo"/> to use when parsing the numbers.
|
|||
/// </summary>
|
|||
/// <value>The culture info.</value>
|
|||
/// <remarks>Defaults to <c>CultureInfo.CurrentCulture</c>.</remarks>
|
|||
public CultureInfo CultureInfo |
|||
{ |
|||
get |
|||
{ |
|||
return _cultureInfo; |
|||
} |
|||
|
|||
set |
|||
{ |
|||
if (value != null) |
|||
{ |
|||
_cultureInfo = value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets a value indicating whether the files has a header row.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// <c>true</c> if this instance has a header row; otherwise, <c>false</c>.
|
|||
/// </value>
|
|||
/// <remarks>Defaults to <see langword="false"/>.</remarks>
|
|||
public bool HasHeaderRow |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Performs the actual reading.
|
|||
/// </summary>
|
|||
/// <param name="stream">The <see cref="Stream"/> to read the matrix from.</param>
|
|||
/// <returns>
|
|||
/// A matrix containing the data from the <see cref="Stream"/>. <see langword="null"/> is returned if the <see cref="Stream"/> is empty.
|
|||
/// </returns>
|
|||
protected override TMatrix DoReadMatrix(Stream stream) |
|||
{ |
|||
var data = new List<string[]>(); |
|||
|
|||
// max is used to supports files like:
|
|||
// 1,2
|
|||
// 3,4,5,6
|
|||
// 7
|
|||
// this creates a 3x4 matrix:
|
|||
// 1, 2, 0 ,0
|
|||
// 3, 4, 5, 6
|
|||
// 7, 0, 0, 0
|
|||
var max = -1; |
|||
|
|||
var reader = new StreamReader(stream); |
|||
var line = reader.ReadLine(); |
|||
if (HasHeaderRow) |
|||
{ |
|||
line = reader.ReadLine(); |
|||
} |
|||
|
|||
var dataType = typeof(TDataType); |
|||
while (line != null) |
|||
{ |
|||
line = line.Trim(); |
|||
if (line.Length > 0) |
|||
{ |
|||
var matches = _regex.Matches(line); |
|||
var row = (from Match match in matches where match.Length > 0 select match.Value).ToArray(); |
|||
max = Math.Max(max, row.Length); |
|||
data.Add(row); |
|||
} |
|||
|
|||
line = reader.ReadLine(); |
|||
} |
|||
|
|||
var ret = (TMatrix)Constructor.Invoke(new object[] { data.Count, max }); |
|||
|
|||
if (data.Count != 0) |
|||
{ |
|||
for (var i = 0; i < data.Count; i++) |
|||
{ |
|||
var row = data[i]; |
|||
for (var j = 0; j < row.Length; j++) |
|||
{ |
|||
// strip off quotes
|
|||
var value = row[j].Replace("'", string.Empty).Replace("\"", string.Empty); |
|||
ret[i, j] = (TDataType)_parseFunction(value); |
|||
} |
|||
} |
|||
} |
|||
|
|||
reader.Close(); |
|||
reader.Dispose(); |
|||
return ret; |
|||
} |
|||
} |
|||
} |
|||
@ -1,145 +0,0 @@ |
|||
// <copyright file="DelimitedWriter.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using Generic; |
|||
|
|||
/// <summary>
|
|||
/// Writes an <see cref="Matrix{TDataType}"/> to delimited text file. If the user does not
|
|||
/// specify a delimiter, a tab separator is used.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class DelimitedWriter : MatrixWriter |
|||
{ |
|||
/// <summary>
|
|||
/// The delimiter to use.
|
|||
/// </summary>
|
|||
private readonly string _delimiter; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedWriter"/> class.
|
|||
/// a comma as the delimiter.
|
|||
/// </summary>
|
|||
public DelimitedWriter() |
|||
{ |
|||
_delimiter = ","; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedWriter"/> class.
|
|||
/// using the given delimiter.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// the delimiter to use.
|
|||
/// </param>
|
|||
public DelimitedWriter(char delimiter) |
|||
{ |
|||
_delimiter = new string(delimiter, 1); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedWriter"/> class.
|
|||
/// using the given delimiter.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// the delimiter to use.
|
|||
/// </param>
|
|||
public DelimitedWriter(string delimiter) |
|||
{ |
|||
_delimiter = delimiter; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the column header values.
|
|||
/// </summary>
|
|||
/// <value>The column header values.</value>
|
|||
/// <remarks>Will write the column headers if the list is not empty or <c>null</c>.</remarks>
|
|||
public IList<string> ColumnHeaders |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the given <see cref="Matrix{TDataType}"/> to the given <see cref="TextWriter"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
/// <param name="matrix">The matrix to write.</param>
|
|||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|||
/// <param name="format">The number format to use on each element.</param>
|
|||
/// <param name="cultureInfo">The culture to use.</param>
|
|||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is <c>null</c>.</exception>
|
|||
protected override void DoWriteMatrix<TDataType>(Matrix<TDataType> matrix, TextWriter writer, string format, CultureInfo cultureInfo) |
|||
{ |
|||
if (matrix == null) |
|||
{ |
|||
throw new ArgumentNullException("matrix"); |
|||
} |
|||
|
|||
if (writer == null) |
|||
{ |
|||
throw new ArgumentNullException("writer"); |
|||
} |
|||
|
|||
if (ColumnHeaders != null && ColumnHeaders.Count > 0) |
|||
{ |
|||
for (var i = 0; i < ColumnHeaders.Count - 1; i++) |
|||
{ |
|||
writer.Write(ColumnHeaders[i]); |
|||
writer.Write(_delimiter); |
|||
} |
|||
|
|||
writer.WriteLine(ColumnHeaders[ColumnHeaders.Count - 1]); |
|||
} |
|||
|
|||
var cols = matrix.ColumnCount - 1; |
|||
var rows = matrix.RowCount - 1; |
|||
for (var i = 0; i < matrix.RowCount; i++) |
|||
{ |
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
writer.Write(matrix[i, j].ToString(format, cultureInfo)); |
|||
if (j != cols) |
|||
{ |
|||
writer.Write(_delimiter); |
|||
} |
|||
} |
|||
|
|||
if (i != rows) |
|||
{ |
|||
writer.Write(Environment.NewLine); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,96 +0,0 @@ |
|||
// <copyright file="Adler32.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
/* This code is a port and simplification of |
|||
adler32.c -- compute the Adler-32 checksum of a data stream |
|||
Copyright (C) 1995-2011 Mark Adler |
|||
zlib license: |
|||
Copyright (C) 1995-2012 Jean-loup Gailly and Mark Adler |
|||
|
|||
This software is provided 'as-is', without any express or implied |
|||
warranty. In no event will the authors be held liable for any damages |
|||
arising from the use of this software. |
|||
|
|||
Permission is granted to anyone to use this software for any purpose, |
|||
including commercial applications, and to alter it and redistribute it |
|||
freely, subject to the following restrictions: |
|||
|
|||
1. The origin of this software must not be misrepresented; you must not |
|||
claim that you wrote the original software. If you use this software |
|||
in a product, an acknowledgment in the product documentation would be |
|||
appreciated but is not required. |
|||
2. Altered source versions must be plainly marked as such, and must not be |
|||
misrepresented as being the original software. |
|||
3. This notice may not be removed or altered from any source distribution. |
|||
|
|||
Jean-loup Gailly Mark Adler |
|||
jloup@gzip.org madler@alumni.caltech.edu |
|||
|
|||
*/ |
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab |
|||
{ |
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
internal static class Adler32 |
|||
{ |
|||
/* largest prime smaller than 65536 */ |
|||
const int Base = 65521; |
|||
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
|||
const int Nmax = 5552; |
|||
|
|||
/// <summary>
|
|||
/// Computes the Adler-32 checksum of the given data.
|
|||
/// </summary>
|
|||
/// <param name="data">The data to create the checksum.</param>
|
|||
/// <returns>The checksum</returns>
|
|||
public static uint Compute(byte[] data) |
|||
{ |
|||
uint adler = 1; |
|||
uint sum2 = 0; |
|||
var len = data.Length; |
|||
var offset = 0; |
|||
while (len > 0) |
|||
{ |
|||
var tlen = len < Nmax ? len : Nmax; |
|||
len -= tlen; |
|||
|
|||
do |
|||
{ |
|||
adler += data[offset++]; |
|||
sum2 += adler; |
|||
} while (--tlen > 0); |
|||
|
|||
adler %= Base; |
|||
sum2 %= Base; |
|||
} |
|||
return adler | (sum2 << 16); |
|||
} |
|||
} |
|||
} |
|||
@ -1,124 +0,0 @@ |
|||
// <copyright file="ArrayClass.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab |
|||
{ |
|||
/// <summary>
|
|||
/// Enumeration for the Matlab array types
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
internal enum ArrayClass : byte |
|||
{ |
|||
/// <summary>
|
|||
/// mxUNKNOWN CLASS
|
|||
/// </summary>
|
|||
Unknown = 0, |
|||
|
|||
/// <summary>
|
|||
/// mxCELL CLASS
|
|||
/// </summary>
|
|||
Cell = 1, |
|||
|
|||
/// <summary>
|
|||
/// mxSTRUCT CLASS
|
|||
/// </summary>
|
|||
Structure = 2, |
|||
|
|||
/// <summary>
|
|||
/// mxOBJECT CLASS
|
|||
/// </summary>
|
|||
Object = 3, |
|||
|
|||
/// <summary>
|
|||
/// mxCHAR CLASS
|
|||
/// </summary>
|
|||
Character = 4, |
|||
|
|||
/// <summary>
|
|||
/// mxSPARSE CLASS
|
|||
/// </summary>
|
|||
Sparse = 5, |
|||
|
|||
/// <summary>
|
|||
/// mxDOUBLE CLASS
|
|||
/// </summary>
|
|||
Double = 6, |
|||
|
|||
/// <summary>
|
|||
/// mxSINGLE CLASS
|
|||
/// </summary>
|
|||
Single = 7, |
|||
|
|||
/// <summary>
|
|||
/// mxINT8 CLASS
|
|||
/// </summary>
|
|||
Int8 = 8, |
|||
|
|||
/// <summary>
|
|||
/// mxUINT8 CLASS
|
|||
/// </summary>
|
|||
UInt8 = 9, |
|||
|
|||
/// <summary>
|
|||
/// mxINT16 CLASS
|
|||
/// </summary>
|
|||
Int16 = 10, |
|||
|
|||
/// <summary>
|
|||
/// mxUINT16 CLASS
|
|||
/// </summary>
|
|||
UInt16 = 11, |
|||
|
|||
/// <summary>
|
|||
/// mxINT32 CLASS
|
|||
/// </summary>
|
|||
Int32 = 12, |
|||
|
|||
/// <summary>
|
|||
/// mxUINT32 CLASS
|
|||
/// </summary>
|
|||
UInt32 = 13, |
|||
|
|||
/// <summary>
|
|||
/// mxINT64 CLASS
|
|||
/// </summary>
|
|||
Int64 = 14, |
|||
|
|||
/// <summary>
|
|||
/// mxUINT64 CLASS
|
|||
/// </summary>
|
|||
UInt64 = 15, |
|||
|
|||
/// <summary>
|
|||
/// mxFUNCTION CLASS
|
|||
/// </summary>
|
|||
Function = 16 |
|||
} |
|||
} |
|||
@ -1,55 +0,0 @@ |
|||
// <copyright file="ArrayFlags.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Matlab Array Flags
|
|||
/// </summary>
|
|||
[Flags] |
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
internal enum ArrayFlags |
|||
{ |
|||
/// <summary>
|
|||
/// Complex flag
|
|||
/// </summary>
|
|||
Complex = 8, |
|||
|
|||
/// <summary>
|
|||
/// Global flag
|
|||
/// </summary>
|
|||
Global = 4, |
|||
|
|||
/// <summary>
|
|||
/// Logical flag
|
|||
/// </summary>
|
|||
Logical = 2 |
|||
} |
|||
} |
|||
@ -1,119 +0,0 @@ |
|||
// <copyright file="DataType.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab |
|||
{ |
|||
/// <summary>
|
|||
/// Matlab data types
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
internal enum DataType |
|||
{ |
|||
/// <summary>
|
|||
/// Unkown type
|
|||
/// </summary>
|
|||
Unknown = 0, |
|||
|
|||
/// <summary>
|
|||
/// miINT8 type
|
|||
/// </summary>
|
|||
Int8 = 1, |
|||
|
|||
/// <summary>
|
|||
/// miUINT8 type
|
|||
/// </summary>
|
|||
UInt8 = 2, |
|||
|
|||
/// <summary>
|
|||
/// miINT16 type
|
|||
/// </summary>
|
|||
Int16 = 3, |
|||
|
|||
/// <summary>
|
|||
/// miUINT16 type
|
|||
/// </summary>
|
|||
UInt16 = 4, |
|||
|
|||
/// <summary>
|
|||
/// miINT32 type
|
|||
/// </summary>
|
|||
Int32 = 5, |
|||
|
|||
/// <summary>
|
|||
/// miUINT32 type
|
|||
/// </summary>
|
|||
UInt32 = 6, |
|||
|
|||
/// <summary>
|
|||
/// miSINGLE type
|
|||
/// </summary>
|
|||
Single = 7, |
|||
|
|||
/// <summary>
|
|||
/// miDOUBLE type
|
|||
/// </summary>
|
|||
Double = 9, |
|||
|
|||
/// <summary>
|
|||
/// miINT64 type
|
|||
/// </summary>
|
|||
Int64 = 12, |
|||
|
|||
/// <summary>
|
|||
/// miUINT6 4type
|
|||
/// </summary>
|
|||
UInt64 = 13, |
|||
|
|||
/// <summary>
|
|||
/// miMATRIX type
|
|||
/// </summary>
|
|||
Matrix = 14, |
|||
|
|||
/// <summary>
|
|||
/// miCOMPRESSED type
|
|||
/// </summary>
|
|||
Compressed = 15, |
|||
|
|||
/// <summary>
|
|||
/// miUTF8 type
|
|||
/// </summary>
|
|||
Utf8 = 16, |
|||
|
|||
/// <summary>
|
|||
/// miUTF16 type
|
|||
/// </summary>
|
|||
Utf16 = 17, |
|||
|
|||
/// <summary>
|
|||
/// miUTF32 type
|
|||
/// </summary>
|
|||
Utf32 = 18 |
|||
} |
|||
} |
|||
@ -1,85 +0,0 @@ |
|||
// <copyright file="MatlabFile.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO.Matlab |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Generic; |
|||
|
|||
/// <summary>
|
|||
/// Represents a Matlab file
|
|||
/// </summary>
|
|||
/// <typeparam name="TDataType">The data type of the matrix to return.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
internal class MatlabFile<TDataType> where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
/// <summary>
|
|||
/// Matrices in a matlab file stored as 1-D arrays
|
|||
/// </summary>
|
|||
private readonly IDictionary<string, Matrix<TDataType>> _matrices = new SortedList<string, Matrix<TDataType>>(); |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the header text.
|
|||
/// </summary>
|
|||
/// <value>The header text.</value>
|
|||
public string HeaderText { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the first name of the matrix.
|
|||
/// </summary>
|
|||
/// <value>The first name of the matrix.</value>
|
|||
public string FirstMatrixName { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the first matrix.
|
|||
/// </summary>
|
|||
/// <value>The first matrix.</value>
|
|||
public Matrix<TDataType> FirstMatrix |
|||
{ |
|||
get |
|||
{ |
|||
if (string.IsNullOrEmpty(FirstMatrixName) || !_matrices.ContainsKey(FirstMatrixName)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return _matrices[FirstMatrixName]; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the matrices.
|
|||
/// </summary>
|
|||
/// <value>The matrices.</value>
|
|||
public IDictionary<string, Matrix<TDataType>> Matrices |
|||
{ |
|||
get { return _matrices; } |
|||
} |
|||
} |
|||
} |
|||
File diff suppressed because it is too large
@ -1,188 +0,0 @@ |
|||
// <copyright file="MatlabReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using Generic; |
|||
using Matlab; |
|||
using Properties; |
|||
|
|||
/// <summary>
|
|||
/// Creates matrices from Matlab files.
|
|||
/// </summary>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class MatlabMatrixReader<TDataType> where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
/// <summary>
|
|||
/// The name of the file to read from.
|
|||
/// </summary>
|
|||
private readonly string _filename; |
|||
|
|||
/// <summary>
|
|||
/// The stream to read from if we are not reading from a file directly.
|
|||
/// </summary>
|
|||
private readonly Stream _stream; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader{TDataType}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="filename">Name of the file to read matrices from.</param>
|
|||
public MatlabMatrixReader(string filename) |
|||
{ |
|||
if (string.IsNullOrEmpty(filename)) |
|||
{ |
|||
throw new ArgumentException(Resources.StringNullOrEmpty, "filename"); |
|||
} |
|||
|
|||
if (!File.Exists(filename)) |
|||
{ |
|||
throw new FileNotFoundException(Resources.FileDoesNotExist, "filename"); |
|||
} |
|||
|
|||
_filename = filename; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader{TDataType}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to reader matrices from.</param>
|
|||
public MatlabMatrixReader(Stream stream) |
|||
{ |
|||
if (stream == null) |
|||
{ |
|||
throw new ArgumentNullException("stream"); |
|||
} |
|||
|
|||
_stream = stream; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the first matrix from the file or stream.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// A sparse or dense matrix depending on how the matrix
|
|||
/// is defined in the Matlab file.
|
|||
/// </returns>
|
|||
public Matrix<TDataType> ReadMatrix() |
|||
{ |
|||
return ReadMatrix(null); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the named matrix from the file or stream.
|
|||
/// </summary>
|
|||
/// <param name="matrixName">The name of the matrix to read.</param>
|
|||
/// <returns>
|
|||
/// A sparse or dense matrix depending on how the matrix
|
|||
/// is defined in the Matlab file.
|
|||
/// <see langword="null"/> is returned if a matrix with the requests name doesn't exist.
|
|||
/// </returns>
|
|||
public Matrix<TDataType> ReadMatrix(string matrixName) |
|||
{ |
|||
Stream stream; |
|||
if (_filename == null) |
|||
{ |
|||
stream = _stream; |
|||
_stream.Seek(0, SeekOrigin.Begin); |
|||
} |
|||
else |
|||
{ |
|||
stream = new FileStream(_filename, FileMode.Open, FileAccess.Read); |
|||
} |
|||
|
|||
var names = string.IsNullOrEmpty(matrixName) ? new string[] { } : new[] { matrixName }; |
|||
var parser = new MatlabParser<TDataType>(stream, names); |
|||
var file = parser.Parse(); |
|||
|
|||
Matrix<TDataType> matrix = null; |
|||
if (string.IsNullOrEmpty(matrixName)) |
|||
{ |
|||
matrix = file.FirstMatrix; |
|||
} |
|||
else if (file.Matrices.ContainsKey(matrixName)) |
|||
{ |
|||
matrix = file.Matrices[matrixName]; |
|||
} |
|||
|
|||
if (_filename != null) |
|||
{ |
|||
stream.Close(); |
|||
stream.Dispose(); |
|||
} |
|||
|
|||
return matrix; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads all matrices from the file or stream.
|
|||
/// </summary>
|
|||
/// <returns>All matrices from the file or stream. The key to the <see cref="IDictionary{T,K}"/>
|
|||
/// is the matrix's name.</returns>
|
|||
public IDictionary<string, Matrix<TDataType>> ReadMatrices() |
|||
{ |
|||
return ReadMatrices(new string[] { }); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the named matrices from the file or stream.
|
|||
/// </summary>
|
|||
/// <param name="names">The names of the matrices to retrieve.</param>
|
|||
/// <returns>
|
|||
/// The named matrices from the file or stream. The key to the <see cref="IDictionary{T,K}"/>
|
|||
/// is the matrix's name.</returns>
|
|||
public IDictionary<string, Matrix<TDataType>> ReadMatrices(IEnumerable<string> names) |
|||
{ |
|||
Stream stream; |
|||
if (_filename == null) |
|||
{ |
|||
stream = _stream; |
|||
_stream.Seek(0, SeekOrigin.Begin); |
|||
} |
|||
else |
|||
{ |
|||
stream = new BufferedStream(new FileStream(_filename, FileMode.Open, FileAccess.Read)); |
|||
} |
|||
|
|||
var parser = new MatlabParser<TDataType>(stream, names); |
|||
var file = parser.Parse(); |
|||
|
|||
if (_filename != null) |
|||
{ |
|||
stream.Close(); |
|||
stream.Dispose(); |
|||
} |
|||
|
|||
return file.Matrices.ToDictionary(matrix => matrix.Key, matrix => matrix.Value); |
|||
} |
|||
} |
|||
} |
|||
@ -1,825 +0,0 @@ |
|||
// <copyright file="MatlabWriter.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.IO.Compression; |
|||
using System.Text; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO |
|||
{ |
|||
using Generic; |
|||
using Matlab; |
|||
using Properties; |
|||
using Storage; |
|||
|
|||
/// <summary>
|
|||
/// Writes matrices to a Matlab file.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class MatlabMatrixWriter : IDisposable |
|||
{ |
|||
/// <summary>
|
|||
/// The file header value
|
|||
/// </summary>
|
|||
private const string HeaderText = "MATLAB 5.0 MAT-file, Platform: .NET 4 - Math.NET Numerics, Created on: "; |
|||
|
|||
/// <summary>
|
|||
/// The length of the header text.
|
|||
/// </summary>
|
|||
private const int HeaderTextLength = 116; |
|||
|
|||
/// <summary>
|
|||
/// Have we written the header yet.
|
|||
/// </summary>
|
|||
private bool _headerWritten; |
|||
|
|||
/// <summary>
|
|||
/// The binary writer to write to.
|
|||
/// </summary>
|
|||
private BinaryWriter _writer; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixWriter"/> class.
|
|||
/// </summary>
|
|||
/// <param name="filename">The name of the Matlab file to save the matrices to.</param>
|
|||
public MatlabMatrixWriter(string filename) |
|||
{ |
|||
if (string.IsNullOrEmpty(filename)) |
|||
{ |
|||
throw new ArgumentException(Resources.StringNullOrEmpty, "filename"); |
|||
} |
|||
|
|||
_writer = new BinaryWriter(new BufferedStream(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|||
/// </summary>
|
|||
public void Dispose() |
|||
{ |
|||
if (_writer != null) |
|||
{ |
|||
_writer.Flush(); |
|||
_writer.Close(); |
|||
_writer = null; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the given <see cref="Matrix{T}"/> to the file.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to write.</param>
|
|||
/// <param name="name">The name of the matrix to store in the file.</param>
|
|||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="name"/> is <c>null</c>.</exception>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, string name) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
if (matrix == null) |
|||
{ |
|||
throw new ArgumentNullException("matrix"); |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(name)) |
|||
{ |
|||
throw new ArgumentException(Resources.StringNullOrEmpty, "name"); |
|||
} |
|||
|
|||
if (name.IndexOf(' ') > -1) |
|||
{ |
|||
throw new ArgumentException(string.Format(Resources.NameCannotContainASpace, name), "name"); |
|||
} |
|||
|
|||
if (!_headerWritten) |
|||
{ |
|||
WriteHeader(); |
|||
_headerWritten = true; |
|||
} |
|||
|
|||
// write datatype
|
|||
_writer.Write((int)DataType.Compressed); |
|||
|
|||
byte[] data = null; |
|||
|
|||
if (typeof(TDataType) == typeof(double)) |
|||
{ |
|||
if (matrix is Double.SparseMatrix) |
|||
{ |
|||
data = GetSparseDataArray((Double.SparseMatrix)(object)matrix, name); |
|||
} |
|||
else |
|||
{ |
|||
data = GetDenseDataArray((Double.Matrix)(object)matrix, name); |
|||
} |
|||
} |
|||
else if (typeof(TDataType) == typeof(float)) |
|||
{ |
|||
if (matrix is Single.SparseMatrix) |
|||
{ |
|||
data = GetSparseDataArray((Single.SparseMatrix)(object)matrix, name); |
|||
} |
|||
else |
|||
{ |
|||
data = GetDenseDataArray((Single.Matrix)(object)matrix, name); |
|||
} |
|||
} |
|||
else if (typeof(TDataType) == typeof(System.Numerics.Complex)) |
|||
{ |
|||
if (matrix is Complex.SparseMatrix) |
|||
{ |
|||
data = GetSparseDataArray((Complex.SparseMatrix)(object)matrix, name); |
|||
} |
|||
else |
|||
{ |
|||
data = GetDenseDataArray((Complex.Matrix)(object)matrix, name); |
|||
} |
|||
} |
|||
else if (typeof(TDataType) == typeof(Numerics.Complex32)) |
|||
{ |
|||
if (matrix is Complex32.SparseMatrix) |
|||
{ |
|||
data = GetSparseDataArray((Complex32.SparseMatrix)(object)matrix, name); |
|||
} |
|||
else |
|||
{ |
|||
data = GetDenseDataArray((Complex32.Matrix)(object)matrix, name); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
throw new NotSupportedException(); |
|||
} |
|||
|
|||
WriteCompressedData(data); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the given <see cref="Matrix{TDataType}"/> to the file.
|
|||
/// </summary>
|
|||
/// <param name="matrices">The matrices to write.</param>
|
|||
/// <param name="names">The names of the matrices to store in the file.</param>
|
|||
/// <exception cref="ArgumentNullException">If either <paramref name="matrices"/> or <paramref name="names"/> is null.</exception>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
public void WriteMatrices<TDataType>(IList<Matrix<TDataType>> matrices, IList<string> names) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
if (matrices == null) |
|||
{ |
|||
throw new ArgumentNullException("matrices"); |
|||
} |
|||
|
|||
if (names == null) |
|||
{ |
|||
throw new ArgumentNullException("names"); |
|||
} |
|||
|
|||
if (matrices.Count != names.Count) |
|||
{ |
|||
throw new ArgumentException(Resources.ArgumentMatrixDimensions); |
|||
} |
|||
|
|||
for (int i = 0; i < matrices.Count; i++) |
|||
{ |
|||
WriteMatrix(matrices[i], names[i]); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Closes the stream the being written to.
|
|||
/// </summary>
|
|||
/// <remarks>Calls <see cref="IDisposable.Dispose"/>.</remarks>
|
|||
public void Close() |
|||
{ |
|||
Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the matrix tag and name.
|
|||
/// </summary>
|
|||
/// <param name="writer">The writer we are using.</param>
|
|||
/// <param name="arrayClass">The array class we are writing.</param>
|
|||
/// <param name="isComplex">if set to <c>true</c> if this a complex matrix.</param>
|
|||
/// <param name="name">The name name of the matrix.</param>
|
|||
/// <param name="rows">The number of rows.</param>
|
|||
/// <param name="columns">The columns of columns.</param>
|
|||
/// <param name="nzmax">The maximum number of non-zero elements.</param>
|
|||
private static void WriteMatrixTagAndName(BinaryWriter writer, ArrayClass arrayClass, bool isComplex, string name, int rows, int columns, int nzmax) |
|||
{ |
|||
writer.Write((int)DataType.Matrix); |
|||
|
|||
// add place holder for data size
|
|||
writer.Write(0); |
|||
|
|||
// write flag, data type and size
|
|||
writer.Write((int)DataType.UInt32); |
|||
writer.Write(8); |
|||
|
|||
// write array class and flags
|
|||
writer.Write((byte)arrayClass); |
|||
if (isComplex) |
|||
{ |
|||
writer.Write((byte)ArrayFlags.Complex); |
|||
} |
|||
else |
|||
{ |
|||
writer.Write((byte)0); |
|||
} |
|||
|
|||
writer.Write((short)0); |
|||
writer.Write(nzmax); |
|||
|
|||
// write dimensions
|
|||
writer.Write((int)DataType.Int32); |
|||
writer.Write(8); |
|||
writer.Write(rows); |
|||
writer.Write(columns); |
|||
|
|||
byte[] nameBytes = Encoding.ASCII.GetBytes(name); |
|||
|
|||
// write name
|
|||
if (nameBytes.Length > 4) |
|||
{ |
|||
writer.Write((int)DataType.Int8); |
|||
writer.Write(nameBytes.Length); |
|||
writer.Write(nameBytes); |
|||
int pad = 8 - (nameBytes.Length % 8); |
|||
PadData(writer, pad); |
|||
} |
|||
else |
|||
{ |
|||
writer.Write((short)DataType.Int8); |
|||
writer.Write((short)nameBytes.Length); |
|||
writer.Write(nameBytes); |
|||
PadData(writer, 4 - nameBytes.Length); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Compresses the data array.
|
|||
/// </summary>
|
|||
/// <param name="data">The data to compress.</param>
|
|||
/// <returns>The compressed data.</returns>
|
|||
private static byte[] CompressData(byte[] data) |
|||
{ |
|||
var adler = BitConverter.GetBytes(Adler32.Compute(data)); |
|||
using (var compressedStream = new MemoryStream()) |
|||
{ |
|||
compressedStream.WriteByte(0x58); |
|||
compressedStream.WriteByte(0x85); |
|||
using (var outputStream = new DeflateStream(compressedStream, CompressionMode.Compress, true)) |
|||
{ |
|||
outputStream.Write(data, 0, data.Length); |
|||
} |
|||
compressedStream.WriteByte(adler[3]); |
|||
compressedStream.WriteByte(adler[2]); |
|||
compressedStream.WriteByte(adler[1]); |
|||
compressedStream.WriteByte(adler[0]); |
|||
return compressedStream.ToArray(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the dense data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetDenseDataArray(Matrix<double> matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Double, false, name, matrix.RowCount, matrix.ColumnCount, 0); |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Double); |
|||
dataWriter.Write(matrix.RowCount * matrix.ColumnCount * 8); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
var column = matrix.Column(j); |
|||
foreach (var value in column) |
|||
{ |
|||
dataWriter.Write(value); |
|||
} |
|||
} |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the dense data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetDenseDataArray(Matrix<float> matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Single, false, name, matrix.RowCount, matrix.ColumnCount, 0); |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Single); |
|||
|
|||
dataWriter.Write(matrix.RowCount * matrix.ColumnCount * 4); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
var column = matrix.Column(j); |
|||
foreach (var value in column) |
|||
{ |
|||
dataWriter.Write(value); |
|||
} |
|||
} |
|||
|
|||
var pad = (matrix.RowCount * matrix.ColumnCount * 4) % 8; |
|||
PadData(dataWriter, pad); |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the dense data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetDenseDataArray(Matrix<System.Numerics.Complex> matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Double, true, name, matrix.RowCount, matrix.ColumnCount, 0); |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Double); |
|||
dataWriter.Write(matrix.RowCount * matrix.ColumnCount * 8); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
var column = matrix.Column(j); |
|||
foreach (var value in column) |
|||
{ |
|||
dataWriter.Write(value.Real); |
|||
} |
|||
} |
|||
|
|||
dataWriter.Write((int)DataType.Double); |
|||
dataWriter.Write(matrix.RowCount * matrix.ColumnCount * 8); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
var column = matrix.Column(j); |
|||
foreach (var value in column) |
|||
{ |
|||
dataWriter.Write(value.Imaginary); |
|||
} |
|||
} |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the dense data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetDenseDataArray(Matrix<Numerics.Complex32> matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Single, true, name, matrix.RowCount, matrix.ColumnCount, 0); |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Single); |
|||
dataWriter.Write(matrix.RowCount * matrix.ColumnCount * 4); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
var column = matrix.Column(j); |
|||
foreach (var value in column) |
|||
{ |
|||
dataWriter.Write(value.Real); |
|||
} |
|||
} |
|||
|
|||
var pad = (matrix.RowCount * matrix.ColumnCount * 4) % 8; |
|||
PadData(dataWriter, pad); |
|||
|
|||
dataWriter.Write((int)DataType.Single); |
|||
dataWriter.Write(matrix.RowCount * matrix.ColumnCount * 4); |
|||
|
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
var column = matrix.Column(j); |
|||
foreach (var value in column) |
|||
{ |
|||
dataWriter.Write(value.Real); |
|||
} |
|||
} |
|||
|
|||
PadData(dataWriter, pad); |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the sparse data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetSparseDataArray(Double.SparseMatrix matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
var nzmax = matrix.NonZerosCount; |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Sparse, false, name, matrix.RowCount, matrix.ColumnCount, nzmax); |
|||
|
|||
// write ir
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item1); |
|||
} |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (nzmax % 2 == 1) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write jc
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write((matrix.ColumnCount + 1) * 4); |
|||
dataWriter.Write(0); |
|||
int count = 0; |
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
count += ((SparseVectorStorage<double>)column.Item2.Storage).ValueCount; |
|||
dataWriter.Write(count); |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (matrix.ColumnCount % 2 == 0) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Double); |
|||
dataWriter.Write(nzmax * 8); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item2); |
|||
} |
|||
} |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the sparse data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetSparseDataArray(Single.SparseMatrix matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
var nzmax = matrix.NonZerosCount; |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Sparse, false, name, matrix.RowCount, matrix.ColumnCount, nzmax); |
|||
|
|||
// write ir
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item1); |
|||
} |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (nzmax % 2 == 1) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write jc
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write((matrix.ColumnCount + 1) * 4); |
|||
dataWriter.Write(0); |
|||
int count = 0; |
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
count += ((SparseVectorStorage<float>)column.Item2.Storage).ValueCount; |
|||
dataWriter.Write(count); |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (matrix.ColumnCount % 2 == 0) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Single); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item2); |
|||
} |
|||
} |
|||
|
|||
var pad = (nzmax * 4) % 8; |
|||
PadData(dataWriter, pad); |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the sparse data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetSparseDataArray(Complex.SparseMatrix matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
var nzmax = matrix.NonZerosCount; |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Sparse, true, name, matrix.RowCount, matrix.ColumnCount, nzmax); |
|||
|
|||
// write ir
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item1); |
|||
} |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (nzmax % 2 == 1) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write jc
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write((matrix.ColumnCount + 1) * 4); |
|||
dataWriter.Write(0); |
|||
int count = 0; |
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
count += ((SparseVectorStorage<System.Numerics.Complex>)column.Item2.Storage).ValueCount; |
|||
dataWriter.Write(count); |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (matrix.ColumnCount % 2 == 0) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Double); |
|||
dataWriter.Write(nzmax * 8); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item2.Real); |
|||
} |
|||
} |
|||
|
|||
dataWriter.Write((int)DataType.Double); |
|||
dataWriter.Write(nzmax * 8); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item2.Real); |
|||
} |
|||
} |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the sparse data array.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to get the data from.</param>
|
|||
/// <param name="name">The name of the matrix.</param>
|
|||
/// <returns>The matrix data as an array.</returns>
|
|||
private static byte[] GetSparseDataArray(Complex32.SparseMatrix matrix, string name) |
|||
{ |
|||
byte[] data; |
|||
using (var dataMemoryStream = new MemoryStream()) |
|||
using (var dataWriter = new BinaryWriter(dataMemoryStream)) |
|||
{ |
|||
var nzmax = matrix.NonZerosCount; |
|||
WriteMatrixTagAndName(dataWriter, ArrayClass.Sparse, true, name, matrix.RowCount, matrix.ColumnCount, nzmax); |
|||
|
|||
// write ir
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item1); |
|||
} |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (nzmax % 2 == 1) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write jc
|
|||
dataWriter.Write((int)DataType.Int32); |
|||
dataWriter.Write((matrix.ColumnCount + 1) * 4); |
|||
dataWriter.Write(0); |
|||
int count = 0; |
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
count += ((SparseVectorStorage<Numerics.Complex32>)column.Item2.Storage).ValueCount; |
|||
dataWriter.Write(count); |
|||
} |
|||
|
|||
// add pad if needed
|
|||
if (matrix.ColumnCount % 2 == 0) |
|||
{ |
|||
dataWriter.Write(0); |
|||
} |
|||
|
|||
// write data
|
|||
dataWriter.Write((int)DataType.Single); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item2.Real); |
|||
} |
|||
} |
|||
|
|||
var pad = (nzmax * 4) % 8; |
|||
PadData(dataWriter, pad); |
|||
|
|||
dataWriter.Write((int)DataType.Single); |
|||
dataWriter.Write(nzmax * 4); |
|||
|
|||
foreach (var column in matrix.ColumnEnumerator()) |
|||
{ |
|||
foreach (var row in column.Item2.GetIndexedEnumerator()) |
|||
{ |
|||
dataWriter.Write(row.Item2.Real); |
|||
} |
|||
} |
|||
|
|||
PadData(dataWriter, pad); |
|||
|
|||
data = dataMemoryStream.ToArray(); |
|||
} |
|||
|
|||
return data; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the compressed data.
|
|||
/// </summary>
|
|||
/// <param name="data">The data to write.</param>
|
|||
private void WriteCompressedData(byte[] data) |
|||
{ |
|||
// fill in data size
|
|||
var size = BitConverter.GetBytes(data.Length); |
|||
data[4] = size[0]; |
|||
data[5] = size[1]; |
|||
data[6] = size[2]; |
|||
data[7] = size[3]; |
|||
|
|||
// compress data
|
|||
var compressedData = CompressData(data); |
|||
|
|||
// write compressed data to file
|
|||
_writer.Write(compressedData.Length); |
|||
_writer.Write(compressedData); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the file header.
|
|||
/// </summary>
|
|||
private void WriteHeader() |
|||
{ |
|||
var header = Encoding.ASCII.GetBytes(HeaderText + DateTime.Now.ToString(Resources.MatlabDateHeaderFormat)); |
|||
_writer.Write(header); |
|||
PadData(_writer, HeaderTextLength - header.Length + 8, 32); |
|||
|
|||
// write version
|
|||
_writer.Write((short)0x100); |
|||
|
|||
// write little endian indicator
|
|||
_writer.Write((byte)0x49); |
|||
_writer.Write((byte)0x4D); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Pads the data with the given byte.
|
|||
/// </summary>
|
|||
/// <param name="writer">Where to write the pad values.</param>
|
|||
/// <param name="bytes">The number of bytes to pad.</param>
|
|||
/// <param name="pad">What value to pad with.</param>
|
|||
private static void PadData(BinaryWriter writer, int bytes, byte pad = (byte)0) |
|||
{ |
|||
for (int i = 0; i < bytes; i++) |
|||
{ |
|||
writer.Write(pad); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,88 +0,0 @@ |
|||
// <copyright file="MatrixReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using Generic; |
|||
using Properties; |
|||
|
|||
/// <summary>
|
|||
/// Base class to read a single <see cref="Matrix{T}"/> from a file or stream.
|
|||
/// </summary>
|
|||
/// <typeparam name="TMatrix">The type of Matrix to return.</typeparam>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public abstract class MatrixReader<TMatrix, TDataType> |
|||
where TMatrix : Matrix<TDataType> |
|||
where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
/// <summary>
|
|||
/// Reads a <see cref="Matrix{T}"/> from a file.
|
|||
/// </summary>
|
|||
/// <param name="file">The file to read the matrix from.</param>
|
|||
/// <returns>A <see cref="Matrix{T}"/> containing the data from the file. <see langword="null" /> is returned if the file is empty.</returns>
|
|||
/// <exception cref="ArgumentNullException">If <paramref name="file"/> is <see langword="null" />.</exception>
|
|||
/// <exception cref="IOException">If the file doesn't exist.</exception>
|
|||
/// <exception cref="FormatException">If a value is not a number or not in a valid format.</exception>
|
|||
/// <exception cref="OverflowException">If a value represents a number less than <see cref="Double.MinValue"/> or greater than <see cref="Double.MaxValue"/>.</exception>
|
|||
public TMatrix ReadMatrix(string file) |
|||
{ |
|||
if (file == null) |
|||
{ |
|||
throw new ArgumentNullException("file", Resources.StringNullOrEmpty); |
|||
} |
|||
|
|||
return ReadMatrix(File.OpenRead(file)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a <see cref="Matrix{T}"/> from a <see cref="Stream"/>.
|
|||
/// </summary>
|
|||
/// <param name="stream">The <see cref="Stream"/> to read the matrix from.</param>
|
|||
/// <returns>A matrix containing the data from the <see cref="Stream"/>. <see langword="null" /> is returned if the <see cref="Stream"/> is empty.</returns>
|
|||
/// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <see langword="null" />.</exception>
|
|||
/// <exception cref="FormatException">If a value is not a number or not in a valid format.</exception>
|
|||
/// <exception cref="OverflowException">If a value represents a number less than <see cref="Double.MinValue"/> or greater than <see cref="Double.MaxValue"/>.</exception>
|
|||
public TMatrix ReadMatrix(Stream stream) |
|||
{ |
|||
if (stream == null) |
|||
{ |
|||
throw new ArgumentNullException("stream"); |
|||
} |
|||
|
|||
return DoReadMatrix(stream); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Subclasses override this method to do the actual reading.
|
|||
/// </summary>
|
|||
/// <param name="stream">The <see cref="Stream"/> to read the matrix from.</param>
|
|||
/// <returns>A matrix containing the data from the <see cref="Stream"/>. <see langword="null" /> is returned if the <see cref="Stream"/> is empty.</returns>
|
|||
protected abstract TMatrix DoReadMatrix(Stream stream); |
|||
} |
|||
} |
|||
@ -1,164 +0,0 @@ |
|||
// <copyright file="MatrixWriter.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.IO |
|||
{ |
|||
using System.Globalization; |
|||
using Generic; |
|||
|
|||
/// <summary>
|
|||
/// Base class to write a single <see cref="Matrix{DataType}"/> to a file or stream.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public abstract class MatrixWriter |
|||
{ |
|||
/// <summary>
|
|||
/// The <see cref="CultureInfo"/> to use.
|
|||
/// </summary>
|
|||
private CultureInfo _cultureInfo = CultureInfo.CurrentCulture; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the <see cref="CultureInfo"/> to use when parsing the numbers.
|
|||
/// </summary>
|
|||
/// <value>The culture info.</value>
|
|||
/// <remarks>Defaults to <c>CultureInfo.CurrentCulture</c>.</remarks>
|
|||
/// <remarks>This property is only used for matrix writers that write out text files.</remarks>
|
|||
public CultureInfo CultureInfo |
|||
{ |
|||
get |
|||
{ |
|||
return _cultureInfo; |
|||
} |
|||
|
|||
set |
|||
{ |
|||
if (value != null) |
|||
{ |
|||
_cultureInfo = value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets he number format to use.
|
|||
/// </summary>
|
|||
/// <value>The number format to use when writing out each element.</value>
|
|||
/// <remarks>This property is only used for matrix writers that write out text files.</remarks>
|
|||
public string Format |
|||
{ |
|||
get; |
|||
set; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the given <see cref="Matrix{DataType}"/> to the given file. If the file already exists,
|
|||
/// the file will be overwritten.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to write.</param>
|
|||
/// <param name="file">The file to write the matrix to.</param>
|
|||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is <c>null</c>.</exception>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, string file) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
if (matrix == null) |
|||
{ |
|||
throw new ArgumentNullException("matrix"); |
|||
} |
|||
|
|||
if (file == null) |
|||
{ |
|||
throw new ArgumentNullException("file"); |
|||
} |
|||
|
|||
using (var writer = new StreamWriter(file)) |
|||
{ |
|||
DoWriteMatrix(matrix, writer, Format, _cultureInfo); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the given <see cref="Matrix{DataType}"/> to the given stream.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to write.</param>
|
|||
/// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
|
|||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is <c>null</c>.</exception>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, Stream stream) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
if (matrix == null) |
|||
{ |
|||
throw new ArgumentNullException("matrix"); |
|||
} |
|||
|
|||
if (stream == null) |
|||
{ |
|||
throw new ArgumentNullException("stream"); |
|||
} |
|||
|
|||
using (var writer = new StreamWriter(stream)) |
|||
{ |
|||
DoWriteMatrix(matrix, writer, Format, _cultureInfo); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Writes the given <see cref="Matrix{DataType}"/> to the given <see cref="TextWriter"/>.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The matrix to write.</param>
|
|||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is <c>null</c>.</exception>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, TextWriter writer) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|||
{ |
|||
if (matrix == null) |
|||
{ |
|||
throw new ArgumentNullException("matrix"); |
|||
} |
|||
|
|||
if (writer == null) |
|||
{ |
|||
throw new ArgumentNullException("writer"); |
|||
} |
|||
|
|||
DoWriteMatrix(matrix, writer, Format, _cultureInfo); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Subclasses must implement this method to do the actually writing.
|
|||
/// </summary>
|
|||
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|||
/// <param name="matrix">The matrix to serialize.</param>
|
|||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|||
/// <param name="format">The number format to use.</param>
|
|||
/// <param name="cultureInfo">The culture to use.</param>
|
|||
protected abstract void DoWriteMatrix<TDataType>(Matrix<TDataType> matrix, TextWriter writer, string format, CultureInfo cultureInfo) where TDataType : struct, IEquatable<TDataType>, IFormattable; |
|||
} |
|||
} |
|||
@ -1,71 +0,0 @@ |
|||
// <copyright file="DelimitedReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Single.IO |
|||
{ |
|||
using System; |
|||
using Generic; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates a <see cref="Matrix{T}"/> from a delimited text file. If the user does not
|
|||
/// specify a delimiter, then any whitespace is used.
|
|||
/// </summary>
|
|||
/// <typeparam name="TMatrix">The type of the matrix to return.</typeparam>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class DelimitedReader<TMatrix> : DelimitedReader<TMatrix, float> |
|||
where TMatrix : Matrix<float> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class using
|
|||
/// any whitespace as the delimiter.
|
|||
/// </summary>
|
|||
public DelimitedReader() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">The delimiter to use.</param>
|
|||
public DelimitedReader(char delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DelimitedReader{TMatrix}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="delimiter">
|
|||
/// The delimiter to use.
|
|||
/// </param>
|
|||
/// <exception cref="ArgumentNullException">
|
|||
/// If <paramref name="delimiter"/> is <see langword="null"/>.
|
|||
/// </exception>
|
|||
public DelimitedReader(string delimiter) : base(delimiter) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
// <copyright file="MatlabReader.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Single.IO |
|||
{ |
|||
using System.IO; |
|||
using LinearAlgebra.IO; |
|||
|
|||
/// <summary>
|
|||
/// Creates matrices from Matlab files.
|
|||
/// </summary>
|
|||
[Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] |
|||
public class MatlabMatrixReader : MatlabMatrixReader<float> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="filename">Name of the file to read matrices from.</param>
|
|||
public MatlabMatrixReader(string filename) : base(filename) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatlabMatrixReader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream to reader matrices from.</param>
|
|||
public MatlabMatrixReader(Stream stream) : base(stream) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,101 +0,0 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<PropertyGroup> |
|||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|||
<ProductVersion>8.0.30703</ProductVersion> |
|||
<SchemaVersion>2.0</SchemaVersion> |
|||
<ProjectGuid>{EB1A5D32-F264-4BCE-BEB7-0B97085075BE}</ProjectGuid> |
|||
<OutputType>Library</OutputType> |
|||
<AppDesignerFolder>Properties</AppDesignerFolder> |
|||
<RootNamespace>MathNet.Numerics</RootNamespace> |
|||
<AssemblyName>MathNet.Numerics.IO</AssemblyName> |
|||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> |
|||
<FileAlignment>512</FileAlignment> |
|||
<!-- Conditional Strong Name --> |
|||
<AssemblyOriginatorKeyFile>..\MathNet.Numerics.snk</AssemblyOriginatorKeyFile> |
|||
<SignAssembly Condition=" '$(SignAssembly)' == '' ">false</SignAssembly> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|||
<OutputPath>..\..\out\debug\Net40\</OutputPath> |
|||
<!-- Conditional Strong Name: NO --> |
|||
<SignAssembly>false</SignAssembly> |
|||
<DebugSymbols>true</DebugSymbols> |
|||
<DebugType>full</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<DefineConstants>DEBUG;TRACE;SYSNUMERICS</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|||
<OutputPath>..\..\out\lib\Net40\</OutputPath> |
|||
<DocumentationFile>..\..\out\lib\Net40\MathNet.Numerics.IO.xml</DocumentationFile> |
|||
<!-- Conditional Strong Name: NO --> |
|||
<SignAssembly>false</SignAssembly> |
|||
<DebugType>pdbonly</DebugType> |
|||
<Optimize>true</Optimize> |
|||
<DefineConstants>TRACE;SYSNUMERICS</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Signed|AnyCPU'"> |
|||
<OutputPath>..\..\out\lib\Net40\</OutputPath> |
|||
<DocumentationFile>..\..\out\lib\Net40\MathNet.Numerics.IO.xml</DocumentationFile> |
|||
<!-- Conditional Strong Name: YES --> |
|||
<SignAssembly>true</SignAssembly> |
|||
<DebugType>pdbonly</DebugType> |
|||
<Optimize>true</Optimize> |
|||
<DefineConstants>TRACE;SYSNUMERICS;STRONGNAME</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> |
|||
</PropertyGroup> |
|||
<ItemGroup> |
|||
<Reference Include="System" /> |
|||
<Reference Include="System.Core" /> |
|||
<Reference Include="System.Numerics" /> |
|||
<Reference Include="System.Xml.Linq" /> |
|||
<Reference Include="System.Data.DataSetExtensions" /> |
|||
<Reference Include="Microsoft.CSharp" /> |
|||
<Reference Include="System.Data" /> |
|||
<Reference Include="System.Xml" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Compile Include="LinearAlgebra\Complex32\IO\DelimitedReader.cs" /> |
|||
<Compile Include="LinearAlgebra\Complex32\IO\MatlabReader.cs" /> |
|||
<Compile Include="LinearAlgebra\Complex\IO\DelimitedReader.cs" /> |
|||
<Compile Include="LinearAlgebra\Complex\IO\MatlabReader.cs" /> |
|||
<Compile Include="LinearAlgebra\Double\IO\DelimitedReader.cs" /> |
|||
<Compile Include="LinearAlgebra\Double\IO\MatlabReader.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\DelimitedReader.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\DelimitedWriter.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\MatlabReader.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\MatlabWriter.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\Matlab\Adler32.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\Matlab\ArrayClass.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\Matlab\ArrayFlags.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\Matlab\DataType.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\Matlab\MatlabFile.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\Matlab\MatlabParser.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\MatrixReader.cs" /> |
|||
<Compile Include="LinearAlgebra\IO\MatrixWriter.cs" /> |
|||
<Compile Include="LinearAlgebra\Single\IO\DelimitedReader.cs" /> |
|||
<Compile Include="LinearAlgebra\Single\IO\MatlabReader.cs" /> |
|||
<Compile Include="Properties\AssemblyInfo.cs" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Numerics\Numerics.csproj"> |
|||
<Project>{B7CAE5F4-A23F-4438-B5BE-41226618B695}</Project> |
|||
<Name>Numerics</Name> |
|||
</ProjectReference> |
|||
</ItemGroup> |
|||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
|||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
|||
Other similar extension points exist, see Microsoft.Common.targets. |
|||
<Target Name="BeforeBuild"> |
|||
</Target> |
|||
<Target Name="AfterBuild"> |
|||
</Target> |
|||
--> |
|||
</Project> |
|||
@ -1,53 +0,0 @@ |
|||
// <copyright file="AssemblyInfo.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://mathnet.opensourcedotnet.info
|
|||
//
|
|||
// Copyright (c) 2009 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
using System; |
|||
using System.Reflection; |
|||
using System.Resources; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
[assembly: AssemblyTitle("Math.NET Numerics IO")] |
|||
[assembly: AssemblyDescription("Math.NET Numerics, providing methods and algorithms for numerical computations in science, engineering and every day use.")] |
|||
[assembly: AssemblyConfiguration("")] |
|||
[assembly: AssemblyCompany("Math.NET Project")] |
|||
[assembly: AssemblyProduct("Math.NET Numerics")] |
|||
[assembly: AssemblyCopyright("Copyright © Math.NET Project")] |
|||
[assembly: AssemblyTrademark("")] |
|||
[assembly: AssemblyCulture("")] |
|||
[assembly: CLSCompliant(true)] |
|||
[assembly: ComVisible(false)] |
|||
[assembly: Guid("5d559feb-5f06-4401-b48b-c08b78f8582a")] |
|||
[assembly: NeutralResourcesLanguage("en")] |
|||
#if STRONGNAME
|
|||
[assembly: InternalsVisibleTo("MathNet.Numerics.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100ed2314a577643d859571b8b9307c6ff2670525c4598fbb307e57ea65ebf5d4417284cb3da9181636480b623f4db8cc3c1947244ba069df0df86e2431621f51a488f9929519a1c5d0ae595f6e2d0e4094685f0c1229ff658360acbb9f63f1a0258e984dda00dc7ad4fd16dbb550ec1ef8a11df138402b7c1998ee224e652c839b")] |
|||
#else
|
|||
[assembly: InternalsVisibleTo("MathNet.Numerics.UnitTests")] |
|||
#endif
|
|||
[assembly: AssemblyVersion("1.0.0.0")] |
|||
[assembly: AssemblyFileVersion("1.0.0.0")] |
|||
@ -1,167 +0,0 @@ |
|||
// <copyright file="DelimitedReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Numerics; |
|||
using System.Text; |
|||
using LinearAlgebra.Complex; |
|||
using LinearAlgebra.Complex.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Delimited reader tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can parse comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseCommaDelimitedData() |
|||
{ |
|||
var data = "a,b,c" + Environment.NewLine |
|||
+ "(1,2)" + Environment.NewLine |
|||
+ "\"2.2\",0.3e1" + Environment.NewLine |
|||
+ "'(4,-5)',5,6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>(',') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0, matrix[0, 0].Real); |
|||
Assert.AreEqual(2.0, matrix[0, 0].Imaginary); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 2]); |
|||
Assert.AreEqual((Complex)2.2, matrix[1, 0]); |
|||
Assert.AreEqual((Complex)3.0, matrix[1, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[1, 2]); |
|||
Assert.AreEqual(4.0, matrix[2, 0].Real); |
|||
Assert.AreEqual(-5.0, matrix[2, 0].Imaginary); |
|||
Assert.AreEqual((Complex)5.0, matrix[2, 1]); |
|||
Assert.AreEqual((Complex)6.0, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseTabDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"2.2\"\t\t0.3e1" + Environment.NewLine |
|||
+ "'4'\t5\t6"; |
|||
|
|||
var reader = new DelimitedReader<SparseMatrix>('\t') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual((Complex)1.0, matrix[0, 0]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 2]); |
|||
Assert.AreEqual((Complex)2.2, matrix[1, 0]); |
|||
Assert.AreEqual((Complex)3.0, matrix[1, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[1, 2]); |
|||
Assert.AreEqual((Complex)4.0, matrix[2, 0]); |
|||
Assert.AreEqual((Complex)5.0, matrix[2, 1]); |
|||
Assert.AreEqual((Complex)6.0, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse white space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseWhiteSpaceDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"(2.2,3.3)\" 0.3e1" + Environment.NewLine |
|||
+ "'4' 5 6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<UserDefinedMatrix> |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual((Complex)1.0, matrix[0, 0]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 2]); |
|||
Assert.AreEqual(2.2, matrix[1, 0].Real); |
|||
Assert.AreEqual(3.3, matrix[1, 0].Imaginary); |
|||
Assert.AreEqual((Complex)3.0, matrix[1, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[1, 2]); |
|||
Assert.AreEqual((Complex)4.0, matrix[2, 0]); |
|||
Assert.AreEqual((Complex)5.0, matrix[2, 1]); |
|||
Assert.AreEqual((Complex)6.0, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParsePeriodDelimitedData() |
|||
{ |
|||
var data = "a.b.c" + Environment.NewLine |
|||
+ "1" + Environment.NewLine |
|||
+ "\"2,2\".0,3e1+0,2e1i" + Environment.NewLine |
|||
+ "'4,0'.5,0.6,0" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>('.') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = new CultureInfo("tr-TR") |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual((Complex)1.0, matrix[0, 0]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 1]); |
|||
Assert.AreEqual((Complex)0.0, matrix[0, 2]); |
|||
Assert.AreEqual((Complex)2.2, matrix[1, 0]); |
|||
Assert.AreEqual(3.0, matrix[1, 1].Real); |
|||
Assert.AreEqual(2.0, matrix[1, 1].Imaginary); |
|||
Assert.AreEqual((Complex)0.0, matrix[1, 2]); |
|||
Assert.AreEqual((Complex)4.0, matrix[2, 0]); |
|||
Assert.AreEqual((Complex)5.0, matrix[2, 1]); |
|||
Assert.AreEqual((Complex)6.0, matrix[2, 2]); |
|||
} |
|||
} |
|||
} |
|||
@ -1,139 +0,0 @@ |
|||
// <copyright file="DelimitedWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO |
|||
{ |
|||
using LinearAlgebra.Complex; |
|||
using LinearAlgebra.IO; |
|||
using NUnit.Framework; |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Numerics; |
|||
|
|||
/// <summary>
|
|||
/// Delimited writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can write comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteCommaDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); |
|||
var writer = new DelimitedWriter(',') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)" + Environment.NewLine |
|||
+ "(4.4, 4.4),(5.5, 5.5),(6.6, 6.6)" + Environment.NewLine |
|||
+ "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWritePeriodDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); |
|||
var culture = new CultureInfo("tr-TR"); |
|||
var writer = new DelimitedWriter('.') |
|||
{ |
|||
CultureInfo = culture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)" + Environment.NewLine |
|||
+ "(4,4, 4,4).(5,5, 5,5).(6,6, 6,6)" + Environment.NewLine |
|||
+ "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteSpaceDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); |
|||
var writer = new DelimitedWriter(' ') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)" + Environment.NewLine |
|||
+ "(4.4, 4.4) (5.5, 5.5) (6.6, 6.6)" + Environment.NewLine |
|||
+ "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteTabDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex(1.1, 1.1), new Complex(2.2, 2.2), new Complex(3.3, 3.3)}, {new Complex(4.4, 4.4), new Complex(5.5, 5.5), new Complex(6.6, 6.6)}, {new Complex(7.7, 7.7), new Complex(8.8, 8.8), new Complex(9.9, 9.9)}}); |
|||
var headers = new[] {"a", "b", "c"}; |
|||
var writer = new DelimitedWriter('\t') |
|||
{ |
|||
ColumnHeaders = headers, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "a\tb\tc" + Environment.NewLine |
|||
+ "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + Environment.NewLine |
|||
+ "(4.4, 4.4)\t(5.5, 5.5)\t(6.6, 6.6)" + Environment.NewLine |
|||
+ "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
} |
|||
} |
|||
@ -1,155 +0,0 @@ |
|||
// <copyright file="MatlabReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO |
|||
{ |
|||
using LinearAlgebra.Complex; |
|||
using LinearAlgebra.Complex.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix reader test.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can read all complex matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadComplexAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/complex.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(3, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
|
|||
var a = matrices["a"]; |
|||
|
|||
Assert.AreEqual(100, a.RowCount); |
|||
Assert.AreEqual(100, a.ColumnCount); |
|||
AssertHelpers.AlmostEqual(27.232498979698409, a.L2Norm(), 15); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read sparse complex matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadSparseComplexAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/sparse_complex.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(3, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(SparseMatrix), matrix.Value.GetType()); |
|||
} |
|||
|
|||
var a = matrices["sa"]; |
|||
|
|||
Assert.AreEqual(100, a.RowCount); |
|||
Assert.AreEqual(100, a.ColumnCount); |
|||
AssertHelpers.AlmostEqual(13.223654390985379, a.L2Norm(), 15); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(30, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex first matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexFirstMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); |
|||
var matrix = dmr.ReadMatrix(); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(100.108979553704, matrix.FrobeniusNorm(), 13); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex named matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexNamedMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); |
|||
Assert.AreEqual(2, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex named matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexNamedMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad" }); |
|||
Assert.AreEqual(1, matrices.Count); |
|||
var ad = matrices["Ad"]; |
|||
Assert.AreEqual(100, ad.RowCount); |
|||
Assert.AreEqual(100, ad.ColumnCount); |
|||
AssertHelpers.AlmostEqual(100.431635988639, ad.FrobeniusNorm(), 13); |
|||
Assert.AreEqual(typeof(DenseMatrix), ad.GetType()); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex named sparse matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexNamedSparseMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); |
|||
var matrix = dmr.ReadMatrix("S"); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(SparseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(17.6385090630805, matrix.FrobeniusNorm(), 12); |
|||
} |
|||
} |
|||
} |
|||
@ -1,161 +0,0 @@ |
|||
// <copyright file="MatlabWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using System.Numerics; |
|||
using LinearAlgebra.Complex; |
|||
using LinearAlgebra.Complex.IO; |
|||
using LinearAlgebra.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Invalid constructor throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void InvalidConstructorThrowsArgumentException() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(null)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrices throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatricesThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile3"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrices throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatricesThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile4"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new[] { matrix }, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteMatrices() |
|||
{ |
|||
Matrix mat1 = new DenseMatrix(5, 3); |
|||
for (var i = 0; i < mat1.ColumnCount; i++) |
|||
{ |
|||
mat1[i, i] = new Complex(i + .1, i + .1); |
|||
} |
|||
|
|||
Matrix mat2 = new DenseMatrix(4, 5); |
|||
for (var i = 0; i < mat2.RowCount; i++) |
|||
{ |
|||
mat2[i, i] = new Complex(i + .1, i + .1); |
|||
} |
|||
|
|||
Matrix mat3 = new SparseMatrix(5, 4); |
|||
mat3[0, 0] = new Complex(1.1, 1.1); |
|||
mat3[0, 2] = new Complex(2.2, 2.2); |
|||
mat3[4, 3] = new Complex(3.3, 3.3); |
|||
|
|||
Matrix mat4 = new SparseMatrix(3, 5); |
|||
mat4[0, 0] = new Complex(1.1, 1.1); |
|||
mat4[0, 2] = new Complex(2.2, 2.2); |
|||
mat4[2, 4] = new Complex(3.3, 3.3); |
|||
|
|||
var write = new[] { mat1, mat2, mat3, mat4 }; |
|||
|
|||
var names = new[] { "mat1", "dense_matrix_2", "s1", "sparse2" }; |
|||
if (File.Exists("test.mat")) |
|||
{ |
|||
File.Delete("test.mat"); |
|||
} |
|||
|
|||
var writer = new MatlabMatrixWriter("test.mat"); |
|||
writer.WriteMatrices(write, names); |
|||
writer.Dispose(); |
|||
|
|||
var reader = new MatlabMatrixReader("test.mat"); |
|||
var read = reader.ReadMatrices(names); |
|||
|
|||
Assert.AreEqual(write.Length, read.Count); |
|||
|
|||
for (var i = 0; i < write.Length; i++) |
|||
{ |
|||
var w = write[i]; |
|||
var r = read[names[i]]; |
|||
|
|||
Assert.AreEqual(w.RowCount, r.RowCount); |
|||
Assert.AreEqual(w.ColumnCount, r.ColumnCount); |
|||
Assert.IsTrue(w.Equals(r)); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrix throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatrixThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile1"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrix throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatrixThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile2"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrix<double>(null, "matrix")); |
|||
writer.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,167 +0,0 @@ |
|||
// <copyright file="DelimitedReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Text; |
|||
using LinearAlgebra.Complex32; |
|||
using LinearAlgebra.Complex32.IO; |
|||
using NUnit.Framework; |
|||
using Complex32 = Numerics.Complex32; |
|||
|
|||
/// <summary>
|
|||
/// Delimited reader tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can parse comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseCommaDelimitedData() |
|||
{ |
|||
var data = "a,b,c" + Environment.NewLine |
|||
+ "(1,2)" + Environment.NewLine |
|||
+ "\"2.2\",0.3e1" + Environment.NewLine |
|||
+ "'(4,-5)',5,6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>(',') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0f, matrix[0, 0].Real); |
|||
Assert.AreEqual(2.0f, matrix[0, 0].Imaginary); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 2]); |
|||
Assert.AreEqual((Complex32)2.2f, matrix[1, 0]); |
|||
Assert.AreEqual((Complex32)3.0f, matrix[1, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[1, 2]); |
|||
Assert.AreEqual(4.0f, matrix[2, 0].Real); |
|||
Assert.AreEqual(-5.0f, matrix[2, 0].Imaginary); |
|||
Assert.AreEqual((Complex32)5.0f, matrix[2, 1]); |
|||
Assert.AreEqual((Complex32)6.0f, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseTabDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"2.2\"\t\t0.3e1" + Environment.NewLine |
|||
+ "'4'\t5\t6"; |
|||
|
|||
var reader = new DelimitedReader<SparseMatrix>('\t') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual((Complex32)1.0f, matrix[0, 0]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 2]); |
|||
Assert.AreEqual((Complex32)2.2f, matrix[1, 0]); |
|||
Assert.AreEqual((Complex32)3.0f, matrix[1, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[1, 2]); |
|||
Assert.AreEqual((Complex32)4.0f, matrix[2, 0]); |
|||
Assert.AreEqual((Complex32)5.0f, matrix[2, 1]); |
|||
Assert.AreEqual((Complex32)6.0f, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse white space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseWhiteSpaceDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"(2.2,3.3)\" 0.3e1" + Environment.NewLine |
|||
+ "'4' 5 6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<UserDefinedMatrix> |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual((Complex32)1.0f, matrix[0, 0]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 2]); |
|||
Assert.AreEqual(2.2f, matrix[1, 0].Real); |
|||
Assert.AreEqual(3.3f, matrix[1, 0].Imaginary); |
|||
Assert.AreEqual((Complex32)3.0f, matrix[1, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[1, 2]); |
|||
Assert.AreEqual((Complex32)4.0f, matrix[2, 0]); |
|||
Assert.AreEqual((Complex32)5.0f, matrix[2, 1]); |
|||
Assert.AreEqual((Complex32)6.0f, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParsePeriodDelimitedData() |
|||
{ |
|||
var data = "a.b.c" + Environment.NewLine |
|||
+ "1" + Environment.NewLine |
|||
+ "\"2,2\".0,3e1+0,2e1i" + Environment.NewLine |
|||
+ "'4,0'.5,0.6,0" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>('.') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = new CultureInfo("tr-TR") |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual((Complex32)1.0f, matrix[0, 0]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 1]); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[0, 2]); |
|||
Assert.AreEqual((Complex32)2.2f, matrix[1, 0]); |
|||
Assert.AreEqual(3.0f, matrix[1, 1].Real); |
|||
Assert.AreEqual(2.0f, matrix[1, 1].Imaginary); |
|||
Assert.AreEqual((Complex32)0.0f, matrix[1, 2]); |
|||
Assert.AreEqual((Complex32)4.0f, matrix[2, 0]); |
|||
Assert.AreEqual((Complex32)5.0f, matrix[2, 1]); |
|||
Assert.AreEqual((Complex32)6.0f, matrix[2, 2]); |
|||
} |
|||
} |
|||
} |
|||
@ -1,139 +0,0 @@ |
|||
// <copyright file="DelimitedWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO |
|||
{ |
|||
using LinearAlgebra.Complex32; |
|||
using LinearAlgebra.IO; |
|||
using NUnit.Framework; |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using Complex32 = Numerics.Complex32; |
|||
|
|||
/// <summary>
|
|||
/// Delimited writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can write comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteCommaDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); |
|||
var writer = new DelimitedWriter(',') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)" + Environment.NewLine |
|||
+ "(4.4, 4.4),(5.5, 5.5),(6.6, 6.6)" + Environment.NewLine |
|||
+ "(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWritePeriodDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); |
|||
var culture = new CultureInfo("tr-TR"); |
|||
var writer = new DelimitedWriter('.') |
|||
{ |
|||
CultureInfo = culture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)" + Environment.NewLine |
|||
+ "(4,4, 4,4).(5,5, 5,5).(6,6, 6,6)" + Environment.NewLine |
|||
+ "(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteSpaceDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); |
|||
var writer = new DelimitedWriter(' ') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)" + Environment.NewLine |
|||
+ "(4.4, 4.4) (5.5, 5.5) (6.6, 6.6)" + Environment.NewLine |
|||
+ "(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteTabDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{new Complex32(1.1f, 1.1f), new Complex32(2.2f, 2.2f), new Complex32(3.3f, 3.3f)}, {new Complex32(4.4f, 4.4f), new Complex32(5.5f, 5.5f), new Complex32(6.6f, 6.6f)}, {new Complex32(7.7f, 7.7f), new Complex32(8.8f, 8.8f), new Complex32(9.9f, 9.9f)}}); |
|||
var headers = new[] {"a", "b", "c"}; |
|||
var writer = new DelimitedWriter('\t') |
|||
{ |
|||
ColumnHeaders = headers, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "a\tb\tc" + Environment.NewLine |
|||
+ "(1.1, 1.1)\t(2.2, 2.2)\t(3.3, 3.3)" + Environment.NewLine |
|||
+ "(4.4, 4.4)\t(5.5, 5.5)\t(6.6, 6.6)" + Environment.NewLine |
|||
+ "(7.7, 7.7)\t(8.8, 8.8)\t(9.9, 9.9)"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
} |
|||
} |
|||
@ -1,155 +0,0 @@ |
|||
// <copyright file="MatlabReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO |
|||
{ |
|||
using LinearAlgebra.Complex32; |
|||
using LinearAlgebra.Complex32.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix reader test.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can read all complex matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadComplexAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/complex.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(3, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
|
|||
var a = matrices["a"]; |
|||
|
|||
Assert.AreEqual(100, a.RowCount); |
|||
Assert.AreEqual(100, a.ColumnCount); |
|||
AssertHelpers.AlmostEqual(27.232498979698409, a.L2Norm().Real, 6); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read spapse complex matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadSparseComplexAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/sparse_complex.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(3, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(SparseMatrix), matrix.Value.GetType()); |
|||
} |
|||
|
|||
var a = matrices["sa"]; |
|||
|
|||
Assert.AreEqual(100, a.RowCount); |
|||
Assert.AreEqual(100, a.ColumnCount); |
|||
AssertHelpers.AlmostEqual(13.223654390985379, a.L2Norm().Real, 5); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(30, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex first matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexFirstMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); |
|||
var matrix = dmr.ReadMatrix(); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(100.108979553704, matrix.FrobeniusNorm().Real, 6); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex named matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexNamedMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); |
|||
Assert.AreEqual(2, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex named matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexNamedMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad" }); |
|||
Assert.AreEqual(1, matrices.Count); |
|||
var ad = matrices["Ad"]; |
|||
Assert.AreEqual(100, ad.RowCount); |
|||
Assert.AreEqual(100, ad.ColumnCount); |
|||
AssertHelpers.AlmostEqual(100.431635988639, ad.FrobeniusNorm().Real, 6); |
|||
Assert.AreEqual(typeof(DenseMatrix), ad.GetType()); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read non-complex named sparse matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNonComplexNamedSparseMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); |
|||
var matrix = dmr.ReadMatrix("S"); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(SparseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(17.6385090630805, matrix.FrobeniusNorm().Real, 6); |
|||
} |
|||
} |
|||
} |
|||
@ -1,161 +0,0 @@ |
|||
// <copyright file="MatlabWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using LinearAlgebra.Complex32; |
|||
using LinearAlgebra.Complex32.IO; |
|||
using LinearAlgebra.IO; |
|||
using NUnit.Framework; |
|||
using Complex32 = Numerics.Complex32; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Invalid constructor throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void InvalidConstructorThrowsArgumentException() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(null)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrices throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatricesThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile3"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrices throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatricesThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile4"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new[] { matrix }, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteMatrices() |
|||
{ |
|||
Matrix mat1 = new DenseMatrix(5, 3); |
|||
for (var i = 0; i < mat1.ColumnCount; i++) |
|||
{ |
|||
mat1[i, i] = new Complex32(i + .1f, i + .1f); |
|||
} |
|||
|
|||
Matrix mat2 = new DenseMatrix(4, 5); |
|||
for (var i = 0; i < mat2.RowCount; i++) |
|||
{ |
|||
mat2[i, i] = new Complex32(i + .1f, i + .1f); |
|||
} |
|||
|
|||
Matrix mat3 = new SparseMatrix(5, 4); |
|||
mat3[0, 0] = new Complex32(1.1f, 1.1f); |
|||
mat3[0, 2] = new Complex32(2.2f, 2.2f); |
|||
mat3[4, 3] = new Complex32(3.3f, 3.3f); |
|||
|
|||
Matrix mat4 = new SparseMatrix(3, 5); |
|||
mat4[0, 0] = new Complex32(1.1f, 1.1f); |
|||
mat4[0, 2] = new Complex32(2.2f, 2.2f); |
|||
mat4[2, 4] = new Complex32(3.3f, 3.3f); |
|||
|
|||
var write = new[] { mat1, mat2, mat3, mat4 }; |
|||
|
|||
var names = new[] { "mat1", "dense_matrix_2", "s1", "sparse2" }; |
|||
if (File.Exists("test.mat")) |
|||
{ |
|||
File.Delete("test.mat"); |
|||
} |
|||
|
|||
var writer = new MatlabMatrixWriter("test.mat"); |
|||
writer.WriteMatrices(write, names); |
|||
writer.Dispose(); |
|||
|
|||
var reader = new MatlabMatrixReader("test.mat"); |
|||
var read = reader.ReadMatrices(names); |
|||
|
|||
Assert.AreEqual(write.Length, read.Count); |
|||
|
|||
for (var i = 0; i < write.Length; i++) |
|||
{ |
|||
var w = write[i]; |
|||
var r = read[names[i]]; |
|||
|
|||
Assert.AreEqual(w.RowCount, r.RowCount); |
|||
Assert.AreEqual(w.ColumnCount, r.ColumnCount); |
|||
Assert.IsTrue(w.Equals(r)); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrix throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatrixThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile1"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrix throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatrixThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile2"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrix<double>(null, "matrix")); |
|||
writer.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,162 +0,0 @@ |
|||
// <copyright file="DelimitedReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Text; |
|||
using LinearAlgebra.Double; |
|||
using LinearAlgebra.Double.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Delimited reader tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can parse comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseCommaDelimitedData() |
|||
{ |
|||
var data = "a,b,c" + Environment.NewLine |
|||
+ "1" + Environment.NewLine |
|||
+ "\"2.2\",0.3e1" + Environment.NewLine |
|||
+ "'4',5,6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>(',') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0, matrix[0, 0]); |
|||
Assert.AreEqual(0.0, matrix[0, 1]); |
|||
Assert.AreEqual(0.0, matrix[0, 2]); |
|||
Assert.AreEqual(2.2, matrix[1, 0]); |
|||
Assert.AreEqual(3.0, matrix[1, 1]); |
|||
Assert.AreEqual(0.0, matrix[1, 2]); |
|||
Assert.AreEqual(4.0, matrix[2, 0]); |
|||
Assert.AreEqual(5.0, matrix[2, 1]); |
|||
Assert.AreEqual(6.0, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseTabDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"2.2\"\t\t0.3e1" + Environment.NewLine |
|||
+ "'4'\t5\t6"; |
|||
|
|||
var reader = new DelimitedReader<SparseMatrix>('\t') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0, matrix[0, 0]); |
|||
Assert.AreEqual(0.0, matrix[0, 1]); |
|||
Assert.AreEqual(0.0, matrix[0, 2]); |
|||
Assert.AreEqual(2.2, matrix[1, 0]); |
|||
Assert.AreEqual(3.0, matrix[1, 1]); |
|||
Assert.AreEqual(0.0, matrix[1, 2]); |
|||
Assert.AreEqual(4.0, matrix[2, 0]); |
|||
Assert.AreEqual(5.0, matrix[2, 1]); |
|||
Assert.AreEqual(6.0, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse white space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseWhiteSpaceDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"2.2\" 0.3e1" + Environment.NewLine |
|||
+ "'4' 5 6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<UserDefinedMatrix> |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0, matrix[0, 0]); |
|||
Assert.AreEqual(0.0, matrix[0, 1]); |
|||
Assert.AreEqual(0.0, matrix[0, 2]); |
|||
Assert.AreEqual(2.2, matrix[1, 0]); |
|||
Assert.AreEqual(3.0, matrix[1, 1]); |
|||
Assert.AreEqual(0.0, matrix[1, 2]); |
|||
Assert.AreEqual(4.0, matrix[2, 0]); |
|||
Assert.AreEqual(5.0, matrix[2, 1]); |
|||
Assert.AreEqual(6.0, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParsePeriodDelimitedData() |
|||
{ |
|||
var data = "a.b.c" + Environment.NewLine |
|||
+ "1" + Environment.NewLine |
|||
+ "\"2,2\".0,3e1" + Environment.NewLine |
|||
+ "'4,0'.5,0.6,0" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>('.') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = new CultureInfo("tr-TR") |
|||
}; |
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0, matrix[0, 0]); |
|||
Assert.AreEqual(0.0, matrix[0, 1]); |
|||
Assert.AreEqual(0.0, matrix[0, 2]); |
|||
Assert.AreEqual(2.2, matrix[1, 0]); |
|||
Assert.AreEqual(3.0, matrix[1, 1]); |
|||
Assert.AreEqual(0.0, matrix[1, 2]); |
|||
Assert.AreEqual(4.0, matrix[2, 0]); |
|||
Assert.AreEqual(5.0, matrix[2, 1]); |
|||
Assert.AreEqual(6.0, matrix[2, 2]); |
|||
} |
|||
} |
|||
} |
|||
@ -1,138 +0,0 @@ |
|||
// <copyright file="DelimitedWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO |
|||
{ |
|||
using LinearAlgebra.Double; |
|||
using LinearAlgebra.IO; |
|||
using NUnit.Framework; |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
|
|||
/// <summary>
|
|||
/// Delimited writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can write comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteCommaDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); |
|||
var writer = new DelimitedWriter(',') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = @"1.1,2.2,3.3" + Environment.NewLine |
|||
+ "4.4,5.5,6.6" + Environment.NewLine |
|||
+ "7.7,8.8,9.9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWritePeriodDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); |
|||
var culture = new CultureInfo("tr-TR"); |
|||
var writer = new DelimitedWriter('.') |
|||
{ |
|||
CultureInfo = culture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = @"1,1.2,2.3,3" + Environment.NewLine |
|||
+ "4,4.5,5.6,6" + Environment.NewLine |
|||
+ "7,7.8,8.9,9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteSpaceDelimitedData() |
|||
{ |
|||
var matrix = SparseMatrix.OfArray(new[,] {{1.1, 0, 0}, {0, 5.5, 0}, {0, 0, 9.9}}); |
|||
var writer = new DelimitedWriter(' ') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = @"1.1 0 0" + Environment.NewLine |
|||
+ "0 5.5 0" + Environment.NewLine |
|||
+ "0 0 9.9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteTabDelimitedData() |
|||
{ |
|||
var matrix = new UserDefinedMatrix(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); |
|||
var headers = new[] {"a", "b", "c"}; |
|||
var writer = new DelimitedWriter('\t') |
|||
{ |
|||
ColumnHeaders = headers, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "a\tb\tc" + Environment.NewLine |
|||
+ "1.1\t2.2\t3.3" + Environment.NewLine |
|||
+ "4.4\t5.5\t6.6" + Environment.NewLine |
|||
+ "7.7\t8.8\t9.9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
} |
|||
} |
|||
@ -1,113 +0,0 @@ |
|||
// <copyright file="MatlabReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO |
|||
{ |
|||
using LinearAlgebra.Double; |
|||
using LinearAlgebra.Double.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix reader test.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can read all matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(30, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read first matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadFirstMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); |
|||
var matrix = dmr.ReadMatrix(); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(100.108979553704, matrix.FrobeniusNorm(), 5); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read named matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNamedMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); |
|||
Assert.AreEqual(2, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read named matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNamedMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad" }); |
|||
Assert.AreEqual(1, matrices.Count); |
|||
var ad = matrices["Ad"]; |
|||
Assert.AreEqual(100, ad.RowCount); |
|||
Assert.AreEqual(100, ad.ColumnCount); |
|||
AssertHelpers.AlmostEqual(100.431635988639, ad.FrobeniusNorm(), 5); |
|||
Assert.AreEqual(typeof(DenseMatrix), ad.GetType()); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read named sparse matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNamedSparseMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); |
|||
var matrix = dmr.ReadMatrix("S"); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(SparseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(17.6385090630805, matrix.FrobeniusNorm(), 12); |
|||
} |
|||
} |
|||
} |
|||
@ -1,160 +0,0 @@ |
|||
// <copyright file="MatlabWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using LinearAlgebra.Double; |
|||
using LinearAlgebra.Double.IO; |
|||
using LinearAlgebra.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Invalid constructor throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void InvalidConstructorThrowsArgumentException() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(null)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrices throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatricesThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile3"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrices throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatricesThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile4"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new[] { matrix }, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteMatrices() |
|||
{ |
|||
Matrix mat1 = new DenseMatrix(5, 3); |
|||
for (var i = 0; i < mat1.ColumnCount; i++) |
|||
{ |
|||
mat1[i, i] = i + .1; |
|||
} |
|||
|
|||
Matrix mat2 = new DenseMatrix(4, 5); |
|||
for (var i = 0; i < mat2.RowCount; i++) |
|||
{ |
|||
mat2[i, i] = i + .1; |
|||
} |
|||
|
|||
Matrix mat3 = new SparseMatrix(5, 4); |
|||
mat3[0, 0] = 1.1; |
|||
mat3[0, 2] = 2.2; |
|||
mat3[4, 3] = 3.3; |
|||
|
|||
Matrix mat4 = new SparseMatrix(3, 5); |
|||
mat4[0, 0] = 1.1; |
|||
mat4[0, 2] = 2.2; |
|||
mat4[2, 4] = 3.3; |
|||
|
|||
var write = new[] { mat1, mat2, mat3, mat4 }; |
|||
|
|||
var names = new[] { "mat1", "dense_matrix_2", "s1", "sparse2" }; |
|||
if (File.Exists("test.mat")) |
|||
{ |
|||
File.Delete("test.mat"); |
|||
} |
|||
|
|||
var writer = new MatlabMatrixWriter("test.mat"); |
|||
writer.WriteMatrices(write, names); |
|||
writer.Dispose(); |
|||
|
|||
var reader = new MatlabMatrixReader("test.mat"); |
|||
var read = reader.ReadMatrices(names); |
|||
|
|||
Assert.AreEqual(write.Length, read.Count); |
|||
|
|||
for (var i = 0; i < write.Length; i++) |
|||
{ |
|||
var w = write[i]; |
|||
var r = read[names[i]]; |
|||
|
|||
Assert.AreEqual(w.RowCount, r.RowCount); |
|||
Assert.AreEqual(w.ColumnCount, r.ColumnCount); |
|||
Assert.IsTrue(w.Equals(r)); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrix throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatrixThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile1"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrix throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatrixThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile2"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrix<double>(null, "matrix")); |
|||
writer.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,162 +0,0 @@ |
|||
// <copyright file="DelimitedReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO |
|||
{ |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
using System.Text; |
|||
using LinearAlgebra.Single; |
|||
using LinearAlgebra.Single.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Delimited reader tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can parse comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseCommaDelimitedData() |
|||
{ |
|||
var data = "a,b,c" + Environment.NewLine |
|||
+ "1" + Environment.NewLine |
|||
+ "\"2.2\",0.3e1" + Environment.NewLine |
|||
+ "'4',5,6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>(',') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0f, matrix[0, 0]); |
|||
Assert.AreEqual(0.0f, matrix[0, 1]); |
|||
Assert.AreEqual(0.0f, matrix[0, 2]); |
|||
Assert.AreEqual(2.2f, matrix[1, 0]); |
|||
Assert.AreEqual(3.0f, matrix[1, 1]); |
|||
Assert.AreEqual(0.0f, matrix[1, 2]); |
|||
Assert.AreEqual(4.0f, matrix[2, 0]); |
|||
Assert.AreEqual(5.0f, matrix[2, 1]); |
|||
Assert.AreEqual(6.0f, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseTabDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"2.2\"\t\t0.3e1" + Environment.NewLine |
|||
+ "'4'\t5\t6"; |
|||
|
|||
var reader = new DelimitedReader<SparseMatrix>('\t') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0f, matrix[0, 0]); |
|||
Assert.AreEqual(0.0f, matrix[0, 1]); |
|||
Assert.AreEqual(0.0f, matrix[0, 2]); |
|||
Assert.AreEqual(2.2f, matrix[1, 0]); |
|||
Assert.AreEqual(3.0f, matrix[1, 1]); |
|||
Assert.AreEqual(0.0f, matrix[1, 2]); |
|||
Assert.AreEqual(4.0f, matrix[2, 0]); |
|||
Assert.AreEqual(5.0f, matrix[2, 1]); |
|||
Assert.AreEqual(6.0f, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse white space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParseWhiteSpaceDelimitedData() |
|||
{ |
|||
var data = "1" + Environment.NewLine |
|||
+ "\"2.2\" 0.3e1" + Environment.NewLine |
|||
+ "'4' 5 6" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<UserDefinedMatrix> |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0f, matrix[0, 0]); |
|||
Assert.AreEqual(0.0f, matrix[0, 1]); |
|||
Assert.AreEqual(0.0f, matrix[0, 2]); |
|||
Assert.AreEqual(2.2f, matrix[1, 0]); |
|||
Assert.AreEqual(3.0f, matrix[1, 1]); |
|||
Assert.AreEqual(0.0f, matrix[1, 2]); |
|||
Assert.AreEqual(4.0f, matrix[2, 0]); |
|||
Assert.AreEqual(5.0f, matrix[2, 1]); |
|||
Assert.AreEqual(6.0f, matrix[2, 2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can parse period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanParsePeriodDelimitedData() |
|||
{ |
|||
var data = "a.b.c" + Environment.NewLine |
|||
+ "1" + Environment.NewLine |
|||
+ "\"2,2\".0,3e1" + Environment.NewLine |
|||
+ "'4,0'.5,0.6,0" + Environment.NewLine; |
|||
|
|||
var reader = new DelimitedReader<DenseMatrix>('.') |
|||
{ |
|||
HasHeaderRow = true, |
|||
CultureInfo = new CultureInfo("tr-TR") |
|||
}; |
|||
|
|||
var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); |
|||
Assert.AreEqual(3, matrix.RowCount); |
|||
Assert.AreEqual(3, matrix.ColumnCount); |
|||
Assert.AreEqual(1.0f, matrix[0, 0]); |
|||
Assert.AreEqual(0.0f, matrix[0, 1]); |
|||
Assert.AreEqual(0.0f, matrix[0, 2]); |
|||
Assert.AreEqual(2.2f, matrix[1, 0]); |
|||
Assert.AreEqual(3.0f, matrix[1, 1]); |
|||
Assert.AreEqual(0.0f, matrix[1, 2]); |
|||
Assert.AreEqual(4.0f, matrix[2, 0]); |
|||
Assert.AreEqual(5.0f, matrix[2, 1]); |
|||
Assert.AreEqual(6.0f, matrix[2, 2]); |
|||
} |
|||
} |
|||
} |
|||
@ -1,138 +0,0 @@ |
|||
// <copyright file="DelimitedWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
//
|
|||
// Copyright (c) 2009-2013 Math.NET
|
|||
//
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
//
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
//
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO |
|||
{ |
|||
using LinearAlgebra.IO; |
|||
using LinearAlgebra.Single; |
|||
using NUnit.Framework; |
|||
using System; |
|||
using System.Globalization; |
|||
using System.IO; |
|||
|
|||
/// <summary>
|
|||
/// Delimited writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class DelimitedWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can write comma delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteCommaDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{1.1f, 2.2f, 3.3f}, {4.4f, 5.5f, 6.6f}, {7.7f, 8.8f, 9.9f}}); |
|||
var writer = new DelimitedWriter(',') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = @"1.1,2.2,3.3" + Environment.NewLine |
|||
+ "4.4,5.5,6.6" + Environment.NewLine |
|||
+ "7.7,8.8,9.9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write period delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWritePeriodDelimitedData() |
|||
{ |
|||
var matrix = DenseMatrix.OfArray(new[,] {{1.1f, 2.2f, 3.3f}, {4.4f, 5.5f, 6.6f}, {7.7f, 8.8f, 9.9f}}); |
|||
var culture = new CultureInfo("tr-TR"); |
|||
var writer = new DelimitedWriter('.') |
|||
{ |
|||
CultureInfo = culture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = @"1,1.2,2.3,3" + Environment.NewLine |
|||
+ "4,4.5,5.6,6" + Environment.NewLine |
|||
+ "7,7.8,8.9,9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write space delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteSpaceDelimitedData() |
|||
{ |
|||
var matrix = SparseMatrix.OfArray(new[,] {{1.1f, 0, 0}, {0, 5.5f, 0}, {0, 0, 9.9f}}); |
|||
var writer = new DelimitedWriter(' ') |
|||
{ |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = @"1.1 0 0" + Environment.NewLine |
|||
+ "0 5.5 0" + Environment.NewLine |
|||
+ "0 0 9.9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write tab delimited data.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteTabDelimitedData() |
|||
{ |
|||
var matrix = new UserDefinedMatrix(new[,] {{1.1f, 2.2f, 3.3f}, {4.4f, 5.5f, 6.6f}, {7.7f, 8.8f, 9.9f}}); |
|||
var headers = new[] {"a", "b", "c"}; |
|||
var writer = new DelimitedWriter('\t') |
|||
{ |
|||
ColumnHeaders = headers, |
|||
CultureInfo = CultureInfo.InvariantCulture |
|||
}; |
|||
var stream = new MemoryStream(); |
|||
writer.WriteMatrix(matrix, stream); |
|||
var data = stream.ToArray(); |
|||
var reader = new StreamReader(new MemoryStream(data)); |
|||
var text = reader.ReadToEnd(); |
|||
var expected = "a\tb\tc" + Environment.NewLine |
|||
+ "1.1\t2.2\t3.3" + Environment.NewLine |
|||
+ "4.4\t5.5\t6.6" + Environment.NewLine |
|||
+ "7.7\t8.8\t9.9"; |
|||
Assert.AreEqual(expected, text); |
|||
} |
|||
} |
|||
} |
|||
@ -1,113 +0,0 @@ |
|||
// <copyright file="MatlabReaderTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO |
|||
{ |
|||
using LinearAlgebra.Single; |
|||
using LinearAlgebra.Single.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix reader test.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixReaderTests |
|||
{ |
|||
/// <summary>
|
|||
/// Can read all matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadAllMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(); |
|||
Assert.AreEqual(30, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read first matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadFirstMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); |
|||
var matrix = dmr.ReadMatrix(); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(100.108979553704f, matrix.FrobeniusNorm(), 6); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read named matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNamedMatrices() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); |
|||
Assert.AreEqual(2, matrices.Count); |
|||
foreach (var matrix in matrices) |
|||
{ |
|||
Assert.AreEqual(typeof(DenseMatrix), matrix.Value.GetType()); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read named matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNamedMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); |
|||
var matrices = dmr.ReadMatrices(new[] { "Ad" }); |
|||
Assert.AreEqual(1, matrices.Count); |
|||
var ad = matrices["Ad"]; |
|||
Assert.AreEqual(100, ad.RowCount); |
|||
Assert.AreEqual(100, ad.ColumnCount); |
|||
AssertHelpers.AlmostEqual(100.431635988639f, ad.FrobeniusNorm(), 6); |
|||
Assert.AreEqual(typeof(DenseMatrix), ad.GetType()); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can read named sparse matrix.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanReadNamedSparseMatrix() |
|||
{ |
|||
var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); |
|||
var matrix = dmr.ReadMatrix("S"); |
|||
Assert.AreEqual(100, matrix.RowCount); |
|||
Assert.AreEqual(100, matrix.ColumnCount); |
|||
Assert.AreEqual(typeof(SparseMatrix), matrix.GetType()); |
|||
AssertHelpers.AlmostEqual(17.6385090630805f, matrix.FrobeniusNorm(), 6); |
|||
} |
|||
} |
|||
} |
|||
@ -1,159 +0,0 @@ |
|||
// <copyright file="MatlabWriterTests.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://numerics.mathdotnet.com
|
|||
// http://github.com/mathnet/mathnet-numerics
|
|||
// http://mathnetnumerics.codeplex.com
|
|||
// Copyright (c) 2009-2010 Math.NET
|
|||
// Permission is hereby granted, free of charge, to any person
|
|||
// obtaining a copy of this software and associated documentation
|
|||
// files (the "Software"), to deal in the Software without
|
|||
// restriction, including without limitation the rights to use,
|
|||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
// copies of the Software, and to permit persons to whom the
|
|||
// Software is furnished to do so, subject to the following
|
|||
// conditions:
|
|||
// The above copyright notice and this permission notice shall be
|
|||
// included in all copies or substantial portions of the Software.
|
|||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|||
// OTHER DEALINGS IN THE SOFTWARE.
|
|||
// </copyright>
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO |
|||
{ |
|||
using System; |
|||
using System.IO; |
|||
using LinearAlgebra.IO; |
|||
using LinearAlgebra.Single; |
|||
using LinearAlgebra.Single.IO; |
|||
using NUnit.Framework; |
|||
|
|||
/// <summary>
|
|||
/// Matlab matrix writer tests.
|
|||
/// </summary>
|
|||
[TestFixture] |
|||
public class MatlabMatrixWriterTests |
|||
{ |
|||
/// <summary>
|
|||
/// Invalid constructor throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void InvalidConstructorThrowsArgumentException() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(null)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrices throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatricesThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile3"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrices throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatricesThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile4"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new[] { matrix }, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Can write matrices.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void CanWriteMatrices() |
|||
{ |
|||
Matrix mat1 = new DenseMatrix(5, 3); |
|||
for (var i = 0; i < mat1.ColumnCount; i++) |
|||
{ |
|||
mat1[i, i] = i + .1f; |
|||
} |
|||
|
|||
Matrix mat2 = new DenseMatrix(4, 5); |
|||
for (var i = 0; i < mat2.RowCount; i++) |
|||
{ |
|||
mat2[i, i] = i + .1f; |
|||
} |
|||
|
|||
Matrix mat3 = new SparseMatrix(5, 4); |
|||
mat3[0, 0] = 1.1f; |
|||
mat3[0, 2] = 2.2f; |
|||
mat3[4, 3] = 3.3f; |
|||
|
|||
Matrix mat4 = new SparseMatrix(3, 5); |
|||
mat4[0, 0] = 1.1f; |
|||
mat4[0, 2] = 2.2f; |
|||
mat4[2, 4] = 3.3f; |
|||
|
|||
var write = new[] { mat1, mat2, mat3, mat4 }; |
|||
|
|||
var names = new[] { "mat1", "dense_matrix_2", "s1", "sparse2" }; |
|||
if (File.Exists("test.mat")) |
|||
{ |
|||
File.Delete("test.mat"); |
|||
} |
|||
|
|||
var writer = new MatlabMatrixWriter("test.mat"); |
|||
writer.WriteMatrices(write, names); |
|||
writer.Dispose(); |
|||
|
|||
var reader = new MatlabMatrixReader("test.mat"); |
|||
var read = reader.ReadMatrices(names); |
|||
|
|||
Assert.AreEqual(write.Length, read.Count); |
|||
|
|||
for (var i = 0; i < write.Length; i++) |
|||
{ |
|||
var w = write[i]; |
|||
var r = read[names[i]]; |
|||
|
|||
Assert.AreEqual(w.RowCount, r.RowCount); |
|||
Assert.AreEqual(w.ColumnCount, r.ColumnCount); |
|||
Assert.IsTrue(w.Equals(r)); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write bad matrix throws <c>ArgumentException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteBadMatrixThrowsArgumentException() |
|||
{ |
|||
Matrix matrix = new DenseMatrix(1, 1); |
|||
var writer = new MatlabMatrixWriter("somefile1"); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, string.Empty)); |
|||
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, null)); |
|||
writer.Dispose(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Write <c>null</c> matrix throws <c>ArgumentNullException</c>.
|
|||
/// </summary>
|
|||
[Test] |
|||
public void WriteNullMatrixThrowsArgumentNullException() |
|||
{ |
|||
var writer = new MatlabMatrixWriter("somefile2"); |
|||
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrix<double>(null, "matrix")); |
|||
writer.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue