Browse Source

Added matrix indexed enumerator

la-knuth
Marcus Cuda 16 years ago
parent
commit
9c18123e6b
  1. 78
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  2. 34
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  3. 34
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  4. 15
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  5. 34
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

78
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -31,16 +31,17 @@
namespace MathNet.Numerics.LinearAlgebra.Complex namespace MathNet.Numerics.LinearAlgebra.Complex
{ {
using System; using System;
using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using Generic; using Generic;
using Properties; using Properties;
using Threading; using Threading;
/// <summary> /// <summary>
/// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format. /// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format.
/// <a href="http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_.28CSR_or_CRS.29">Wikipedia - CSR</a>. /// <a href="http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_.28CSR_or_CRS.29">Wikipedia - CSR</a>.
/// </summary> /// </summary>
public class SparseMatrix : Matrix public class SparseMatrix : Matrix
{ {
/// <summary> /// <summary>
/// Object for use in "lock" /// Object for use in "lock"
@ -58,7 +59,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// array using the row-major storage mapping described in a compressed sparse row (CSR) format. /// array using the row-major storage mapping described in a compressed sparse row (CSR) format.
/// </summary> /// </summary>
private Complex[] _nonZeroValues = new Complex[0]; private Complex[] _nonZeroValues = new Complex[0];
/// <summary> /// <summary>
/// Gets the number of non zero elements in the matrix. /// Gets the number of non zero elements in the matrix.
/// </summary> /// </summary>
@ -68,7 +69,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
get; get;
private set; private set;
} }
/// <summary> /// <summary>
/// An array containing the column indices of the non-zero values. Element "I" of the array /// An array containing the column indices of the non-zero values. Element "I" of the array
/// is the number of the column in matrix that contains the I-th value in the <see cref="_nonZeroValues"/> array. /// is the number of the column in matrix that contains the I-th value in the <see cref="_nonZeroValues"/> array.
@ -84,11 +85,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="columns"> /// <param name="columns">
/// The number of columns. /// The number of columns.
/// </param> /// </param>
public SparseMatrix(int rows, int columns) : base(rows, columns) public SparseMatrix(int rows, int columns)
: base(rows, columns)
{ {
_rowIndex = new int[rows]; _rowIndex = new int[rows];
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class. This matrix is square with a given size. /// Initializes a new instance of the <see cref="SparseMatrix"/> class. This matrix is square with a given size.
/// </summary> /// </summary>
@ -96,7 +98,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// If <paramref name="order"/> is less than one. /// If <paramref name="order"/> is less than one.
/// </exception> /// </exception>
public SparseMatrix(int order) : this(order, order) public SparseMatrix(int order)
: this(order, order)
{ {
} }
@ -110,7 +113,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// The number of columns. /// The number of columns.
/// </param> /// </param>
/// <param name="value">The value which we assign to each element of the matrix.</param> /// <param name="value">The value which we assign to each element of the matrix.</param>
public SparseMatrix(int rows, int columns, Complex value) : this(rows, columns) public SparseMatrix(int rows, int columns, Complex value)
: this(rows, columns)
{ {
if (value == 0.0) if (value == 0.0)
{ {
@ -132,7 +136,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
_nonZeroValues[i] = value; _nonZeroValues[i] = value;
_columnIndices[i] = j; _columnIndices[i] = j;
} }
// Set proper row pointers // Set proper row pointers
for (var i = 0; i < _rowIndex.Length; i++) for (var i = 0; i < _rowIndex.Length; i++)
{ {
@ -148,7 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="array">The one dimensional array to create this matrix from. This array should store the matrix in column-major order. see: http://en.wikipedia.org/wiki/Column-major_order </param> /// <param name="array">The one dimensional array to create this matrix from. This array should store the matrix in column-major order. see: http://en.wikipedia.org/wiki/Column-major_order </param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="array"/> length is less than <paramref name="rows"/> * <paramref name="columns"/>. /// <exception cref="ArgumentOutOfRangeException">If <paramref name="array"/> length is less than <paramref name="rows"/> * <paramref name="columns"/>.
/// </exception> /// </exception>
public SparseMatrix(int rows, int columns, Complex[] array) : this(rows, columns) public SparseMatrix(int rows, int columns, Complex[] array)
: this(rows, columns)
{ {
if (rows * columns > array.Length) if (rows * columns > array.Length)
{ {
@ -168,7 +173,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Initializes a new instance of the <see cref="SparseMatrix"/> class from a 2D array. /// Initializes a new instance of the <see cref="SparseMatrix"/> class from a 2D array.
/// </summary> /// </summary>
/// <param name="array">The 2D array to create this matrix from.</param> /// <param name="array">The 2D array to create this matrix from.</param>
public SparseMatrix(Complex[,] array) : this(array.GetLength(0), array.GetLength(1)) public SparseMatrix(Complex[,] array)
: this(array.GetLength(0), array.GetLength(1))
{ {
var rows = array.GetLength(0); var rows = array.GetLength(0);
var columns = array.GetLength(1); var columns = array.GetLength(1);
@ -577,7 +583,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return index >= 0 ? _nonZeroValues[index] : 0.0; return index >= 0 ? _nonZeroValues[index] : 0.0;
} }
} }
/// <summary> /// <summary>
/// Sets the value of the given element. /// Sets the value of the given element.
/// </summary> /// </summary>
@ -682,7 +688,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
_nonZeroValues[i - 1] = _nonZeroValues[i]; _nonZeroValues[i - 1] = _nonZeroValues[i];
_columnIndices[i - 1] = _columnIndices[i]; _columnIndices[i - 1] = _columnIndices[i];
} }
// Decrease value in Row // Decrease value in Row
for (var i = row + 1; i < _rowIndex.Length; i++) for (var i = row + 1; i < _rowIndex.Length; i++)
{ {
@ -699,7 +705,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Array.Resize(ref _columnIndices, NonZerosCount); Array.Resize(ref _columnIndices, NonZerosCount);
} }
} }
/// <summary> /// <summary>
/// Find item Index in nonZeroValues array /// Find item Index in nonZeroValues array
/// </summary> /// </summary>
@ -714,7 +720,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount;
return Array.BinarySearch(_columnIndices, startIndex, endIndex - startIndex, column); return Array.BinarySearch(_columnIndices, startIndex, endIndex - startIndex, column);
} }
/// <summary> /// <summary>
/// Calculates the amount with which to grow the storage array's if they need to be /// Calculates the amount with which to grow the storage array's if they need to be
/// increased in size. /// increased in size.
@ -794,7 +800,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Array.Copy(_rowIndex, sparseTarget._rowIndex, RowCount); Array.Copy(_rowIndex, sparseTarget._rowIndex, RowCount);
} }
} }
/// <summary> /// <summary>
/// Returns a hash code for this instance. /// Returns a hash code for this instance.
/// </summary> /// </summary>
@ -1012,7 +1018,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
resultSparseMatrix.NonZerosCount = NonZerosCount + lowerSparseMatrix.NonZerosCount; resultSparseMatrix.NonZerosCount = NonZerosCount + lowerSparseMatrix.NonZerosCount;
resultSparseMatrix._nonZeroValues = new Complex[resultSparseMatrix.NonZerosCount]; resultSparseMatrix._nonZeroValues = new Complex[resultSparseMatrix.NonZerosCount];
resultSparseMatrix._columnIndices = new int[resultSparseMatrix.NonZerosCount]; resultSparseMatrix._columnIndices = new int[resultSparseMatrix.NonZerosCount];
Array.Copy(_nonZeroValues, 0, resultSparseMatrix._nonZeroValues, 0, NonZerosCount); Array.Copy(_nonZeroValues, 0, resultSparseMatrix._nonZeroValues, 0, NonZerosCount);
Array.Copy(lowerSparseMatrix._nonZeroValues, 0, resultSparseMatrix._nonZeroValues, NonZerosCount, lowerSparseMatrix.NonZerosCount); Array.Copy(lowerSparseMatrix._nonZeroValues, 0, resultSparseMatrix._nonZeroValues, NonZerosCount, lowerSparseMatrix.NonZerosCount);
@ -1371,8 +1377,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the negation.</param> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result) protected override void DoNegate(Matrix<Complex> result)
{ {
CopyTo(result); CopyTo(result);
DoMultiply(-1, result); DoMultiply(-1, result);
} }
/// <summary> /// <summary>
@ -1470,5 +1476,39 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
} }
} }
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public override IEnumerable<Tuple<int, int, Complex>> IndexedEnumerator()
{
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
yield return new Tuple<int, int, Complex>(row, _columnIndices[index], _nonZeroValues[index]);
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
for (var index = _rowIndex[lastRow]; index < NonZerosCount; index++)
{
yield return new Tuple<int, int, Complex>(lastRow, _columnIndices[index], _nonZeroValues[index]);
}
}
}
} }
} }

