diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 392833ac..cba3033c 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -605,28 +605,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseVector(result); } - /// - /// Multiplies a scalar to each element of the vector. - /// - /// The scalar to multiply. - /// A new vector that is the multiplication of the vector and the scalar. - public override Vector Multiply(Complex scalar) - { - if (scalar == Complex.One) - { - return Clone(); - } - - if (scalar == Complex.Zero) - { - return new SparseVector(Count); - } - - var copy = new SparseVector(this); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy._storage.Values, copy._storage.Values); - return copy; - } - /// /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// @@ -638,22 +616,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// 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) { diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 61ed8d8d..783389f4 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -605,28 +605,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseVector(result); } - /// - /// Multiplies a scalar to each element of the vector. - /// - /// The scalar to multiply. - /// A new vector that is the multiplication of the vector and the scalar. - public override Vector Multiply(Complex32 scalar) - { - if (scalar == Complex32.One) - { - return Clone(); - } - - if (scalar == Complex32.Zero) - { - return new SparseVector(Count); - } - - var copy = new SparseVector(this); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy._storage.Values, copy._storage.Values); - return copy; - } - /// /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// @@ -638,22 +616,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// protected override void DoMultiply(Complex32 scalar, Vector result) { - if (scalar == Complex32.One) - { - if (!ReferenceEquals(this, result)) - { - CopyTo(result); - } - - return; - } - - if (scalar == Complex32.Zero) - { - result.Clear(); - return; - } - var sparseResult = result as SparseVector; if (sparseResult == null) { diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index efbd74ce..7e5e38db 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -568,28 +568,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseVector(result); } - /// - /// Multiplies a scalar to each element of the vector. - /// - /// The scalar to multiply. - /// A new vector that is the multiplication of the vector and the scalar. - public override Vector Multiply(double scalar) - { - if (scalar == 1.0) - { - return Clone(); - } - - if (scalar == 0) - { - return new SparseVector(Count); - } - - var copy = new SparseVector(this); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy._storage.Values, copy._storage.Values); - return copy; - } - /// /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// @@ -601,22 +579,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// protected override void DoMultiply(double scalar, Vector result) { - if (scalar == 1.0) - { - if (!ReferenceEquals(this, result)) - { - CopyTo(result); - } - - return; - } - - if (scalar == 0) - { - result.Clear(); - return; - } - var sparseResult = result as SparseVector; if (sparseResult == null) { @@ -634,7 +596,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double sparseResult._storage.Indices = new int[_storage.ValueCount]; Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); sparseResult._storage.Values = new double[_storage.ValueCount]; - Buffer.BlockCopy(_storage.Values, 0, sparseResult._storage.Values, 0, _storage.ValueCount * Constants.SizeOfDouble); + Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); } Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._storage.Values, sparseResult._storage.Values); diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index b1059688..b34b8140 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -543,13 +543,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The scalar to multiply. /// /// A new vector that is the multiplication of the vector and the scalar. - public virtual Vector Multiply(T scalar) + public Vector Multiply(T scalar) { if (scalar.Equals(One)) { return Clone(); } + if (scalar.Equals(Zero)) + { + return CreateVector(Count); + } + var result = CreateVector(Count); DoMultiply(scalar, result); return result; @@ -570,7 +575,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// If this vector and are not the same size. /// - public virtual void Multiply(T scalar, Vector result) + public void Multiply(T scalar, Vector result) { if (result == null) { @@ -582,6 +587,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } + if (scalar.Equals(One)) + { + CopyTo(result); + return; + } + + if (scalar.Equals(Zero)) + { + result.Clear(); + return; + } + DoMultiply(scalar, result); } @@ -644,7 +661,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The scalar to divide with. /// /// A new vector that is the division of the vector and the scalar. - public virtual Vector Divide(T scalar) + public Vector Divide(T scalar) { if (scalar.Equals(One)) { @@ -671,7 +688,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// If this vector and are not the same size. /// - public virtual void Divide(T scalar, Vector result) + public void Divide(T scalar, Vector result) { if (result == null) { @@ -683,6 +700,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } + if (scalar.Equals(One)) + { + CopyTo(result); + return; + } + DoDivide(scalar, result); } diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index e16796ec..205d0f01 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -563,28 +563,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseVector(result); } - /// - /// Multiplies a scalar to each element of the vector. - /// - /// The scalar to multiply. - /// A new vector that is the multiplication of the vector and the scalar. - public override Vector Multiply(float scalar) - { - if (scalar == 1.0f) - { - return Clone(); - } - - if (scalar == 0f) - { - return new SparseVector(Count); - } - - var copy = new SparseVector(this); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy._storage.Values, copy._storage.Values); - return copy; - } - /// /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// @@ -596,22 +574,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// protected override void DoMultiply(float scalar, Vector result) { - if (scalar == 1.0) - { - if (!ReferenceEquals(this, result)) - { - CopyTo(result); - } - - return; - } - - if (scalar == 0) - { - result.Clear(); - return; - } - var sparseResult = result as SparseVector; if (sparseResult == null) { @@ -629,7 +591,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single sparseResult._storage.Indices = new int[_storage.ValueCount]; Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); sparseResult._storage.Values = new float[_storage.ValueCount]; - Buffer.BlockCopy(_storage.Values, 0, sparseResult._storage.Values, 0, _storage.ValueCount * Constants.SizeOfFloat); + Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); } Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._storage.Values, sparseResult._storage.Values);