// // 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-2010 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. /// public class SparseVector : Vector { /// /// Lock object for the indexer. /// private readonly object _lockObject = new object(); /// /// 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[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(Complex[] array) : this(array.Length) { for (var i = 0; i < array.Length; 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[_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[0, _nonZeroIndices[i]] = _nonZeroValues[i]; } return matrix; } /// Gets or sets the value at the given . /// The index of the value to get or set. /// The value of the vector at the given . /// If is negative or /// greater than the size of the vector. public override Complex this[int index] { get { // If index is out of bounds if ((index < 0) || (index >= Count)) { throw new IndexOutOfRangeException(); } lock (_lockObject) { // Search if item idex exists in NonZeroIndices array in range "0 - complex nonzero values count" var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index); if (itemIndex >= 0) { return _nonZeroValues[itemIndex]; } } return Complex.Zero; } set { // If index is out of bounds if ((index < 0) || (index >= Count)) { throw new IndexOutOfRangeException(); } lock (_lockObject) { SetValue(index, value); } } } /// /// 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) { CommonParallel.For( 0, Count, index => target[index] = this[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 complex to each element of the vector. /// /// The complex to add. /// A copy of the vector with the complex added. public override Vector Add(Complex complex) { if (complex == Complex.Zero) { return Clone(); } var copy = (SparseVector)Clone(); for (var i = 0; i < Count; i++) { copy[i] += complex; } return copy; } /// /// Adds a complex to each element of the vector and stores the result in the result vector. /// /// The complex to add. /// The vector to store the result of the addition. /// If the result vector is . /// If this vector and are not the same size. public override void Add(Complex complex, Vector result) { if (result == null) { throw new ArgumentNullException("result"); } if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } base.Add(complex, result); } /// /// Adds another vector to this vector. /// /// The vector to add to this one. /// A new vector containing the sum of both vectors. /// If the other vector is . /// If this vector and are not the same size. public override Vector Add(Vector other) { if (other == null) { throw new ArgumentNullException("other"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } var sparseVector = other as SparseVector; if (sparseVector == null) { return base.Add(other); } var copy = (SparseVector)Clone(); copy.AddScaledSparseVector(Complex.One, sparseVector); return copy; } /// /// Adds the scaled sparse vector. /// /// The alpha. /// The other. private void AddScaledSparseVector(Complex alpha, SparseVector other) { if (other == null) { throw new ArgumentNullException("other"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } if (alpha == Complex.Zero) { return; } // I don't use ILinearAlgebraProvider because we will get no benefit due to "lock" in this[index] // Possible fucniton in ILinearAlgebraProvider may be AddSparseVectorToScaledSparseVector(T[] y, int[] yIndices, T alpha, T[] x, int[] xIndices); // But it require to develop value setting algorithm and due to "lock" it will be even more greedy then implemented below if (ReferenceEquals(this, other)) { // Adding the same instance of sparse vector. That means if we modify "this" then "other" will be modified too. // To avoid such problem lets change values in internal storage of "this" if (alpha == Complex.One) { for (var i = 0; i < NonZerosCount; i++) { _nonZeroValues[i] += _nonZeroValues[i]; } } else if (alpha == -Complex.One) { Clear(); // Vector is subtracted from itself return; } else { for (var i = 0; i < NonZerosCount; i++) { _nonZeroValues[i] += alpha * _nonZeroValues[i]; } } } else { // "this" and "other" are different objects, so by modifying "this" the "other" object will not be changed if (alpha == Complex.One) { for (var i = 0; i < other.NonZerosCount; i++) { this[other._nonZeroIndices[i]] += other._nonZeroValues[i]; } } else { for (var i = 0; i < other.NonZerosCount; i++) { this[other._nonZeroIndices[i]] += alpha * other._nonZeroValues[i]; } } } } /// /// 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. /// If the other vector is . /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. public override void Add(Vector other, Vector result) { if (result == null) { throw new ArgumentNullException("result"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = Add(other); tmp.CopyTo(result); } else { var sparse = result as SparseVector; if (sparse == null) { base.Add(other, result); } else { var sparseother = other as SparseVector; if (sparseother == null) { base.Add(other, result); } else { CopyTo(result); sparse.AddScaledSparseVector(Complex.One, sparseother); } } } } /// /// 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 Vector operator +(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return 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 Vector 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.Add(rightSide); } /// /// Subtracts a complex from each element of the vector. /// /// The complex to subtract. /// A new vector containing the subtraction of this vector and the complex. public override Vector Subtract(Complex complex) { if (complex == Complex.Zero) { return Clone(); } var copy = (SparseVector)Clone(); for (var i = 0; i < Count; i++) { copy[i] -= complex; } return copy; } /// /// Subtracts a complex from each element of the vector and stores the result in the result vector. /// /// The complex to subtract. /// The vector to store the result of the subtraction. /// If the result vector is . /// If this vector and are not the same size. public override void Subtract(Complex complex, Vector result) { if (result == null) { throw new ArgumentNullException("result"); } if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } base.Subtract(complex, result); } /// /// Subtracts another vector from this vector. /// /// The vector to subtract from this one. /// A new vector containing the subtraction of the the two vectors. /// If the other vector is . /// If this vector and are not the same size. public override Vector Subtract(Vector other) { if (other == null) { throw new ArgumentNullException("other"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } var sparseVector = other as SparseVector; if (sparseVector == null) { return base.Subtract(other); } var copy = (SparseVector)Clone(); copy.AddScaledSparseVector(-Complex.One, sparseVector); return copy; } /// /// 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. /// If the other vector is . /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. public override void Subtract(Vector other, Vector result) { if (result == null) { throw new ArgumentNullException("result"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = Subtract(other); tmp.CopyTo(result); } else { var sparse = result as SparseVector; if (sparse == null) { base.Subtract(other, result); } else { var sparseother = other as SparseVector; if (sparseother == null) { base.Subtract(other, result); } else { CopyTo(result); sparse.AddScaledSparseVector(-Complex.One, sparseother); } } } } /// /// 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 Vector operator -(SparseVector rightSide) { if (rightSide == null) { throw new ArgumentNullException("rightSide"); } return 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 Vector 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.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 complex to each element of the vector. /// /// The complex to multiply. /// A new vector that is the multiplication of the vector and the complex. public override Vector Multiply(Complex complex) { if (complex == Complex.One) { return Clone(); } if (complex == Complex.Zero) { var copy = Clone(); copy.Clear(); // Set array empty return copy; } else { var copy = (SparseVector)Clone(); Control.LinearAlgebraProvider.ScaleArray(complex, copy._nonZeroValues); return copy; } } /// /// Computes the dot product between this vector and another vector. /// /// The other vector to add. /// The result of the addition. /// If is not of the same size. /// If is . public override Complex DotProduct(Vector other) { if (other == null) { throw new ArgumentNullException("other"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } var result = Complex.Zero; // base implementation iterates though all elements, but we need only take non-zeros for (var i = 0; i < NonZerosCount; i++) { result += _nonZeroValues[i] * other[_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[i - index] = this[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++) { this[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. /// /// The vector to pointwise multiply with this one. /// A new vector which is the pointwise multiplication of the two vectors. /// If the other vector is . /// If this vector and are not the same size. public override Vector PointwiseMultiply(Vector other) { if (other == null) { throw new ArgumentNullException("other"); } if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } var copy = new SparseVector(Count); for (var i = 0; i < _nonZeroIndices.Length; i++) { var d = _nonZeroValues[i] * other[_nonZeroIndices[i]]; if (d != Complex.Zero) { copy[_nonZeroIndices[i]] = d; } } return copy; } /// /// 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 /*SparseMatrix*/ 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 = CommonParallel.Aggregate( 0, NonZerosCount, index => 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 /// /// 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 private void SetValue(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 /// /// Check equality. If this is regular vector, then check by base implementation. If Sparse - use own method. /// /// Object to compare /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { var sparseVector = obj as SparseVector; if (sparseVector == null) { return base.Equals(obj); } // Accept if the argument is the same object as this. if (ReferenceEquals(this, sparseVector)) { return true; } if ((Count != sparseVector.Count) || (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 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 /// /// 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 key being the element index and the 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 KeyValuePair(_nonZeroIndices[i], _nonZeroValues[i]); } } } }