// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // // Copyright (c) 2009-2011 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // namespace MathNet.Numerics.LinearAlgebra.Complex { using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using Generic; using NumberTheory; using Properties; using Threading; /// /// A vector with sparse storage. /// /// The sparse vector is not thread safe. [Serializable] public class SparseVector : Vector { /// /// Gets the vector's internal data. The array containing the actual values; only the non-zero values are stored. /// private Complex[] _nonZeroValues = new Complex[0]; /// /// The indices of the non-zero entries. /// private int[] _nonZeroIndices = new int[0]; /// /// Gets the number of non zero elements in the vector. /// /// The number of non zero elements. public int NonZerosCount { get; private set; } #region Constructors /// /// Initializes a new instance of the class with a given size. /// /// /// the size of the vector. /// /// /// If is less than one. /// public SparseVector(int size) : base(size) { } /// /// Initializes a new instance of the class with a given size /// and each element set to the given value; /// /// /// the size of the vector. /// /// /// the value to set each element to. /// /// /// If is less than one. /// public SparseVector(int size, Complex value) : this(size) { if (value == Complex.Zero) { // Skip adding values return; } // We already know that this vector is "full", let's allocate all needed memory _nonZeroValues = new Complex[size]; _nonZeroIndices = new int[size]; NonZerosCount = size; CommonParallel.For( 0, Count, index => { _nonZeroValues[index] = value; _nonZeroIndices[index] = index; }); } /// /// Initializes a new instance of the class by /// copying the values from another. /// /// /// The vector to create the new vector from. /// public SparseVector(Vector other) : this(other.Count) { var vector = other as SparseVector; if (vector == null) { for (var i = 0; i < other.Count; i++) { this[i] = other.At(i); } } else { _nonZeroValues = new Complex[vector.NonZerosCount]; _nonZeroIndices = new int[vector.NonZerosCount]; NonZerosCount = vector.NonZerosCount; // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value if (vector.NonZerosCount != 0) { CommonParallel.For(0, vector.NonZerosCount, index => _nonZeroValues[index] = vector._nonZeroValues[index]); Buffer.BlockCopy(vector._nonZeroIndices, 0, _nonZeroIndices, 0, vector.NonZerosCount * Constants.SizeOfInt); } } } /// /// Initializes a new instance of the class by /// copying the values from another. /// /// /// The vector to create the new vector from. /// public SparseVector(SparseVector other) : this(other.Count) { // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value _nonZeroValues = new Complex[other.NonZerosCount]; _nonZeroIndices = new int[other.NonZerosCount]; NonZerosCount = other.NonZerosCount; if (other.NonZerosCount != 0) { CommonParallel.For(0, other.NonZerosCount, index => _nonZeroValues[index] = other._nonZeroValues[index]); Buffer.BlockCopy(other._nonZeroIndices, 0, _nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt); } } /// /// Initializes a new instance of the class for an array. /// /// The array to create this vector from. /// The vector copy the array. Any changes to the vector will NOT change the array. public SparseVector(IList array) : this(array.Count) { for (var i = 0; i < array.Count; i++) { this[i] = array[i]; } } #endregion /// /// Create a matrix based on this vector in column form (one single column). /// /// This vector as a column matrix. public override Matrix ToColumnMatrix() { var matrix = new SparseMatrix(Count, 1); for (var i = 0; i < NonZerosCount; i++) { matrix.At(_nonZeroIndices[i], 0, _nonZeroValues[i]); } return matrix; } /// /// Create a matrix based on this vector in row form (one single row). /// /// This vector as a row matrix. public override Matrix ToRowMatrix() { var matrix = new SparseMatrix(1, Count); for (var i = 0; i < NonZerosCount; i++) { matrix.At(0, _nonZeroIndices[i], _nonZeroValues[i]); } return matrix; } /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. /// /// /// The number of rows. /// /// /// The number of columns. /// /// /// A matrix with the given dimensions. /// public override Matrix CreateMatrix(int rows, int columns) { return new SparseMatrix(rows, columns); } /// /// Creates a Vector of the given size using the same storage type /// as this vector. /// /// /// The size of the Vector to create. /// /// /// The new Vector. /// public override Vector CreateVector(int size) { return new SparseVector(size); } /// /// Resets all values to zero. /// public override void Clear() { NonZerosCount = 0; } /// /// Copies the values of this vector into the target vector. /// /// /// The vector to copy elements into. /// /// /// If is . /// /// /// If is not the same size as this vector. /// public override void CopyTo(Vector target) { if (target == null) { throw new ArgumentNullException("target"); } if (Count != target.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } if (ReferenceEquals(this, target)) { return; } var otherVector = target as SparseVector; if (otherVector == null) { target.Clear(); for (var index = 0; index < NonZerosCount; index++) { target.At(_nonZeroIndices[index], _nonZeroValues[index]); } } else { // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value otherVector._nonZeroValues = new Complex[NonZerosCount]; otherVector._nonZeroIndices = new int[NonZerosCount]; otherVector.NonZerosCount = NonZerosCount; if (NonZerosCount != 0) { CommonParallel.For(0, NonZerosCount, index => otherVector._nonZeroValues[index] = _nonZeroValues[index]); Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); } } } /// /// Conjugates vector and save result to /// /// Target vector public override void Conjugate(Vector target) { if (target == null) { throw new ArgumentNullException("target"); } if (Count != target.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } if (ReferenceEquals(this, target)) { var tmp = CreateVector(Count); Conjugate(tmp); tmp.CopyTo(target); } var otherVector = target as SparseVector; if (otherVector == null) { base.Conjugate(target); } else { // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value otherVector._nonZeroValues = new Complex[NonZerosCount]; otherVector._nonZeroIndices = new int[NonZerosCount]; otherVector.NonZerosCount = NonZerosCount; if (NonZerosCount != 0) { CommonParallel.For(0, NonZerosCount, index => otherVector._nonZeroValues[index] = _nonZeroValues[index].Conjugate()); Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); } } } #region Operators and supplementary functions /// /// Adds a scalar to each element of the vector and stores the result in the result vector. /// /// /// The scalar to add. /// /// /// The vector to store the result of the addition. /// protected override void DoAdd(Complex scalar, Vector result) { if (scalar == Complex.Zero) { if (!ReferenceEquals(this, result)) { CopyTo(result); } return; } if (ReferenceEquals(this, result)) { CommonParallel.For( 0, NonZerosCount, index => _nonZeroValues[index] += scalar); } else { for (var index = 0; index < Count; index++) { result.At(index, At(index) + scalar); } } } /// /// Adds another vector to this vector and stores the result into the result vector. /// /// /// The vector to add to this one. /// /// /// The vector to store the result of the addition. /// protected override void DoAdd(Vector other, Vector result) { if (ReferenceEquals(this, result)) { CommonParallel.For( 0, NonZerosCount, index => _nonZeroValues[index] += _nonZeroValues[index]); } else { for (var index = 0; index < Count; index++) { result.At(index, At(index) + other.At(index)); } } } /// /// Returns a Vector containing the same values of . /// /// This method is included for completeness. /// The vector to get the values from. /// A vector containing a the same values as . /// If is . public static SparseVector operator +(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return (SparseVector)rightSide.Plus(); } /// /// Adds two Vectors together and returns the results. /// /// One of the vectors to add. /// The other vector to add. /// The result of the addition. /// If and are not the same size. /// If or is . public static SparseVector operator +(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (leftSide.Count != rightSide.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } return (SparseVector)leftSide.Add(rightSide); } /// /// Subtracts a scalar from each element of the vector and stores the result in the result vector. /// /// /// The scalar to subtract. /// /// /// The vector to store the result of the subtraction. /// protected override void DoSubtract(Complex scalar, Vector result) { DoAdd(-scalar, result); } /// /// Subtracts another vector to this vector and stores the result into the result vector. /// /// /// The vector to subtract from this one. /// /// /// The vector to store the result of the subtraction. /// protected override void DoSubtract(Vector other, Vector result) { if (ReferenceEquals(this, other)) { result.Clear(); return; } for (var index = 0; index < Count; index++) { result.At(index, At(index) - other.At(index)); } } /// /// Returns a Vector containing the negated values of . /// /// The vector to get the values from. /// A vector containing the negated values as . /// If is . public static SparseVector operator -(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return (SparseVector)rightSide.Negate(); } /// /// Subtracts two Vectors and returns the results. /// /// The vector to subtract from. /// The vector to subtract. /// The result of the subtraction. /// If and are not the same size. /// If or is . public static SparseVector operator -(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (leftSide.Count != rightSide.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } return (SparseVector)leftSide.Subtract(rightSide); } /// /// Returns a negated vector. /// /// The negated vector. /// Added as an alternative to the unary negation operator. public override Vector Negate() { var result = new SparseVector(Count) { _nonZeroValues = new Complex[NonZerosCount], _nonZeroIndices = new int[NonZerosCount], NonZerosCount = NonZerosCount }; if (NonZerosCount != 0) { CommonParallel.For( 0, NonZerosCount, index => result._nonZeroValues[index] = -_nonZeroValues[index]); Buffer.BlockCopy(_nonZeroIndices, 0, result._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); } return result; } /// /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// /// /// The scalar to multiply. /// /// /// The vector to store the result of the multiplication. /// protected override void DoMultiply(Complex scalar, Vector result) { if (scalar == Complex.One) { if (!ReferenceEquals(this, result)) { CopyTo(result); } return; } if (scalar == Complex.Zero) { result.Clear(); return; } var sparseResult = result as SparseVector; if (sparseResult == null) { result.Clear(); for (var index = 0; index < NonZerosCount; index++) { result.At(_nonZeroIndices[index], scalar * _nonZeroValues[index]); } } else { if (!ReferenceEquals(this, result)) { sparseResult.NonZerosCount = NonZerosCount; sparseResult._nonZeroIndices = new int[NonZerosCount]; Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, _nonZeroIndices.Length * Constants.SizeOfInt); sparseResult._nonZeroValues = new Complex[_nonZeroValues.Length]; } Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues, sparseResult._nonZeroValues); } } /// /// Computes the dot product between this vector and another vector. /// /// /// The other vector to add. /// /// s /// The result of the addition. /// protected override Complex DoDotProduct(Vector other) { var result = Complex.Zero; if (ReferenceEquals(this, other)) { for (var i = 0; i < NonZerosCount; i++) { result += _nonZeroValues[i] * _nonZeroValues[i]; } } else { for (var i = 0; i < NonZerosCount; i++) { result += _nonZeroValues[i] * other.At(_nonZeroIndices[i]); } } return result; } /// /// Multiplies a vector with a complex. /// /// The vector to scale. /// The complex value. /// The result of the multiplication. /// If is . public static SparseVector operator *(SparseVector leftSide, Complex rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return (SparseVector)leftSide.Multiply(rightSide); } /// /// Multiplies a vector with a complex. /// /// The complex value. /// The vector to scale. /// The result of the multiplication. /// If is . public static SparseVector operator *(Complex leftSide, SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return (SparseVector)rightSide.Multiply(leftSide); } /// /// Computes the dot product between two Vectors. /// /// The left row vector. /// The right column vector. /// The dot product between the two vectors. /// If and are not the same size. /// If or is . public static Complex operator *(SparseVector leftSide, SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } if (leftSide == null) { throw new ArgumentNullException("leftSide"); } if (leftSide.Count != rightSide.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide"); } return leftSide.DotProduct(rightSide); } /// /// Divides a vector with a complex. /// /// The vector to divide. /// The complex value. /// The result of the division. /// If is . public static SparseVector operator /(SparseVector leftSide, Complex rightSide) { if (leftSide == null) { throw new ArgumentNullException("leftSide"); } return (SparseVector)leftSide.Multiply(Complex.One / rightSide); } /// /// Returns the index of the absolute minimum element. /// /// The index of absolute minimum element. public override int AbsoluteMinimumIndex() { if (NonZerosCount == 0) { // No non-zero elements. Return 0 return 0; } var index = 0; var min = _nonZeroValues[index].Magnitude; for (var i = 1; i < NonZerosCount; i++) { var test = _nonZeroValues[i].Magnitude; if (test < min) { index = i; min = test; } } return _nonZeroIndices[index]; } /// /// Creates a vector containing specified elements. /// /// The first element to begin copying from. /// The number of elements to copy. /// A vector containing a copy of the specified elements. /// If is not positive or /// greater than or equal to the size of the vector. /// If + is greater than or equal to the size of the vector. /// /// If is not positive. public override Vector SubVector(int index, int length) { if (index < 0 || index >= Count) { throw new ArgumentOutOfRangeException("index"); } if (length <= 0) { throw new ArgumentOutOfRangeException("length"); } if (index + length > Count) { throw new ArgumentOutOfRangeException("length"); } var result = new SparseVector(length); for (var i = index; i < index + length; i++) { result.At(i - index, At(i)); } return result; } /// /// Set the values of this vector to the given values. /// /// The array containing the values to use. /// If is . /// If is not the same size as this vector. public override void SetValues(Complex[] values) { if (values == null) { throw new ArgumentNullException("values"); } if (values.Length != Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); } for (var i = 0; i < values.Length; i++) { At(i, values[i]); } } /// /// Computes the sum of the vector's elements. /// /// The sum of the vector's elements. public override Complex Sum() { var result = Complex.Zero; for (var i = 0; i < NonZerosCount; i++) { result += _nonZeroValues[i]; } return result; } /// /// Computes the sum of the absolute value of the vector's elements. /// /// The sum of the absolute value of the vector's elements. public override Complex SumMagnitudes() { double result = 0; for (var i = 0; i < NonZerosCount; i++) { result += _nonZeroValues[i].Magnitude; } return result; } /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// /// The vector to pointwise multiply with this one. /// The vector to store the result of the pointwise multiplication. protected override void DoPointwiseMultiply(Vector other, Vector result) { if (ReferenceEquals(this, other)) { for (var i = 0; i < NonZerosCount; i++) { _nonZeroValues[i] *= _nonZeroValues[i]; } } else { for (var i = 0; i < NonZerosCount; i++) { var index = _nonZeroIndices[i]; result.At(index, other.At(index) * _nonZeroValues[i]); } } } /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// /// The vector to pointwise multiply with this one. /// The vector to store the result of the pointwise multiplication. protected override void DoPointwiseDivide(Vector other, Vector result) { if (ReferenceEquals(this, other)) { for (var i = 0; i < NonZerosCount; i++) { _nonZeroValues[i] /= _nonZeroValues[i]; } } else { for (var i = 0; i < NonZerosCount; i++) { var index = _nonZeroIndices[i]; result.At(index, _nonZeroValues[i] / other.At(index)); } } } /// /// Outer product of two vectors /// /// First vector /// Second vector /// Matrix M[i,j] = u[i]*v[j] /// If the u vector is . /// If the v vector is . public static Matrix OuterProduct(SparseVector u, SparseVector v) { if (u == null) { throw new ArgumentNullException("u"); } if (v == null) { throw new ArgumentNullException("v"); } var matrix = new SparseMatrix(u.Count, v.Count); for (var i = 0; i < u.NonZerosCount; i++) { for (var j = 0; j < v.NonZerosCount; j++) { if (u._nonZeroIndices[i] == v._nonZeroIndices[j]) { matrix.At(i, j, u._nonZeroValues[i] * v._nonZeroValues[j]); } } } return matrix; } /// /// Outer product of this and another vector. /// /// The vector to operate on. /// /// Matrix M[i,j] = this[i] * v[j]. /// public Matrix OuterProduct(SparseVector v) { return OuterProduct(this, v); } #endregion #region Vector Norms /// /// Computes the p-Norm. /// /// The p value. /// Scalar ret = (sum(abs(this[i])^p))^(1/p) public override Complex Norm(double p) { if (1 > p) { throw new ArgumentOutOfRangeException("p"); } if (NonZerosCount == 0) { return 0.0; } if (2.0 == p) { return _nonZeroValues.Aggregate(Complex.Zero, SpecialFunctions.Hypotenuse).Magnitude; } if (Double.IsPositiveInfinity(p)) { return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, _nonZeroValues[index].Magnitude), Math.Max); } var sum = 0.0; for (var index = 0; index < NonZerosCount; index++) { sum += Math.Pow(_nonZeroValues[index].Magnitude, p); } return Math.Pow(sum, 1.0 / p); } #endregion #region Parse Functions /// /// Creates a double sparse vector based on a string. The string can be in the following formats (without the /// quotes): 'n', 'n,n,..', '(n,n,..)', '[n,n,...]', where n is a Complex. /// /// /// A double sparse vector containing the values specified by the given string. /// /// /// The string to parse. /// public static SparseVector Parse(string value) { return Parse(value, null); } /// /// Creates a double sparse vector based on a string. The string can be in the following formats (without the /// quotes): 'n', 'n;n;..', '(n;n;..)', '[n;n;...]', where n is a Complex. /// /// /// A double sparse vector containing the values specified by the given string. /// /// /// the string to parse. /// /// /// An that supplies culture-specific formatting information. /// public static SparseVector Parse(string value, IFormatProvider formatProvider) { if (value == null) { throw new ArgumentNullException(value); } value = value.Trim(); if (value.Length == 0) { throw new FormatException(); } // strip out parens if (value.StartsWith("(", StringComparison.Ordinal)) { if (!value.EndsWith(")", StringComparison.Ordinal)) { throw new FormatException(); } value = value.Substring(1, value.Length - 2).Trim(); } if (value.StartsWith("[", StringComparison.Ordinal)) { if (!value.EndsWith("]", StringComparison.Ordinal)) { throw new FormatException(); } value = value.Substring(1, value.Length - 2).Trim(); } // keywords var textInfo = formatProvider.GetTextInfo(); var keywords = new[] { textInfo.ListSeparator }; // lexing var tokens = new LinkedList(); GlobalizationHelper.Tokenize(tokens.AddFirst(value), keywords, 0); var token = tokens.First; if (token == null || tokens.Count.IsEven()) { throw new FormatException(); } // parsing var data = new Complex[(tokens.Count + 1) >> 1]; for (var i = 0; i < data.Length; i++) { if (token == null || token.Value == textInfo.ListSeparator) { throw new FormatException(); } data[i] = token.Value.ToComplex(formatProvider); token = token.Next; if (token != null) { token = token.Next; } } return new SparseVector(data); } /// /// Converts the string representation of a complex sparse vector to double-precision sparse vector equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// /// A string containing a complex vector to convert. /// /// /// The parsed value. /// /// /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will be null. /// public static bool TryParse(string value, out SparseVector result) { return TryParse(value, null, out result); } /// /// Converts the string representation of a complex sparse vector to double-precision sparse vector equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// /// A string containing a complex vector to convert. /// /// /// An that supplies culture-specific formatting information about value. /// /// /// The parsed value. /// /// /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will be null. /// public static bool TryParse(string value, IFormatProvider formatProvider, out SparseVector result) { bool ret; try { result = Parse(value, formatProvider); ret = true; } catch (ArgumentNullException) { result = null; ret = false; } catch (FormatException) { result = null; ret = false; } return ret; } #endregion /// /// Gets the value at the given index. /// /// Value real index in array /// The value at the given index. internal protected override Complex At(int index) { // Search if item idex exists in NonZeroIndices array in range "0 - real nonzero values count" var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index); return itemIndex >= 0 ? _nonZeroValues[itemIndex] : Complex.Zero; } /// /// Delete, Add or Update the value in NonZeroValues and NonZeroIndices /// /// Value real index in array /// The value to set. /// This method assume that index is between 0 and Array Size internal protected override void At(int index, Complex value) { // Search if "index" already exists in range "0 - complex nonzero values count" var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index); if (itemIndex >= 0) { // Item already exist at itemIndex if (value == Complex.Zero) { // Value is zero. Let's delete it from Values and Indices array for (var i = itemIndex + 1; i < NonZerosCount; i++) { _nonZeroValues[i - 1] = _nonZeroValues[i]; _nonZeroIndices[i - 1] = _nonZeroIndices[i]; } 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 ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) { Array.Resize(ref _nonZeroValues, NonZerosCount); Array.Resize(ref _nonZeroIndices, NonZerosCount); } } else { _nonZeroValues[itemIndex] = value; } } else { if (value == Complex.Zero) { return; } itemIndex = ~itemIndex; // Index where to put new value // Check if the storage needs to be increased if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) { // Value and Indices arrays are completely full so we increase the 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 = NonZerosCount - 1; i > itemIndex - 1; i--) { _nonZeroValues[i + 1] = _nonZeroValues[i]; _nonZeroIndices[i + 1] = _nonZeroIndices[i]; } // Add the value and the column index _nonZeroValues[itemIndex] = value; _nonZeroIndices[itemIndex] = index; // increase the number of non-zero numbers by one NonZerosCount += 1; } } /// /// Calculates the amount with which to grow the storage array's if they need to be /// increased in size. /// /// The amount grown. private int GrowthSize() { int delta; if (_nonZeroValues.Length > 1024) { delta = _nonZeroValues.Length / 4; } else { if (_nonZeroValues.Length > 256) { delta = 512; } else { delta = _nonZeroValues.Length > 64 ? 128 : 32; } } return delta; } #region System.Object override public override string ToString(string format, IFormatProvider formatProvider) { if (Count > 20) { return String.Format("SparseVectorOfComplex({0},{1},{2})", Count, NonZerosCount, GetHashCode()); } return base.ToString(format, formatProvider); } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { var hashNum = Math.Min(NonZerosCount, 20); long hash = 0; for (var i = 0; i < hashNum; i++) { #if SILVERLIGHT hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode()); #else hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode()); #endif } return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4); } #endregion /// /// Indicates whether the current object is equal to another object of the same type. /// /// /// An object to compare with this object. /// /// /// true if the current object is equal to the parameter; otherwise, false. /// public override bool Equals(Vector other) { // Reject equality when the argument is null or has a different length. if (other == null) { return false; } if (Count != other.Count) { return false; } // Accept if the argument is the same object as this. if (ReferenceEquals(this, other)) { return true; } var sparseVector = other as SparseVector; if (sparseVector == null) { return base.Equals(other); } if (NonZerosCount != sparseVector.NonZerosCount) { return false; } // If all else fails, perform element wise comparison. for (var index = 0; index < NonZerosCount; index++) { if (!_nonZeroValues[index].AlmostEqual(sparseVector._nonZeroValues[index]) || (_nonZeroIndices[index] != sparseVector._nonZeroIndices[index])) { return false; } } return true; } /// /// Returns an that contains the position and value of the element. /// /// /// An over this vector that contains the position and value of each /// element. /// /// /// The enumerator returns a /// /// with the first value being the element index and the second value /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements /// with a zero value. /// public override IEnumerable> GetIndexedEnumerator() { for (var i = 0; i < NonZerosCount; i++) { yield return new Tuple(_nonZeroIndices[i], _nonZeroValues[i]); } } /// /// Returns the data contained in the vector as an array. /// /// /// The vector's data as an array. /// public override Complex[] ToArray() { var ret = new Complex[Count]; for (var i = 0; i < NonZerosCount; i++) { ret[_nonZeroIndices[i]] = _nonZeroValues[i]; } return ret; } } }