diff --git a/src/Data/Matlab/Formatter.cs b/src/Data/Matlab/Formatter.cs index b62637de..6b8df327 100644 --- a/src/Data/Matlab/Formatter.cs +++ b/src/Data/Matlab/Formatter.cs @@ -234,7 +234,7 @@ namespace MathNet.Numerics.Data.Matlab where T : struct, IEquatable, IFormattable { var transposed = matrix.Transpose(); - var storage = ((SparseCompressedRowMatrixStorage)transposed.Storage); + var storage = (SparseCompressedRowMatrixStorage)transposed.Storage; bool smallBlock; int nzcount = storage.ValueCount; diff --git a/src/Data/Matlab/MatlabReader.cs b/src/Data/Matlab/MatlabReader.cs index eb4a6cf0..c8c1fc54 100644 --- a/src/Data/Matlab/MatlabReader.cs +++ b/src/Data/Matlab/MatlabReader.cs @@ -63,25 +63,25 @@ namespace MathNet.Numerics.Data.Matlab /// /// Unpacks the matrix of a MATLAB matrix data object. /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Matrix Unpack(MatlabMatrix matrixData) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix Unpack(MatlabMatrix matrixData) + where T : struct, IEquatable, IFormattable { - return Parser.ParseMatrix(matrixData.Data); + return Parser.ParseMatrix(matrixData.Data); } /// /// Read the first or a specific matrix from a MATLAB file stream. /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Matrix Read(Stream stream, string matrixName = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix Read(Stream stream, string matrixName = null) + where T : struct, IEquatable, IFormattable { var matrices = List(stream); if (string.IsNullOrEmpty(matrixName)) { - return Unpack(matrices.First()); + return Unpack(matrices.First()); } var matrix = matrices.Find(m => m.Name == matrixName); @@ -90,45 +90,45 @@ namespace MathNet.Numerics.Data.Matlab throw new KeyNotFoundException("Matrix with the provided name was not found."); } - return Unpack(matrix); + return Unpack(matrix); } /// /// Read the first or a specific matrix from a MATLAB file. /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Matrix Read(string filePath, string matrixName = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix Read(string filePath, string matrixName = null) + where T : struct, IEquatable, IFormattable { using (var stream = File.OpenRead(filePath)) { - return Read(stream, matrixName); + return Read(stream, matrixName); } } /// /// Read all matrices or those with matching name from a MATLAB file stream. /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Dictionary> ReadAll(Stream stream, params string[] matrixNames) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Dictionary> ReadAll(Stream stream, params string[] matrixNames) + where T : struct, IEquatable, IFormattable { var names = new HashSet(matrixNames); return List(stream) .Where(m => names.Count == 0 || names.Contains(m.Name)) - .ToDictionary(m => m.Name, Unpack); + .ToDictionary(m => m.Name, Unpack); } /// /// Read all matrices or those with matching name from a MATLAB file. /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Dictionary> ReadAll(string filePath, params string[] matrixNames) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Dictionary> ReadAll(string filePath, params string[] matrixNames) + where T : struct, IEquatable, IFormattable { using (var stream = File.OpenRead(filePath)) { - return ReadAll(stream, matrixNames); + return ReadAll(stream, matrixNames); } } } diff --git a/src/Data/Matlab/MatlabWriter.cs b/src/Data/Matlab/MatlabWriter.cs index ece9edbd..8c552f64 100644 --- a/src/Data/Matlab/MatlabWriter.cs +++ b/src/Data/Matlab/MatlabWriter.cs @@ -54,30 +54,30 @@ namespace MathNet.Numerics.Data.Matlab } } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static MatlabMatrix Pack(Matrix matrix, string matrixName) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static MatlabMatrix Pack(Matrix matrix, string matrixName) + where T : struct, IEquatable, IFormattable { return Formatter.FormatMatrix(matrix, matrixName); } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static void Write(Stream stream, Matrix matrix, string matrixName) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static void Write(Stream stream, Matrix matrix, string matrixName) + where T : struct, IEquatable, IFormattable { Store(stream, new[] { Pack(matrix, matrixName) }); } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static void Write(string filePath, Matrix matrix, string matrixName) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static void Write(string filePath, Matrix matrix, string matrixName) + where T : struct, IEquatable, IFormattable { Store(filePath, new[] { Pack(matrix, matrixName) }); } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static void Write(Stream stream, IList> matrices, IList names) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static void Write(Stream stream, IList> matrices, IList names) + where T : struct, IEquatable, IFormattable { if (matrices.Count != names.Count) { @@ -87,9 +87,9 @@ namespace MathNet.Numerics.Data.Matlab Store(stream, matrices.Zip(names, Pack)); } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static void Write(string filePath, IList> matrices, IList names) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static void Write(string filePath, IList> matrices, IList names) + where T : struct, IEquatable, IFormattable { if (matrices.Count != names.Count) { @@ -99,16 +99,16 @@ namespace MathNet.Numerics.Data.Matlab Store(filePath, matrices.Zip(names, Pack)); } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static void Write(Stream stream, IEnumerable>> matrices) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static void Write(Stream stream, IEnumerable>> matrices) + where T : struct, IEquatable, IFormattable { Store(stream, matrices.Select(kv => Pack(kv.Value, kv.Key))); } - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static void Write(string filePath, IEnumerable>> matrices) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static void Write(string filePath, IEnumerable>> matrices) + where T : struct, IEquatable, IFormattable { Store(filePath, matrices.Select(kv => Pack(kv.Value, kv.Key))); } diff --git a/src/Data/Matlab/Parser.cs b/src/Data/Matlab/Parser.cs index 909f4dba..1d9dbfde 100644 --- a/src/Data/Matlab/Parser.cs +++ b/src/Data/Matlab/Parser.cs @@ -218,7 +218,7 @@ namespace MathNet.Numerics.Data.Matlab ReadElementTag(reader, out type, out size, out smallBlock); // direct copy if possible - if (type == DataType.Double && dataType == typeof(double) || type == DataType.Single && dataType == typeof(float)) + if ((type == DataType.Double && dataType == typeof(double)) || (type == DataType.Single && dataType == typeof(float))) { Buffer.BlockCopy(reader.ReadBytes(size), 0, data, 0, size); } diff --git a/src/Data/Text/DelimitedReader.cs b/src/Data/Text/DelimitedReader.cs index 3f54bad3..d5f7b08c 100644 --- a/src/Data/Text/DelimitedReader.cs +++ b/src/Data/Text/DelimitedReader.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2013 Math.NET +// Copyright (c) 2009-2014 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -44,7 +44,7 @@ namespace MathNet.Numerics.Data.Text /// Creates a from a delimited text file. If the user does not /// specify a delimiter, then any whitespace is used. /// - public class DelimitedReader + public static class DelimitedReader { /// /// The base regular expression. @@ -56,83 +56,18 @@ namespace MathNet.Numerics.Data.Text /// static readonly ConcurrentDictionary RegexCache = new ConcurrentDictionary(); - /// - /// The delimiter to use for parsing. Defaults to any whitespace. - /// - public string Delimiter { get; set; } - - /// - /// Whether to create sparse matrices or not. Defaults to false. - /// - public bool Sparse { get; set; } - - /// - /// 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; } - - /// - /// Gets or sets the to use when parsing the numbers. - /// - /// The culture info. - /// Defaults to CultureInfo.CurrentCulture. - public IFormatProvider FormatProvider { get; set; } - - /// - /// Performs the actual reading. - /// - /// The path and name of the file to read the matrix from. - /// - /// A matrix containing the data from the . - /// - public Matrix ReadMatrix(string filePath) - where TDataType : struct, IEquatable, IFormattable - { - return ReadFile(filePath, Sparse, Delimiter, HasHeaderRow, FormatProvider); - } - - /// - /// Performs the actual reading. - /// - /// The to read the matrix from. - /// - /// A matrix containing the data from the . - /// - public Matrix ReadMatrix(Stream stream) - where TDataType : struct, IEquatable, IFormattable - { - return ReadStream(stream, Sparse, Delimiter, HasHeaderRow, FormatProvider); - } - - /// - /// Performs the actual reading. - /// - /// The to read the matrix from. - /// - /// A matrix containing the data from the . - /// - public Matrix ReadMatrix(TextReader reader) - where TDataType : struct, IEquatable, IFormattable - { - return Read(reader, Sparse, Delimiter, HasHeaderRow, FormatProvider); - } - /// /// Reads a from the given . /// /// The to read the matrix from. - /// Whether the the returned matrix should be constructed as sparse (true) or dense (false). Default: false. + /// Whether the returned matrix should be constructed as sparse (true) or dense (false). Default: false. /// Number delimiter between numbers of the same line. Supports Regex groups. Default: "\s" (white space). /// Whether the first row contains column headers or not. Default: false. /// The culture to use. Default: null. /// A matrix containing the data from the . - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Matrix Read(TextReader reader, bool sparse = false, string delimiter = @"\s", bool hasHeaders = false, IFormatProvider formatProvider = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix Read(TextReader reader, bool sparse = false, string delimiter = @"\s", bool hasHeaders = false, IFormatProvider formatProvider = null) + where T : struct, IEquatable, IFormattable { if (string.IsNullOrEmpty(delimiter)) { @@ -173,8 +108,8 @@ namespace MathNet.Numerics.Data.Text line = reader.ReadLine(); } - var parse = CreateParser(formatProvider); - var matrix = sparse ? Matrix.Build.Sparse(data.Count, max) : Matrix.Build.Dense(data.Count, max); + var parse = CreateParser(formatProvider); + var matrix = sparse ? Matrix.Build.Sparse(data.Count, max) : Matrix.Build.Dense(data.Count, max); var storage = matrix.Storage; for (var i = 0; i < data.Count; i++) @@ -198,18 +133,18 @@ namespace MathNet.Numerics.Data.Text /// Reads a from the given file. /// /// The path and name of the file to read the matrix from. - /// Whether the the returned matrix should be constructed as sparse (true) or dense (false). Default: false. + /// Whether the returned matrix should be constructed as sparse (true) or dense (false). Default: false. /// Number delimiter between numbers of the same line. Supports Regex groups. Default: "\s" (white space). /// Whether the first row contains column headers or not. Default: false. /// The culture to use. Default: null. /// A matrix containing the data from the . - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Matrix ReadFile(string filePath, bool sparse = false, string delimiter = @"\s", bool hasHeaders = false, IFormatProvider formatProvider = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix Read(string filePath, bool sparse = false, string delimiter = @"\s", bool hasHeaders = false, IFormatProvider formatProvider = null) + where T : struct, IEquatable, IFormattable { using (var reader = new StreamReader(filePath)) { - return Read(reader, sparse, delimiter, hasHeaders, formatProvider); + return Read(reader, sparse, delimiter, hasHeaders, formatProvider); } } @@ -217,18 +152,18 @@ namespace MathNet.Numerics.Data.Text /// Reads a from the given . /// /// The to read the matrix from. - /// Whether the the returned matrix should be constructed as sparse (true) or dense (false). Default: false. + /// Whether the returned matrix should be constructed as sparse (true) or dense (false). Default: false. /// Number delimiter between numbers of the same line. Supports Regex groups. Default: "\s" (white space). /// Whether the first row contains column headers or not. Default: false. /// The culture to use. Default: null. /// A matrix containing the data from the . - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public static Matrix ReadStream(Stream stream, bool sparse = false, string delimiter = @"\s", bool hasHeaders = false, IFormatProvider formatProvider = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix Read(Stream stream, bool sparse = false, string delimiter = @"\s", bool hasHeaders = false, IFormatProvider formatProvider = null) + where T : struct, IEquatable, IFormattable { using (var reader = new StreamReader(stream)) { - return Read(reader, sparse, delimiter, hasHeaders, formatProvider); + return Read(reader, sparse, delimiter, hasHeaders, formatProvider); } } diff --git a/src/Data/Text/DelimitedWriter.cs b/src/Data/Text/DelimitedWriter.cs index fa88c6a6..42e09012 100644 --- a/src/Data/Text/DelimitedWriter.cs +++ b/src/Data/Text/DelimitedWriter.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2013 Math.NET +// Copyright (c) 2009-2014 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -39,97 +39,21 @@ namespace MathNet.Numerics.Data.Text /// Writes an to delimited text file. If the user does not /// specify a delimiter, a tab separator is used. /// - public class DelimitedWriter + public static class DelimitedWriter { - /// - /// Initializes a new instance of the class, using a tabulator as the delimiter. - /// - public DelimitedWriter() - { - Delimiter = "\t"; - } - - /// - /// Initializes a new instance of the class, using the given delimiter. - /// - public DelimitedWriter(string delimiter) - { - Delimiter = delimiter; - } - - /// - /// The delimiter to use. - /// - public string Delimiter { get; private set; } - - /// - /// Gets or sets column headers. Headers are only written if the header list is neither null nor empty. - /// - public IList ColumnHeaders { get; set; } - - /// - /// Gets or sets the number format to use when writing numbers. - /// - public string Format { get; set; } - - /// - /// Gets or sets the number format culture provider to use when writing numbers. - /// - public IFormatProvider FormatProvider { 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 filePath) - where TDataType : struct, IEquatable, IFormattable - { - WriteFile(matrix, filePath, Delimiter, ColumnHeaders, Format, FormatProvider); - } - - /// - /// 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 - { - WriteStream(matrix, stream, Delimiter, ColumnHeaders, Format, FormatProvider); - } - - /// - /// 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 - { - Write(matrix, writer, Delimiter, ColumnHeaders, Format, FormatProvider); - } - /// /// Writes a matrix to the given TextWriter. Optionally accepts custom column headers, delimiter, number format and culture. /// - /// The matrix to write. /// The to write the matrix to. + /// The matrix to write. /// Number delimiter to write between numbers of the same line. Default: "\t" (tabulator). /// Custom column header. Headers are only written if non-null and non-empty headers are provided. Default: null. /// The number format to use on each element. Default: null. /// The culture to use. Default: null. /// If either or is null. - /// The data type of the Matrix. It can be either: Double, Single, Complex, or Complex32. - public static void Write(Matrix matrix, TextWriter writer, string delimiter = "\t", IList columnHeaders = null, string format = null, IFormatProvider formatProvider = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: Double, Single, Complex, or Complex32. + public static void Write(TextWriter writer, Matrix matrix, string delimiter = "\t", IList columnHeaders = null, string format = null, IFormatProvider formatProvider = null) + where T : struct, IEquatable, IFormattable { if (matrix == null) { @@ -175,40 +99,40 @@ namespace MathNet.Numerics.Data.Text /// /// Writes a matrix to the given file. Optionally accepts custom column headers, delimiter, number format and culture. /// - /// The matrix to write. /// The path and name of the file to write the matrix to. If the file already exists, the file will be overwritten. + /// The matrix to write. /// Number delimiter to write between numbers of the same line. Default: "\t" (tabulator). /// Custom column header. Headers are only written if non-null and non-empty headers are provided. Default: null. /// The number format to use on each element. Default: null. /// The culture to use. Default: null. /// If either or is null. - /// The data type of the Matrix. It can be either: Double, Single, Complex, or Complex32. - public static void WriteFile(Matrix matrix, string filePath, string delimiter = "\t", IList columnHeaders = null, string format = null, IFormatProvider formatProvider = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: Double, Single, Complex, or Complex32. + public static void Write(string filePath, Matrix matrix, string delimiter = "\t", IList columnHeaders = null, string format = null, IFormatProvider formatProvider = null) + where T : struct, IEquatable, IFormattable { using (var writer = new StreamWriter(filePath)) { - Write(matrix, writer, delimiter, columnHeaders, format, formatProvider); + Write(writer, matrix, delimiter: delimiter, columnHeaders: columnHeaders, format: format, formatProvider: formatProvider); } } /// /// Writes a matrix to the given stream. Optionally accepts custom column headers, delimiter, number format and culture. /// - /// The matrix to write. /// The to write the matrix to. + /// The matrix to write. /// Number delimiter to write between numbers of the same line. Default: "\t" (tabulator). /// Custom column header. Headers are only written if non-null and non-empty headers are provided. Default: null. /// The number format to use on each element. Default: null. /// The culture to use. Default: null. /// If either or is null. - /// The data type of the Matrix. It can be either: Double, Single, Complex, or Complex32. - public static void WriteStream(Matrix matrix, Stream stream, string delimiter = "\t", IList columnHeaders = null, string format = null, IFormatProvider formatProvider = null) - where TDataType : struct, IEquatable, IFormattable + /// The data type of the Matrix. It can be either: Double, Single, Complex, or Complex32. + public static void Write(Stream stream, Matrix matrix, string delimiter = "\t", IList columnHeaders = null, string format = null, IFormatProvider formatProvider = null) + where T : struct, IEquatable, IFormattable { using (var writer = new StreamWriter(stream)) { - Write(matrix, writer, delimiter, columnHeaders, format, formatProvider); + Write(writer, matrix, delimiter: delimiter, columnHeaders: columnHeaders, format: format, formatProvider: formatProvider); } } } diff --git a/src/DataUnitTests/Text/DelimitedReaderTests.cs b/src/DataUnitTests/Text/DelimitedReaderTests.cs index 49ffafd1..08483857 100644 --- a/src/DataUnitTests/Text/DelimitedReaderTests.cs +++ b/src/DataUnitTests/Text/DelimitedReaderTests.cs @@ -55,14 +55,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text + "\"2.2\",0.3e1" + Environment.NewLine + "'4',5,6" + Environment.NewLine; - var reader = new DelimitedReader - { - Delimiter = ",", - HasHeaderRow = true, - FormatProvider = CultureInfo.InvariantCulture - }; - - var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); + var matrix = DelimitedReader.Read(new MemoryStream(Encoding.UTF8.GetBytes(data)), delimiter: ",", hasHeaders: true, formatProvider: CultureInfo.InvariantCulture); Assert.AreEqual(3, matrix.RowCount); Assert.AreEqual(3, matrix.ColumnCount); Assert.AreEqual(1.0, matrix[0, 0]); @@ -86,7 +79,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text + "\"2.2\"\t\t0.3e1" + Environment.NewLine + "'4'\t5\t6"; - var matrix = DelimitedReader.ReadStream(new MemoryStream(Encoding.UTF8.GetBytes(data)), delimiter: "\t", formatProvider: CultureInfo.InvariantCulture); + var matrix = DelimitedReader.Read(new MemoryStream(Encoding.UTF8.GetBytes(data)), delimiter: "\t", formatProvider: CultureInfo.InvariantCulture); Assert.AreEqual(3, matrix.RowCount); Assert.AreEqual(3, matrix.ColumnCount); Assert.AreEqual(1.0f, matrix[0, 0]); @@ -110,12 +103,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text + "\"2.2\" 0.3e1" + Environment.NewLine + "'4' 5 6" + Environment.NewLine; - var reader = new DelimitedReader - { - FormatProvider = CultureInfo.InvariantCulture - }; - - var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); + var matrix = DelimitedReader.Read(new MemoryStream(Encoding.UTF8.GetBytes(data)), formatProvider: CultureInfo.InvariantCulture); Assert.AreEqual(3, matrix.RowCount); Assert.AreEqual(3, matrix.ColumnCount); Assert.AreEqual(1.0f, matrix[0, 0]); @@ -140,13 +128,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text + "\"2,2\".0,3e1" + Environment.NewLine + "'4,0'.5,0.6,0" + Environment.NewLine; - var reader = new DelimitedReader - { - Delimiter = ".", - HasHeaderRow = true, - FormatProvider = new CultureInfo("tr-TR") - }; - var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); + var matrix = DelimitedReader.Read(new MemoryStream(Encoding.UTF8.GetBytes(data)), delimiter: ".", hasHeaders: true, formatProvider: new CultureInfo("tr-TR")); Assert.AreEqual(3, matrix.RowCount); Assert.AreEqual(3, matrix.ColumnCount); Assert.AreEqual(1.0, matrix[0, 0]); @@ -199,14 +181,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text + "\"2.2\",0.3e1" + Environment.NewLine + "'(4,-5)',5,6" + Environment.NewLine; - var reader = new DelimitedReader - { - Delimiter = ",", - HasHeaderRow = true, - FormatProvider = CultureInfo.InvariantCulture - }; - - var matrix = reader.ReadMatrix(new MemoryStream(Encoding.UTF8.GetBytes(data))); + var matrix = DelimitedReader.Read(new MemoryStream(Encoding.UTF8.GetBytes(data)), delimiter: ",", hasHeaders: true, formatProvider: CultureInfo.InvariantCulture); Assert.AreEqual(3, matrix.RowCount); Assert.AreEqual(3, matrix.ColumnCount); Assert.AreEqual(1.0f, matrix[0, 0].Real); diff --git a/src/DataUnitTests/Text/DelimitedWriterTests.cs b/src/DataUnitTests/Text/DelimitedWriterTests.cs index 66c3ce3a..bc364ff3 100644 --- a/src/DataUnitTests/Text/DelimitedWriterTests.cs +++ b/src/DataUnitTests/Text/DelimitedWriterTests.cs @@ -54,12 +54,8 @@ namespace MathNet.Numerics.Data.UnitTests.Text {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(",") - { - FormatProvider = CultureInfo.InvariantCulture - }; var stream = new MemoryStream(); - writer.WriteMatrix(matrix, stream); + DelimitedWriter.Write(stream, matrix, ",", formatProvider: CultureInfo.InvariantCulture); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); @@ -79,12 +75,8 @@ namespace MathNet.Numerics.Data.UnitTests.Text {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(",") - { - FormatProvider = CultureInfo.InvariantCulture - }; var stream = new MemoryStream(); - writer.WriteMatrix(matrix, stream); + DelimitedWriter.Write(stream, matrix, ",", formatProvider: CultureInfo.InvariantCulture); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); @@ -98,12 +90,8 @@ namespace MathNet.Numerics.Data.UnitTests.Text public void CanWriteCommaDelimitedDoubleData() { 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(",") - { - FormatProvider = CultureInfo.InvariantCulture - }; var stream = new MemoryStream(); - writer.WriteMatrix(matrix, stream); + DelimitedWriter.Write(stream, matrix, ",", formatProvider: CultureInfo.InvariantCulture); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); @@ -117,12 +105,8 @@ namespace MathNet.Numerics.Data.UnitTests.Text public void CanWriteCommaDelimitedSingleData() { var matrix = LinearAlgebra.Single.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(",") - { - FormatProvider = CultureInfo.InvariantCulture - }; var stream = new MemoryStream(); - writer.WriteMatrix(matrix, stream); + DelimitedWriter.Write(stream, matrix, ",", formatProvider: CultureInfo.InvariantCulture); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); @@ -140,7 +124,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text { var matrix = DenseMatrix.OfArray(new[,] {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}, {7.7, 8.8, 9.9}}); var stream = new MemoryStream(); - DelimitedWriter.WriteStream(matrix, stream, ".", formatProvider: new CultureInfo("tr-TR")); + DelimitedWriter.Write(stream, matrix, delimiter: ".", formatProvider: new CultureInfo("tr-TR")); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd(); @@ -158,7 +142,7 @@ namespace MathNet.Numerics.Data.UnitTests.Text { var matrix = SparseMatrix.OfArray(new[,] {{1.1, 0, 0}, {0, 5.5, 0}, {0, 0, 9.9}}); var stream = new MemoryStream(); - DelimitedWriter.WriteStream(matrix, stream, " "); + DelimitedWriter.Write(stream, matrix, " "); var data = stream.ToArray(); var reader = new StreamReader(new MemoryStream(data)); var text = reader.ReadToEnd();