//
// 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;
}
}