@ -40,9 +40,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public class SparseVector : Vector
{
/// <summary>
/// Lock oj bect for the indexer.
/// Lock obj ect for the indexer.
/// </summary>
private readonly object l ockObject = new object ( ) ;
private readonly object _l ockObject = new object ( ) ;
/// <summary>
/// Gets the vector's internal data. The array containing the actual values; only the non-zero values are stored.
@ -96,22 +96,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
if ( value = = 0.0 )
{
// Skip adding values
// Skip adding values
return ;
}
// We already know that this vector is "full", let's allocate all needed memory
this . _ nonZeroValues = new double [ size ] ;
this . _ nonZeroIndices = new int [ size ] ;
this . NonZerosCount = size ;
_ nonZeroValues = new double [ size ] ;
_ nonZeroIndices = new int [ size ] ;
NonZerosCount = size ;
CommonParallel . For (
0 ,
this . Count ,
Count ,
index = >
{
this . _ nonZeroValues [ index ] = value ;
this . _ nonZeroIndices [ index ] = index ;
_ nonZeroValues [ index ] = value ;
_ nonZeroIndices [ index ] = index ;
} ) ;
}
@ -134,13 +134,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this . _ nonZeroValues = new double [ vector . NonZerosCount ] ;
this . _ nonZeroIndices = new int [ vector . NonZerosCount ] ;
this . NonZerosCount = vector . NonZerosCount ;
_ nonZeroValues = new double [ vector . NonZerosCount ] ;
_ nonZeroIndices = new int [ vector . NonZerosCount ] ;
NonZerosCount = vector . NonZerosCount ;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
Buffer . BlockCopy ( vector . _ nonZeroValues , 0 , this . _ nonZeroValues , 0 , vector . NonZerosCount * Constants . SizeOfDouble ) ;
Buffer . BlockCopy ( vector . _ nonZeroIndices , 0 , this . _ nonZeroIndices , 0 , vector . NonZerosCount * Constants . SizeOfInt ) ;
Buffer . BlockCopy ( vector . _ nonZeroValues , 0 , _ nonZeroValues , 0 , vector . NonZerosCount * Constants . SizeOfDouble ) ;
Buffer . BlockCopy ( vector . _ nonZeroIndices , 0 , _ nonZeroIndices , 0 , vector . NonZerosCount * Constants . SizeOfInt ) ;
}
}
@ -154,12 +154,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public SparseVector ( SparseVector other ) : this ( other . Count )
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
this . _ nonZeroValues = new double [ other . NonZerosCount ] ;
this . _ nonZeroIndices = new int [ other . NonZerosCount ] ;
this . NonZerosCount = other . NonZerosCount ;
_ nonZeroValues = new double [ other . NonZerosCount ] ;
_ nonZeroIndices = new int [ other . NonZerosCount ] ;
NonZerosCount = other . NonZerosCount ;
Buffer . BlockCopy ( other . _ nonZeroValues , 0 , this . _ nonZeroValues , 0 , other . NonZerosCount * Constants . SizeOfDouble ) ;
Buffer . BlockCopy ( other . _ nonZeroIndices , 0 , this . _ nonZeroIndices , 0 , other . NonZerosCount * Constants . SizeOfInt ) ;
Buffer . BlockCopy ( other . _ nonZeroValues , 0 , _ nonZeroValues , 0 , other . NonZerosCount * Constants . SizeOfDouble ) ;
Buffer . BlockCopy ( other . _ nonZeroIndices , 0 , _ nonZeroIndices , 0 , other . NonZerosCount * Constants . SizeOfInt ) ;
}
/// <summary>
@ -183,10 +183,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a column matrix.</returns>
public override Matrix ToColumnMatrix ( )
{
var matrix = new SparseMatrix ( this . Count , 1 ) ;
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
var matrix = new SparseMatrix ( Count , 1 ) ;
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
matrix [ this . _ nonZeroIndices [ i ] , 0 ] = this . _ nonZeroValues [ i ] ;
matrix [ _ nonZeroIndices [ i ] , 0 ] = _ nonZeroValues [ i ] ;
}
return matrix ;
@ -198,10 +198,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a row matrix.</returns>
public override Matrix ToRowMatrix ( )
{
var matrix = new SparseMatrix ( 1 , this . Count ) ;
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
var matrix = new SparseMatrix ( 1 , Count ) ;
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
matrix [ 0 , this . _ nonZeroIndices [ i ] ] = this . _ nonZeroValues [ i ] ;
matrix [ 0 , _ nonZeroIndices [ i ] ] = _ nonZeroValues [ i ] ;
}
return matrix ;
@ -217,18 +217,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
get
{
// If index is out of bounds
if ( ( index < 0 ) | | ( index > = this . Count ) )
if ( ( index < 0 ) | | ( index > = Count ) )
{
throw new IndexOutOfRangeException ( ) ;
}
lock ( this . l ockObject)
lock ( _l ockObject )
{
// Search if item idex exists in NonZeroIndices array in range "0 - real nonzero values count"
var itemIndex = Array . BinarySearch ( this . _ nonZeroIndices , 0 , this . NonZerosCount , index ) ;
var itemIndex = Array . BinarySearch ( _ nonZeroIndices , 0 , NonZerosCount , index ) ;
if ( itemIndex > = 0 )
{
return this . _ nonZeroValues [ itemIndex ] ;
return _ nonZeroValues [ itemIndex ] ;
}
}
@ -238,14 +238,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
set
{
// If index is out of bounds
if ( ( index < 0 ) | | ( index > = this . Count ) )
if ( ( index < 0 ) | | ( index > = Count ) )
{
throw new IndexOutOfRangeException ( ) ;
}
lock ( this . l ockObject)
lock ( _l ockObject )
{
this . SetValue ( index , value ) ;
SetValue ( index , value ) ;
}
}
}
@ -284,11 +284,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Clears this instance .
/// Resets all values to zero .
/// </summary>
public void Clear ( )
public override void Clear ( )
{
this . NonZerosCount = 0 ;
NonZerosCount = 0 ;
}
/// <summary>
@ -310,7 +310,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "target" ) ;
}
if ( this . Count ! = target . Count )
if ( Count ! = target . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "target" ) ;
}
@ -325,33 +325,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel . For (
0 ,
this . Count ,
Count ,
index = > target [ index ] = this [ index ] ) ;
}
else
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
otherVector . _ nonZeroValues = new double [ this . NonZerosCount ] ;
otherVector . _ nonZeroIndices = new int [ this . NonZerosCount ] ;
otherVector . NonZerosCount = this . NonZerosCount ;
otherVector . _ nonZeroValues = new double [ NonZerosCount ] ;
otherVector . _ nonZeroIndices = new int [ NonZerosCount ] ;
otherVector . NonZerosCount = NonZerosCount ;
Buffer . BlockCopy ( this . _ nonZeroValues , 0 , otherVector . _ nonZeroValues , 0 , this . NonZerosCount * Constants . SizeOfDouble ) ;
Buffer . BlockCopy ( this . _ nonZeroIndices , 0 , otherVector . _ nonZeroIndices , 0 , this . NonZerosCount * Constants . SizeOfInt ) ;
Buffer . BlockCopy ( _ nonZeroValues , 0 , otherVector . _ nonZeroValues , 0 , NonZerosCount * Constants . SizeOfDouble ) ;
Buffer . BlockCopy ( _ nonZeroIndices , 0 , otherVector . _ nonZeroIndices , 0 , NonZerosCount * Constants . SizeOfInt ) ;
}
}
#region Operators and supplementary functions
// NOTE: There are no operators as:
// public static implicit operator SparseVector(double[] array)
// and
// public static implicit operator double[](SparseVector vector)
// as it is in DenseVector. Because when creating vector from double[] values are copied to internal storage and if user wants
// to get double[] he should call SparseVector.ToArray(), then double[] will be generated and returned to a user.\
// In DenseVector implementation reference to double[] is assigned to interanl storage when casting from double[] and returned
// when casting to double[]
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
@ -363,7 +353,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return ;
}
for ( var i = 0 ; i < this . Count ; i + + )
for ( var i = 0 ; i < Count ; i + + )
{
this [ i ] + = scalar ;
}
@ -383,12 +373,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "result" ) ;
}
if ( this . Count ! = result . Count )
if ( Count ! = result . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "result" ) ;
}
this . CopyTo ( result ) ;
CopyTo ( result ) ;
result . Add ( scalar ) ;
}
@ -405,7 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
@ -418,7 +408,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this . AddScaledSparceVector ( 1.0 , sparseVector ) ;
AddScaledSparceVector ( 1.0 , sparseVector ) ;
}
}
@ -434,7 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
@ -453,21 +443,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double
// To avoid such problem lets change values in internal storage of "this"
if ( alpha = = 1.0 )
{
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
this . _ nonZeroValues [ i ] + = this . _ nonZeroValues [ i ] ;
_ nonZeroValues [ i ] + = _ nonZeroValues [ i ] ;
}
}
else if ( alpha = = - 1.0 )
{
this . Clear ( ) ; // Vector is subtracted from itself
Clear ( ) ; // Vector is subtracted from itself
return ;
}
else
{
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
this . _ nonZeroValues [ i ] + = alpha * this . _ nonZeroValues [ i ] ;
_ nonZeroValues [ i ] + = alpha * _ nonZeroValues [ i ] ;
}
}
}
@ -507,12 +497,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "result" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
if ( this . Count ! = result . Count )
if ( Count ! = result . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "result" ) ;
}
@ -525,7 +515,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this . CopyTo ( result ) ;
CopyTo ( result ) ;
result . Add ( other ) ;
}
}
@ -588,7 +578,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return ;
}
for ( var i = 0 ; i < this . Count ; i + + )
for ( var i = 0 ; i < Count ; i + + )
{
this [ i ] - = scalar ;
}
@ -608,12 +598,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "result" ) ;
}
if ( this . Count ! = result . Count )
if ( Count ! = result . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "result" ) ;
}
this . CopyTo ( result ) ;
CopyTo ( result ) ;
result . Subtract ( scalar ) ;
}
@ -630,7 +620,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
@ -643,7 +633,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this . AddScaledSparceVector ( - 1.0 , sparseVector ) ;
AddScaledSparceVector ( - 1.0 , sparseVector ) ;
}
}
@ -663,12 +653,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "result" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
if ( this . Count ! = result . Count )
if ( Count ! = result . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "result" ) ;
}
@ -681,7 +671,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this . CopyTo ( result ) ;
CopyTo ( result ) ;
result . Subtract ( other ) ;
}
}
@ -739,19 +729,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector Negate ( )
{
var result = new SparseVector ( this . Count )
var result = new SparseVector ( Count )
{
_ nonZeroValues = new double [ this . NonZerosCount ] ,
_ nonZeroIndices = new int [ this . NonZerosCount ] ,
NonZerosCount = this . NonZerosCount
_ nonZeroValues = new double [ NonZerosCount ] ,
_ nonZeroIndices = new int [ NonZerosCount ] ,
NonZerosCount = NonZerosCount
} ;
Buffer . BlockCopy ( this . _ nonZeroIndices , 0 , result . _ nonZeroIndices , 0 , this . NonZerosCount * Constants . SizeOfInt ) ;
Buffer . BlockCopy ( _ nonZeroIndices , 0 , result . _ nonZeroIndices , 0 , NonZerosCount * Constants . SizeOfInt ) ;
CommonParallel . For (
0 ,
this . NonZerosCount ,
index = > result . _ nonZeroValues [ index ] = - this . _ nonZeroValues [ index ] ) ;
NonZerosCount ,
index = > result . _ nonZeroValues [ index ] = - _ nonZeroValues [ index ] ) ;
return result ;
}
@ -769,11 +759,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if ( scalar = = 0 )
{
this . Clear ( ) ; // Set array empty
Clear ( ) ; // Set array empty
return ;
}
Control . LinearAlgebraProvider . ScaleArray ( scalar , this . _ nonZeroValues ) ;
Control . LinearAlgebraProvider . ScaleArray ( scalar , _ nonZeroValues ) ;
}
/// <summary>
@ -790,7 +780,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
@ -798,9 +788,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
double result = 0 ;
// base implementation iterates though all elements, but we need only take non-zeros
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
result + = this . _ nonZeroValues [ i ] * other [ this . _ nonZeroIndices [ i ] ] ;
result + = _ nonZeroValues [ i ] * other [ _ nonZeroIndices [ i ] ] ;
}
return result ;
@ -897,17 +887,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex ( )
{
if ( this . NonZerosCount = = 0 )
if ( NonZerosCount = = 0 )
{
// No non-zero elements. Return 0
return 0 ;
}
var index = 0 ;
var min = Math . Abs ( this . _ nonZeroValues [ index ] ) ;
for ( var i = 1 ; i < this . NonZerosCount ; i + + )
var min = Math . Abs ( _ nonZeroValues [ index ] ) ;
for ( var i = 1 ; i < NonZerosCount ; i + + )
{
var test = Math . Abs ( this . _ nonZeroValues [ i ] ) ;
var test = Math . Abs ( _ nonZeroValues [ i ] ) ;
if ( test < min )
{
index = i ;
@ -915,7 +905,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
return this . _ nonZeroIndices [ index ] ;
return _ nonZeroIndices [ index ] ;
}
/// <summary>
@ -931,7 +921,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public override Vector SubVector ( int index , int length )
{
if ( index < 0 | | index > = this . Count )
if ( index < 0 | | index > = Count )
{
throw new ArgumentOutOfRangeException ( "index" ) ;
}
@ -941,7 +931,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException ( "length" ) ;
}
if ( index + length > this . Count )
if ( index + length > Count )
{
throw new ArgumentOutOfRangeException ( "length" ) ;
}
@ -968,7 +958,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "values" ) ;
}
if ( values . Length ! = this . Count )
if ( values . Length ! = Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "values" ) ;
}
@ -985,23 +975,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex ( )
{
if ( this . NonZerosCount = = 0 )
if ( NonZerosCount = = 0 )
{
return 0 ;
}
var index = 0 ;
var max = this . _ nonZeroValues [ 0 ] ;
for ( var i = 1 ; i < this . NonZerosCount ; i + + )
var max = _ nonZeroValues [ 0 ] ;
for ( var i = 1 ; i < NonZerosCount ; i + + )
{
if ( max < this . _ nonZeroValues [ i ] )
if ( max < _ nonZeroValues [ i ] )
{
index = i ;
max = this . _ nonZeroValues [ i ] ;
max = _ nonZeroValues [ i ] ;
}
}
return this . _ nonZeroIndices [ index ] ;
return _ nonZeroIndices [ index ] ;
}
/// <summary>
@ -1010,23 +1000,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of minimum element.</returns>
public override int MinimumIndex ( )
{
if ( this . NonZerosCount = = 0 )
if ( NonZerosCount = = 0 )
{
return 0 ;
}
var index = 0 ;
var min = this . _ nonZeroValues [ 0 ] ;
for ( var i = 1 ; i < this . NonZerosCount ; i + + )
var min = _ nonZeroValues [ 0 ] ;
for ( var i = 1 ; i < NonZerosCount ; i + + )
{
if ( min > this . _ nonZeroValues [ i ] )
if ( min > _ nonZeroValues [ i ] )
{
index = i ;
min = this . _ nonZeroValues [ i ] ;
min = _ nonZeroValues [ i ] ;
}
}
return this . _ nonZeroIndices [ index ] ;
return _ nonZeroIndices [ index ] ;
}
/// <summary>
@ -1036,9 +1026,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double Sum ( )
{
double result = 0 ;
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
result + = this . _ nonZeroValues [ i ] ;
result + = _ nonZeroValues [ i ] ;
}
return result ;
@ -1051,9 +1041,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double SumMagnitudes ( )
{
double result = 0 ;
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
result + = Math . Abs ( this . _ nonZeroValues [ i ] ) ;
result + = Math . Abs ( _ nonZeroValues [ i ] ) ;
}
return result ;
@ -1072,13 +1062,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
// We cannot iterate using NonZeroCount because the value may be changed (if multiply by 0)
for ( var i = 0 ; i < this . Count ; i + + )
for ( var i = 0 ; i < Count ; i + + )
{
this [ i ] * = other [ i ] ;
}
@ -1105,12 +1095,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
if ( this . Count ! = result . Count )
if ( Count ! = result . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "result" ) ;
}
@ -1118,12 +1108,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if ( ReferenceEquals ( this , result ) | | ReferenceEquals ( other , result ) )
{
var tmp = result . CreateVector ( result . Count ) ;
this . PointwiseMultiply ( other , tmp ) ;
PointwiseMultiply ( other , tmp ) ;
tmp . CopyTo ( result ) ;
}
else
{
this . CopyTo ( result ) ;
CopyTo ( result ) ;
result . PointwiseMultiply ( other ) ;
}
}
@ -1141,15 +1131,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
// base implementation iterates though all elements, but we need only take non-zeros
for ( var i = 0 ; i < this . NonZerosCount ; i + + )
for ( var i = 0 ; i < NonZerosCount ; i + + )
{
this [ this . _ nonZeroIndices [ i ] ] / = other [ this . _ nonZeroIndices [ i ] ] ;
this [ _ nonZeroIndices [ i ] ] / = other [ _ nonZeroIndices [ i ] ] ;
}
}
@ -1174,12 +1164,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException ( "other" ) ;
}
if ( this . Count ! = other . Count )
if ( Count ! = other . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "other" ) ;
}
if ( this . Count ! = result . Count )
if ( Count ! = result . Count )
{
throw new ArgumentException ( Resources . ArgumentVectorsSameLength , "result" ) ;
}
@ -1187,12 +1177,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if ( ReferenceEquals ( this , result ) | | ReferenceEquals ( other , result ) )
{
var tmp = result . CreateVector ( result . Count ) ;
this . PointwiseDivide ( other , tmp ) ;
PointwiseDivide ( other , tmp ) ;
tmp . CopyTo ( result ) ;
}
else
{
this . CopyTo ( result ) ;
CopyTo ( result ) ;
result . PointwiseDivide ( other ) ;
}
}
@ -1249,7 +1239,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException ( Resources . ArgumentMustBePositive , "length" ) ;
}
var v = ( SparseVector ) this . CreateVector ( length ) ;
var v = ( SparseVector ) CreateVector ( length ) ;
for ( var index = 0 ; index < v . Count ; index + + )
{
v [ index ] = randomDistribution . Sample ( ) ;
@ -1275,7 +1265,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException ( Resources . ArgumentMustBePositive , "length" ) ;
}
var v = ( SparseVector ) this . CreateVector ( length ) ;
var v = ( SparseVector ) CreateVector ( length ) ;
for ( var index = 0 ; index < v . Count ; index + + )
{
v [ index ] = randomDistribution . Sample ( ) ;
@ -1315,8 +1305,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var sum = CommonParallel . Aggregate (
0 ,
this . NonZerosCount ,
index = > Math . Pow ( Math . Abs ( this . _ nonZeroValues [ index ] ) , p ) ) ;
NonZerosCount ,
index = > Math . Pow ( Math . Abs ( _ nonZeroValues [ index ] ) , p ) ) ;
return Math . Pow ( sum , 1.0 / p ) ;
}
@ -1327,7 +1317,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>Scalar <c>ret = max(abs(this[i]))</c></returns>
public override double NormInfinity ( )
{
return CommonParallel . Select ( 0 , this . NonZerosCount , ( index , localData ) = > localData = Math . Max ( localData , Math . Abs ( this . _ nonZeroValues [ index ] ) ) , Math . Max ) ;
return CommonParallel . Select ( 0 , NonZerosCount , ( index , localData ) = > localData = Math . Max ( localData , Math . Abs ( _ nonZeroValues [ index ] ) ) , Math . Max ) ;
}
#endregion
@ -1500,7 +1490,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
private void SetValue ( int index , double value )
{
// Search if "index" already exists in range "0 - real nonzero values count"
var itemIndex = Array . BinarySearch ( this . _ nonZeroIndices , 0 , this . NonZerosCount , index ) ;
var itemIndex = Array . BinarySearch ( _ nonZeroIndices , 0 , NonZerosCount , index ) ;
if ( itemIndex > = 0 )
{
@ -1508,56 +1498,61 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if ( value = = 0.0 )
{
// Value is zero. Let's delete it from Values and Indices array
for ( var i = itemIndex + 1 ; i < this . NonZerosCount ; i + + )
for ( var i = itemIndex + 1 ; i < NonZerosCount ; i + + )
{
this . _ nonZeroValues [ i - 1 ] = this . _ nonZeroValues [ i ] ;
this . _ nonZeroIndices [ i - 1 ] = this . _ nonZeroIndices [ i ] ;
_ nonZeroValues [ i - 1 ] = _ nonZeroValues [ i ] ;
_ nonZeroIndices [ i - 1 ] = _ nonZeroIndices [ i ] ;
}
this . NonZerosCount - = 1 ;
NonZerosCount - = 1 ;
// Check if the storage needs to be shrink. This is reasonable to do if
// there are a lot of non-zero elements and storage is two times bigger
if ( ( this . NonZerosCount > 1 0 2 4 ) & & ( this . NonZerosCount < this . _ nonZeroIndices . Length / 2 ) )
if ( ( NonZerosCount > 1 0 2 4 ) & & ( NonZerosCount < _ nonZeroIndices . Length / 2 ) )
{
Array . Resize ( ref this . _ nonZeroValues , this . NonZerosCount ) ;
Array . Resize ( ref this . _ nonZeroIndices , this . NonZerosCount ) ;
Array . Resize ( ref _ nonZeroValues , NonZerosCount ) ;
Array . Resize ( ref _ nonZeroIndices , NonZerosCount ) ;
}
}
else
{
this . _ nonZeroValues [ itemIndex ] = value ;
_ nonZeroValues [ itemIndex ] = value ;
}
}
else
{
if ( value = = 0.0 )
{
return ;
}
itemIndex = ~ itemIndex ; // Index where to put new value
// Check if the storage needs to be increased
if ( ( this . NonZerosCount = = this . _ nonZeroValues . Length ) & & ( this . NonZerosCount < this . Count ) )
if ( ( NonZerosCount = = _ nonZeroValues . Length ) & & ( NonZerosCount < Count ) )
{
// Value and Indices arrays are completely full so we increase the size
var size = Math . Min ( this . _ nonZeroValues . Length + this . GrowthSize ( ) , this . Count ) ;
Array . Resize ( ref this . _ nonZeroValues , size ) ;
Array . Resize ( ref this . _ nonZeroIndices , size ) ;
var size = Math . Min ( _ nonZeroValues . Length + GrowthSize ( ) , Count ) ;
Array . Resize ( ref _ nonZeroValues , size ) ;
Array . Resize ( ref _ nonZeroIndices , size ) ;
}
// Move all values (with an position larger than index) in the value array
// to the next position
// move all values (with an position larger than index) in the columIndices
// array to the next position
for ( var i = this . NonZerosCount - 1 ; i > itemIndex - 1 ; i - - )
for ( var i = NonZerosCount - 1 ; i > itemIndex - 1 ; i - - )
{
this . _ nonZeroValues [ i + 1 ] = this . _ nonZeroValues [ i ] ;
this . _ nonZeroIndices [ i + 1 ] = this . _ nonZeroIndices [ i ] ;
_ nonZeroValues [ i + 1 ] = _ nonZeroValues [ i ] ;
_ nonZeroIndices [ i + 1 ] = _ nonZeroIndices [ i ] ;
}
// Add the value and the column index
this . _ nonZeroValues [ itemIndex ] = value ;
this . _ nonZeroIndices [ itemIndex ] = index ;
_ nonZeroValues [ itemIndex ] = value ;
_ nonZeroIndices [ itemIndex ] = index ;
// increase the number of non-zero numbers by one
this . NonZerosCount + = 1 ;
NonZerosCount + = 1 ;
}
}
@ -1569,19 +1564,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
private int GrowthSize ( )
{
int delta ;
if ( this . _ nonZeroValues . Length > 1 0 2 4 )
if ( _ nonZeroValues . Length > 1 0 2 4 )
{
delta = this . _ nonZeroValues . Length / 4 ;
delta = _ nonZeroValues . Length / 4 ;
}
else
{
if ( this . _ nonZeroValues . Length > 2 5 6 )
if ( _ nonZeroValues . Length > 2 5 6 )
{
delta = 5 1 2 ;
}
else
{
delta = this . _ nonZeroValues . Length > 6 4 ? 1 2 8 : 3 2 ;
delta = _ nonZeroValues . Length > 6 4 ? 1 2 8 : 3 2 ;
}
}
@ -1612,15 +1607,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return true ;
}
if ( ( this . Count ! = sparseVector . Count ) | | ( this . NonZerosCount ! = sparseVector . NonZerosCount ) )
if ( ( Count ! = sparseVector . Count ) | | ( NonZerosCount ! = sparseVector . NonZerosCount ) )
{
return false ;
}
// If all else fails, perform element wise comparison.
for ( var index = 0 ; index < this . NonZerosCount ; index + + )
for ( var index = 0 ; index < NonZerosCount ; index + + )
{
if ( ! this . _ nonZeroValues [ index ] . AlmostEqual ( sparseVector . _ nonZeroValues [ index ] ) | | ( this . _ nonZeroIndices [ index ] ! = sparseVector . _ nonZeroIndices [ index ] ) )
if ( ! _ nonZeroValues [ index ] . AlmostEqual ( sparseVector . _ nonZeroValues [ index ] ) | | ( _ nonZeroIndices [ index ] ! = sparseVector . _ nonZeroIndices [ index ] ) )
{
return false ;
}
@ -1637,14 +1632,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns>
public override int GetHashCode ( )
{
var hashNum = Math . Min ( this . NonZerosCount , 2 0 ) ;
var hashNum = Math . Min ( NonZerosCount , 2 0 ) ;
long hash = 0 ;
for ( var i = 0 ; i < hashNum ; i + + )
{
#if SILVERLIGHT
hash ^ = Precision . DoubleToInt64Bits ( this . _ nonZeroValues [ i ] ) ;
#else
hash ^ = BitConverter . DoubleToInt64Bits ( this . _ nonZeroValues [ i ] ) ;
hash ^ = BitConverter . DoubleToInt64Bits ( _ nonZeroValues [ i ] ) ;
#endif
}
@ -1653,4 +1648,4 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion
}
}
}