forked from tsai/mathnet-numerics
10 changed files with 901 additions and 1168 deletions
@ -0,0 +1,150 @@ |
|||||
|
// <copyright file="NumericArrayFormatter.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-2014 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.IO; |
||||
|
using System.Numerics; |
||||
|
using MathNet.Numerics.LinearAlgebra; |
||||
|
|
||||
|
namespace MathNet.Numerics.Data.Matlab |
||||
|
{ |
||||
|
internal static class NumericArrayFormatter |
||||
|
{ |
||||
|
internal static void Write(BinaryWriter writer, Matrix<double> matrix) |
||||
|
{ |
||||
|
// write data
|
||||
|
writer.Write((int)DataType.Double); |
||||
|
writer.Write(matrix.RowCount*matrix.ColumnCount*8); |
||||
|
|
||||
|
for (var j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
var column = matrix.Column(j); |
||||
|
foreach (var value in column) |
||||
|
{ |
||||
|
writer.Write(value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, Matrix<float> matrix) |
||||
|
{ |
||||
|
// write data
|
||||
|
int size = matrix.RowCount*matrix.ColumnCount*4; |
||||
|
writer.Write((int)DataType.Single); |
||||
|
writer.Write(size); |
||||
|
|
||||
|
for (var j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
var column = matrix.Column(j); |
||||
|
foreach (var value in column) |
||||
|
{ |
||||
|
writer.Write(value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PadData(writer, size%8); |
||||
|
} |
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, Matrix<Complex> matrix) |
||||
|
{ |
||||
|
// write data
|
||||
|
int size = matrix.RowCount*matrix.ColumnCount*8; |
||||
|
writer.Write((int)DataType.Double); |
||||
|
writer.Write(size); |
||||
|
|
||||
|
for (var j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
var column = matrix.Column(j); |
||||
|
foreach (var value in column) |
||||
|
{ |
||||
|
writer.Write(value.Real); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
writer.Write((int)DataType.Double); |
||||
|
writer.Write(size); |
||||
|
|
||||
|
for (var j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
var column = matrix.Column(j); |
||||
|
foreach (var value in column) |
||||
|
{ |
||||
|
writer.Write(value.Imaginary); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, Matrix<Complex32> matrix) |
||||
|
{ |
||||
|
// write data
|
||||
|
int size = matrix.RowCount*matrix.ColumnCount*4; |
||||
|
writer.Write((int)DataType.Single); |
||||
|
writer.Write(size); |
||||
|
|
||||
|
for (var j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
var column = matrix.Column(j); |
||||
|
foreach (var value in column) |
||||
|
{ |
||||
|
writer.Write(value.Real); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PadData(writer, size%8); |
||||
|
|
||||
|
writer.Write((int)DataType.Single); |
||||
|
writer.Write(size); |
||||
|
|
||||
|
for (var j = 0; j < matrix.ColumnCount; j++) |
||||
|
{ |
||||
|
var column = matrix.Column(j); |
||||
|
foreach (var value in column) |
||||
|
{ |
||||
|
writer.Write(value.Real); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PadData(writer, size%8); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Pads the data with the given byte.
|
||||
|
/// </summary>
|
||||
|
/// <param name="writer">Where to write the pad values.</param>
|
||||
|
/// <param name="bytes">The number of bytes to pad.</param>
|
||||
|
/// <param name="pad">What value to pad with.</param>
|
||||
|
static void PadData(BinaryWriter writer, int bytes, byte pad = (byte)0) |
||||
|
{ |
||||
|
for (var i = 0; i < bytes; i++) |
||||
|
{ |
||||
|
writer.Write(pad); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,257 +0,0 @@ |
|||||
// <copyright file="NumericArrayReader.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-2014 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; |
|
||||
using System.Numerics; |
|
||||
using MathNet.Numerics.LinearAlgebra; |
|
||||
|
|
||||
namespace MathNet.Numerics.Data.Matlab |
|
||||
{ |
|
||||
internal static class NumericArrayReader<TDataType> |
|
||||
where TDataType : struct, IEquatable<TDataType>, IFormattable |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Populates a dense matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="isComplex">if set to <c>true</c> if the MATLAB complex flag is set.</param>
|
|
||||
/// <param name="rows">The number of rows.</param>
|
|
||||
/// <param name="columns">The number of columns.</param>
|
|
||||
/// <param name="size">The length of the stored data.</param>
|
|
||||
/// <returns>Returns a populated dense matrix.</returns>
|
|
||||
public static Matrix<TDataType> PopulateDenseMatrix(DataType type, BinaryReader reader, bool isComplex, int rows, int columns, int size) |
|
||||
{ |
|
||||
var dataType = typeof (TDataType); |
|
||||
Matrix<TDataType> matrix; |
|
||||
if (type == DataType.Double && dataType == typeof (double)) |
|
||||
{ |
|
||||
var count = rows*columns; |
|
||||
var data = new double[count]; |
|
||||
Buffer.BlockCopy(reader.ReadBytes(count*Constants.SizeOfDouble), 0, data, 0, count*Constants.SizeOfDouble); |
|
||||
matrix = (Matrix<TDataType>)(object)new LinearAlgebra.Double.DenseMatrix(rows, columns, data); |
|
||||
} |
|
||||
else if (type == DataType.Single && dataType == typeof (float)) |
|
||||
{ |
|
||||
var count = rows*columns; |
|
||||
var data = new float[count]; |
|
||||
Buffer.BlockCopy(reader.ReadBytes(count*Constants.SizeOfFloat), 0, data, 0, count*Constants.SizeOfFloat); |
|
||||
matrix = (Matrix<TDataType>)(object)new LinearAlgebra.Single.DenseMatrix(rows, columns, data); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
matrix = Matrix<TDataType>.Build.Dense(rows, columns); |
|
||||
|
|
||||
if (dataType == typeof (double)) |
|
||||
{ |
|
||||
if (isComplex) |
|
||||
{ |
|
||||
throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); |
|
||||
} |
|
||||
|
|
||||
PopulateDoubleDenseMatrix((Matrix<double>)(object)matrix, type, reader, rows, columns); |
|
||||
} |
|
||||
else if (dataType == typeof (float)) |
|
||||
{ |
|
||||
if (isComplex) |
|
||||
{ |
|
||||
throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); |
|
||||
} |
|
||||
|
|
||||
PopulateSingleDenseMatrix((Matrix<float>)(object)matrix, type, reader, rows, columns); |
|
||||
} |
|
||||
else if (dataType == typeof (Complex)) |
|
||||
{ |
|
||||
PopulateComplexDenseMatrix((Matrix<Complex>)(object)matrix, type, isComplex, reader, rows, columns, size); |
|
||||
} |
|
||||
else if (dataType == typeof (Complex32)) |
|
||||
{ |
|
||||
PopulateComplex32DenseMatrix((Matrix<Complex32>)(object)matrix, type, isComplex, reader, rows, columns, size); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
throw new NotSupportedException(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return matrix; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the double dense matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to populate.</param>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="rows">The number of rows.</param>
|
|
||||
/// <param name="columns">The number of columns.</param>
|
|
||||
static void PopulateDoubleDenseMatrix(Matrix<double> matrix, DataType type, BinaryReader reader, int rows, int columns) |
|
||||
{ |
|
||||
for (var j = 0; j < columns; j++) |
|
||||
{ |
|
||||
for (var i = 0; i < rows; i++) |
|
||||
{ |
|
||||
matrix.At(i, j, ReadDoubleValue(type, reader)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the complex dense matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to populate.</param>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="isComplex">if set to <c>true</c> if the MATLAB complex flag is set.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="rows">The number of rows.</param>
|
|
||||
/// <param name="columns">The number of columns.</param>
|
|
||||
/// <param name="dataSize">The length of the stored data.</param>
|
|
||||
static void PopulateComplexDenseMatrix(Matrix<Complex> matrix, DataType type, bool isComplex, BinaryReader reader, int rows, int columns, int dataSize) |
|
||||
{ |
|
||||
for (var j = 0; j < columns; j++) |
|
||||
{ |
|
||||
for (var i = 0; i < rows; i++) |
|
||||
{ |
|
||||
matrix.At(i, j, ReadDoubleValue(type, reader)); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if (isComplex) |
|
||||
{ |
|
||||
var skip = dataSize%8; |
|
||||
|
|
||||
// skip pad
|
|
||||
reader.ReadBytes(skip); |
|
||||
|
|
||||
// skip header
|
|
||||
type = (DataType)reader.ReadInt32(); |
|
||||
reader.ReadInt32(); |
|
||||
|
|
||||
for (var j = 0; j < columns; j++) |
|
||||
{ |
|
||||
for (var i = 0; i < rows; i++) |
|
||||
{ |
|
||||
matrix.At(i, j, new Complex(matrix.At(i, j).Real, ReadDoubleValue(type, reader))); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the complex32 dense matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to populate.</param>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="isComplex">if set to <c>true</c> if the MATLAB complex flag is set.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="rows">The number of rows.</param>
|
|
||||
/// <param name="columns">The number of columns.</param>
|
|
||||
/// <param name="dataSize">The length of the stored data.</param>
|
|
||||
static void PopulateComplex32DenseMatrix(Matrix<Complex32> matrix, DataType type, bool isComplex, BinaryReader reader, int rows, int columns, int dataSize) |
|
||||
{ |
|
||||
for (var j = 0; j < columns; j++) |
|
||||
{ |
|
||||
for (var i = 0; i < rows; i++) |
|
||||
{ |
|
||||
matrix.At(i, j, (float)ReadDoubleValue(type, reader)); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if (isComplex) |
|
||||
{ |
|
||||
var skip = dataSize%8; |
|
||||
|
|
||||
// skip pad
|
|
||||
reader.ReadBytes(skip); |
|
||||
|
|
||||
// skip header
|
|
||||
type = (DataType)reader.ReadInt32(); |
|
||||
reader.ReadInt32(); |
|
||||
|
|
||||
for (var j = 0; j < columns; j++) |
|
||||
{ |
|
||||
for (var i = 0; i < rows; i++) |
|
||||
{ |
|
||||
matrix.At(i, j, new Complex32(matrix.At(i, j).Real, (float)ReadDoubleValue(type, reader))); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the float dense matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="matrix">The matrix to populate.</param>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="rows">The number of rows.</param>
|
|
||||
/// <param name="columns">The number of columns.</param>
|
|
||||
static void PopulateSingleDenseMatrix(Matrix<float> matrix, DataType type, BinaryReader reader, int rows, int columns) |
|
||||
{ |
|
||||
for (var j = 0; j < columns; j++) |
|
||||
{ |
|
||||
for (var i = 0; i < rows; i++) |
|
||||
{ |
|
||||
matrix.At(i, j, (float)ReadDoubleValue(type, reader)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
static double ReadDoubleValue(DataType type, BinaryReader reader) |
|
||||
{ |
|
||||
switch (type) |
|
||||
{ |
|
||||
case DataType.Double: |
|
||||
return reader.ReadDouble(); |
|
||||
case DataType.Int8: |
|
||||
return reader.ReadSByte(); |
|
||||
case DataType.UInt8: |
|
||||
return reader.ReadByte(); |
|
||||
case DataType.Int16: |
|
||||
return reader.ReadInt16(); |
|
||||
case DataType.UInt16: |
|
||||
return reader.ReadUInt16(); |
|
||||
case DataType.Int32: |
|
||||
return reader.ReadInt32(); |
|
||||
case DataType.UInt32: |
|
||||
return reader.ReadUInt32(); |
|
||||
case DataType.Single: |
|
||||
return reader.ReadSingle(); |
|
||||
case DataType.Int64: |
|
||||
return reader.ReadInt64(); |
|
||||
case DataType.UInt64: |
|
||||
return reader.ReadUInt64(); |
|
||||
default: |
|
||||
throw new NotSupportedException(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,301 @@ |
|||||
|
// <copyright file="SparseArrayFormatter.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-2014 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.IO; |
||||
|
using System.Numerics; |
||||
|
using MathNet.Numerics.LinearAlgebra.Storage; |
||||
|
|
||||
|
namespace MathNet.Numerics.Data.Matlab |
||||
|
{ |
||||
|
internal static class SparseArrayFormatter |
||||
|
{ |
||||
|
//public static void FormatSparseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, string name, bool isComplex, int rows, int columns, int size)
|
||||
|
// where T : struct, IEquatable<T>, IFormattable
|
||||
|
//{
|
||||
|
// var transposed = matrix.Transpose();
|
||||
|
// var storage = (SparseCompressedRowMatrixStorage<T>)transposed.Storage;
|
||||
|
|
||||
|
// WriteMatrixTagAndName(writer, ArrayClass.Sparse, isComplex, name, storage);
|
||||
|
//}
|
||||
|
|
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, LinearAlgebra.Double.SparseMatrix matrix) |
||||
|
{ |
||||
|
var nzmax = matrix.NonZerosCount; |
||||
|
|
||||
|
// write ir
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item1); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (nzmax%2 == 1) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write jc
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write((matrix.ColumnCount + 1)*4); |
||||
|
writer.Write(0); |
||||
|
var count = 0; |
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
count += ((SparseVectorStorage<double>)column.Storage).ValueCount; |
||||
|
writer.Write(count); |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (matrix.ColumnCount%2 == 0) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write data
|
||||
|
writer.Write((int)DataType.Double); |
||||
|
writer.Write(nzmax*8); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item2); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, LinearAlgebra.Single.SparseMatrix matrix) |
||||
|
{ |
||||
|
var nzmax = matrix.NonZerosCount; |
||||
|
|
||||
|
// write ir
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item1); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (nzmax%2 == 1) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write jc
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write((matrix.ColumnCount + 1)*4); |
||||
|
writer.Write(0); |
||||
|
var count = 0; |
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
count += ((SparseVectorStorage<float>)column.Storage).ValueCount; |
||||
|
writer.Write(count); |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (matrix.ColumnCount%2 == 0) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write data
|
||||
|
writer.Write((int)DataType.Single); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item2); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var pad = nzmax*4%8; |
||||
|
PadData(writer, pad); |
||||
|
} |
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, LinearAlgebra.Complex.SparseMatrix matrix) |
||||
|
{ |
||||
|
var nzmax = matrix.NonZerosCount; |
||||
|
|
||||
|
// write ir
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item1); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (nzmax%2 == 1) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write jc
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write((matrix.ColumnCount + 1)*4); |
||||
|
writer.Write(0); |
||||
|
var count = 0; |
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
count += ((SparseVectorStorage<Complex>)column.Storage).ValueCount; |
||||
|
writer.Write(count); |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (matrix.ColumnCount%2 == 0) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write data
|
||||
|
writer.Write((int)DataType.Double); |
||||
|
writer.Write(nzmax*8); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item2.Real); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
writer.Write((int)DataType.Double); |
||||
|
writer.Write(nzmax*8); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item2.Imaginary); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
internal static void Write(BinaryWriter writer, LinearAlgebra.Complex32.SparseMatrix matrix) |
||||
|
{ |
||||
|
var nzmax = matrix.NonZerosCount; |
||||
|
|
||||
|
// write ir
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item1); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (nzmax%2 == 1) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write jc
|
||||
|
writer.Write((int)DataType.Int32); |
||||
|
writer.Write((matrix.ColumnCount + 1)*4); |
||||
|
writer.Write(0); |
||||
|
var count = 0; |
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
count += ((SparseVectorStorage<Complex32>)column.Storage).ValueCount; |
||||
|
writer.Write(count); |
||||
|
} |
||||
|
|
||||
|
// add pad if needed
|
||||
|
if (matrix.ColumnCount%2 == 0) |
||||
|
{ |
||||
|
writer.Write(0); |
||||
|
} |
||||
|
|
||||
|
// write data
|
||||
|
writer.Write((int)DataType.Single); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item2.Real); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var pad = nzmax*4%8; |
||||
|
PadData(writer, pad); |
||||
|
|
||||
|
writer.Write((int)DataType.Single); |
||||
|
writer.Write(nzmax*4); |
||||
|
|
||||
|
foreach (var column in matrix.EnumerateColumns()) |
||||
|
{ |
||||
|
foreach (var row in column.EnumerateNonZeroIndexed()) |
||||
|
{ |
||||
|
writer.Write(row.Item2.Imaginary); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PadData(writer, pad); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Pads the data with the given byte.
|
||||
|
/// </summary>
|
||||
|
/// <param name="writer">Where to write the pad values.</param>
|
||||
|
/// <param name="bytes">The number of bytes to pad.</param>
|
||||
|
/// <param name="pad">What value to pad with.</param>
|
||||
|
static void PadData(BinaryWriter writer, int bytes, byte pad = (byte)0) |
||||
|
{ |
||||
|
for (var i = 0; i < bytes; i++) |
||||
|
{ |
||||
|
writer.Write(pad); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,251 +0,0 @@ |
|||||
// <copyright file="SparseArrayReader.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-2014 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; |
|
||||
using System.Numerics; |
|
||||
using MathNet.Numerics.LinearAlgebra; |
|
||||
using MathNet.Numerics.LinearAlgebra.Storage; |
|
||||
|
|
||||
namespace MathNet.Numerics.Data.Matlab |
|
||||
{ |
|
||||
internal static class SparseArrayReader<TDataType> |
|
||||
where TDataType : struct, IEquatable<TDataType>, IFormattable |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Populates a sparse matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="reader">The reader.</param>
|
|
||||
/// <param name="isComplex">if set to <c>true</c> if the MATLAB complex flag is set.</param>
|
|
||||
/// <param name="rows">The number of rows.</param>
|
|
||||
/// <param name="columns">The number of columns.</param>
|
|
||||
/// <param name="size">The size of the block.</param>
|
|
||||
/// <returns>A populated sparse matrix.</returns>
|
|
||||
public static Matrix<TDataType> PopulateSparseMatrix(BinaryReader reader, bool isComplex, int rows, int columns, int size) |
|
||||
{ |
|
||||
// Create matrix with CSR storage.
|
|
||||
var matrix = Matrix<TDataType>.Build.Sparse(columns, rows); |
|
||||
|
|
||||
// MATLAB sparse matrices are actually stored as CSC, so just read the data and then transpose.
|
|
||||
var storage = matrix.Storage as SparseCompressedRowMatrixStorage<TDataType>; |
|
||||
|
|
||||
// populate the row data array
|
|
||||
var ir = storage.ColumnIndices = new int[size/4]; |
|
||||
for (var i = 0; i < ir.Length; i++) |
|
||||
{ |
|
||||
ir[i] = reader.ReadInt32(); |
|
||||
} |
|
||||
|
|
||||
Parser.AlignData(reader.BaseStream, size, false); |
|
||||
|
|
||||
// skip data type since it will always be int32
|
|
||||
reader.BaseStream.Seek(4, SeekOrigin.Current); |
|
||||
|
|
||||
// populate the column data array
|
|
||||
var jcsize = reader.ReadInt32(); |
|
||||
var jc = storage.RowPointers; |
|
||||
if (jc.Length != jcsize/4) |
|
||||
{ |
|
||||
throw new Exception("invalid jcsize"); |
|
||||
} |
|
||||
|
|
||||
for (var j = 0; j < jc.Length; j++) |
|
||||
{ |
|
||||
jc[j] = reader.ReadInt32(); |
|
||||
} |
|
||||
|
|
||||
Parser.AlignData(reader.BaseStream, jcsize, false); |
|
||||
|
|
||||
var type = (DataType)reader.ReadInt32(); |
|
||||
var dataSize = reader.ReadInt32(); |
|
||||
|
|
||||
var dataType = typeof (TDataType); |
|
||||
|
|
||||
// Allocate memory for matrix values
|
|
||||
var data = storage.Values = new TDataType[jc[columns]]; |
|
||||
|
|
||||
if (dataType == typeof (double)) |
|
||||
{ |
|
||||
if (isComplex) |
|
||||
{ |
|
||||
throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); |
|
||||
} |
|
||||
|
|
||||
PopulateDoubleSparseMatrix(type, (double[])(object)data, reader); |
|
||||
} |
|
||||
else if (dataType == typeof (float)) |
|
||||
{ |
|
||||
if (isComplex) |
|
||||
{ |
|
||||
throw new ArgumentException("Invalid TDataType. Matrix is stored as a complex matrix, but a real data type was given."); |
|
||||
} |
|
||||
|
|
||||
PopulateSingleSparseMatrix(type, (float[])(object)data, reader); |
|
||||
} |
|
||||
else if (dataType == typeof (Complex)) |
|
||||
{ |
|
||||
PopulateComplexSparseMatrix(type, isComplex, (Complex[])(object)data, reader, dataSize); |
|
||||
} |
|
||||
else if (dataType == typeof (Complex32)) |
|
||||
{ |
|
||||
PopulateComplex32SparseMatrix(type, isComplex, (Complex32[])(object)data, reader, dataSize); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
throw new NotSupportedException(); |
|
||||
} |
|
||||
|
|
||||
return matrix.Transpose(); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the double sparse matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="data">The matrix values array.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
static void PopulateDoubleSparseMatrix(DataType type, double[] data, BinaryReader reader) |
|
||||
{ |
|
||||
for (var i = 0; i < data.Length; i++) |
|
||||
{ |
|
||||
data[i] = ReadDoubleValue(type, reader); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the float sparse matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="data">The matrix values array.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
static void PopulateSingleSparseMatrix(DataType type, float[] data, BinaryReader reader) |
|
||||
{ |
|
||||
for (var i = 0; i < data.Length; i++) |
|
||||
{ |
|
||||
data[i] = (float)ReadDoubleValue(type, reader); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the complex sparse matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="isComplex">if set to <c>true</c> if the MATLAB complex flag is set.</param>
|
|
||||
/// <param name="data">The matrix values array.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="dataSize">The length of the stored data.</param>
|
|
||||
static void PopulateComplexSparseMatrix(DataType type, bool isComplex, Complex[] data, BinaryReader reader, int dataSize) |
|
||||
{ |
|
||||
for (var i = 0; i < data.Length; i++) |
|
||||
{ |
|
||||
data[i] = ReadDoubleValue(type, reader); |
|
||||
} |
|
||||
|
|
||||
if (isComplex) |
|
||||
{ |
|
||||
var skip = dataSize%8; |
|
||||
|
|
||||
// skip pad
|
|
||||
reader.ReadBytes(skip); |
|
||||
|
|
||||
// skip header
|
|
||||
type = (DataType)reader.ReadInt32(); |
|
||||
reader.ReadInt32(); |
|
||||
|
|
||||
for (var i = 0; i < data.Length; i++) |
|
||||
{ |
|
||||
data[i] += new Complex(0.0, ReadDoubleValue(type, reader)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Populates the complex32 sparse matrix.
|
|
||||
/// </summary>
|
|
||||
/// <param name="type">The MATLAB data type.</param>
|
|
||||
/// <param name="isComplex">if set to <c>true</c> if the MATLAB complex flag is set.</param>
|
|
||||
/// <param name="data">The matrix values array.</param>
|
|
||||
/// <param name="reader">The reader to read from.</param>
|
|
||||
/// <param name="dataSize">The length of the stored data.</param>
|
|
||||
static void PopulateComplex32SparseMatrix(DataType type, bool isComplex, Complex32[] data, BinaryReader reader, int dataSize) |
|
||||
{ |
|
||||
for (var i = 0; i < data.Length; i++) |
|
||||
{ |
|
||||
data[i] = (float)ReadDoubleValue(type, reader); |
|
||||
} |
|
||||
|
|
||||
if (isComplex) |
|
||||
{ |
|
||||
var skip = dataSize%8; |
|
||||
|
|
||||
// skip pad
|
|
||||
reader.ReadBytes(skip); |
|
||||
|
|
||||
// skip header
|
|
||||
type = (DataType)reader.ReadInt32(); |
|
||||
reader.ReadInt32(); |
|
||||
|
|
||||
for (var i = 0; i < data.Length; i++) |
|
||||
{ |
|
||||
data[i] += new Complex32(0.0f, (float)ReadDoubleValue(type, reader)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
static double ReadDoubleValue(DataType type, BinaryReader reader) |
|
||||
{ |
|
||||
switch (type) |
|
||||
{ |
|
||||
case DataType.Double: |
|
||||
return reader.ReadDouble(); |
|
||||
case DataType.Int8: |
|
||||
return reader.ReadSByte(); |
|
||||
case DataType.UInt8: |
|
||||
return reader.ReadByte(); |
|
||||
case DataType.Int16: |
|
||||
return reader.ReadInt16(); |
|
||||
case DataType.UInt16: |
|
||||
return reader.ReadUInt16(); |
|
||||
case DataType.Int32: |
|
||||
return reader.ReadInt32(); |
|
||||
case DataType.UInt32: |
|
||||
return reader.ReadUInt32(); |
|
||||
case DataType.Single: |
|
||||
return reader.ReadSingle(); |
|
||||
case DataType.Int64: |
|
||||
return reader.ReadInt64(); |
|
||||
case DataType.UInt64: |
|
||||
return reader.ReadUInt64(); |
|
||||
default: |
|
||||
throw new NotSupportedException(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue