forked from tsai/mathnet-numerics
15 changed files with 292 additions and 520 deletions
@ -1,168 +0,0 @@ |
|||||
// <copyright file="DelimitedWriter.cs" company="Math.NET">
|
|
||||
// Math.NET Numerics, part of the Math.NET Project
|
|
||||
// http://numerics.mathdotnet.com
|
|
||||
// http://github.com/mathnet/mathnet-numerics
|
|
||||
// http://mathnetnumerics.codeplex.com
|
|
||||
//
|
|
||||
// Copyright (c) 2009-2010 Math.NET
|
|
||||
//
|
|
||||
// Permission is hereby granted, free of charge, to any person
|
|
||||
// obtaining a copy of this software and associated documentation
|
|
||||
// files (the "Software"), to deal in the Software without
|
|
||||
// restriction, including without limitation the rights to use,
|
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||
// copies of the Software, and to permit persons to whom the
|
|
||||
// Software is furnished to do so, subject to the following
|
|
||||
// conditions:
|
|
||||
// The above copyright notice and this permission notice shall be
|
|
||||
// included in all copies or substantial portions of the Software.
|
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
|
||||
// </copyright>
|
|
||||
|
|
||||
namespace MathNet.Numerics.LinearAlgebra.Single.IO |
|
||||
{ |
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Globalization; |
|
||||
using System.IO; |
|
||||
using Generic; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes an <see cref="Matrix{T}"/> to delimited text file. If the user does not
|
|
||||
/// specify a delimiter, a tab separator is used.
|
|
||||
/// </summary>
|
|
||||
public class DelimitedWriter : MatrixWriter |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// The delimiter to use.
|
|
||||
/// </summary>
|
|
||||
private readonly string _delimiter; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// The <see cref="CultureInfo"/> to use.
|
|
||||
/// </summary>
|
|
||||
private CultureInfo _cultureInfo = CultureInfo.CurrentCulture; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="DelimitedWriter"/> class using
|
|
||||
/// a comma as the delimiter.
|
|
||||
/// </summary>
|
|
||||
public DelimitedWriter() |
|
||||
{ |
|
||||
_delimiter = ","; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="DelimitedWriter"/> class
|
|
||||
/// using the given delimiter.
|
|
||||
/// </summary>
|
|
||||
/// <param name="delimiter">
|
|
||||
/// the delimiter to use.
|
|
||||
/// </param>
|
|
||||
public DelimitedWriter(char delimiter) |
|
||||
{ |
|
||||
_delimiter = new string(delimiter, 1); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="DelimitedWriter"/> class
|
|
||||
/// using the given delimiter.
|
|
||||
/// </summary>
|
|
||||
/// <param name="delimiter">
|
|
||||
/// the delimiter to use.
|
|
||||
/// </param>
|
|
||||
public DelimitedWriter(string delimiter) |
|
||||
{ |
|
||||
_delimiter = delimiter; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the column header values.
|
|
||||
/// </summary>
|
|
||||
/// <value>The column header values.</value>
|
|
||||
/// <remarks>Will write the column headers if the list is not empty or <c>null</c>.</remarks>
|
|
||||
public IList<string> ColumnHeaders |
|
||||
{ |
|
||||
get; |
|
||||
set; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the <see cref="CultureInfo"/> to use when parsing the numbers.
|
|
||||
/// </summary>
|
|
||||
/// <value>The culture info.</value>
|
|
||||
/// <remarks>Defaults to <c>CultureInfo.CurrentCulture</c>.</remarks>
|
|
||||
public CultureInfo CultureInfo |
|
||||
{ |
|
||||
get |
|
||||
{ |
|
||||
return _cultureInfo; |
|
||||
} |
|
||||
|
|
||||
set |
|
||||
{ |
|
||||
if (value != null) |
|
||||
{ |
|
||||
_cultureInfo = value; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given <see cref="TextWriter"/>.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to write.</param>
|
|
||||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|
||||
/// <param name="format">The format to use on each element.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is <c>null</c>.</exception>
|
|
||||
protected override void DoWriteMatrix(Matrix<float> matrix, TextWriter writer, string format) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (writer == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("writer"); |
|
||||
} |
|
||||
|
|
||||
if (ColumnHeaders != null && ColumnHeaders.Count > 0) |
|
||||
{ |
|
||||
for (var i = 0; i < ColumnHeaders.Count - 1; i++) |
|
||||
{ |
|
||||
writer.Write(ColumnHeaders[i]); |
|
||||
writer.Write(_delimiter); |
|
||||
} |
|
||||
|
|
||||
writer.WriteLine(ColumnHeaders[ColumnHeaders.Count - 1]); |
|
||||
} |
|
||||
|
|
||||
var cols = matrix.ColumnCount - 1; |
|
||||
var rows = matrix.RowCount - 1; |
|
||||
for (var i = 0; i < matrix.RowCount; i++) |
|
||||
{ |
|
||||
for (var j = 0; j < matrix.ColumnCount; j++) |
|
||||
{ |
|
||||
writer.Write(matrix[i, j].ToString(format, _cultureInfo)); |
|
||||
if (j != cols) |
|
||||
{ |
|
||||
writer.Write(_delimiter); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if (i != rows) |
|
||||
{ |
|
||||
writer.Write(Environment.NewLine); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,197 +0,0 @@ |
|||||
// <copyright file="MatrixWriter.cs" company="Math.NET">
|
|
||||
// Math.NET Numerics, part of the Math.NET Project
|
|
||||
// http://numerics.mathdotnet.com
|
|
||||
// http://github.com/mathnet/mathnet-numerics
|
|
||||
// http://mathnetnumerics.codeplex.com
|
|
||||
//
|
|
||||
// Copyright (c) 2009-2010 Math.NET
|
|
||||
//
|
|
||||
// Permission is hereby granted, free of charge, to any person
|
|
||||
// obtaining a copy of this software and associated documentation
|
|
||||
// files (the "Software"), to deal in the Software without
|
|
||||
// restriction, including without limitation the rights to use,
|
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||
// copies of the Software, and to permit persons to whom the
|
|
||||
// Software is furnished to do so, subject to the following
|
|
||||
// conditions:
|
|
||||
// The above copyright notice and this permission notice shall be
|
|
||||
// included in all copies or substantial portions of the Software.
|
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
|
||||
// </copyright>
|
|
||||
|
|
||||
using System; |
|
||||
using System.IO; |
|
||||
|
|
||||
namespace MathNet.Numerics.LinearAlgebra.Single.IO |
|
||||
{ |
|
||||
using Generic; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Base class to write a single <see cref="Matrix{T}"/> to a file or stream.
|
|
||||
/// </summary>
|
|
||||
public abstract class MatrixWriter |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given file. If the file already exists,
|
|
||||
/// the file will be overwritten.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to write.</param>
|
|
||||
/// <param name="file">The file to write the matrix to.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is <c>null</c>.</exception>
|
|
||||
public void WriteMatrix(Matrix<float> matrix, string file) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (file == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("file"); |
|
||||
} |
|
||||
|
|
||||
using (var writer = new StreamWriter(file)) |
|
||||
{ |
|
||||
DoWriteMatrix(matrix, writer, null); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given file. If the file already exists,
|
|
||||
/// the file will be overwritten.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">the matrix to write.</param>
|
|
||||
/// <param name="file">The file to write the matrix to.</param>
|
|
||||
/// <param name="format">The format to use on each element.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is <c>null</c>.</exception>
|
|
||||
public void WriteMatrix(Matrix<float> matrix, string file, string format) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (file == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("file"); |
|
||||
} |
|
||||
|
|
||||
if (File.Exists(file)) |
|
||||
{ |
|
||||
File.Delete(file); |
|
||||
} |
|
||||
|
|
||||
using (var writer = new StreamWriter(file)) |
|
||||
{ |
|
||||
DoWriteMatrix(matrix, writer, format); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given stream.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to write.</param>
|
|
||||
/// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is <c>null</c>.</exception>
|
|
||||
public void WriteMatrix(Matrix<float> matrix, Stream stream) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (stream == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("stream"); |
|
||||
} |
|
||||
|
|
||||
using (var writer = new StreamWriter(stream)) |
|
||||
{ |
|
||||
DoWriteMatrix(matrix, writer, null); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given stream.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The <see cref="TextWriter"/> to write.</param>
|
|
||||
/// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
|
|
||||
/// <param name="format">The format to use on each element.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is <c>null</c>.</exception>
|
|
||||
public void WriteMatrix(Matrix<float> matrix, Stream stream, string format) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (stream == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("stream"); |
|
||||
} |
|
||||
|
|
||||
using (var writer = new StreamWriter(stream)) |
|
||||
{ |
|
||||
DoWriteMatrix(matrix, writer, format); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given <see cref="TextWriter"/>.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to write.</param>
|
|
||||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is <c>null</c>.</exception>
|
|
||||
public void WriteMatrix(Matrix<float> matrix, TextWriter writer) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (writer == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("writer"); |
|
||||
} |
|
||||
|
|
||||
DoWriteMatrix(matrix, writer, null); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Writes the given <see cref="Matrix{T}"/> to the given <see cref="TextWriter"/>.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to write.</param>
|
|
||||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|
||||
/// <param name="format">The format to use on each element.</param>
|
|
||||
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is <c>null</c>.</exception>
|
|
||||
public void WriteMatrix(Matrix<float> matrix, TextWriter writer, string format) |
|
||||
{ |
|
||||
if (matrix == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("matrix"); |
|
||||
} |
|
||||
|
|
||||
if (writer == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException("writer"); |
|
||||
} |
|
||||
|
|
||||
DoWriteMatrix(matrix, writer, format); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Subclasses must implement this method to do the actually writing.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to serialize.</param>
|
|
||||
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|
||||
/// <param name="format">The format for the new matrix.</param>
|
|
||||
protected abstract void DoWriteMatrix(Matrix<float> matrix, TextWriter writer, string format); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,90 @@ |
|||||
|
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using System.Numerics; |
||||
|
using System.IO; |
||||
|
using LinearAlgebra.Complex; |
||||
|
using LinearAlgebra.IO; |
||||
|
using MbUnit.Framework; |
||||
|
|
||||
|
[TestFixture] |
||||
|
public class DelimitedWriterTests |
||||
|
{ |
||||
|
[Test] |
||||
|
public void CanWriteCommaDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex>(','); |
||||
|
var stream = new MemoryStream(); |
||||
|
writer.WriteMatrix(matrix, stream); |
||||
|
var data = stream.ToArray(); |
||||
|
var reader = new StreamReader(new MemoryStream(data)); |
||||
|
var text = reader.ReadToEnd(); |
||||
|
const string expected = @"(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)
|
||||
|
(4.4, 4.4),(5.5, 5.5),(6.6, 6.6) |
||||
|
(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)";
|
||||
|
Assert.AreEqual(expected, text); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void CanWritePeriodDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex>('.') |
||||
|
{ |
||||
|
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(); |
||||
|
const string expected = @"(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)
|
||||
|
(4,4, 4,4).(5,5, 5,5).(6,6, 6,6) |
||||
|
(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)";
|
||||
|
Assert.AreEqual(expected, text); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void CanWriteSpaceDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex>(' '); |
||||
|
var stream = new MemoryStream(); |
||||
|
writer.WriteMatrix(matrix, stream); |
||||
|
var data = stream.ToArray(); |
||||
|
var reader = new StreamReader(new MemoryStream(data)); |
||||
|
var text = reader.ReadToEnd(); |
||||
|
const string expected = @"(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)
|
||||
|
(4.4, 4.4) (5.5, 5.5) (6.6, 6.6) |
||||
|
(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)";
|
||||
|
Assert.AreEqual(expected, text); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void CanWriteTabDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex>('\t') |
||||
|
{ |
||||
|
ColumnHeaders = headers |
||||
|
}; |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,90 @@ |
|||||
|
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using System.IO; |
||||
|
using LinearAlgebra.Complex32; |
||||
|
using Numerics; |
||||
|
using LinearAlgebra.IO; |
||||
|
using MbUnit.Framework; |
||||
|
|
||||
|
[TestFixture] |
||||
|
public class DelimitedWriterTests |
||||
|
{ |
||||
|
[Test] |
||||
|
public void CanWriteCommaDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex32>(','); |
||||
|
var stream = new MemoryStream(); |
||||
|
writer.WriteMatrix(matrix, stream); |
||||
|
var data = stream.ToArray(); |
||||
|
var reader = new StreamReader(new MemoryStream(data)); |
||||
|
var text = reader.ReadToEnd(); |
||||
|
const string expected = @"(1.1, 1.1),(2.2, 2.2),(3.3, 3.3)
|
||||
|
(4.4, 4.4),(5.5, 5.5),(6.6, 6.6) |
||||
|
(7.7, 7.7),(8.8, 8.8),(9.9, 9.9)";
|
||||
|
Assert.AreEqual(expected, text); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void CanWritePeriodDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex32>('.') |
||||
|
{ |
||||
|
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(); |
||||
|
const string expected = @"(1,1, 1,1).(2,2, 2,2).(3,3, 3,3)
|
||||
|
(4,4, 4,4).(5,5, 5,5).(6,6, 6,6) |
||||
|
(7,7, 7,7).(8,8, 8,8).(9,9, 9,9)";
|
||||
|
Assert.AreEqual(expected, text); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void CanWriteSpaceDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex32>(' '); |
||||
|
var stream = new MemoryStream(); |
||||
|
writer.WriteMatrix(matrix, stream); |
||||
|
var data = stream.ToArray(); |
||||
|
var reader = new StreamReader(new MemoryStream(data)); |
||||
|
var text = reader.ReadToEnd(); |
||||
|
const string expected = @"(1.1, 1.1) (2.2, 2.2) (3.3, 3.3)
|
||||
|
(4.4, 4.4) (5.5, 5.5) (6.6, 6.6) |
||||
|
(7.7, 7.7) (8.8, 8.8) (9.9, 9.9)";
|
||||
|
Assert.AreEqual(expected, text); |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void CanWriteTabDelimitedData() |
||||
|
{ |
||||
|
var matrix = new DenseMatrix(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<Complex32>('\t') |
||||
|
{ |
||||
|
ColumnHeaders = headers |
||||
|
}; |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue