Browse Source

Cleanups

dependabot/nuget/NUnit3TestAdapter-4.2.0
Christoph Ruegg 5 years ago
parent
commit
67e4c8e043
  1. 43
      src/Data.Matlab/Formatter.cs
  2. 46
      src/Data.Matlab/Parser.cs
  3. 4
      src/Data.Text/DelimitedWriter.cs
  4. 8
      src/Data.Text/MatrixMarketReader.cs

43
src/Data.Matlab/Formatter.cs

@ -105,17 +105,17 @@ namespace MathNet.Numerics.Data.Matlab
{ {
if (matrix == null) if (matrix == null)
{ {
throw new ArgumentNullException("matrix"); throw new ArgumentNullException(nameof(matrix));
} }
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
{ {
throw new ArgumentException("String parameter cannot be empty or null.", "name"); throw new ArgumentException("String parameter cannot be empty or null.", nameof(name));
} }
if (name.IndexOf(' ') > -1) if (name.IndexOf(' ') > -1)
{ {
throw new ArgumentException(string.Format("Name cannot contain a space.name: {0}", name), "name"); throw new ArgumentException($"Name cannot contain a space.name: {name}", nameof(name));
} }
var dataType = typeof(T); var dataType = typeof(T);
@ -167,7 +167,7 @@ namespace MathNet.Numerics.Data.Matlab
writer.Write((byte)(sparse ? ArrayClass.Sparse : doublePrecision ? ArrayClass.Double : ArrayClass.Single)); writer.Write((byte)(sparse ? ArrayClass.Sparse : doublePrecision ? ArrayClass.Double : ArrayClass.Single));
writer.Write((byte)(complex ? ArrayFlags.Complex : 0)); writer.Write((byte)(complex ? ArrayFlags.Complex : 0));
writer.Write((short)0); writer.Write((short)0);
writer.Write((int)sparseNonZeroValues); writer.Write(sparseNonZeroValues);
// Dimensions Array tag: data type + size (8 bytes) // Dimensions Array tag: data type + size (8 bytes)
writer.Write((int)DataType.Int32); writer.Write((int)DataType.Int32);
@ -178,11 +178,10 @@ namespace MathNet.Numerics.Data.Matlab
writer.Write(matrix.ColumnCount); writer.Write(matrix.ColumnCount);
// Array Name: // Array Name:
bool smallBlock;
var nameBytes = Encoding.ASCII.GetBytes(name); var nameBytes = Encoding.ASCII.GetBytes(name);
WriteElementTag(writer, DataType.Int8, nameBytes.Length, out smallBlock); WriteElementTag(writer, DataType.Int8, nameBytes.Length, out var isSmallBlock);
writer.Write(nameBytes); writer.Write(nameBytes);
PadElement(writer, nameBytes.Length, smallBlock); PadElement(writer, nameBytes.Length, isSmallBlock);
if (sparse) if (sparse)
{ {
@ -203,9 +202,8 @@ namespace MathNet.Numerics.Data.Matlab
{ {
int count = matrix.RowCount*matrix.ColumnCount; int count = matrix.RowCount*matrix.ColumnCount;
bool smallBlock;
int size = doublePrecision ? count*8 : count*4; int size = doublePrecision ? count*8 : count*4;
WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out smallBlock); WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out var isSmallBlock);
var data = ((DenseColumnMajorMatrixStorage<T>)matrix.Storage).Data; var data = ((DenseColumnMajorMatrixStorage<T>)matrix.Storage).Data;
@ -219,14 +217,14 @@ namespace MathNet.Numerics.Data.Matlab
} }
else if (doublePrecision) else if (doublePrecision)
{ {
WriteComplexArray(writer, (Complex[])(object)data, data.Length, size, ref smallBlock); WriteComplexArray(writer, (Complex[])(object)data, data.Length, size, ref isSmallBlock);
} }
else else
{ {
WriteComplex32Array(writer, (Complex32[])(object)data, data.Length, size, ref smallBlock); WriteComplex32Array(writer, (Complex32[])(object)data, data.Length, size, ref isSmallBlock);
} }
PadElement(writer, size, smallBlock); PadElement(writer, size, isSmallBlock);
} }
static void WriteSparseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool complex, bool doublePrecision) static void WriteSparseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool complex, bool doublePrecision)
@ -235,32 +233,31 @@ namespace MathNet.Numerics.Data.Matlab
var transposed = matrix.Transpose(); var transposed = matrix.Transpose();
var storage = (SparseCompressedRowMatrixStorage<T>)transposed.Storage; var storage = (SparseCompressedRowMatrixStorage<T>)transposed.Storage;
bool smallBlock;
int nzcount = storage.ValueCount; int nzcount = storage.ValueCount;
// row data array // row data array
var ir = storage.ColumnIndices; var ir = storage.ColumnIndices;
WriteElementTag(writer, DataType.Int32, nzcount*4, out smallBlock); WriteElementTag(writer, DataType.Int32, nzcount*4, out var isSmallBlock);
for (var i = 0; i < nzcount; i++) for (var i = 0; i < nzcount; i++)
{ {
writer.Write(ir[i]); writer.Write(ir[i]);
} }
PadElement(writer, nzcount*4, smallBlock); PadElement(writer, nzcount*4, isSmallBlock);
// column data array // column data array
var jc = storage.RowPointers; var jc = storage.RowPointers;
WriteElementTag(writer, DataType.Int32, jc.Length*4, out smallBlock); WriteElementTag(writer, DataType.Int32, jc.Length*4, out isSmallBlock);
for (var i = 0; i < jc.Length; i++) for (var i = 0; i < jc.Length; i++)
{ {
writer.Write(jc[i]); writer.Write(jc[i]);
} }
PadElement(writer, jc.Length*4, smallBlock); PadElement(writer, jc.Length*4, isSmallBlock);
// values // values
int size = doublePrecision ? nzcount*8 : nzcount*4; int size = doublePrecision ? nzcount*8 : nzcount*4;
WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out smallBlock); WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out isSmallBlock);
if (doublePrecision && !complex) if (doublePrecision && !complex)
{ {
@ -272,14 +269,14 @@ namespace MathNet.Numerics.Data.Matlab
} }
else if (doublePrecision) else if (doublePrecision)
{ {
WriteComplexArray(writer, (Complex[])(object)storage.Values, nzcount, size, ref smallBlock); WriteComplexArray(writer, (Complex[])(object)storage.Values, nzcount, size, ref isSmallBlock);
} }
else else
{ {
WriteComplex32Array(writer, (Complex32[])(object)storage.Values, nzcount, size, ref smallBlock); WriteComplex32Array(writer, (Complex32[])(object)storage.Values, nzcount, size, ref isSmallBlock);
} }
PadElement(writer, size, smallBlock); PadElement(writer, size, isSmallBlock);
} }
static void WriteDoubleArray(BinaryWriter writer, double[] data, int count) static void WriteDoubleArray(BinaryWriter writer, double[] data, int count)
@ -348,7 +345,7 @@ namespace MathNet.Numerics.Data.Matlab
} }
} }
static void PadElement(BinaryWriter writer, int size, bool smallBlock, byte padValue = (byte)0) static void PadElement(BinaryWriter writer, int size, bool smallBlock, byte padValue = 0)
{ {
var blockSize = smallBlock ? SmallBlockSize : LargeBlockSize; var blockSize = smallBlock ? SmallBlockSize : LargeBlockSize;
var offset = 0; var offset = 0;
@ -364,7 +361,7 @@ namespace MathNet.Numerics.Data.Matlab
} }
} }
static void Pad(BinaryWriter writer, int count, byte padValue = (byte)0) static void Pad(BinaryWriter writer, int count, byte padValue = 0)
{ {
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {

46
src/Data.Matlab/Parser.cs

@ -89,10 +89,7 @@ namespace MathNet.Numerics.Data.Matlab
// small format: size (2 bytes), type (2 bytes), data (4 bytes) // small format: size (2 bytes), type (2 bytes), data (4 bytes)
// long format: type (4 bytes), size (4 bytes), data (size, aligned to 8 bytes) // long format: type (4 bytes), size (4 bytes), data (size, aligned to 8 bytes)
DataType type; ReadElementTag(reader, out var type, out var size, out var isSmallBlock);
int size;
bool smallBlock;
ReadElementTag(reader, out type, out size, out smallBlock);
// read element data of the size provided in the element header // read element data of the size provided in the element header
// uncompress if compressed // uncompress if compressed
@ -105,7 +102,7 @@ namespace MathNet.Numerics.Data.Matlab
{ {
data = new byte[size]; data = new byte[size];
reader.Read(data, 0, size); reader.Read(data, 0, size);
SkipElementPadding(reader, size, smallBlock); SkipElementPadding(reader, size, isSmallBlock);
} }
if (type == DataType.Matrix) if (type == DataType.Matrix)
@ -169,12 +166,9 @@ namespace MathNet.Numerics.Data.Matlab
var columns = reader.ReadInt32(); var columns = reader.ReadInt32();
// Array name // Array name
DataType type; ReadElementTag(reader, out _, out var size, out var isSmallBlock);
int size;
bool smallBlock;
ReadElementTag(reader, out type, out size, out smallBlock);
reader.BaseStream.Seek(size, SeekOrigin.Current); reader.BaseStream.Seek(size, SeekOrigin.Current);
SkipElementPadding(reader, size, smallBlock); SkipElementPadding(reader, size, isSmallBlock);
// Data // Data
switch (arrayClass) switch (arrayClass)
@ -209,12 +203,8 @@ namespace MathNet.Numerics.Data.Matlab
var count = rows*columns; var count = rows*columns;
var data = new T[count]; var data = new T[count];
DataType type;
int size;
bool smallBlock;
// read real part array // read real part array
ReadElementTag(reader, out type, out size, out smallBlock); ReadElementTag(reader, out var type, out var size, out var isSmallBlock);
// direct copy if possible // 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)))
@ -241,18 +231,18 @@ namespace MathNet.Numerics.Data.Matlab
} }
else if (dataType == typeof(Complex)) else if (dataType == typeof(Complex))
{ {
PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref smallBlock); PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref isSmallBlock);
} }
else if (dataType == typeof(Complex32)) else if (dataType == typeof(Complex32))
{ {
PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref smallBlock); PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref isSmallBlock);
} }
else else
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
SkipElementPadding(reader, size, smallBlock); SkipElementPadding(reader, size, isSmallBlock);
return Matrix<T>.Build.Dense(rows, columns, data); return Matrix<T>.Build.Dense(rows, columns, data);
} }
@ -273,22 +263,18 @@ namespace MathNet.Numerics.Data.Matlab
// MATLAB sparse matrices are actually stored as CSC, so just read the data and then transpose. // MATLAB sparse matrices are actually stored as CSC, so just read the data and then transpose.
var storage = matrix.Storage as SparseCompressedRowMatrixStorage<T>; var storage = matrix.Storage as SparseCompressedRowMatrixStorage<T>;
DataType type;
int size;
bool smallBlock;
// populate the row data array // populate the row data array
ReadElementTag(reader, out type, out size, out smallBlock); ReadElementTag(reader, out var type, out var size, out var isSmallBlock);
var ir = storage.ColumnIndices = new int[size/4]; var ir = storage.ColumnIndices = new int[size/4];
for (var i = 0; i < ir.Length; i++) for (var i = 0; i < ir.Length; i++)
{ {
ir[i] = reader.ReadInt32(); ir[i] = reader.ReadInt32();
} }
SkipElementPadding(reader, size, smallBlock); SkipElementPadding(reader, size, isSmallBlock);
// populate the column data array // populate the column data array
ReadElementTag(reader, out type, out size, out smallBlock); ReadElementTag(reader, out type, out size, out isSmallBlock);
var jc = storage.RowPointers; var jc = storage.RowPointers;
if (jc.Length != size/4) if (jc.Length != size/4)
{ {
@ -300,10 +286,10 @@ namespace MathNet.Numerics.Data.Matlab
jc[j] = reader.ReadInt32(); jc[j] = reader.ReadInt32();
} }
SkipElementPadding(reader, size, smallBlock); SkipElementPadding(reader, size, isSmallBlock);
// populate the values // populate the values
ReadElementTag(reader, out type, out size, out smallBlock); ReadElementTag(reader, out type, out size, out isSmallBlock);
var dataType = typeof(T); var dataType = typeof(T);
var data = storage.Values = new T[jc[columns]]; var data = storage.Values = new T[jc[columns]];
@ -327,18 +313,18 @@ namespace MathNet.Numerics.Data.Matlab
} }
else if (dataType == typeof(Complex)) else if (dataType == typeof(Complex))
{ {
PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref smallBlock); PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref isSmallBlock);
} }
else if (dataType == typeof(Complex32)) else if (dataType == typeof(Complex32))
{ {
PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref smallBlock); PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref isSmallBlock);
} }
else else
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
SkipElementPadding(reader, size, smallBlock); SkipElementPadding(reader, size, isSmallBlock);
return matrix.Transpose(); return matrix.Transpose();
} }

