diff --git a/MathNet.Numerics.sln b/MathNet.Numerics.sln index 7ade3c5b..df30eb8a 100644 --- a/MathNet.Numerics.sln +++ b/MathNet.Numerics.sln @@ -24,8 +24,6 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharpUnitTests", "src\FSha EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "src\UnitTests\UnitTests.csproj", "{DAF07AA8-C5C9-4963-98F7-2C3285064DAD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Numerics.IO", "src\Numerics.IO\Numerics.IO.csproj", "{EB1A5D32-F264-4BCE-BEB7-0B97085075BE}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -69,12 +67,6 @@ Global {DAF07AA8-C5C9-4963-98F7-2C3285064DAD}.Release|Any CPU.Build.0 = Release|Any CPU {DAF07AA8-C5C9-4963-98F7-2C3285064DAD}.Release-Signed|Any CPU.ActiveCfg = Release-Signed|Any CPU {DAF07AA8-C5C9-4963-98F7-2C3285064DAD}.Release-Signed|Any CPU.Build.0 = Release-Signed|Any CPU - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE}.Release|Any CPU.Build.0 = Release|Any CPU - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE}.Release-Signed|Any CPU.ActiveCfg = Release-Signed|Any CPU - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE}.Release-Signed|Any CPU.Build.0 = Release-Signed|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Numerics.IO/LinearAlgebra/Complex/IO/DelimitedReader.cs b/src/Numerics.IO/LinearAlgebra/Complex/IO/DelimitedReader.cs deleted file mode 100644 index d38b33f5..00000000 --- a/src/Numerics.IO/LinearAlgebra/Complex/IO/DelimitedReader.cs +++ /dev/null @@ -1,72 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.Complex.IO -{ - using System; - using System.Numerics; - using Generic; - using LinearAlgebra.IO; - - /// - /// Creates a from a delimited text file. If the user does not - /// specify a delimiter, then any whitespace is used. - /// - /// The type of the matrix to return. - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class DelimitedReader : DelimitedReader - where TMatrix : Matrix - { - /// - /// Initializes a new instance of the class using - /// any whitespace as the delimiter. - /// - public DelimitedReader() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The delimiter to use. - public DelimitedReader(char delimiter) : base(delimiter) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// The delimiter to use. - /// - /// - /// If is . - /// - public DelimitedReader(string delimiter) : base(delimiter) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Complex/IO/MatlabReader.cs b/src/Numerics.IO/LinearAlgebra/Complex/IO/MatlabReader.cs deleted file mode 100644 index 5012ed14..00000000 --- a/src/Numerics.IO/LinearAlgebra/Complex/IO/MatlabReader.cs +++ /dev/null @@ -1,56 +0,0 @@ -// -// 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. -// - -using System; - -namespace MathNet.Numerics.LinearAlgebra.Complex.IO -{ - using System.IO; - using LinearAlgebra.IO; - - /// - /// Creates matrices from Matlab files. - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class MatlabMatrixReader : MatlabMatrixReader - { - /// - /// Initializes a new instance of the class. - /// - /// Name of the file to read matrices from. - public MatlabMatrixReader(string filename) : base(filename) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to reader matrices from. - public MatlabMatrixReader(Stream stream) : base(stream) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Complex32/IO/DelimitedReader.cs b/src/Numerics.IO/LinearAlgebra/Complex32/IO/DelimitedReader.cs deleted file mode 100644 index 7e39cd9a..00000000 --- a/src/Numerics.IO/LinearAlgebra/Complex32/IO/DelimitedReader.cs +++ /dev/null @@ -1,72 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.Complex32.IO -{ - using System; - using Generic; - using LinearAlgebra.IO; - using Numerics; - - /// - /// Creates a from a delimited text file. If the user does not - /// specify a delimiter, then any whitespace is used. - /// - /// The type of the matrix to return. - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class DelimitedReader : DelimitedReader - where TMatrix : Matrix - { - /// - /// Initializes a new instance of the class using - /// any whitespace as the delimiter. - /// - public DelimitedReader() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The delimiter to use. - public DelimitedReader(char delimiter) : base(delimiter) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// The delimiter to use. - /// - /// - /// If is . - /// - public DelimitedReader(string delimiter) : base(delimiter) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Complex32/IO/MatlabReader.cs b/src/Numerics.IO/LinearAlgebra/Complex32/IO/MatlabReader.cs deleted file mode 100644 index 640e1aba..00000000 --- a/src/Numerics.IO/LinearAlgebra/Complex32/IO/MatlabReader.cs +++ /dev/null @@ -1,56 +0,0 @@ -// -// 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. -// - -using System; - -namespace MathNet.Numerics.LinearAlgebra.Complex32.IO -{ - using System.IO; - using LinearAlgebra.IO; - - /// - /// Creates matrices from Matlab files. - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class MatlabMatrixReader : MatlabMatrixReader - { - /// - /// Initializes a new instance of the class. - /// - /// Name of the file to read matrices from. - public MatlabMatrixReader(string filename) : base(filename) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to reader matrices from. - public MatlabMatrixReader(Stream stream) : base(stream) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Double/IO/DelimitedReader.cs b/src/Numerics.IO/LinearAlgebra/Double/IO/DelimitedReader.cs deleted file mode 100644 index 54b73f98..00000000 --- a/src/Numerics.IO/LinearAlgebra/Double/IO/DelimitedReader.cs +++ /dev/null @@ -1,71 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.Double.IO -{ - using System; - using Generic; - using LinearAlgebra.IO; - - /// - /// Creates a from a delimited text file. If the user does not - /// specify a delimiter, then any whitespace is used. - /// - /// The type of the matrix to return. - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class DelimitedReader : DelimitedReader - where TMatrix : Matrix - { - /// - /// Initializes a new instance of the class using - /// any whitespace as the delimiter. - /// - public DelimitedReader() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The delimiter to use. - public DelimitedReader(char delimiter) : base(delimiter) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// The delimiter to use. - /// - /// - /// If is . - /// - public DelimitedReader(string delimiter) : base(delimiter) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Double/IO/MatlabReader.cs b/src/Numerics.IO/LinearAlgebra/Double/IO/MatlabReader.cs deleted file mode 100644 index fd85a6e8..00000000 --- a/src/Numerics.IO/LinearAlgebra/Double/IO/MatlabReader.cs +++ /dev/null @@ -1,56 +0,0 @@ -// -// 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. -// - -using System; - -namespace MathNet.Numerics.LinearAlgebra.Double.IO -{ - using System.IO; - using LinearAlgebra.IO; - - /// - /// Creates matrices from Matlab files. - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class MatlabMatrixReader : MatlabMatrixReader - { - /// - /// Initializes a new instance of the class. - /// - /// Name of the file to read matrices from. - public MatlabMatrixReader(string filename) : base(filename) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to reader matrices from. - public MatlabMatrixReader(Stream stream) : base(stream) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/DelimitedReader.cs b/src/Numerics.IO/LinearAlgebra/IO/DelimitedReader.cs deleted file mode 100644 index 4fa808f9..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/DelimitedReader.cs +++ /dev/null @@ -1,291 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Creates a from a delimited text file. If the user does not - /// specify a delimiter, then any whitespace is used. - /// - /// The type of the matrix to return. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class DelimitedReader : MatrixReader - where TMatrix : Matrix - where TDataType : struct, IEquatable, IFormattable - { - /// - /// Converts a string into the given data type. - /// - /// - /// The number as a string to convert. - /// - /// The converted number. - private delegate object ParseNumber(string number); - - /// - /// The function that will do the conversion for a given type. - /// - private ParseNumber _parseFunction; - - /// - /// Initializes static members of the class. - /// - 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(); - } - } - - /// - /// Converts the string into a Complex32. - /// - /// The number to convert. - /// The converted number. - private object ConvertComplex32(string number) - { - return number.ToComplex32(_cultureInfo); - } - - /// - /// Converts the string into a Complex. - /// - /// The number to convert. - /// The converted number. - private object ConvertComplex(string number) - { - return number.ToComplex(_cultureInfo); - } - - /// - /// Converts the string into a double. - /// - /// The number to convert. - /// The converted number. - private object ConvertDouble(string number) - { - return double.Parse(number, NumberStyles.Any, _cultureInfo); - } - - /// - /// Converts the string into a float. - /// - /// The number to convert. - /// The converted number. - private object ConvertFloat(string number) - { - return float.Parse(number, NumberStyles.Any, _cultureInfo); - } - - /// - /// Constructor to create matrix instance. - /// - private static readonly ConstructorInfo Constructor = typeof(TMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - - /// - /// The base regular expression. - /// - private static readonly string Base = "\\([^\\)]*\\)|'[^']*'|\"[^\"]*\"|[^{0}]*"; - - /// - /// The regular expression to use. - /// - private readonly Regex _regex; - - /// - /// The to use. - /// - private CultureInfo _cultureInfo = CultureInfo.CurrentCulture; - - /// - /// Initializes a new instance of the class using - /// any whitespace as the delimiter. - /// - public DelimitedReader() - { - _regex = new Regex(string.Format(Base, @"\s"), RegexOptions.Compiled); - SetParser(); - } - - /// - /// Initializes a new instance of the class. - /// - /// The delimiter to use. - public DelimitedReader(char delimiter) - { - _regex = new Regex(string.Format(Base, delimiter), RegexOptions.Compiled); - SetParser(); - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// The delimiter to use. - /// - /// - /// If is . - /// - public DelimitedReader(string delimiter) - { - if (delimiter == null) - { - throw new ArgumentNullException("delimiter"); - } - - _regex = new Regex(string.Format(Base, delimiter), RegexOptions.Compiled); - SetParser(); - } - - /// - /// Gets or sets the to use when parsing the numbers. - /// - /// The culture info. - /// Defaults to CultureInfo.CurrentCulture. - public CultureInfo CultureInfo - { - get - { - return _cultureInfo; - } - - set - { - if (value != null) - { - _cultureInfo = value; - } - } - } - - /// - /// Gets or sets a value indicating whether the files has a header row. - /// - /// - /// true if this instance has a header row; otherwise, false. - /// - /// Defaults to . - public bool HasHeaderRow - { - get; - set; - } - - /// - /// Performs the actual reading. - /// - /// The to read the matrix from. - /// - /// A matrix containing the data from the . is returned if the is empty. - /// - protected override TMatrix DoReadMatrix(Stream stream) - { - var data = new List(); - - // 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; - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/DelimitedWriter.cs b/src/Numerics.IO/LinearAlgebra/IO/DelimitedWriter.cs deleted file mode 100644 index 4ee0509e..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/DelimitedWriter.cs +++ /dev/null @@ -1,145 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.IO -{ - using System; - using System.Collections.Generic; - using System.Globalization; - using System.IO; - using Generic; - - /// - /// Writes an to delimited text file. If the user does not - /// specify a delimiter, a tab separator is used. - /// - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class DelimitedWriter : MatrixWriter - { - /// - /// The delimiter to use. - /// - private readonly string _delimiter; - - /// - /// Initializes a new instance of the class. - /// a comma as the delimiter. - /// - public DelimitedWriter() - { - _delimiter = ","; - } - - /// - /// Initializes a new instance of the class. - /// using the given delimiter. - /// - /// - /// the delimiter to use. - /// - public DelimitedWriter(char delimiter) - { - _delimiter = new string(delimiter, 1); - } - - /// - /// Initializes a new instance of the class. - /// using the given delimiter. - /// - /// - /// the delimiter to use. - /// - public DelimitedWriter(string delimiter) - { - _delimiter = delimiter; - } - - /// - /// Gets or sets the column header values. - /// - /// The column header values. - /// Will write the column headers if the list is not empty or null. - public IList ColumnHeaders - { - get; - set; - } - - /// - /// Writes the given to the given . - /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - /// The matrix to write. - /// The to write the matrix to. - /// The number format to use on each element. - /// The culture to use. - /// If either or is null. - protected override void DoWriteMatrix(Matrix 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); - } - } - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/Matlab/Adler32.cs b/src/Numerics.IO/LinearAlgebra/IO/Matlab/Adler32.cs deleted file mode 100644 index c09db775..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/Matlab/Adler32.cs +++ /dev/null @@ -1,96 +0,0 @@ -// -// 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. -// - -/* 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; - - /// - /// Computes the Adler-32 checksum of the given data. - /// - /// The data to create the checksum. - /// The checksum - 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); - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/Matlab/ArrayClass.cs b/src/Numerics.IO/LinearAlgebra/IO/Matlab/ArrayClass.cs deleted file mode 100644 index fedf313a..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/Matlab/ArrayClass.cs +++ /dev/null @@ -1,124 +0,0 @@ -// -// 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. -// - -using System; - -namespace MathNet.Numerics.LinearAlgebra.IO.Matlab -{ - /// - /// Enumeration for the Matlab array types - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - internal enum ArrayClass : byte - { - /// - /// mxUNKNOWN CLASS - /// - Unknown = 0, - - /// - /// mxCELL CLASS - /// - Cell = 1, - - /// - /// mxSTRUCT CLASS - /// - Structure = 2, - - /// - /// mxOBJECT CLASS - /// - Object = 3, - - /// - /// mxCHAR CLASS - /// - Character = 4, - - /// - /// mxSPARSE CLASS - /// - Sparse = 5, - - /// - /// mxDOUBLE CLASS - /// - Double = 6, - - /// - /// mxSINGLE CLASS - /// - Single = 7, - - /// - /// mxINT8 CLASS - /// - Int8 = 8, - - /// - /// mxUINT8 CLASS - /// - UInt8 = 9, - - /// - /// mxINT16 CLASS - /// - Int16 = 10, - - /// - /// mxUINT16 CLASS - /// - UInt16 = 11, - - /// - /// mxINT32 CLASS - /// - Int32 = 12, - - /// - /// mxUINT32 CLASS - /// - UInt32 = 13, - - /// - /// mxINT64 CLASS - /// - Int64 = 14, - - /// - /// mxUINT64 CLASS - /// - UInt64 = 15, - - /// - /// mxFUNCTION CLASS - /// - Function = 16 - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/Matlab/ArrayFlags.cs b/src/Numerics.IO/LinearAlgebra/IO/Matlab/ArrayFlags.cs deleted file mode 100644 index 91366984..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/Matlab/ArrayFlags.cs +++ /dev/null @@ -1,55 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.IO.Matlab -{ - using System; - - /// - /// Matlab Array Flags - /// - [Flags] - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - internal enum ArrayFlags - { - /// - /// Complex flag - /// - Complex = 8, - - /// - /// Global flag - /// - Global = 4, - - /// - /// Logical flag - /// - Logical = 2 - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/Matlab/DataType.cs b/src/Numerics.IO/LinearAlgebra/IO/Matlab/DataType.cs deleted file mode 100644 index 393ebcd2..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/Matlab/DataType.cs +++ /dev/null @@ -1,119 +0,0 @@ -// -// 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. -// - -using System; - -namespace MathNet.Numerics.LinearAlgebra.IO.Matlab -{ - /// - /// Matlab data types - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - internal enum DataType - { - /// - /// Unkown type - /// - Unknown = 0, - - /// - /// miINT8 type - /// - Int8 = 1, - - /// - /// miUINT8 type - /// - UInt8 = 2, - - /// - /// miINT16 type - /// - Int16 = 3, - - /// - /// miUINT16 type - /// - UInt16 = 4, - - /// - /// miINT32 type - /// - Int32 = 5, - - /// - /// miUINT32 type - /// - UInt32 = 6, - - /// - /// miSINGLE type - /// - Single = 7, - - /// - /// miDOUBLE type - /// - Double = 9, - - /// - /// miINT64 type - /// - Int64 = 12, - - /// - /// miUINT6 4type - /// - UInt64 = 13, - - /// - /// miMATRIX type - /// - Matrix = 14, - - /// - /// miCOMPRESSED type - /// - Compressed = 15, - - /// - /// miUTF8 type - /// - Utf8 = 16, - - /// - /// miUTF16 type - /// - Utf16 = 17, - - /// - /// miUTF32 type - /// - Utf32 = 18 - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabFile.cs b/src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabFile.cs deleted file mode 100644 index 75981099..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabFile.cs +++ /dev/null @@ -1,85 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.IO.Matlab -{ - using System; - using System.Collections.Generic; - using Generic; - - /// - /// Represents a Matlab file - /// - /// The data type of the matrix to return. - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - internal class MatlabFile where TDataType : struct, IEquatable, IFormattable - { - /// - /// Matrices in a matlab file stored as 1-D arrays - /// - private readonly IDictionary> _matrices = new SortedList>(); - - /// - /// Gets or sets the header text. - /// - /// The header text. - public string HeaderText { get; set; } - - /// - /// Gets or sets the first name of the matrix. - /// - /// The first name of the matrix. - public string FirstMatrixName { get; set; } - - /// - /// Gets the first matrix. - /// - /// The first matrix. - public Matrix FirstMatrix - { - get - { - if (string.IsNullOrEmpty(FirstMatrixName) || !_matrices.ContainsKey(FirstMatrixName)) - { - return null; - } - - return _matrices[FirstMatrixName]; - } - } - - /// - /// Gets the matrices. - /// - /// The matrices. - public IDictionary> Matrices - { - get { return _matrices; } - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabParser.cs b/src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabParser.cs deleted file mode 100644 index 3631a78d..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/Matlab/MatlabParser.cs +++ /dev/null @@ -1,1571 +0,0 @@ -// -// 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. -// - -using System.IO.Compression; - -namespace MathNet.Numerics.LinearAlgebra.IO.Matlab -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Numerics; - using System.Reflection; - using System.Text; - using Generic; - using Properties; - using Complex32 = Numerics.Complex32; - - /// - /// Parse a Matlab file - /// - /// The data type of the matrix. - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - internal class MatlabParser - where TDataType : struct, IEquatable, IFormattable - { - /// - /// Large Block Size - /// - private const int LargeBlockSize = 8; - - /// - /// Little Endian Indicator - /// - private const byte LittleEndianIndicator = 0x49; - - /// - /// Small Block Size - /// - private const int SmallBlockSize = 4; - - /// - /// Holds the names of the matrices in the file. - /// - private readonly IList _names = new List(); - - /// - /// The stream to read the matlab file from. - /// - private readonly Stream _stream; - - /// - /// Initializes a new instance of the class. - /// - /// Name of the file. - public MatlabParser(string fileName) - : this(fileName, new string[0]) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to read from. - public MatlabParser(Stream stream) - : this(stream, new string[0]) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to read from. - /// The name of the objects to retrieve. - public MatlabParser(Stream stream, IEnumerable objectNames) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - _stream = stream; - SetNames(objectNames); - } - - /// - /// Initializes a new instance of the class. - /// - /// Name of the file. - /// The name of the objects to retrieve. - public MatlabParser(string fileName, IEnumerable objectNames) - { - if (string.IsNullOrEmpty(fileName)) - { - throw new ArgumentException(Resources.StringNullOrEmpty, "filename"); - } - - _stream = File.OpenRead(fileName); - SetNames(objectNames); - } - - /// - /// Copies the names of the objects to retrieve to a local field. - /// - /// The name of the objects to retrieve. - private void SetNames(IEnumerable objectNames) - { - foreach (var name in objectNames) - { - _names.Add(name); - } - } - - /// - /// Parses the file. - /// - /// The parsed Matlab file as a object. - public MatlabFile Parse() - { - var file = new MatlabFile(); - - using (var reader = new BinaryReader(_stream)) - { - file.HeaderText = Encoding.ASCII.GetString(reader.ReadBytes(116)); - - // skipping subsystem offsets - reader.BaseStream.Position = 126; - - if (reader.ReadByte() != LittleEndianIndicator) - { - throw new NotSupportedException(Resources.BigEndianNotSupported); - } - - // skip version since it is always 0x0100. - reader.BaseStream.Position = 128; - var length = _stream.Length; - - // for each data block add a matlab object to the file. - while (reader.BaseStream.Position < length) - { - var type = (DataType)reader.ReadInt16(); - int size = reader.ReadInt16(); - var smallBlock = true; - if (size == 0) - { - size = reader.ReadInt32(); - smallBlock = false; - } - - byte[] data; - if (type == DataType.Compressed) - { - data = DecompressBlock(reader.ReadBytes(size), ref type); - } - else - { - data = new byte[size]; - reader.Read(data, 0, size); - AlignData(reader.BaseStream, size, smallBlock); - } - - if (type == DataType.Matrix) - { - AddMatrix(data, file); - } - else - { - throw new NotSupportedException(string.Format(Resources.NotSupportedType, type)); - } - } - } - - return file; - } - - /// - /// Aligns the data. - /// - /// The stream. - /// The size of the array. - /// if set to true if reading from a small block. - private static void AlignData(Stream stream, int size, bool smallBlock) - { - var blockSize = smallBlock ? SmallBlockSize : LargeBlockSize; - var offset = 0; - var mod = size % blockSize; - if (mod != 0) - { - offset = blockSize - mod; - } - - stream.Seek(offset, SeekOrigin.Current); - } - - /// - /// Decompresses the block. - /// - /// The compressed data. - /// The type data type contained in the block. - /// The decompressed block. - private static byte[] DecompressBlock(byte[] compressed, ref DataType type) - { - byte[] data; - using (var compressedStream = new MemoryStream(compressed, 2, compressed.Length-6)) - { - using (var decompressor = new DeflateStream(compressedStream, CompressionMode.Decompress)) - using(var decompressed = new MemoryStream()) - { - decompressor.CopyTo(decompressed); - decompressed.Position = 0; - var buf = new byte[4]; - decompressed.Read(buf, 0, 4); - type = (DataType)BitConverter.ToInt32(buf, 0); - decompressed.Read(buf, 0, 4); - var size = BitConverter.ToInt32(buf, 0); - data = new byte[size]; - decompressed.Read(data, 0, size); - } - } - - return data; - } - - /// - /// Adds a matrix from the actual file into our presentation of a matlab file. - /// - /// The data of the matrix. - /// The instance. - private void AddMatrix(byte[] data, MatlabFile file) - { - using (var ms = new MemoryStream(data)) - { - using (var reader = new BinaryReader(ms)) - { - // skip tag - doesn't tell us anything we don't already know - reader.BaseStream.Seek(8, SeekOrigin.Current); - - var arrayClass = (ArrayClass)reader.ReadByte(); - var flags = reader.ReadByte(); - var isComplex = (flags & (byte)ArrayFlags.Complex) == (byte)ArrayFlags.Complex; - - // skip unneeded bytes - reader.BaseStream.Seek(10, SeekOrigin.Current); - - var numDimensions = reader.ReadInt32() / 8; - if (numDimensions > 2) - { - throw new NotSupportedException(Resources.MoreThan2D); - } - - var rows = reader.ReadInt32(); - var columns = reader.ReadInt32(); - - // skip unneeded bytes - reader.BaseStream.Seek(2, SeekOrigin.Current); - int size = reader.ReadInt16(); - var smallBlock = true; - if (size == 0) - { - size = reader.ReadInt32(); - smallBlock = false; - } - - var name = Encoding.ASCII.GetString(reader.ReadBytes(size)); - AlignData(reader.BaseStream, size, smallBlock); - - // only grab wanted objects - if (_names.Count != 0 && !_names.Contains(name)) - { - return; - } - - var type = (DataType)reader.ReadInt16(); - size = reader.ReadInt16(); - if (size == 0) - { - size = reader.ReadInt32(); - } - - Matrix matrix; - switch (arrayClass) - { - case ArrayClass.Sparse: - matrix = PopulateSparseMatrix(reader, isComplex, rows, columns, size); - break; - case ArrayClass.Function: - case ArrayClass.Character: - case ArrayClass.Object: - case ArrayClass.Structure: - case ArrayClass.Cell: - case ArrayClass.Unknown: - throw new NotSupportedException(); - default: - matrix = PopulateDenseMatrix(type, reader, isComplex, rows, columns, size); - break; - } - - file.Matrices.Add(name, matrix); - if (file.FirstMatrixName == null) - { - file.FirstMatrixName = name; - } - } - } - } - - /// - /// Populates a sparse matrix. - /// - /// The reader. - /// if set to true if the Matlab complex flag is set. - /// The number of rows. - /// The number of columns. - /// The size of the block. - /// A populated sparse matrix. - private static Matrix PopulateSparseMatrix(BinaryReader reader, bool isComplex, int rows, int columns, int size) - { - // populate the row data array - var ir = new int[size / 4]; - for (var i = 0; i < ir.Length; i++) - { - ir[i] = reader.ReadInt32(); - } - - AlignData(reader.BaseStream, size, false); - - // skip data type since it will always be int32 - reader.BaseStream.Seek(4, SeekOrigin.Current); - - // populate the column data array - var jcsize = reader.ReadInt32(); - var jc = new int[jcsize / 4]; - for (var j = 0; j < jc.Length; j++) - { - jc[j] = reader.ReadInt32(); - } - - AlignData(reader.BaseStream, jcsize, false); - - var type = (DataType)reader.ReadInt32(); - var dataSize = reader.ReadInt32(); - - var matrix = CreateMatrix(true, rows, columns); - var dataType = typeof(TDataType); - - if (dataType == typeof(double)) - { - if (isComplex) - { - throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); - } - - PopulateDoubleSparseMatrix((Matrix)(object)matrix, type, ir, jc, reader); - } - else if (dataType == typeof(float)) - { - if (isComplex) - { - throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); - } - - PopulateSingleSparseMatrix((Matrix)(object)matrix, type, ir, jc, reader); - } - else if (dataType == typeof(Complex)) - { - PopulateComplexSparseMatrix((Matrix)(object)matrix, type, isComplex, ir, jc, reader, dataSize); - } - else if (dataType == typeof(Complex32)) - { - PopulateComplex32SparseMatrix((Matrix)(object)matrix, type, isComplex, ir, jc, reader, dataSize); - } - else - { - throw new NotSupportedException(); - } - - return matrix; - } - - /// - /// Populates the double sparse matrix. - /// - /// The matrix to populate - /// The Matlab data type. - /// The row indices. - /// The column indices. - /// The reader to read from. - private static void PopulateDoubleSparseMatrix(Matrix matrix, DataType type, IList ir, IList jc, BinaryReader reader) - { - var col = 0; - for (var i = 0; i < ir.Count; i++) - { - var row = ir[i]; - while(jc[col + 1] == i) - { - col++; - } - - switch (type) - { - case DataType.Int8: - matrix.At(row, col, reader.ReadSByte()); - break; - case DataType.UInt8: - matrix.At(row, col, reader.ReadByte()); - break; - case DataType.Int16: - matrix.At(row, col, reader.ReadInt16()); - break; - case DataType.UInt16: - matrix.At(row, col, reader.ReadUInt16()); - break; - case DataType.Int32: - matrix.At(row, col, reader.ReadInt32()); - break; - case DataType.UInt32: - matrix.At(row, col, reader.ReadUInt32()); - break; - case DataType.Single: - matrix.At(row, col, reader.ReadSingle()); - break; - case DataType.Int64: - matrix.At(row, col, reader.ReadInt64()); - break; - case DataType.UInt64: - matrix.At(row, col, reader.ReadUInt64()); - break; - case DataType.Double: - matrix.At(row, col, reader.ReadDouble()); - break; - default: - throw new NotSupportedException(); - } - } - } - - /// - /// Populates the float sparse matrix. - /// - /// The matrix to populate - /// The Matlab data type. - /// The row indices. - /// The column indices. - /// The reader to read from. - private static void PopulateSingleSparseMatrix(Matrix matrix, DataType type, IList ir, IList jc, BinaryReader reader) - { - var col = 0; - for (var i = 0; i < ir.Count; i++) - { - var row = ir[i]; - while (jc[col + 1] == i) - { - col++; - } - - switch (type) - { - case DataType.Int8: - matrix.At(row, col, reader.ReadSByte()); - break; - case DataType.UInt8: - matrix.At(row, col, reader.ReadByte()); - break; - case DataType.Int16: - matrix.At(row, col, reader.ReadInt16()); - break; - case DataType.UInt16: - matrix.At(row, col, reader.ReadUInt16()); - break; - case DataType.Int32: - matrix.At(row, col, reader.ReadInt32()); - break; - case DataType.UInt32: - matrix.At(row, col, reader.ReadUInt32()); - break; - case DataType.Single: - matrix.At(row, col, reader.ReadSingle()); - break; - case DataType.Int64: - matrix.At(row, col, reader.ReadInt64()); - break; - case DataType.UInt64: - matrix.At(row, col, reader.ReadUInt64()); - break; - case DataType.Double: - matrix.At(row, col, Convert.ToSingle(reader.ReadDouble())); - break; - default: - throw new NotSupportedException(); - } - } - } - - /// - /// Populates the complex sparse matrix. - /// - /// The matrix to populate - /// The Matlab data type. - /// if set to true if the Matlab complex flag is set. - /// The row indices. - /// The column indices. - /// The reader to read from. - /// The length of the stored data. - private static void PopulateComplexSparseMatrix(Matrix matrix, DataType type, bool isComplex, IList ir, IList jc, BinaryReader reader, int dataSize) - { - var col = 0; - for (var i = 0; i < ir.Count; i++) - { - var row = ir[i]; - while (jc[col + 1] == i) - { - col++; - } - - switch (type) - { - case DataType.Int8: - matrix.At(row, col, reader.ReadSByte()); - break; - case DataType.UInt8: - matrix.At(row, col, reader.ReadByte()); - break; - case DataType.Int16: - matrix.At(row, col, reader.ReadInt16()); - break; - case DataType.UInt16: - matrix.At(row, col, reader.ReadUInt16()); - break; - case DataType.Int32: - matrix.At(row, col, reader.ReadInt32()); - break; - case DataType.UInt32: - matrix.At(row, col, reader.ReadUInt32()); - break; - case DataType.Single: - matrix.At(row, col, reader.ReadSingle()); - break; - case DataType.Int64: - matrix.At(row, col, reader.ReadInt64()); - break; - case DataType.UInt64: - matrix.At(row, col, reader.ReadUInt64()); - break; - case DataType.Double: - matrix.At(row, col, reader.ReadDouble()); - break; - default: - throw new NotSupportedException(); - } - } - - if (isComplex) - { - var skip = dataSize % 8; - - // skip pad - reader.ReadBytes(skip); - - // skip header - type = (DataType)reader.ReadInt32(); - reader.ReadInt32(); - col = 0; - for (var i = 0; i < ir.Count; i++) - { - var row = ir[i]; - while (jc[col + 1] == i) - { - col++; - } - - var real = matrix.At(row, col).Real; - switch (type) - { - case DataType.Int8: - matrix.At(row, col, new Complex(real, reader.ReadSByte())); - break; - case DataType.UInt8: - matrix.At(row, col, new Complex(real, reader.ReadByte())); - break; - case DataType.Int16: - matrix.At(row, col, new Complex(real, reader.ReadInt16())); - break; - case DataType.UInt16: - matrix.At(row, col, new Complex(real, reader.ReadUInt16())); - break; - case DataType.Int32: - matrix.At(row, col, new Complex(real, reader.ReadInt32())); - break; - case DataType.UInt32: - matrix.At(row, col, new Complex(real, reader.ReadUInt32())); - break; - case DataType.Single: - matrix.At(row, col, new Complex(real, reader.ReadSingle())); - break; - case DataType.Int64: - matrix.At(row, col, new Complex(real, reader.ReadInt64())); - break; - case DataType.UInt64: - matrix.At(row, col, new Complex(real, reader.ReadUInt64())); - break; - case DataType.Double: - matrix.At(row, col, new Complex(real, reader.ReadDouble())); - break; - default: - throw new NotSupportedException(); - } - } - } - } - - /// - /// Populates the complex32 sparse matrix. - /// - /// The matrix to populate - /// The Matlab data type. - /// if set to true if the Matlab complex flag is set. - /// The row indices. - /// The column indices. - /// The reader to read from. - /// The length of the stored data. - private static void PopulateComplex32SparseMatrix(Matrix matrix, DataType type, bool isComplex, IList ir, IList jc, BinaryReader reader, int dataSize) - { - var col = 0; - for (var i = 0; i < ir.Count; i++) - { - var row = ir[i]; - while (jc[col + 1] == i) - { - col++; - } - - switch (type) - { - case DataType.Int8: - matrix.At(row, col, reader.ReadSByte()); - break; - case DataType.UInt8: - matrix.At(row, col, reader.ReadByte()); - break; - case DataType.Int16: - matrix.At(row, col, reader.ReadInt16()); - break; - case DataType.UInt16: - matrix.At(row, col, reader.ReadUInt16()); - break; - case DataType.Int32: - matrix.At(row, col, reader.ReadInt32()); - break; - case DataType.UInt32: - matrix.At(row, col, reader.ReadUInt32()); - break; - case DataType.Single: - matrix.At(row, col, reader.ReadSingle()); - break; - case DataType.Int64: - matrix.At(row, col, reader.ReadInt64()); - break; - case DataType.UInt64: - matrix.At(row, col, reader.ReadUInt64()); - break; - case DataType.Double: - matrix.At(row, col, Convert.ToSingle(reader.ReadDouble())); - break; - default: - throw new NotSupportedException(); - } - } - - if (isComplex) - { - var skip = dataSize % 8; - - // skip pad - reader.ReadBytes(skip); - - // skip header - type = (DataType)reader.ReadInt32(); - reader.ReadInt32(); - - col = 0; - for (var i = 0; i < ir.Count; i++) - { - var row = ir[i]; - while (jc[col + 1] == i) - { - col++; - } - - var real = matrix.At(row, col).Real; - switch (type) - { - case DataType.Int8: - matrix.At(row, col, new Complex32(real, reader.ReadSByte())); - break; - case DataType.UInt8: - matrix.At(row, col, new Complex32(real, reader.ReadByte())); - break; - case DataType.Int16: - matrix.At(row, col, new Complex32(real, reader.ReadInt16())); - break; - case DataType.UInt16: - matrix.At(row, col, new Complex32(real, reader.ReadUInt16())); - break; - case DataType.Int32: - matrix.At(row, col, new Complex32(real, reader.ReadInt32())); - break; - case DataType.UInt32: - matrix.At(row, col, new Complex32(real, reader.ReadUInt32())); - break; - case DataType.Single: - matrix.At(row, col, new Complex32(real, reader.ReadSingle())); - break; - case DataType.Int64: - matrix.At(row, col, new Complex32(real, reader.ReadInt64())); - break; - case DataType.UInt64: - matrix.At(row, col, new Complex32(real, reader.ReadUInt64())); - break; - case DataType.Double: - matrix.At(row, col, new Complex32(real, Convert.ToSingle(reader.ReadDouble()))); - break; - default: - throw new NotSupportedException(); - } - } - } - } - - /// - /// Populates a dense matrix. - /// - /// The Matlab data type. - /// The reader to read from. - /// if set to true if the Matlab complex flag is set. - /// The number of rows. - /// The number of columns. - /// The length of the stored data. - /// Returns a populated dense matrix. - private static Matrix PopulateDenseMatrix(DataType type, BinaryReader reader, bool isComplex, int rows, int columns, int size) - { - var matrix = CreateMatrix(false, rows, columns); - - var dataType = typeof(TDataType); - - if (dataType == typeof(double)) - { - if (isComplex) - { - throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); - } - - PopulateDoubleDenseMatrix((Matrix)(object)matrix, type, reader, rows, columns); - } - else if (dataType == typeof(float)) - { - if (isComplex) - { - throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); - } - - PopulateSingleDenseMatrix((Matrix)(object)matrix, type, reader, rows, columns); - } - else if (dataType == typeof(Complex)) - { - PopulateComplexDenseMatrix((Matrix)(object)matrix, type, isComplex, reader, rows, columns, size); - } - else if (dataType == typeof(Complex32)) - { - PopulateComplex32DenseMatrix((Matrix)(object)matrix, type, isComplex, reader, rows, columns, size); - } - else - { - throw new NotSupportedException(); - } - - return matrix; - } - - /// - /// Populates the double dense matrix. - /// - /// The matrix to populate. - /// The Matlab data type. - /// The reader to read from. - /// The number of rows. - /// The number of columns. - public static void PopulateDoubleDenseMatrix(Matrix matrix, DataType type, BinaryReader reader, int rows, int columns) - { - switch (type) - { - case DataType.Int8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSByte()); - } - } - - break; - case DataType.UInt8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadByte()); - } - } - - break; - case DataType.Int16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt16()); - } - } - - break; - case DataType.UInt16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt16()); - } - } - - break; - case DataType.Int32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt32()); - } - } - - break; - case DataType.UInt32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt32()); - } - } - - break; - case DataType.Single: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSingle()); - } - } - - break; - case DataType.Int64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt64()); - } - } - - break; - case DataType.UInt64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt64()); - } - } - - break; - case DataType.Double: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadDouble()); - } - } - - break; - default: - throw new NotSupportedException(); - } - } - - /// - /// Populates the complex dense matrix. - /// - /// The matrix to populate. - /// The Matlab data type. - /// if set to true if the Matlab complex flag is set. - /// The reader to read from. - /// The number of rows. - /// The number of columns. - /// The length of the stored data. - public static void PopulateComplexDenseMatrix(Matrix matrix, DataType type, bool isComplex, BinaryReader reader, int rows, int columns, int dataSize) - { - switch (type) - { - case DataType.Int8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSByte()); - } - } - - break; - case DataType.UInt8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadByte()); - } - } - - break; - case DataType.Int16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt16()); - } - } - - break; - case DataType.UInt16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt16()); - } - } - - break; - case DataType.Int32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt32()); - } - } - - break; - case DataType.UInt32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt32()); - } - } - - break; - case DataType.Single: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSingle()); - } - } - - break; - case DataType.Int64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt64()); - } - } - - break; - case DataType.UInt64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt64()); - } - } - - break; - case DataType.Double: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadDouble()); - } - } - - break; - default: - throw new NotSupportedException(); - } - - if (isComplex) - { - var skip = dataSize % 8; - - // skip pad - reader.ReadBytes(skip); - - // skip header - type = (DataType)reader.ReadInt32(); - reader.ReadInt32(); - - switch (type) - { - case DataType.Int8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadSByte())); - } - } - - break; - case DataType.UInt8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadByte())); - } - } - - break; - case DataType.Int16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadInt16())); - } - } - - break; - case DataType.UInt16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadUInt16())); - } - } - - break; - case DataType.Int32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadInt32())); - } - } - - break; - case DataType.UInt32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadUInt32())); - } - } - - break; - case DataType.Single: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadSingle())); - } - } - - break; - case DataType.Int64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadInt64())); - } - } - - break; - case DataType.UInt64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadUInt64())); - } - } - - break; - case DataType.Double: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex(matrix.At(i, j).Real, reader.ReadDouble())); - } - } - - break; - default: - throw new NotSupportedException(); - } - } - } - - /// - /// Populates the complex32 dense matrix. - /// - /// The matrix to populate. - /// The Matlab data type. - /// if set to true if the Matlab complex flag is set. - /// The reader to read from. - /// The number of rows. - /// The number of columns. - /// The length of the stored data. - public static void PopulateComplex32DenseMatrix(Matrix matrix, DataType type, bool isComplex, BinaryReader reader, int rows, int columns, int dataSize) - { - switch (type) - { - case DataType.Int8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSByte()); - } - } - - break; - case DataType.UInt8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadByte()); - } - } - - break; - case DataType.Int16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt16()); - } - } - - break; - case DataType.UInt16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt16()); - } - } - - break; - case DataType.Int32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt32()); - } - } - - break; - case DataType.UInt32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt32()); - } - } - - break; - case DataType.Single: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSingle()); - } - } - - break; - case DataType.Int64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt64()); - } - } - - break; - case DataType.UInt64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt64()); - } - } - - break; - case DataType.Double: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, Convert.ToSingle(reader.ReadDouble())); - } - } - - break; - default: - throw new NotSupportedException(); - } - - if (isComplex) - { - var skip = dataSize % 8; - - // skip pad - reader.ReadBytes(skip); - - // skip header - type = (DataType)reader.ReadInt32(); - reader.ReadInt32(); - - switch (type) - { - case DataType.Int8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadSByte())); - } - } - - break; - case DataType.UInt8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadByte())); - } - } - - break; - case DataType.Int16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadInt16())); - } - } - - break; - case DataType.UInt16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadUInt16())); - } - } - - break; - case DataType.Int32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadInt32())); - } - } - - break; - case DataType.UInt32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadUInt32())); - } - } - - break; - case DataType.Single: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadSingle())); - } - } - - break; - case DataType.Int64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadInt64())); - } - } - - break; - case DataType.UInt64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, reader.ReadUInt64())); - } - } - - break; - case DataType.Double: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, new Complex32(matrix.At(i, j).Real, Convert.ToSingle(reader.ReadDouble()))); - } - } - - break; - default: - throw new NotSupportedException(); - } - } - } - - /// - /// Populates the float dense matrix. - /// - /// The matrix to populate. - /// The Matlab data type. - /// The reader to read from. - /// The number of rows. - /// The number of columns. - public static void PopulateSingleDenseMatrix(Matrix matrix, DataType type, BinaryReader reader, int rows, int columns) - { - switch (type) - { - case DataType.Int8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSByte()); - } - } - - break; - case DataType.UInt8: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadByte()); - } - } - - break; - case DataType.Int16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt16()); - } - } - - break; - case DataType.UInt16: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt16()); - } - } - - break; - case DataType.Int32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt32()); - } - } - - break; - case DataType.UInt32: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt32()); - } - } - - break; - case DataType.Single: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadSingle()); - } - } - - break; - case DataType.Int64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadInt64()); - } - } - - break; - case DataType.UInt64: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, reader.ReadUInt64()); - } - } - - break; - case DataType.Double: - for (var j = 0; j < columns; j++) - { - for (var i = 0; i < rows; i++) - { - matrix.At(i, j, Convert.ToSingle(reader.ReadDouble())); - } - } - - break; - default: - throw new NotSupportedException(); - } - } - - /// - /// Creates a matrix. - /// - /// if set to true, creates a sparse matrix. - /// The number of rows. - /// The number of columns. - /// A matrix with the specified storage. - private static Matrix CreateMatrix(bool sparse, int rows, int columns) - { - ConstructorInfo constructor; - var dataType = typeof(TDataType); - - if (sparse) - { - if (dataType == typeof(double)) - { - constructor = typeof(LinearAlgebra.Double.SparseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else if (dataType == typeof(float)) - { - constructor = typeof(LinearAlgebra.Single.SparseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else if (dataType == typeof(Complex)) - { - constructor = typeof(LinearAlgebra.Complex.SparseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else if (dataType == typeof(Complex32)) - { - constructor = typeof(LinearAlgebra.Complex32.SparseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else - { - throw new NotSupportedException(); - } - } - else - { - if (dataType == typeof(double)) - { - constructor = typeof(LinearAlgebra.Double.DenseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else if (dataType == typeof(float)) - { - constructor = typeof(LinearAlgebra.Single.DenseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else if (dataType == typeof(Complex)) - { - constructor = typeof(LinearAlgebra.Complex.DenseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else if (dataType == typeof(Complex32)) - { - constructor = typeof(LinearAlgebra.Complex32.DenseMatrix).GetConstructor(new[] { typeof(int), typeof(int) }); - } - else - { - throw new NotSupportedException(); - } - } - - return (Matrix)constructor.Invoke(new object[] { rows, columns }); - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/MatlabReader.cs b/src/Numerics.IO/LinearAlgebra/IO/MatlabReader.cs deleted file mode 100644 index 5a7a639e..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/MatlabReader.cs +++ /dev/null @@ -1,188 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.IO -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using Generic; - using Matlab; - using Properties; - - /// - /// Creates matrices from Matlab files. - /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class MatlabMatrixReader where TDataType : struct, IEquatable, IFormattable - { - /// - /// The name of the file to read from. - /// - private readonly string _filename; - - /// - /// The stream to read from if we are not reading from a file directly. - /// - private readonly Stream _stream; - - /// - /// Initializes a new instance of the class. - /// - /// Name of the file to read matrices from. - 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; - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to reader matrices from. - public MatlabMatrixReader(Stream stream) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - _stream = stream; - } - - /// - /// Reads the first matrix from the file or stream. - /// - /// - /// A sparse or dense matrix depending on how the matrix - /// is defined in the Matlab file. - /// - public Matrix ReadMatrix() - { - return ReadMatrix(null); - } - - /// - /// Reads the named matrix from the file or stream. - /// - /// The name of the matrix to read. - /// - /// A sparse or dense matrix depending on how the matrix - /// is defined in the Matlab file. - /// is returned if a matrix with the requests name doesn't exist. - /// - public Matrix 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(stream, names); - var file = parser.Parse(); - - Matrix 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; - } - - /// - /// Reads all matrices from the file or stream. - /// - /// All matrices from the file or stream. The key to the - /// is the matrix's name. - public IDictionary> ReadMatrices() - { - return ReadMatrices(new string[] { }); - } - - /// - /// Reads the named matrices from the file or stream. - /// - /// The names of the matrices to retrieve. - /// - /// The named matrices from the file or stream. The key to the - /// is the matrix's name. - public IDictionary> ReadMatrices(IEnumerable 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(stream, names); - var file = parser.Parse(); - - if (_filename != null) - { - stream.Close(); - stream.Dispose(); - } - - return file.Matrices.ToDictionary(matrix => matrix.Key, matrix => matrix.Value); - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/MatlabWriter.cs b/src/Numerics.IO/LinearAlgebra/IO/MatlabWriter.cs deleted file mode 100644 index 4824a7dc..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/MatlabWriter.cs +++ /dev/null @@ -1,825 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Writes matrices to a Matlab file. - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class MatlabMatrixWriter : IDisposable - { - /// - /// The file header value - /// - private const string HeaderText = "MATLAB 5.0 MAT-file, Platform: .NET 4 - Math.NET Numerics, Created on: "; - - /// - /// The length of the header text. - /// - private const int HeaderTextLength = 116; - - /// - /// Have we written the header yet. - /// - private bool _headerWritten; - - /// - /// The binary writer to write to. - /// - private BinaryWriter _writer; - - /// - /// Initializes a new instance of the class. - /// - /// The name of the Matlab file to save the matrices to. - 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))); - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - if (_writer != null) - { - _writer.Flush(); - _writer.Close(); - _writer = null; - } - } - - /// - /// Writes the given to the file. - /// - /// The matrix to write. - /// The name of the matrix to store in the file. - /// If either or is null. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public void WriteMatrix(Matrix matrix, string name) where TDataType : struct, IEquatable, 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); - } - - /// - /// Writes the given to the file. - /// - /// The matrices to write. - /// The names of the matrices to store in the file. - /// If either or is null. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public void WriteMatrices(IList> matrices, IList names) where TDataType : struct, IEquatable, 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]); - } - } - - /// - /// Closes the stream the being written to. - /// - /// Calls . - public void Close() - { - Dispose(); - } - - /// - /// Writes the matrix tag and name. - /// - /// The writer we are using. - /// The array class we are writing. - /// if set to true if this a complex matrix. - /// The name name of the matrix. - /// The number of rows. - /// The columns of columns. - /// The maximum number of non-zero elements. - 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); - } - } - - /// - /// Compresses the data array. - /// - /// The data to compress. - /// The compressed data. - 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(); - } - } - - /// - /// Gets the dense data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - private static byte[] GetDenseDataArray(Matrix 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; - } - - /// - /// Gets the dense data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - private static byte[] GetDenseDataArray(Matrix 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; - } - - /// - /// Gets the dense data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - private static byte[] GetDenseDataArray(Matrix 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; - } - - /// - /// Gets the dense data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - private static byte[] GetDenseDataArray(Matrix 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; - } - - /// - /// Gets the sparse data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - 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)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; - } - - /// - /// Gets the sparse data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - 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)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; - } - - /// - /// Gets the sparse data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - 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)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; - } - - /// - /// Gets the sparse data array. - /// - /// The matrix to get the data from. - /// The name of the matrix. - /// The matrix data as an array. - 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)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; - } - - /// - /// Writes the compressed data. - /// - /// The data to write. - 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); - } - - /// - /// Writes the file header. - /// - 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); - } - - /// - /// Pads the data with the given byte. - /// - /// Where to write the pad values. - /// The number of bytes to pad. - /// What value to pad with. - private static void PadData(BinaryWriter writer, int bytes, byte pad = (byte)0) - { - for (int i = 0; i < bytes; i++) - { - writer.Write(pad); - } - } - } -} \ No newline at end of file diff --git a/src/Numerics.IO/LinearAlgebra/IO/MatrixReader.cs b/src/Numerics.IO/LinearAlgebra/IO/MatrixReader.cs deleted file mode 100644 index e30e9485..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/MatrixReader.cs +++ /dev/null @@ -1,88 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.IO -{ - using System; - using System.IO; - using Generic; - using Properties; - - /// - /// Base class to read a single from a file or stream. - /// - /// The type of Matrix to return. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public abstract class MatrixReader - where TMatrix : Matrix - where TDataType : struct, IEquatable, IFormattable - { - /// - /// Reads a from a file. - /// - /// The file to read the matrix from. - /// A containing the data from the file. is returned if the file is empty. - /// If is . - /// If the file doesn't exist. - /// If a value is not a number or not in a valid format. - /// If a value represents a number less than or greater than . - public TMatrix ReadMatrix(string file) - { - if (file == null) - { - throw new ArgumentNullException("file", Resources.StringNullOrEmpty); - } - - return ReadMatrix(File.OpenRead(file)); - } - - /// - /// Reads a from a . - /// - /// The to read the matrix from. - /// A matrix containing the data from the . is returned if the is empty. - /// If is . - /// If a value is not a number or not in a valid format. - /// If a value represents a number less than or greater than . - public TMatrix ReadMatrix(Stream stream) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - return DoReadMatrix(stream); - } - - /// - /// Subclasses override this method to do the actual reading. - /// - /// The to read the matrix from. - /// A matrix containing the data from the . is returned if the is empty. - protected abstract TMatrix DoReadMatrix(Stream stream); - } -} diff --git a/src/Numerics.IO/LinearAlgebra/IO/MatrixWriter.cs b/src/Numerics.IO/LinearAlgebra/IO/MatrixWriter.cs deleted file mode 100644 index 214115e1..00000000 --- a/src/Numerics.IO/LinearAlgebra/IO/MatrixWriter.cs +++ /dev/null @@ -1,164 +0,0 @@ -// -// 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. -// - -using System; -using System.IO; - -namespace MathNet.Numerics.LinearAlgebra.IO -{ - using System.Globalization; - using Generic; - - /// - /// Base class to write a single to a file or stream. - /// - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public abstract class MatrixWriter - { - /// - /// The to use. - /// - private CultureInfo _cultureInfo = CultureInfo.CurrentCulture; - - /// - /// Gets or sets the to use when parsing the numbers. - /// - /// The culture info. - /// Defaults to CultureInfo.CurrentCulture. - /// This property is only used for matrix writers that write out text files. - public CultureInfo CultureInfo - { - get - { - return _cultureInfo; - } - - set - { - if (value != null) - { - _cultureInfo = value; - } - } - } - - /// - /// Gets or sets he number format to use. - /// - /// The number format to use when writing out each element. - /// This property is only used for matrix writers that write out text files. - public string Format - { - get; - set; - } - - /// - /// Writes the given to the given file. If the file already exists, - /// the file will be overwritten. - /// - /// The matrix to write. - /// The file to write the matrix to. - /// If either or is null. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public void WriteMatrix(Matrix matrix, string file) where TDataType : struct, IEquatable, 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); - } - } - - /// - /// Writes the given to the given stream. - /// - /// The matrix to write. - /// The to write the matrix to. - /// If either or is null. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public void WriteMatrix(Matrix matrix, Stream stream) where TDataType : struct, IEquatable, 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); - } - } - - /// - /// Writes the given to the given . - /// - /// The matrix to write. - /// The to write the matrix to. - /// If either or is null. - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public void WriteMatrix(Matrix matrix, TextWriter writer) where TDataType : struct, IEquatable, IFormattable - { - if (matrix == null) - { - throw new ArgumentNullException("matrix"); - } - - if (writer == null) - { - throw new ArgumentNullException("writer"); - } - - DoWriteMatrix(matrix, writer, Format, _cultureInfo); - } - - /// - /// Subclasses must implement this method to do the actually writing. - /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - /// The matrix to serialize. - /// The to write the matrix to. - /// The number format to use. - /// The culture to use. - protected abstract void DoWriteMatrix(Matrix matrix, TextWriter writer, string format, CultureInfo cultureInfo) where TDataType : struct, IEquatable, IFormattable; - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Single/IO/DelimitedReader.cs b/src/Numerics.IO/LinearAlgebra/Single/IO/DelimitedReader.cs deleted file mode 100644 index 9b69faee..00000000 --- a/src/Numerics.IO/LinearAlgebra/Single/IO/DelimitedReader.cs +++ /dev/null @@ -1,71 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.LinearAlgebra.Single.IO -{ - using System; - using Generic; - using LinearAlgebra.IO; - - /// - /// Creates a from a delimited text file. If the user does not - /// specify a delimiter, then any whitespace is used. - /// - /// The type of the matrix to return. - [Obsolete("Use MathNet.Numerics.Data.Text instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class DelimitedReader : DelimitedReader - where TMatrix : Matrix - { - /// - /// Initializes a new instance of the class using - /// any whitespace as the delimiter. - /// - public DelimitedReader() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The delimiter to use. - public DelimitedReader(char delimiter) : base(delimiter) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// The delimiter to use. - /// - /// - /// If is . - /// - public DelimitedReader(string delimiter) : base(delimiter) - { - } - } -} diff --git a/src/Numerics.IO/LinearAlgebra/Single/IO/MatlabReader.cs b/src/Numerics.IO/LinearAlgebra/Single/IO/MatlabReader.cs deleted file mode 100644 index 94bbcd4f..00000000 --- a/src/Numerics.IO/LinearAlgebra/Single/IO/MatlabReader.cs +++ /dev/null @@ -1,56 +0,0 @@ -// -// 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. -// - -using System; - -namespace MathNet.Numerics.LinearAlgebra.Single.IO -{ - using System.IO; - using LinearAlgebra.IO; - - /// - /// Creates matrices from Matlab files. - /// - [Obsolete("Use MathNet.Numerics.Data.Matlab instead (separate NuGet package). Scheduled for removal in v3.0.")] - public class MatlabMatrixReader : MatlabMatrixReader - { - /// - /// Initializes a new instance of the class. - /// - /// Name of the file to read matrices from. - public MatlabMatrixReader(string filename) : base(filename) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to reader matrices from. - public MatlabMatrixReader(Stream stream) : base(stream) - { - } - } -} diff --git a/src/Numerics.IO/Numerics.IO.csproj b/src/Numerics.IO/Numerics.IO.csproj deleted file mode 100644 index b23cab05..00000000 --- a/src/Numerics.IO/Numerics.IO.csproj +++ /dev/null @@ -1,101 +0,0 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE} - Library - Properties - MathNet.Numerics - MathNet.Numerics.IO - v4.0 - 512 - - ..\MathNet.Numerics.snk - false - - - ..\..\out\debug\Net40\ - - false - true - full - false - DEBUG;TRACE;SYSNUMERICS - prompt - 4 - - - ..\..\out\lib\Net40\ - ..\..\out\lib\Net40\MathNet.Numerics.IO.xml - - false - pdbonly - true - TRACE;SYSNUMERICS - prompt - 4 - AllRules.ruleset - - - ..\..\out\lib\Net40\ - ..\..\out\lib\Net40\MathNet.Numerics.IO.xml - - true - pdbonly - true - TRACE;SYSNUMERICS;STRONGNAME - prompt - 4 - AllRules.ruleset - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {B7CAE5F4-A23F-4438-B5BE-41226618B695} - Numerics - - - - - \ No newline at end of file diff --git a/src/Numerics.IO/Properties/AssemblyInfo.cs b/src/Numerics.IO/Properties/AssemblyInfo.cs deleted file mode 100644 index 95669881..00000000 --- a/src/Numerics.IO/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,53 +0,0 @@ -// -// 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. -// - -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")] diff --git a/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedReaderTests.cs deleted file mode 100644 index cb8283df..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedReaderTests.cs +++ /dev/null @@ -1,167 +0,0 @@ -// -// 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. -// -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; - - /// - /// Delimited reader tests. - /// - [TestFixture] - public class DelimitedReaderTests - { - /// - /// Can parse comma delimited data. - /// - [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(',') - { - 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]); - } - - /// - /// Can parse tab delimited data. - /// - [Test] - public void CanParseTabDelimitedData() - { - var data = "1" + Environment.NewLine - + "\"2.2\"\t\t0.3e1" + Environment.NewLine - + "'4'\t5\t6"; - - var reader = new DelimitedReader('\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]); - } - - /// - /// Can parse white space delimited data. - /// - [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 - { - 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]); - } - - /// - /// Can parse period delimited data. - /// - [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('.') - { - 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]); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs deleted file mode 100644 index b6f097c5..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex/IO/DelimitedWriterTests.cs +++ /dev/null @@ -1,139 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Delimited writer tests. - /// - [TestFixture] - public class DelimitedWriterTests - { - /// - /// Can write comma delimited data. - /// - [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); - } - - /// - /// Can write period delimited data. - /// - [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); - } - - /// - /// Can write space delimited data. - /// - [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); - } - - /// - /// Can write tab delimited data. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabReaderTests.cs deleted file mode 100644 index 5fdbedb5..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabReaderTests.cs +++ /dev/null @@ -1,155 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO -{ - using LinearAlgebra.Complex; - using LinearAlgebra.Complex.IO; - using NUnit.Framework; - - /// - /// Matlab matrix reader test. - /// - [TestFixture] - public class MatlabMatrixReaderTests - { - /// - /// Can read all complex matrices. - /// - [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); - } - - /// - /// Can read sparse complex matrices. - /// - [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); - } - - /// - /// Can read non-complex matrices. - /// - [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()); - } - } - - /// - /// Can read non-complex first matrix. - /// - [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); - } - - /// - /// Can read non-complex named matrices. - /// - [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()); - } - } - - /// - /// Can read non-complex named matrix. - /// - [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()); - } - - /// - /// Can read non-complex named sparse matrix. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs deleted file mode 100644 index ad69ea87..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs +++ /dev/null @@ -1,161 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Matlab matrix writer tests. - /// - [TestFixture] - public class MatlabMatrixWriterTests - { - /// - /// Invalid constructor throws ArgumentException. - /// - [Test] - public void InvalidConstructorThrowsArgumentException() - { - Assert.Throws(() => new MatlabMatrixWriter(string.Empty)); - Assert.Throws(() => new MatlabMatrixWriter(null)); - } - - /// - /// Write bad matrices throws ArgumentException. - /// - [Test] - public void WriteBadMatricesThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile3"); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); - writer.Dispose(); - } - - /// - /// Write null matrices throws ArgumentNullException. - /// - [Test] - public void WriteNullMatricesThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile4"); - Assert.Throws(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); - Matrix matrix = new DenseMatrix(1, 1); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, null)); - writer.Dispose(); - } - - /// - /// Can write matrices. - /// - [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)); - } - } - - /// - /// Write bad matrix throws ArgumentException. - /// - [Test] - public void WriteBadMatrixThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile1"); - Assert.Throws(() => writer.WriteMatrix(matrix, string.Empty)); - Assert.Throws(() => writer.WriteMatrix(matrix, null)); - writer.Dispose(); - } - - /// - /// Write null matrix throws ArgumentNullException. - /// - [Test] - public void WriteNullMatrixThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile2"); - Assert.Throws(() => writer.WriteMatrix(null, "matrix")); - writer.Dispose(); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedReaderTests.cs deleted file mode 100644 index 01ed3748..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedReaderTests.cs +++ /dev/null @@ -1,167 +0,0 @@ -// -// 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. -// -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; - - /// - /// Delimited reader tests. - /// - [TestFixture] - public class DelimitedReaderTests - { - /// - /// Can parse comma delimited data. - /// - [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(',') - { - 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]); - } - - /// - /// Can parse tab delimited data. - /// - [Test] - public void CanParseTabDelimitedData() - { - var data = "1" + Environment.NewLine - + "\"2.2\"\t\t0.3e1" + Environment.NewLine - + "'4'\t5\t6"; - - var reader = new DelimitedReader('\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]); - } - - /// - /// Can parse white space delimited data. - /// - [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 - { - 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]); - } - - /// - /// Can parse period delimited data. - /// - [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('.') - { - 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]); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs deleted file mode 100644 index 5c51f19f..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex32/IO/DelimitedWriterTests.cs +++ /dev/null @@ -1,139 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Delimited writer tests. - /// - [TestFixture] - public class DelimitedWriterTests - { - /// - /// Can write comma delimited data. - /// - [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); - } - - /// - /// Can write period delimited data. - /// - [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); - } - - /// - /// Can write space delimited data. - /// - [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); - } - - /// - /// Can write tab delimited data. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabReaderTests.cs deleted file mode 100644 index b0581010..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabReaderTests.cs +++ /dev/null @@ -1,155 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO -{ - using LinearAlgebra.Complex32; - using LinearAlgebra.Complex32.IO; - using NUnit.Framework; - - /// - /// Matlab matrix reader test. - /// - [TestFixture] - public class MatlabMatrixReaderTests - { - /// - /// Can read all complex matrices. - /// - [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); - } - - /// - /// Can read spapse complex matrices. - /// - [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); - } - - /// - /// Can read non-complex matrices. - /// - [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()); - } - } - - /// - /// Can read non-complex first matrix. - /// - [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); - } - - /// - /// Can read non-complex named matrices. - /// - [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()); - } - } - - /// - /// Can read non-complex named matrix. - /// - [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()); - } - - /// - /// Can read non-complex named sparse matrix. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs deleted file mode 100644 index 71aa7338..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs +++ /dev/null @@ -1,161 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Matlab matrix writer tests. - /// - [TestFixture] - public class MatlabMatrixWriterTests - { - /// - /// Invalid constructor throws ArgumentException. - /// - [Test] - public void InvalidConstructorThrowsArgumentException() - { - Assert.Throws(() => new MatlabMatrixWriter(string.Empty)); - Assert.Throws(() => new MatlabMatrixWriter(null)); - } - - /// - /// Write bad matrices throws ArgumentException. - /// - [Test] - public void WriteBadMatricesThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile3"); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); - writer.Dispose(); - } - - /// - /// Write null matrices throws ArgumentNullException. - /// - [Test] - public void WriteNullMatricesThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile4"); - Assert.Throws(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); - Matrix matrix = new DenseMatrix(1, 1); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, null)); - writer.Dispose(); - } - - /// - /// Can write matrices. - /// - [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)); - } - } - - /// - /// Write bad matrix throws ArgumentException. - /// - [Test] - public void WriteBadMatrixThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile1"); - Assert.Throws(() => writer.WriteMatrix(matrix, string.Empty)); - Assert.Throws(() => writer.WriteMatrix(matrix, null)); - writer.Dispose(); - } - - /// - /// Write null matrix throws ArgumentNullException. - /// - [Test] - public void WriteNullMatrixThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile2"); - Assert.Throws(() => writer.WriteMatrix(null, "matrix")); - writer.Dispose(); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedReaderTests.cs deleted file mode 100644 index 6092b222..00000000 --- a/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedReaderTests.cs +++ /dev/null @@ -1,162 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Delimited reader tests. - /// - [TestFixture] - public class DelimitedReaderTests - { - /// - /// Can parse comma delimited data. - /// - [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(',') - { - 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]); - } - - /// - /// Can parse tab delimited data. - /// - [Test] - public void CanParseTabDelimitedData() - { - var data = "1" + Environment.NewLine - + "\"2.2\"\t\t0.3e1" + Environment.NewLine - + "'4'\t5\t6"; - - var reader = new DelimitedReader('\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]); - } - - /// - /// Can parse white space delimited data. - /// - [Test] - public void CanParseWhiteSpaceDelimitedData() - { - var data = "1" + Environment.NewLine - + "\"2.2\" 0.3e1" + Environment.NewLine - + "'4' 5 6" + Environment.NewLine; - - var reader = new DelimitedReader - { - 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]); - } - - /// - /// Can parse period delimited data. - /// - [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('.') - { - 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]); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs deleted file mode 100644 index 8646e6a5..00000000 --- a/src/UnitTests/LinearAlgebraTests/Double/IO/DelimitedWriterTests.cs +++ /dev/null @@ -1,138 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO -{ - using LinearAlgebra.Double; - using LinearAlgebra.IO; - using NUnit.Framework; - using System; - using System.Globalization; - using System.IO; - - /// - /// Delimited writer tests. - /// - [TestFixture] - public class DelimitedWriterTests - { - /// - /// Can write comma delimited data. - /// - [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); - } - - /// - /// Can write period delimited data. - /// - [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); - } - - /// - /// Can write space delimited data. - /// - [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); - } - - /// - /// Can write tab delimited data. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/IO/MatlabReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Double/IO/MatlabReaderTests.cs deleted file mode 100644 index 197832cd..00000000 --- a/src/UnitTests/LinearAlgebraTests/Double/IO/MatlabReaderTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.IO -{ - using LinearAlgebra.Double; - using LinearAlgebra.Double.IO; - using NUnit.Framework; - - /// - /// Matlab matrix reader test. - /// - [TestFixture] - public class MatlabMatrixReaderTests - { - /// - /// Can read all matrices. - /// - [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()); - } - } - - /// - /// Can read first matrix. - /// - [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); - } - - /// - /// Can read named matrices. - /// - [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()); - } - } - - /// - /// Can read named matrix. - /// - [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()); - } - - /// - /// Can read named sparse matrix. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/IO/MatlabWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Double/IO/MatlabWriterTests.cs deleted file mode 100644 index f4c3c5db..00000000 --- a/src/UnitTests/LinearAlgebraTests/Double/IO/MatlabWriterTests.cs +++ /dev/null @@ -1,160 +0,0 @@ -// -// 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. -// - -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; - - /// - /// Matlab matrix writer tests. - /// - [TestFixture] - public class MatlabMatrixWriterTests - { - /// - /// Invalid constructor throws ArgumentException. - /// - [Test] - public void InvalidConstructorThrowsArgumentException() - { - Assert.Throws(() => new MatlabMatrixWriter(string.Empty)); - Assert.Throws(() => new MatlabMatrixWriter(null)); - } - - /// - /// Write bad matrices throws ArgumentException. - /// - [Test] - public void WriteBadMatricesThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile3"); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); - writer.Dispose(); - } - - /// - /// Write null matrices throws ArgumentNullException. - /// - [Test] - public void WriteNullMatricesThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile4"); - Assert.Throws(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); - Matrix matrix = new DenseMatrix(1, 1); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, null)); - writer.Dispose(); - } - - /// - /// Can write matrices. - /// - [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)); - } - } - - /// - /// Write bad matrix throws ArgumentException. - /// - [Test] - public void WriteBadMatrixThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile1"); - Assert.Throws(() => writer.WriteMatrix(matrix, string.Empty)); - Assert.Throws(() => writer.WriteMatrix(matrix, null)); - writer.Dispose(); - } - - /// - /// Write null matrix throws ArgumentNullException. - /// - [Test] - public void WriteNullMatrixThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile2"); - Assert.Throws(() => writer.WriteMatrix(null, "matrix")); - writer.Dispose(); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs index 283c87da..d90dc919 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs @@ -31,7 +31,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { using LinearAlgebra.Double; - using LinearAlgebra.Double.IO; using NUnit.Framework; using System; using System.Collections.Generic; @@ -304,39 +303,5 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double Assert.AreEqual(Order, matrix.ColumnCount); Assert.DoesNotThrow(() => matrix[0, 0] = 1); } - - [Test] - public void CanClearSubMatrixEx() - { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); - var matrix = dmr.ReadMatrix("S"); - var matrix2 = matrix.Clone(); - - // Zero the 40th row - for (int column = 0; column < matrix.ColumnCount; column++) - { - matrix.At(39, column, 0.0); - } - matrix2.ClearRow(39); - - // Zero the 4th column - for (int row = 0; row < matrix.RowCount; row++) - { - matrix.At(row, 3, 0.0); - } - matrix2.ClearColumn(3); - - // Zero submatrix rows 20..30 columns 8..10 - for (int row = 19; row <= 29; row++) - { - for (int column = 7; column <= 9; column++) - { - matrix.At(row, column, 0.0); - } - } - matrix2.ClearSubMatrix(19, 11, 7, 3); - - Assert.That(matrix2.Equals(matrix), Is.True); - } } } diff --git a/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedReaderTests.cs deleted file mode 100644 index 915e8769..00000000 --- a/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedReaderTests.cs +++ /dev/null @@ -1,162 +0,0 @@ -// -// 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. -// -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; - - /// - /// Delimited reader tests. - /// - [TestFixture] - public class DelimitedReaderTests - { - /// - /// Can parse comma delimited data. - /// - [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(',') - { - 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]); - } - - /// - /// Can parse tab delimited data. - /// - [Test] - public void CanParseTabDelimitedData() - { - var data = "1" + Environment.NewLine - + "\"2.2\"\t\t0.3e1" + Environment.NewLine - + "'4'\t5\t6"; - - var reader = new DelimitedReader('\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]); - } - - /// - /// Can parse white space delimited data. - /// - [Test] - public void CanParseWhiteSpaceDelimitedData() - { - var data = "1" + Environment.NewLine - + "\"2.2\" 0.3e1" + Environment.NewLine - + "'4' 5 6" + Environment.NewLine; - - var reader = new DelimitedReader - { - 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]); - } - - /// - /// Can parse period delimited data. - /// - [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('.') - { - 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]); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs deleted file mode 100644 index 6779c876..00000000 --- a/src/UnitTests/LinearAlgebraTests/Single/IO/DelimitedWriterTests.cs +++ /dev/null @@ -1,138 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO -{ - using LinearAlgebra.IO; - using LinearAlgebra.Single; - using NUnit.Framework; - using System; - using System.Globalization; - using System.IO; - - /// - /// Delimited writer tests. - /// - [TestFixture] - public class DelimitedWriterTests - { - /// - /// Can write comma delimited data. - /// - [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); - } - - /// - /// Can write period delimited data. - /// - [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); - } - - /// - /// Can write space delimited data. - /// - [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); - } - - /// - /// Can write tab delimited data. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Single/IO/MatlabReaderTests.cs b/src/UnitTests/LinearAlgebraTests/Single/IO/MatlabReaderTests.cs deleted file mode 100644 index 91d73ab4..00000000 --- a/src/UnitTests/LinearAlgebraTests/Single/IO/MatlabReaderTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -// -// 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. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.IO -{ - using LinearAlgebra.Single; - using LinearAlgebra.Single.IO; - using NUnit.Framework; - - /// - /// Matlab matrix reader test. - /// - [TestFixture] - public class MatlabMatrixReaderTests - { - /// - /// Can read all matrices. - /// - [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()); - } - } - - /// - /// Can read first matrix. - /// - [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); - } - - /// - /// Can read named matrices. - /// - [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()); - } - } - - /// - /// Can read named matrix. - /// - [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()); - } - - /// - /// Can read named sparse matrix. - /// - [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); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Single/IO/MatlabWriterTests.cs b/src/UnitTests/LinearAlgebraTests/Single/IO/MatlabWriterTests.cs deleted file mode 100644 index 694d002b..00000000 --- a/src/UnitTests/LinearAlgebraTests/Single/IO/MatlabWriterTests.cs +++ /dev/null @@ -1,159 +0,0 @@ -// -// 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. -// -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; - - /// - /// Matlab matrix writer tests. - /// - [TestFixture] - public class MatlabMatrixWriterTests - { - /// - /// Invalid constructor throws ArgumentException. - /// - [Test] - public void InvalidConstructorThrowsArgumentException() - { - Assert.Throws(() => new MatlabMatrixWriter(string.Empty)); - Assert.Throws(() => new MatlabMatrixWriter(null)); - } - - /// - /// Write bad matrices throws ArgumentException. - /// - [Test] - public void WriteBadMatricesThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile3"); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new string[] { null })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" })); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" })); - writer.Dispose(); - } - - /// - /// Write null matrices throws ArgumentNullException. - /// - [Test] - public void WriteNullMatricesThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile4"); - Assert.Throws(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" })); - Matrix matrix = new DenseMatrix(1, 1); - Assert.Throws(() => writer.WriteMatrices(new[] { matrix }, null)); - writer.Dispose(); - } - - /// - /// Can write matrices. - /// - [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)); - } - } - - /// - /// Write bad matrix throws ArgumentException. - /// - [Test] - public void WriteBadMatrixThrowsArgumentException() - { - Matrix matrix = new DenseMatrix(1, 1); - var writer = new MatlabMatrixWriter("somefile1"); - Assert.Throws(() => writer.WriteMatrix(matrix, string.Empty)); - Assert.Throws(() => writer.WriteMatrix(matrix, null)); - writer.Dispose(); - } - - /// - /// Write null matrix throws ArgumentNullException. - /// - [Test] - public void WriteNullMatrixThrowsArgumentNullException() - { - var writer = new MatlabMatrixWriter("somefile2"); - Assert.Throws(() => writer.WriteMatrix(null, "matrix")); - writer.Dispose(); - } - } -} diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index a0a08328..5bccbf55 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -200,18 +200,6 @@ Code - - Code - - - Code - - - Code - - - Code - Code @@ -348,18 +336,6 @@ Code - - Code - - - Code - - - Code - - - Code - Code @@ -498,18 +474,6 @@ Code - - Code - - - Code - - - Code - - - Code - Code @@ -646,18 +610,6 @@ Code - - Code - - - Code - - - Code - - - Code - Code @@ -801,10 +753,6 @@ - - {EB1A5D32-F264-4BCE-BEB7-0B97085075BE} - Numerics.IO - {B7CAE5F4-A23F-4438-B5BE-41226618B695} Numerics