Browse Source

Data: refactor MatlabMatrixReader to simpler static design

provider
Christoph Ruegg 12 years ago
parent
commit
1ef8575621
  1. 17
      build.fsx
  2. 171
      src/Data/Matlab/MatlabReader.cs
  3. 72
      src/DataUnitTests/Matlab/MatlabReaderTests.cs
  4. 12
      src/DataUnitTests/Matlab/MatlabWriterTests.cs

17
build.fsx

@ -122,14 +122,12 @@ let nativePackageVersion = nativeRelease.NugetVersion
let nativeReleaseNotes = nativeRelease.Notes |> List.map (fun l -> l.Replace("*","").Replace("`","")) |> toLines let nativeReleaseNotes = nativeRelease.Notes |> List.map (fun l -> l.Replace("*","").Replace("`","")) |> toLines
trace (sprintf " Math.NET Numerics Native Providers v%s" nativePackageVersion) trace (sprintf " Math.NET Numerics Native Providers v%s" nativePackageVersion)
let nativeSummary = "Intel MKL native libraries for Math.NET Numerics. Requires an Intel MKL license if redistributed."
let nativeMKLWin32Pack = let nativeMKLWin32Pack =
{ Id = "MathNet.Numerics.MKL.Win-x86" { Id = "MathNet.Numerics.MKL.Win-x86"
Version = nativePackageVersion Version = nativePackageVersion
Title = "Math.NET Numerics - MKL Native Libraries (Windows 32-bit)" Title = "Math.NET Numerics - MKL Native Libraries (Windows 32-bit)"
Summary = nativeSummary Summary = ""
Description = nativeSummary Description = "Intel MKL native libraries for Math.NET Numerics. Requires an Intel MKL license if redistributed."
ReleaseNotes = nativeReleaseNotes ReleaseNotes = nativeReleaseNotes
Tags = "math numeric statistics probability integration interpolation linear algebra matrix fft native mkl" Tags = "math numeric statistics probability integration interpolation linear algebra matrix fft native mkl"
Authors = [ "Christoph Ruegg"; "Marcus Cuda"; "Jurgen Van Gael" ] Authors = [ "Christoph Ruegg"; "Marcus Cuda"; "Jurgen Van Gael" ]
@ -154,15 +152,12 @@ let dataReleaseNotes = dataRelease.Notes |> List.map (fun l -> l.Replace("*","")
trace (sprintf " Math.NET Numerics Data Extensions v%s" dataPackageVersion) trace (sprintf " Math.NET Numerics Data Extensions v%s" dataPackageVersion)
trace "" trace ""
let dataTextSummary = "Text Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use."
let dataMatlabSummary = "MathWorks MATLAB Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use."
let dataTextPack = let dataTextPack =
{ Id = "MathNet.Numerics.Data.Text" { Id = "MathNet.Numerics.Data.Text"
Version = dataPackageVersion Version = dataPackageVersion
Title = "Math.NET Numerics - Text Data I/O Extensions" Title = "Math.NET Numerics - Text Data I/O Extensions"
Summary = dataTextSummary Summary = ""
Description = dataTextSummary Description = "Text Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use."
ReleaseNotes = dataReleaseNotes ReleaseNotes = dataReleaseNotes
Tags = "math numeric data text csv tsv json xml" Tags = "math numeric data text csv tsv json xml"
Authors = [ "Christoph Ruegg"; "Marcus Cuda" ] Authors = [ "Christoph Ruegg"; "Marcus Cuda" ]
@ -174,8 +169,8 @@ let dataMatlabPack =
{ Id = "MathNet.Numerics.Data.Matlab" { Id = "MathNet.Numerics.Data.Matlab"
Version = dataPackageVersion Version = dataPackageVersion
Title = "Math.NET Numerics - MATLAB Data I/O Extensions" Title = "Math.NET Numerics - MATLAB Data I/O Extensions"
Summary = dataMatlabSummary Summary = ""
Description = dataMatlabSummary Description = "MathWorks MATLAB Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use."
ReleaseNotes = dataReleaseNotes ReleaseNotes = dataReleaseNotes
Tags = "math numeric data matlab" Tags = "math numeric data matlab"
Authors = [ "Christoph Ruegg"; "Marcus Cuda" ] Authors = [ "Christoph Ruegg"; "Marcus Cuda" ]

171
src/Data/Matlab/MatlabReader.cs

@ -33,60 +33,17 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Data.Matlab namespace MathNet.Numerics.Data.Matlab
{ {
/// <summary> /// <summary>
/// Creates matrices from Matlab files. /// Creates matrices from Matlab files.
/// </summary> /// </summary>
/// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam> public static class MatlabMatrixReader
public class MatlabMatrixReader<TDataType> where TDataType : struct, IEquatable<TDataType>, IFormattable
{ {
/// <summary> /// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
/// The name of the file to read from. public static Matrix<TDataType> ReadMatrix<TDataType>(Stream stream, string matrixName = null)
/// </summary> where TDataType : struct, IEquatable<TDataType>, IFormattable
private readonly string _filename;
/// <summary>
/// The stream to read from if we are not reading from a file directly.
/// </summary>
private readonly Stream _stream;
/// <summary>
/// Initializes a new instance of the <see cref="MatlabMatrixReader{TDataType}"/> class.
/// </summary>
/// <param name="filename">Name of the file to read matrices from.</param>
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;
}
/// <summary>
/// Initializes a new instance of the <see cref="MatlabMatrixReader{TDataType}"/> class.
/// </summary>
/// <param name="stream">The stream to reader matrices from.</param>
public MatlabMatrixReader(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
_stream = stream;
}
public static Matrix<TDataType> ReadMatrix(Stream stream, string matrixName = null)
{ {
var names = string.IsNullOrEmpty(matrixName) ? new string[] { } : new[] { matrixName }; var names = string.IsNullOrEmpty(matrixName) ? new string[] { } : new[] { matrixName };
var parser = new MatlabParser<TDataType>(stream, names); var parser = new MatlabParser<TDataType>(stream, names);
@ -103,125 +60,33 @@ namespace MathNet.Numerics.Data.Matlab
return file.Matrices[matrixName]; return file.Matrices[matrixName];
} }
public static Matrix<TDataType> ReadMatrix(string filePath, string matrixName = null) /// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
public static Matrix<TDataType> ReadMatrix<TDataType>(string filePath, string matrixName = null)
where TDataType : struct, IEquatable<TDataType>, IFormattable
{ {
using (var stream = File.OpenRead(filePath)) using (var stream = File.OpenRead(filePath))
{ {
return ReadMatrix(stream, matrixName); return ReadMatrix<TDataType>(stream, matrixName);
} }
} }
public static Dictionary<string, Matrix<TDataType>> ReadMatrices(Stream stream, params string[] matrixNames) /// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
{ public static Dictionary<string, Matrix<TDataType>> ReadMatrices<TDataType>(Stream stream, params string[] matrixNames)
var reader = new MatlabMatrixReader<TDataType>(stream); where TDataType : struct, IEquatable<TDataType>, IFormattable
return reader.ReadMatrices(matrixNames);
}
public static Dictionary<string, Matrix<TDataType>> ReadMatrices(string filePath, params string[] matrixNames)
{ {
using (var stream = File.OpenRead(filePath)) var parser = new MatlabParser<TDataType>(stream, matrixNames);
{
return ReadMatrices(stream, matrixNames);
}
}
/// <summary>
/// Reads the first matrix from the file or stream.
/// </summary>
/// <returns>
/// A sparse or dense matrix depending on how the matrix
/// is defined in the Matlab file.
/// </returns>
public Matrix<TDataType> ReadMatrix()
{
return ReadMatrix(null);
}
/// <summary>
/// Reads the named matrix from the file or stream.
/// </summary>
/// <param name="matrixName">The name of the matrix to read.</param>
/// <returns>
/// A sparse or dense matrix depending on how the matrix
/// is defined in the Matlab file.
/// <see langword="null"/> is returned if a matrix with the requests name doesn't exist.
/// </returns>
public Matrix<TDataType> 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<TDataType>(stream, names);
var file = parser.Parse(); var file = parser.Parse();
return file.Matrices.ToDictionary(matrix => matrix.Key, matrix => matrix.Value);
Matrix<TDataType> 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;
}
/// <summary>
/// Reads all matrices from the file or stream.
/// </summary>
/// <returns>All matrices from the file or stream. The key to the <see cref="IDictionary{T,K}"/>
/// is the matrix's name.</returns>
public Dictionary<string, Matrix<TDataType>> ReadMatrices()
{
return ReadMatrices(new string[] { });
} }
/// <summary> /// <typeparam name="TDataType">The data type of the Matrix. It can be either: double, float, Complex, or Complex32.</typeparam>
/// Reads the named matrices from the file or stream. public static Dictionary<string, Matrix<TDataType>> ReadMatrices<TDataType>(string filePath, params string[] matrixNames)
/// </summary> where TDataType : struct, IEquatable<TDataType>, IFormattable
/// <param name="names">The names of the matrices to retrieve.</param>
/// <returns>
/// The named matrices from the file or stream. The key to the <see cref="IDictionary{T,K}"/>
/// is the matrix's name.</returns>
public Dictionary<string, Matrix<TDataType>> ReadMatrices(IEnumerable<string> names)
{ {
Stream stream; using (var stream = File.OpenRead(filePath))
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<TDataType>(stream, names);
var file = parser.Parse();
if (_filename != null)
{ {
stream.Close(); return ReadMatrices<TDataType>(stream, matrixNames);
stream.Dispose();
} }
return file.Matrices.ToDictionary(matrix => matrix.Key, matrix => matrix.Value);
} }
} }
} }

72
src/DataUnitTests/Matlab/MatlabReaderTests.cs

@ -42,8 +42,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadAllMatrices() public void CanReadAllMatrices()
{ {
var dmr = new MatlabMatrixReader<double>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<double>("./data/Matlab/collection.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(30, matrices.Count); Assert.AreEqual(30, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -57,8 +56,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadFirstMatrix() public void CanReadFirstMatrix()
{ {
var dmr = new MatlabMatrixReader<double>("./data/Matlab/A.mat"); var matrix = MatlabMatrixReader.ReadMatrix<double>("./data/Matlab/A.mat");
var matrix = dmr.ReadMatrix();
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Double.DenseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Double.DenseMatrix), matrix.GetType());
@ -71,8 +69,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNamedMatrices() public void CanReadNamedMatrices()
{ {
var dmr = new MatlabMatrixReader<double>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<double>("./data/Matlab/collection.mat", "Ad", "Au64");
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" });
Assert.AreEqual(2, matrices.Count); Assert.AreEqual(2, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -86,8 +83,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNamedMatrix() public void CanReadNamedMatrix()
{ {
var dmr = new MatlabMatrixReader<double>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<double>("./data/Matlab/collection.mat", "Ad");
var matrices = dmr.ReadMatrices(new[] { "Ad" });
Assert.AreEqual(1, matrices.Count); Assert.AreEqual(1, matrices.Count);
var ad = matrices["Ad"]; var ad = matrices["Ad"];
Assert.AreEqual(100, ad.RowCount); Assert.AreEqual(100, ad.RowCount);
@ -102,8 +98,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNamedSparseMatrix() public void CanReadNamedSparseMatrix()
{ {
var dmr = new MatlabMatrixReader<double>("./data/Matlab/sparse-small.mat"); var matrix = MatlabMatrixReader.ReadMatrix<double>("./data/Matlab/sparse-small.mat", "S");
var matrix = dmr.ReadMatrix("S");
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Double.SparseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Double.SparseMatrix), matrix.GetType());
@ -116,8 +111,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadComplexAllMatrices() public void CanReadComplexAllMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/complex.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex>("./data/Matlab/complex.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(3, matrices.Count); Assert.AreEqual(3, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -137,8 +131,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadSparseComplexAllMatrices() public void CanReadSparseComplexAllMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/sparse_complex.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex>("./data/Matlab/sparse_complex.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(3, matrices.Count); Assert.AreEqual(3, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -158,8 +151,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplexAllMatrices() public void CanReadNonComplexAllMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex>("./data/Matlab/collection.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(30, matrices.Count); Assert.AreEqual(30, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -173,8 +165,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplexFirstMatrix() public void CanReadNonComplexFirstMatrix()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/A.mat"); var matrix = MatlabMatrixReader.ReadMatrix<Complex>("./data/Matlab/A.mat");
var matrix = dmr.ReadMatrix();
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Complex.DenseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Complex.DenseMatrix), matrix.GetType());
@ -187,8 +178,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplexNamedMatrices() public void CanReadNonComplexNamedMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex>("./data/Matlab/collection.mat", "Ad", "Au64");
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" });
Assert.AreEqual(2, matrices.Count); Assert.AreEqual(2, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -202,8 +192,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplexNamedMatrix() public void CanReadNonComplexNamedMatrix()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex>("./data/Matlab/collection.mat", "Ad");
var matrices = dmr.ReadMatrices(new[] { "Ad" });
Assert.AreEqual(1, matrices.Count); Assert.AreEqual(1, matrices.Count);
var ad = matrices["Ad"]; var ad = matrices["Ad"];
Assert.AreEqual(100, ad.RowCount); Assert.AreEqual(100, ad.RowCount);
@ -218,8 +207,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplexNamedSparseMatrix() public void CanReadNonComplexNamedSparseMatrix()
{ {
var dmr = new MatlabMatrixReader<Complex>("./data/Matlab/sparse-small.mat"); var matrix = MatlabMatrixReader.ReadMatrix<Complex>("./data/Matlab/sparse-small.mat", "S");
var matrix = dmr.ReadMatrix("S");
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Complex.SparseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Complex.SparseMatrix), matrix.GetType());
@ -232,8 +220,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadComplex32AllMatrices() public void CanReadComplex32AllMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/complex.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex32>("./data/Matlab/complex.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(3, matrices.Count); Assert.AreEqual(3, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -253,8 +240,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadSparseComplex32AllMatrices() public void CanReadSparseComplex32AllMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/sparse_complex.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex32>("./data/Matlab/sparse_complex.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(3, matrices.Count); Assert.AreEqual(3, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -274,8 +260,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplex32AllMatrices() public void CanReadNonComplex32AllMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex32>("./data/Matlab/collection.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(30, matrices.Count); Assert.AreEqual(30, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -289,8 +274,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplex32FirstMatrix() public void CanReadNonComplex32FirstMatrix()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/A.mat"); var matrix = MatlabMatrixReader.ReadMatrix<Complex32>("./data/Matlab/A.mat");
var matrix = dmr.ReadMatrix();
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Complex32.DenseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Complex32.DenseMatrix), matrix.GetType());
@ -303,8 +287,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplex32NamedMatrices() public void CanReadNonComplex32NamedMatrices()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex32>("./data/Matlab/collection.mat", "Ad", "Au64");
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" });
Assert.AreEqual(2, matrices.Count); Assert.AreEqual(2, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -318,8 +301,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplex32NamedMatrix() public void CanReadNonComplex32NamedMatrix()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<Complex32>("./data/Matlab/collection.mat", "Ad");
var matrices = dmr.ReadMatrices(new[] { "Ad" });
Assert.AreEqual(1, matrices.Count); Assert.AreEqual(1, matrices.Count);
var ad = matrices["Ad"]; var ad = matrices["Ad"];
Assert.AreEqual(100, ad.RowCount); Assert.AreEqual(100, ad.RowCount);
@ -334,8 +316,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadNonComplex32NamedSparseMatrix() public void CanReadNonComplex32NamedSparseMatrix()
{ {
var dmr = new MatlabMatrixReader<Complex32>("./data/Matlab/sparse-small.mat"); var matrix = MatlabMatrixReader.ReadMatrix<Complex32>("./data/Matlab/sparse-small.mat", "S");
var matrix = dmr.ReadMatrix("S");
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Complex32.SparseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Complex32.SparseMatrix), matrix.GetType());
@ -348,8 +329,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadFloatAllMatrices() public void CanReadFloatAllMatrices()
{ {
var dmr = new MatlabMatrixReader<float>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<float>("./data/Matlab/collection.mat");
var matrices = dmr.ReadMatrices();
Assert.AreEqual(30, matrices.Count); Assert.AreEqual(30, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -363,8 +343,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadFloatFirstMatrix() public void CanReadFloatFirstMatrix()
{ {
var dmr = new MatlabMatrixReader<float>("./data/Matlab/A.mat"); var matrix = MatlabMatrixReader.ReadMatrix<float>("./data/Matlab/A.mat");
var matrix = dmr.ReadMatrix();
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Single.DenseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Single.DenseMatrix), matrix.GetType());
@ -377,8 +356,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadFloatNamedMatrices() public void CanReadFloatNamedMatrices()
{ {
var dmr = new MatlabMatrixReader<float>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<float>("./data/Matlab/collection.mat", "Ad", "Au64");
var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" });
Assert.AreEqual(2, matrices.Count); Assert.AreEqual(2, matrices.Count);
foreach (var matrix in matrices) foreach (var matrix in matrices)
{ {
@ -392,8 +370,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadFloatNamedMatrix() public void CanReadFloatNamedMatrix()
{ {
var dmr = new MatlabMatrixReader<float>("./data/Matlab/collection.mat"); var matrices = MatlabMatrixReader.ReadMatrices<float>("./data/Matlab/collection.mat", "Ad");
var matrices = dmr.ReadMatrices(new[] { "Ad" });
Assert.AreEqual(1, matrices.Count); Assert.AreEqual(1, matrices.Count);
var ad = matrices["Ad"]; var ad = matrices["Ad"];
Assert.AreEqual(100, ad.RowCount); Assert.AreEqual(100, ad.RowCount);
@ -408,8 +385,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
[Test] [Test]
public void CanReadFloatNamedSparseMatrix() public void CanReadFloatNamedSparseMatrix()
{ {
var dmr = new MatlabMatrixReader<float>("./data/Matlab/sparse-small.mat"); var matrix = MatlabMatrixReader.ReadMatrix<float>("./data/Matlab/sparse-small.mat", "S");
var matrix = dmr.ReadMatrix("S");
Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.RowCount);
Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(100, matrix.ColumnCount);
Assert.AreEqual(typeof(LinearAlgebra.Single.SparseMatrix), matrix.GetType()); Assert.AreEqual(typeof(LinearAlgebra.Single.SparseMatrix), matrix.GetType());

12
src/DataUnitTests/Matlab/MatlabWriterTests.cs

@ -116,8 +116,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
writer.WriteMatrices(write, names); writer.WriteMatrices(write, names);
writer.Dispose(); writer.Dispose();
var reader = new MatlabMatrixReader<double>("testd.mat"); var read = MatlabMatrixReader.ReadMatrices<double>("testd.mat", names);
var read = reader.ReadMatrices(names);
Assert.AreEqual(write.Length, read.Count); Assert.AreEqual(write.Length, read.Count);
@ -172,8 +171,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
writer.WriteMatrices(write, names); writer.WriteMatrices(write, names);
writer.Dispose(); writer.Dispose();
var reader = new MatlabMatrixReader<float>("tests.mat"); var read = MatlabMatrixReader.ReadMatrices<float>("tests.mat", names);
var read = reader.ReadMatrices(names);
Assert.AreEqual(write.Length, read.Count); Assert.AreEqual(write.Length, read.Count);
@ -228,8 +226,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
writer.WriteMatrices(write, names); writer.WriteMatrices(write, names);
writer.Dispose(); writer.Dispose();
var reader = new MatlabMatrixReader<Complex32>("testc.mat"); var read = MatlabMatrixReader.ReadMatrices<Complex32>("testc.mat", names);
var read = reader.ReadMatrices(names);
Assert.AreEqual(write.Length, read.Count); Assert.AreEqual(write.Length, read.Count);
@ -284,8 +281,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab
writer.WriteMatrices(write, names); writer.WriteMatrices(write, names);
writer.Dispose(); writer.Dispose();
var reader = new MatlabMatrixReader<Complex>("testz.mat"); var read = MatlabMatrixReader.ReadMatrices<Complex>("testz.mat", names);
var read = reader.ReadMatrices(names);
Assert.AreEqual(write.Length, read.Count); Assert.AreEqual(write.Length, read.Count);

Loading…
Cancel
Save