|
|
|
@ -31,48 +31,61 @@ using System.IO; |
|
|
|
|
|
|
|
namespace MathNet.Numerics.LinearAlgebra.IO |
|
|
|
{ |
|
|
|
using System.Globalization; |
|
|
|
using Generic; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Base class to write a single <see cref="Matrix{DataType}"/> to a file or stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TDataType">The data type of the matrix.</typeparam>
|
|
|
|
public abstract class MatrixWriter<TDataType> where TDataType : struct, IEquatable<TDataType>, IFormattable |
|
|
|
public abstract class MatrixWriter |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Writes the given <see cref="Matrix{DataType}"/> to the given file. If the file already exists,
|
|
|
|
/// the file will be overwritten.
|
|
|
|
/// The <see cref="CultureInfo"/> to use.
|
|
|
|
/// </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<TDataType> matrix, string file) |
|
|
|
{ |
|
|
|
if (matrix == null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException("matrix"); |
|
|
|
} |
|
|
|
private CultureInfo _cultureInfo = CultureInfo.CurrentCulture; |
|
|
|
|
|
|
|
if (file == null) |
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the <see cref="CultureInfo"/> to use when parsing the numbers.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The culture info.</value>
|
|
|
|
/// <remarks>Defaults to <c>CultureInfo.CurrentCulture</c>.</remarks>
|
|
|
|
/// <remarks>This property is only used for matrix writers that write out text files.</remarks>
|
|
|
|
public CultureInfo CultureInfo |
|
|
|
{ |
|
|
|
get |
|
|
|
{ |
|
|
|
throw new ArgumentNullException("file"); |
|
|
|
return _cultureInfo; |
|
|
|
} |
|
|
|
|
|
|
|
using (var writer = new StreamWriter(file)) |
|
|
|
set |
|
|
|
{ |
|
|
|
DoWriteMatrix(matrix, writer, null); |
|
|
|
if (value != null) |
|
|
|
{ |
|
|
|
_cultureInfo = value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets he number format to use.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The number format to use when writing out each element.</value>
|
|
|
|
/// <remarks>This property is only used for matrix writers that write out text files.</remarks>
|
|
|
|
public string Format |
|
|
|
{ |
|
|
|
get; |
|
|
|
set; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Writes the given <see cref="Matrix{DataType}"/> to the given file. If the file already exists,
|
|
|
|
/// the file will be overwritten.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="matrix">the matrix to write.</param>
|
|
|
|
/// <param name="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<TDataType> matrix, string file, string format) |
|
|
|
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|
|
|
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, string file) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|
|
|
{ |
|
|
|
if (matrix == null) |
|
|
|
{ |
|
|
|
@ -84,14 +97,9 @@ namespace MathNet.Numerics.LinearAlgebra.IO |
|
|
|
throw new ArgumentNullException("file"); |
|
|
|
} |
|
|
|
|
|
|
|
if (File.Exists(file)) |
|
|
|
{ |
|
|
|
File.Delete(file); |
|
|
|
} |
|
|
|
|
|
|
|
using (var writer = new StreamWriter(file)) |
|
|
|
{ |
|
|
|
DoWriteMatrix(matrix, writer, format); |
|
|
|
DoWriteMatrix(matrix, writer, Format, _cultureInfo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -101,32 +109,8 @@ namespace MathNet.Numerics.LinearAlgebra.IO |
|
|
|
/// <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<TDataType> 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{DataType}"/> 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<TDataType> matrix, Stream stream, string format) |
|
|
|
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|
|
|
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, Stream stream) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|
|
|
{ |
|
|
|
if (matrix == null) |
|
|
|
{ |
|
|
|
@ -140,29 +124,8 @@ namespace MathNet.Numerics.LinearAlgebra.IO |
|
|
|
|
|
|
|
using (var writer = new StreamWriter(stream)) |
|
|
|
{ |
|
|
|
DoWriteMatrix(matrix, writer, format); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Writes the given <see cref="Matrix{DataType}"/> to the given <see cref="TextWriter"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="matrix">The matrix to write.</param>
|
|
|
|
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|
|
|
/// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is <c>null</c>.</exception>
|
|
|
|
public void WriteMatrix(Matrix<TDataType> matrix, TextWriter writer) |
|
|
|
{ |
|
|
|
if (matrix == null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException("matrix"); |
|
|
|
} |
|
|
|
|
|
|
|
if (writer == null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException("writer"); |
|
|
|
DoWriteMatrix(matrix, writer, Format, _cultureInfo); |
|
|
|
} |
|
|
|
|
|
|
|
DoWriteMatrix(matrix, writer, null); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -170,9 +133,9 @@ namespace MathNet.Numerics.LinearAlgebra.IO |
|
|
|
/// </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<TDataType> matrix, TextWriter writer, string format) |
|
|
|
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|
|
|
public void WriteMatrix<TDataType>(Matrix<TDataType> matrix, TextWriter writer) where TDataType : struct, IEquatable<TDataType>, IFormattable |
|
|
|
{ |
|
|
|
if (matrix == null) |
|
|
|
{ |
|
|
|
@ -184,15 +147,17 @@ namespace MathNet.Numerics.LinearAlgebra.IO |
|
|
|
throw new ArgumentNullException("writer"); |
|
|
|
} |
|
|
|
|
|
|
|
DoWriteMatrix(matrix, writer, format); |
|
|
|
DoWriteMatrix(matrix, writer, Format, _cultureInfo); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Subclasses must implement this method to do the actually writing.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
|
|
|
|
/// <param name="matrix">The matrix to serialize.</param>
|
|
|
|
/// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
|
|
|
|
/// <param name="format">The format for the new matrix.</param>
|
|
|
|
protected abstract void DoWriteMatrix(Matrix<TDataType> matrix, TextWriter writer, string format); |
|
|
|
/// <param name="format">The number format to use.</param>
|
|
|
|
/// <param name="cultureInfo">The culture to use.</param>
|
|
|
|
protected abstract void DoWriteMatrix<TDataType>(Matrix<TDataType> matrix, TextWriter writer, string format, CultureInfo cultureInfo) where TDataType : struct, IEquatable<TDataType>, IFormattable; |
|
|
|
} |
|
|
|
} |
|
|
|
|