4
src/Data.Text/DelimitedWriter.cs

@ -59,12 +59,12 @@ namespace MathNet.Numerics.Data.Text
{ {
if (matrix == null) if (matrix == null)
{ {
throw new ArgumentNullException("matrix"); throw new ArgumentNullException(nameof(matrix));
} }
if (writer == null) if (writer == null)
{ {
throw new ArgumentNullException("writer"); throw new ArgumentNullException(nameof(writer));
} }
if (columnHeaders != null && columnHeaders.Count > 0) if (columnHeaders != null && columnHeaders.Count > 0)

8
src/Data.Text/MatrixMarketReader.cs

@ -123,9 +123,7 @@ namespace MathNet.Numerics.Data.Text
public static Matrix<T> ReadMatrix<T>(TextReader reader) where T : struct, IEquatable<T>, IFormattable public static Matrix<T> ReadMatrix<T>(TextReader reader) where T : struct, IEquatable<T>, IFormattable
{ {
bool complex, sparse; ExpectHeader(reader, true, out var complex, out var sparse, out var symmetry);
MatrixMarketSymmetry symmetry;
ExpectHeader(reader, true, out complex, out sparse, out symmetry);
var parse = CreateValueParser<T>(complex); var parse = CreateValueParser<T>(complex);
@ -200,9 +198,7 @@ namespace MathNet.Numerics.Data.Text
public static Vector<T> ReadVector<T>(TextReader reader) public static Vector<T> ReadVector<T>(TextReader reader)
where T : struct, IEquatable<T>, IFormattable where T : struct, IEquatable<T>, IFormattable
{ {
bool complex, sparse; ExpectHeader(reader, false, out var complex, out var sparse, out _);
MatrixMarketSymmetry symmetry;
ExpectHeader(reader, false, out complex, out sparse, out symmetry);
var parse = CreateValueParser<T>(complex); var parse = CreateValueParser<T>(complex);

Loading…
Cancel
Save