Fix #20
This commit is contained in:
parent
fffd539f4f
commit
5cd2528b71
@ -201,6 +201,19 @@ namespace MatFileHandler.Tests
|
||||
Assert.That((structure["y", 0, 2] as IArrayOf<double>)?[0, 2], Is.EqualTo(3.0));
|
||||
Assert.That((structure["x", 1, 2] as IArrayOf<float>)?[0], Is.EqualTo(1.5f));
|
||||
Assert.That(structure["y", 1, 2].IsEmpty, Is.True);
|
||||
|
||||
Assert.That(structure["y", 0, 2].ConvertTo2dDoubleArray(), Is.EqualTo(
|
||||
new double[,]
|
||||
{
|
||||
{ 1, 2, 3 },
|
||||
{ 4, 5, 6 },
|
||||
}));
|
||||
Assert.That(structure["y", 0, 2].ConvertToMultidimensionalDoubleArray(), Is.EqualTo(
|
||||
new double[,]
|
||||
{
|
||||
{ 1, 2, 3 },
|
||||
{ 4, 5, 6 },
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -221,6 +234,15 @@ namespace MatFileHandler.Tests
|
||||
Assert.That(sparseArray[0, 4], Is.EqualTo(0.0));
|
||||
Assert.That(sparseArray[3, 0], Is.EqualTo(0.0));
|
||||
Assert.That(sparseArray[3, 4], Is.EqualTo(0.0));
|
||||
|
||||
Assert.That(sparseArray.ConvertTo2dDoubleArray(), Is.EqualTo(
|
||||
new double[,]
|
||||
{
|
||||
{ 0, 0, 0, 0, 0 },
|
||||
{ 0, 1, 2, 0, 0 },
|
||||
{ 0, 3, 0, 4, 0 },
|
||||
{ 0, 0, 0, 0, 0 },
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -462,6 +484,47 @@ namespace MatFileHandler.Tests
|
||||
Assert.That(d0, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_3DArrays()
|
||||
{
|
||||
var matFile = GetTests("good")["issue20.mat"];
|
||||
var obj = matFile["a3d"].Value;
|
||||
var values = obj.ConvertToDoubleArray();
|
||||
Assert.That(values, Is.EqualTo(Enumerable.Range(1, 24)));
|
||||
var expected = new double[3, 4, 2]
|
||||
{
|
||||
{
|
||||
{ 1, 13 },
|
||||
{ 4, 16 },
|
||||
{ 7, 19 },
|
||||
{ 10, 22 },
|
||||
},
|
||||
{
|
||||
{ 2, 14 },
|
||||
{ 5, 17 },
|
||||
{ 8, 20 },
|
||||
{ 11, 23 },
|
||||
},
|
||||
{
|
||||
{ 3, 15 },
|
||||
{ 6, 18 },
|
||||
{ 9, 21 },
|
||||
{ 12, 24 },
|
||||
},
|
||||
};
|
||||
Assert.That(obj.ConvertToMultidimensionalDoubleArray(), Is.EqualTo(expected));
|
||||
Assert.That(obj.ConvertTo2dDoubleArray(), Is.EqualTo(null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_4DArrays()
|
||||
{
|
||||
var matFile = GetTests("good")["issue20.mat"];
|
||||
var obj = matFile["a4d"].Value;
|
||||
Assert.That(obj.ConvertToDoubleArray(), Is.EqualTo(Enumerable.Range(1, 120)));
|
||||
Assert.That(obj.ConvertTo2dDoubleArray(), Is.EqualTo(null));
|
||||
}
|
||||
|
||||
private static AbstractTestDataFactory<IMatFile> GetTests(string factoryName) =>
|
||||
new MatTestDataFactory(Path.Combine(TestDirectory, factoryName));
|
||||
|
||||
|
BIN
MatFileHandler.Tests/test-data/good/issue20.mat
Normal file
BIN
MatFileHandler.Tests/test-data/good/issue20.mat
Normal file
Binary file not shown.
@ -1,5 +1,6 @@
|
||||
// Copyright 2017-2018 Alexander Luzgarev
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace MatFileHandler
|
||||
@ -36,5 +37,48 @@ namespace MatFileHandler
|
||||
{
|
||||
return dimensions.Aggregate(1, (x, y) => x * y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rearrange data from a flat (column-major) array into a multi-dimensional array.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Array element type.</typeparam>
|
||||
/// <param name="dimensions">Target array dimensions.</param>
|
||||
/// <param name="data">Flat (column-major) data to rearrange.</param>
|
||||
/// <returns>An array of specified dimensions containing data from the original flat array, layed out according to column-major order.</returns>
|
||||
public static Array UnflattenArray<T>(this int[] dimensions, T[] data)
|
||||
{
|
||||
var result = Array.CreateInstance(typeof(T), dimensions);
|
||||
var n = dimensions.NumberOfElements();
|
||||
var indices = new int[dimensions.Length];
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
result.SetValue(data[i], indices);
|
||||
IncrementMultiIndex(dimensions, indices);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void IncrementMultiIndex(int[] dimensions, int[] indices)
|
||||
{
|
||||
var currentPosition = 0;
|
||||
while (true)
|
||||
{
|
||||
if (currentPosition >= indices.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
indices[currentPosition]++;
|
||||
if (indices[currentPosition] >= dimensions[currentPosition])
|
||||
{
|
||||
indices[currentPosition] = 0;
|
||||
currentPosition++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
// Copyright 2017-2018 Alexander Luzgarev
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace MatFileHandler
|
||||
@ -30,6 +31,18 @@ namespace MatFileHandler
|
||||
/// <returns>Array of values of the array, converted to Double, or null if the conversion is not possible.</returns>
|
||||
double[]? ConvertToDoubleArray();
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert the array to a 2-dimensional array of Double values.
|
||||
/// </summary>
|
||||
/// <returns>2-dimensional array of values of the array, converted to Double, or null if the conversion is not possible (for example, when the array has more than 2 dimensions).</returns>
|
||||
double[,]? ConvertTo2dDoubleArray();
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert the array to a multidimensional array of Double values.
|
||||
/// </summary>
|
||||
/// <returns>Multidimensional array of values of the array, converted to Double, or null if the conversion is not possible.</returns>
|
||||
Array? ConvertToMultidimensionalDoubleArray();
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert the array to an array of Complex values.
|
||||
/// </summary>
|
||||
|
@ -1,5 +1,6 @@
|
||||
// Copyright 2017-2018 Alexander Luzgarev
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
namespace MatFileHandler
|
||||
@ -59,6 +60,18 @@ namespace MatFileHandler
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual double[,]? ConvertTo2dDoubleArray()
|
||||
{
|
||||
return ConvertToMultidimensionalDoubleArray() as double[,];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual Array? ConvertToMultidimensionalDoubleArray()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual Complex[]? ConvertToComplexArray()
|
||||
{
|
||||
|
@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2018 Alexander Luzgarev
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
namespace MatFileHandler
|
||||
@ -59,6 +57,14 @@ namespace MatFileHandler
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Array? ConvertToMultidimensionalDoubleArray()
|
||||
{
|
||||
var doubleData = ConvertToDoubleArray();
|
||||
var rearrangedData = Dimensions.UnflattenArray(doubleData);
|
||||
return rearrangedData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert the array to an array of Complex values.
|
||||
/// </summary>
|
||||
|
@ -64,6 +64,23 @@ namespace MatFileHandler
|
||||
return data as double[] ?? data.Select(x => Convert.ToDouble(x)).ToArray();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Array? ConvertToMultidimensionalDoubleArray()
|
||||
{
|
||||
if (Dimensions.Length != 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var result = new double[Dimensions[0], Dimensions[1]];
|
||||
foreach (var pair in Data)
|
||||
{
|
||||
result[pair.Key.row, pair.Key.column] = Convert.ToDouble(pair.Value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert the array to an array of Complex values.
|
||||
/// </summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user