34
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -31,6 +31,7 @@
namespace MathNet.Numerics.LinearAlgebra.Complex32 namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
using System; using System;
using System.Collections.Generic;
using Generic; using Generic;
using Numerics; using Numerics;
using Properties; using Properties;
@ -1470,5 +1471,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
} }
} }
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public override IEnumerable<Tuple<int, int, Complex32>> IndexedEnumerator()
{
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
yield return new Tuple<int, int, Complex32>(row, _columnIndices[index], _nonZeroValues[index]);
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
for (var index = _rowIndex[lastRow]; index < NonZerosCount; index++)
{
yield return new Tuple<int, int, Complex32>(lastRow, _columnIndices[index], _nonZeroValues[index]);
}
}
}
} }
} }

34
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -31,6 +31,7 @@
namespace MathNet.Numerics.LinearAlgebra.Double namespace MathNet.Numerics.LinearAlgebra.Double
{ {
using System; using System;
using System.Collections.Generic;
using Generic; using Generic;
using Properties; using Properties;
using Threading; using Threading;
@ -1469,5 +1470,38 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
} }
} }
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public override IEnumerable<Tuple<int, int, double>> IndexedEnumerator()
{
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
yield return new Tuple<int, int, double>(row, _columnIndices[index], _nonZeroValues[index]);
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
for (var index = _rowIndex[lastRow]; index < NonZerosCount; index++)
{
yield return new Tuple<int, int, double>(lastRow, _columnIndices[index], _nonZeroValues[index]);
}
}
}
} }
} }

