@ -31,16 +31,17 @@
namespace MathNet.Numerics.LinearAlgebra.Complex
{
using System ;
using System.Collections.Generic ;
using System.Numerics ;
using Generic ;
using Properties ;
using Threading ;
/// <summary>
/// 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>.
/// </summary>
public class SparseMatrix : Matrix
public class SparseMatrix : Matrix
{
/// <summary>
/// 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.
/// </summary>
private Complex [ ] _ nonZeroValues = new Complex [ 0 ] ;
/// <summary>
/// Gets the number of non zero elements in the matrix.
/// </summary>
@ -68,7 +69,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
get ;
private set ;
}
/// <summary>
/// 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.
@ -84,11 +85,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="columns">
/// The number of columns.
/// </param>
public SparseMatrix ( int rows , int columns ) : base ( rows , columns )
public SparseMatrix ( int rows , int columns )
: base ( rows , columns )
{
_ rowIndex = new int [ rows ] ;
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class. This matrix is square with a given size.
/// </summary>
@ -96,7 +98,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <exception cref="ArgumentException">
/// If <paramref name="order"/> is less than one.
/// </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.
/// </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 )
{
@ -132,7 +136,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
_ nonZeroValues [ i ] = value ;
_ columnIndices [ i ] = j ;
}
// Set proper row pointers
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>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="array"/> length is less than <paramref name="rows"/> * <paramref name="columns"/>.
/// </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 )
{
@ -168,7 +173,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Initializes a new instance of the <see cref="SparseMatrix"/> class from a 2D array.
/// </summary>
/// <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 columns = array . GetLength ( 1 ) ;
@ -577,7 +583,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return index > = 0 ? _ nonZeroValues [ index ] : 0.0 ;
}
}
/// <summary>
/// Sets the value of the given element.
/// </summary>
@ -682,7 +688,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
_ nonZeroValues [ i - 1 ] = _ nonZeroValues [ i ] ;
_ columnIndices [ i - 1 ] = _ columnIndices [ i ] ;
}
// Decrease value in Row
for ( var i = row + 1 ; i < _ rowIndex . Length ; i + + )
{
@ -699,7 +705,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Array . Resize ( ref _ columnIndices , NonZerosCount ) ;
}
}
/// <summary>
/// Find item Index in nonZeroValues array
/// </summary>
@ -714,7 +720,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
var endIndex = row < _ rowIndex . Length - 1 ? _ rowIndex [ row + 1 ] : NonZerosCount ;
return Array . BinarySearch ( _ columnIndices , startIndex , endIndex - startIndex , column ) ;
}
/// <summary>
/// Calculates the amount with which to grow the storage array's if they need to be
/// increased in size.
@ -794,7 +800,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Array . Copy ( _ rowIndex , sparseTarget . _ rowIndex , RowCount ) ;
}
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
@ -1012,7 +1018,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
resultSparseMatrix . NonZerosCount = NonZerosCount + lowerSparseMatrix . NonZerosCount ;
resultSparseMatrix . _ nonZeroValues = new Complex [ resultSparseMatrix . NonZerosCount ] ;
resultSparseMatrix . _ columnIndices = new int [ resultSparseMatrix . NonZerosCount ] ;
Array . Copy ( _ nonZeroValues , 0 , resultSparseMatrix . _ nonZeroValues , 0 , 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>
protected override void DoNegate ( Matrix < Complex > result )
{
CopyTo ( result ) ;
DoMultiply ( - 1 , result ) ;
CopyTo ( result ) ;
DoMultiply ( - 1 , result ) ;
}
/// <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 ] ) ;
}
}
}
}
}