Browse Source

LA: add AsColumnMajorArray and related, that may directly return internal arrays if the schema is matching

v3
Christoph Ruegg 10 years ago
parent
commit
fd169e236c
  1. 111
      src/Numerics/LinearAlgebra/Matrix.cs
  2. 5
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  3. 25
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  4. 5
      src/UnitTests/LinearAlgebraTests/Complex/UserDefinedMatrix.cs
  5. 5
      src/UnitTests/LinearAlgebraTests/Complex32/UserDefinedMatrix.cs
  6. 5
      src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrix.cs
  7. 5
      src/UnitTests/LinearAlgebraTests/Single/UserDefinedMatrix.cs

111
src/Numerics/LinearAlgebra/Matrix.cs

@ -1280,6 +1280,8 @@ namespace MathNet.Numerics.LinearAlgebra
/// <summary> /// <summary>
/// Returns this matrix as a multidimensional array. /// Returns this matrix as a multidimensional array.
/// The returned array will be independent from this matrix.
/// A new memory block will be allocated for the array.
/// </summary> /// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns> /// <returns>A multidimensional containing the values of this matrix.</returns>
public T[,] ToArray() public T[,] ToArray()
@ -1288,7 +1290,9 @@ namespace MathNet.Numerics.LinearAlgebra
} }
/// <summary> /// <summary>
/// Returns the matrix's elements as an array with the data laid out column-wise. /// Returns the matrix's elements as an array with the data laid out column by column (column major).
/// The returned array will be independent from this matrix.
/// A new memory block will be allocated for the array.
/// </summary> /// </summary>
/// <example><pre> /// <example><pre>
/// 1, 2, 3 /// 1, 2, 3
@ -1296,15 +1300,23 @@ namespace MathNet.Numerics.LinearAlgebra
/// 7, 8, 9 /// 7, 8, 9
/// </pre></example> /// </pre></example>
/// <returns>An array containing the matrix's elements.</returns> /// <returns>An array containing the matrix's elements.</returns>
/// <seealso cref="ToRowWiseArray"/> /// <seealso cref="ToRowMajorArray"/>
/// <seealso cref="Enumerate(Zeros)"/> /// <seealso cref="Enumerate(Zeros)"/>
public T[] ToColumnWiseArray() public T[] ToColumnMajorArray()
{ {
return Storage.ToColumnMajorArray(); return Storage.ToColumnMajorArray();
} }
[Obsolete("Use ToColumnMajorArray instead. Will be removed in v4.")]
public T[] ToColumnWiseArray()
{
return ToColumnMajorArray();
}
/// <summary> /// <summary>
/// Returns the matrix's elements as an array with the data laid row-wise. /// Returns the matrix's elements as an array with the data laid row by row (row major).
/// The returned array will be independent from this matrix.
/// A new memory block will be allocated for the array.
/// </summary> /// </summary>
/// <example><pre> /// <example><pre>
/// 1, 2, 3 /// 1, 2, 3
@ -1312,15 +1324,24 @@ namespace MathNet.Numerics.LinearAlgebra
/// 7, 8, 9 /// 7, 8, 9
/// </pre></example> /// </pre></example>
/// <returns>An array containing the matrix's elements.</returns> /// <returns>An array containing the matrix's elements.</returns>
/// <seealso cref="ToColumnWiseArray"/> /// <seealso cref="ToColumnMajorArray"/>
/// <seealso cref="Enumerate(Zeros)"/> /// <seealso cref="Enumerate(Zeros)"/>
public T[] ToRowWiseArray() public T[] ToRowMajorArray()
{ {
return Storage.ToRowMajorArray(); return Storage.ToRowMajorArray();
} }
[Obsolete("Use ToRowMajorArray instead. Will be removed in v4.")]
public T[] ToRowWiseArray()
{
return ToRowMajorArray();
}
/// <summary> /// <summary>
/// Returns this matrix as array of row arrays. /// Returns this matrix as array of row arrays.
/// The returned arrays will be independent from this matrix.
/// A new memory block will be allocated for the arrays.
/// </summary> /// </summary>
public T[][] ToRowArrays() public T[][] ToRowArrays()
{ {
@ -1329,12 +1350,86 @@ namespace MathNet.Numerics.LinearAlgebra
/// <summary> /// <summary>
/// Returns this matrix as array of column arrays. /// Returns this matrix as array of column arrays.
/// The returned arrays will be independent from this matrix.
/// A new memory block will be allocated for the arrays.
/// </summary> /// </summary>
public T[][] ToColumnArrays() public T[][] ToColumnArrays()
{ {
return Storage.ToColumnArrays(); return Storage.ToColumnArrays();
} }
/// <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.
/// Use ToArray instead if you always need an independent array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
public T[,] AsArray()
{
return Storage.AsArray();
}
/// <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.
/// Use ToColumnMajorArray instead if you always need an independent array.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
/// <seealso cref="ToRowMajorArray"/>
/// <seealso cref="Enumerate(Zeros)"/>
public T[] AsColumnMajorArray()
{
return Storage.AsColumnMajorArray();
}
/// <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.
/// Use ToRowMajorArray instead if you always need an independent array.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 2, 3, 4, 5, 6, 7, 8, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
/// <seealso cref="ToColumnMajorArray"/>
/// <seealso cref="Enumerate(Zeros)"/>
public T[] AsRowMajorArray()
{
return Storage.AsRowMajorArray();
}
/// <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.
/// Use ToRowArrays instead if you always need an independent array.
/// </summary>
public T[][] AsRowArrays()
{
return Storage.AsRowArrays();
}
/// <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.
/// Use ToColumnArrays instead if you always need an independent array.
/// </summary>
public T[][] AsColumnArrays()
{
return Storage.AsColumnArrays();
}
/// <summary> /// <summary>
/// Returns an IEnumerable that can be used to iterate through all values of the matrix. /// Returns an IEnumerable that can be used to iterate through all values of the matrix.
/// </summary> /// </summary>
@ -1342,8 +1437,6 @@ namespace MathNet.Numerics.LinearAlgebra
/// The enumerator will include all values, even if they are zero. /// The enumerator will include all values, even if they are zero.
/// The ordering of the values is unspecified (not necessarily column-wise or row-wise). /// The ordering of the values is unspecified (not necessarily column-wise or row-wise).
/// </remarks> /// </remarks>
/// <seealso cref="ToColumnWiseArray"/>
/// <seealso cref="ToRowWiseArray"/>
public IEnumerable<T> Enumerate() public IEnumerable<T> Enumerate()
{ {
return Storage.Enumerate(); return Storage.Enumerate();
@ -1356,8 +1449,6 @@ namespace MathNet.Numerics.LinearAlgebra
/// The enumerator will include all values, even if they are zero. /// The enumerator will include all values, even if they are zero.
/// The ordering of the values is unspecified (not necessarily column-wise or row-wise). /// The ordering of the values is unspecified (not necessarily column-wise or row-wise).
/// </remarks> /// </remarks>
/// <seealso cref="ToColumnWiseArray"/>
/// <seealso cref="ToRowWiseArray"/>
public IEnumerable<T> Enumerate(Zeros zeros = Zeros.Include) public IEnumerable<T> Enumerate(Zeros zeros = Zeros.Include)
{ {
switch (zeros) switch (zeros)

5
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -664,6 +664,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return ret; return ret;
} }
public override T[] AsColumnMajorArray()
{
return Data;
}
// ENUMERATION // ENUMERATION
public override IEnumerable<T> Enumerate() public override IEnumerable<T> Enumerate()

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

@ -565,6 +565,31 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return ret; return ret;
} }
public virtual T[] AsRowMajorArray()
{
return ToRowMajorArray();
}
public virtual T[] AsColumnMajorArray()
{
return ToColumnMajorArray();
}
public virtual T[][] AsRowArrays()
{
return ToRowArrays();
}
public virtual T[][] AsColumnArrays()
{
return ToColumnArrays();
}
public virtual T[,] AsArray()
{
return ToArray();
}
// ENUMERATION // ENUMERATION
public virtual IEnumerable<T> Enumerate() public virtual IEnumerable<T> Enumerate()

5
src/UnitTests/LinearAlgebraTests/Complex/UserDefinedMatrix.cs

@ -79,6 +79,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {
Data[row, column] = value; Data[row, column] = value;
} }
public override Complex[,] AsArray()
{
return Data;
}
} }
/// <summary> /// <summary>

5
src/UnitTests/LinearAlgebraTests/Complex32/UserDefinedMatrix.cs

@ -75,6 +75,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {
Data[row, column] = value; Data[row, column] = value;
} }
public override Complex32[,] AsArray()
{
return Data;
}
} }
/// <summary> /// <summary>

5
src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrix.cs

@ -73,6 +73,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {
Data[row, column] = value; Data[row, column] = value;
} }
public override double[,] AsArray()
{
return Data;
}
} }
/// <summary> /// <summary>

5
src/UnitTests/LinearAlgebraTests/Single/UserDefinedMatrix.cs

@ -73,6 +73,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
Data[row, column] = value; Data[row, column] = value;
} }
public override float[,] AsArray()
{
return Data;
}
} }
/// <summary> /// <summary>

Loading…
Cancel
Save