15
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -1788,5 +1788,20 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <summary>Calculates the infinity norm of this matrix.</summary> /// <summary>Calculates the infinity norm of this matrix.</summary>
/// <returns>The infinity norm of this matrix.</returns> /// <returns>The infinity norm of this matrix.</returns>
public abstract T InfinityNorm(); public abstract T InfinityNorm();
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public virtual IEnumerable<Tuple<int, int, T>> IndexedEnumerator()
{
for (var row = 0; row < RowCount; row++)
{
for (var column = 0; column < ColumnCount; column++)
{
yield return new Tuple<int, int, T>(row, column, At(row, column));
}
}
}
} }
} }

34
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -31,6 +31,7 @@
namespace MathNet.Numerics.LinearAlgebra.Single namespace MathNet.Numerics.LinearAlgebra.Single
{ {
using System; using System;
using System.Collections.Generic;
using Generic; using Generic;
using Properties; using Properties;
using Threading; using Threading;
@ -1469,5 +1470,38 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
} }
} }
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public override IEnumerable<Tuple<int, int, float>> IndexedEnumerator()
{
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
yield return new Tuple<int, int, float>(row, _columnIndices[index], _nonZeroValues[index]);
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
for (var index = _rowIndex[lastRow]; index < NonZerosCount; index++)
{
yield return new Tuple<int, int, float>(lastRow, _columnIndices[index], _nonZeroValues[index]);
}
}
}
} }
} }

Loading…
Cancel
Save