Browse Source

LA: change matrix.As*() methods to return null instead of fallback to copy in case of a mismatch

v3
Christoph Ruegg 10 years ago
parent
commit
532f743907
  1. 25
      src/Numerics/LinearAlgebra/Matrix.cs
  2. 10
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  3. 30
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs

25
src/Numerics/LinearAlgebra/Matrix.cs

@ -1372,9 +1372,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Returns this matrix as a multidimensional array.
/// If the internal data structure matches the requested shape it is returned directly, without copying.
/// If the internal structure is returned, changes to the array and the matrix will affect each other.
/// Returns the internal multidimensional array of this matrix if, and only if, this matrix is stored by such an array internally.
/// Otherwise returns null. Changes to the returned array and the matrix will affect each other.
/// Use ToArray instead if you always need an independent array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
@ -1384,9 +1383,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid out column by column (column major).
/// If the internal data structure matches the requested shape it is returned directly, without copying.
/// If the internal structure is returned, changes to the array and the matrix will affect each other.
/// Returns the internal column by column (column major) array of this matrix if, and only if, this matrix is stored by such arrays internally.
/// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
/// Use ToColumnMajorArray instead if you always need an independent array.
/// </summary>
/// <example><pre>
@ -1403,9 +1401,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid row by row (row major).
/// If the internal data structure matches the requested shape it is returned directly, without copying.
/// If the internal structure is returned, changes to the array and the matrix will affect each other.
/// Returns the internal row by row (row major) array of this matrix if, and only if, this matrix is stored by such arrays internally.
/// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
/// Use ToRowMajorArray instead if you always need an independent array.
/// </summary>
/// <example><pre>
@ -1422,9 +1419,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Returns this matrix as array of row arrays.
/// If the internal data structure matches the requested shape it is returned directly, without copying.
/// If the internal structure is returned, changes to the array and the matrix will affect each other.
/// Returns the internal row arrays of this matrix if, and only if, this matrix is stored by such arrays internally.
/// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
/// Use ToRowArrays instead if you always need an independent array.
/// </summary>
public T[][] AsRowArrays()
@ -1433,9 +1429,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Returns this matrix as array of column arrays.
/// If the internal data structure matches the requested shape it is returned directly, without copying.
/// If the internal structure is returned, changes to the array and the matrix will affect each other.
/// Returns the internal column arrays of this matrix if, and only if, this matrix is stored by such arrays internally.
/// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
/// Use ToColumnArrays instead if you always need an independent array.
/// </summary>
public T[][] AsColumnArrays()

10
src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs

@ -581,27 +581,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
public virtual T[] AsRowMajorArray()
{
return ToRowMajorArray();
return null;
}
public virtual T[] AsColumnMajorArray()
{
return ToColumnMajorArray();
return null;
}
public virtual T[][] AsRowArrays()
{
return ToRowArrays();
return null;
}
public virtual T[][] AsColumnArrays()
{
return ToColumnArrays();
return null;
}
public virtual T[,] AsArray()
{
return ToArray();
return null;
}
// ENUMERATION

30
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs

@ -307,6 +307,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
Matrix<T> matrix = Get(testMatrix);
var array = matrix.AsArray();
if (array == null)
{
return;
}
Assert.That(array, Is.SameAs(matrix.AsArray()));
Assert.That(array, Is.Not.SameAs(matrix.ToArray()));
Assert.That(array.GetLength(0), Is.EqualTo(matrix.RowCount));
Assert.That(array.GetLength(1), Is.EqualTo(matrix.ColumnCount));
@ -324,6 +330,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
Matrix<T> matrix = Get(testMatrix);
var columnArrays = matrix.AsColumnArrays();
if (columnArrays == null)
{
return;
}
Assert.That(columnArrays, Is.SameAs(matrix.AsColumnArrays()));
Assert.That(columnArrays, Is.Not.SameAs(matrix.ToColumnArrays()));
Assert.That(columnArrays.Length, Is.EqualTo(matrix.ColumnCount));
Assert.That(columnArrays[0].Length, Is.EqualTo(matrix.RowCount));
@ -341,6 +353,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
Matrix<T> matrix = Get(testMatrix);
var rowArrays = matrix.AsRowArrays();
if (rowArrays == null)
{
return;
}
Assert.That(rowArrays, Is.SameAs(matrix.AsRowArrays()));
Assert.That(rowArrays, Is.Not.SameAs(matrix.ToRowArrays()));
Assert.That(rowArrays.Length, Is.EqualTo(matrix.RowCount));
Assert.That(rowArrays[0].Length, Is.EqualTo(matrix.ColumnCount));
@ -358,6 +376,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
Matrix<T> matrix = Get(testMatrix);
var array = matrix.AsColumnMajorArray();
if (array == null)
{
return;
}
Assert.That(array, Is.SameAs(matrix.AsColumnMajorArray()));
Assert.That(array, Is.Not.SameAs(matrix.ToColumnMajorArray()));
Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount));
for (int i = 0; i < array.Length; i++)
@ -371,6 +395,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
{
Matrix<T> matrix = Get(testMatrix);
var array = matrix.AsRowMajorArray();
if (array == null)
{
return;
}
Assert.That(array, Is.SameAs(matrix.AsRowMajorArray()));
Assert.That(array, Is.Not.SameAs(matrix.ToRowMajorArray()));
Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount));
for (int i = 0; i < array.Length; i++)

Loading…
Cancel
Save