diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index fdb48569..96db81a2 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -357,6 +357,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// Adds a scalar to each element of the vector and stores the result in the result vector. + /// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled + /// sparse vector and very inefficient. Would be better to work with a dense vector instead. /// /// /// The scalar to add. @@ -378,10 +380,25 @@ namespace MathNet.Numerics.LinearAlgebra.Complex if (ReferenceEquals(this, result)) { - CommonParallel.For( - 0, - NonZerosCount, - index => _nonZeroValues[index] += scalar); + //populate a new vector with the scalar + var vnonZeroValues = new Complex[this.Count]; + var vnonZeroIndices = new int[this.Count]; + for (int index = 0; index < this.Count; index++) + { + vnonZeroIndices[index] = index; + vnonZeroValues[index] = scalar; + } + + //populate the non zero values from this + for (int j = 0; j < this.NonZerosCount; j++) + { + vnonZeroValues[this._nonZeroIndices[j]] = this._nonZeroValues[j] + scalar; + } + + //assign this vectors arrary to the new arrays. + this._nonZeroValues = vnonZeroValues; + this._nonZeroIndices = vnonZeroIndices; + this.NonZerosCount = this.Count; } else { diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 69c32a76..206d24d3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -387,6 +387,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// Adds a scalar to each element of the vector and stores the result in the result vector. + /// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled + /// sparse vector and very inefficient. Would be better to work with a dense vector instead. /// /// /// The scalar to add. @@ -408,10 +410,26 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 if (ReferenceEquals(this, result)) { - CommonParallel.For( - 0, - NonZerosCount, - index => _nonZeroValues[index] += scalar); + //populate a new vector with the scalar + var vnonZeroValues = new Complex32[this.Count]; + var vnonZeroIndices = new int[this.Count]; + for (int index = 0; index < this.Count; index++) + { + vnonZeroIndices[index] = index; + vnonZeroValues[index] = scalar; + } + + //populate the non zero values from this + for (int j = 0; j < this.NonZerosCount; j++) + { + vnonZeroValues[this._nonZeroIndices[j]] = this._nonZeroValues[j] + scalar; + } + + //assign this vectors arrary to the new arrays. + this._nonZeroValues = vnonZeroValues; + this._nonZeroIndices = vnonZeroIndices; + this.NonZerosCount = this.Count; + } else { diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index dc142180..43c03991 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -305,6 +305,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Adds a scalar to each element of the vector and stores the result in the result vector. + /// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled + /// sparse vector and very inefficient. Would be better to work with a dense vector instead. /// /// /// The scalar to add. @@ -326,10 +328,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result)) { - CommonParallel.For( - 0, - NonZerosCount, - index => _nonZeroValues[index] += scalar); + //populate a new vector with the scalar + var vnonZeroValues = new double[this.Count]; + var vnonZeroIndices = new int[this.Count]; + for (int index = 0; index < this.Count; index++) + { + vnonZeroIndices[index] = index; + vnonZeroValues[index] = scalar; + } + + //populate the non zero values from this + for (int j = 0; j < this.NonZerosCount; j++) + { + vnonZeroValues[this._nonZeroIndices[j]] = this._nonZeroValues[j] + scalar; + } + + //assign this vectors arrary to the new arrays. + this._nonZeroValues = vnonZeroValues; + this._nonZeroIndices = vnonZeroIndices; + this.NonZerosCount = this.Count; + } else { diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 5333d290..5bb20311 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -335,6 +335,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// Adds a scalar to each element of the vector and stores the result in the result vector. + /// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled + /// sparse vector and very inefficient. Would be better to work with a dense vector instead. /// /// /// The scalar to add. @@ -356,11 +358,27 @@ namespace MathNet.Numerics.LinearAlgebra.Single if (ReferenceEquals(this, result)) { - CommonParallel.For( - 0, - NonZerosCount, - index => _nonZeroValues[index] += scalar); + //populate a new vector with the scalar + var vnonZeroValues = new float[this.Count]; + var vnonZeroIndices = new int[this.Count]; + for (int index = 0; index < this.Count; index++) + { + vnonZeroIndices[index] = index; + vnonZeroValues[index] = scalar; + } + + //populate the non zero values from this + for (int j = 0; j < this.NonZerosCount; j++) + { + vnonZeroValues[this._nonZeroIndices[j]] = this._nonZeroValues[j] + scalar; + } + + //assign this vectors arrary to the new arrays. + this._nonZeroValues = vnonZeroValues; + this._nonZeroIndices = vnonZeroIndices; + this.NonZerosCount = this.Count; } + else { for (var index = 0; index < Count; index++)