From 289dad9181ac6e4339a4a7404e26c70f6857b399 Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Wed, 22 Dec 2010 21:49:45 +0800 Subject: [PATCH] removed redundant copyto's --- src/MathNet.Numerics.5.1.ReSharper | 37 ++--------- .../Double/LinearAlgebraProviderTests.cs | 15 ++--- .../ILinearAlgebraProviderOfT.cs | 12 ++-- .../ManagedLinearAlgebraProvider.Complex.cs | 40 +++++++----- .../ManagedLinearAlgebraProvider.Complex32.cs | 36 +++++++---- .../ManagedLinearAlgebraProvider.Double.cs | 35 ++++++----- .../ManagedLinearAlgebraProvider.Single.cs | 36 +++++++---- .../LinearAlgebra/native.generic.include | 24 ++++--- .../LinearAlgebra/Complex/DenseMatrix.cs | 6 +- .../LinearAlgebra/Complex/DenseVector.cs | 62 +++++++----------- .../LinearAlgebra/Complex/DiagonalMatrix.cs | 15 +++-- .../LinearAlgebra/Complex/SparseMatrix.cs | 8 ++- .../LinearAlgebra/Complex/SparseVector.cs | 14 ++--- .../LinearAlgebra/Complex32/DenseMatrix.cs | 6 +- .../LinearAlgebra/Complex32/DenseVector.cs | 62 +++++++----------- .../LinearAlgebra/Complex32/DiagonalMatrix.cs | 15 +++-- .../LinearAlgebra/Complex32/SparseMatrix.cs | 8 ++- .../LinearAlgebra/Complex32/SparseVector.cs | 14 ++--- .../LinearAlgebra/Double/DenseMatrix.cs | 6 +- .../LinearAlgebra/Double/DenseVector.cs | 63 +++++++------------ .../LinearAlgebra/Double/DiagonalMatrix.cs | 17 +++-- .../LinearAlgebra/Double/SparseMatrix.cs | 8 ++- .../LinearAlgebra/Double/SparseVector.cs | 14 ++--- .../LinearAlgebra/Single/DenseMatrix.cs | 6 +- .../LinearAlgebra/Single/DenseVector.cs | 63 +++++++------------ .../LinearAlgebra/Single/DiagonalMatrix.cs | 15 +++-- .../LinearAlgebra/Single/SparseMatrix.cs | 8 ++- .../LinearAlgebra/Single/SparseVector.cs | 14 ++--- 28 files changed, 298 insertions(+), 361 deletions(-) diff --git a/src/MathNet.Numerics.5.1.ReSharper b/src/MathNet.Numerics.5.1.ReSharper index 3c3388e5..a11bfa15 100644 --- a/src/MathNet.Numerics.5.1.ReSharper +++ b/src/MathNet.Numerics.5.1.ReSharper @@ -10,35 +10,7 @@ en-US false - Wishart -Wikipedia -Marsaglia -Xorshift -λ -Matlab -Matlab -Matlab -Endian -indices -&lt -&gt -Frobenius -Pointwise -multipcation -kronecker -Cholesky -Eigen -mxn -nxn -Nist -NIST's -Excel's -ipiv -blocksize -Dont -dll -Criterium -inline + @@ -56,6 +28,7 @@ inline Any + false 80 @@ -139,7 +112,7 @@ inline - false + true Private @@ -363,7 +336,7 @@ inline (?<remove>_) - false + true Any @@ -386,7 +359,7 @@ inline - false + true Any diff --git a/src/NUnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs b/src/NUnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs index c737c0a2..648b37c1 100644 --- a/src/NUnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs +++ b/src/NUnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs @@ -88,23 +88,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double public void CanAddVectorToScaledVectorDouble() { var result = new double[_y.Length]; - Array.Copy(_y, result, _y.Length); - Provider.AddVectorToScaledVector(result, 0, _x); + Provider.AddVectorToScaledVector(_y, 0, _x, result); for (var i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i], result[i]); } - Array.Copy(_y, result, _y.Length); - Provider.AddVectorToScaledVector(result, 1, _x); + Provider.AddVectorToScaledVector(_y, 1, _x, result); for (var i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i] + _x[i], result[i]); } - Array.Copy(_y, result, _y.Length); - Provider.AddVectorToScaledVector(result, Math.PI, _x); + Provider.AddVectorToScaledVector(_y, Math.PI, _x, result); for (var i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i] + (Math.PI * _x[i]), result[i]); @@ -119,15 +116,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double { var result = new double[_y.Length]; - Array.Copy(_y, result, _y.Length); - Provider.ScaleArray(1, result); + Provider.ScaleArray(1, _y, result); for (var i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i], result[i]); } - Array.Copy(_y, result, _y.Length); - Provider.ScaleArray(Math.PI, result); + Provider.ScaleArray(Math.PI, _y, result); for (var i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i] * Math.PI, result[i]); diff --git a/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs b/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs index 90456694..43de7f26 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs @@ -94,21 +94,23 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra int QueryWorkspaceBlockSize(string methodName);*/ /// - /// Adds a scaled vector to another: y += alpha*x. + /// Adds a scaled vector to another: result = y + alpha*x. /// /// The vector to update. /// The value to scale by. /// The vector to add to . - /// This is equivalent to the AXPY BLAS routine. - void AddVectorToScaledVector(T[] y, T alpha, T[] x); + /// The result of the addition. + /// This is similar to the AXPY BLAS routine. + void AddVectorToScaledVector(T[] y, T alpha, T[] x, T[] result); /// /// Scales an array. Can be used to scale a vector and a matrix. /// /// The scalar. /// The values to scale. - /// This is equivalent to the SCAL BLAS routine. - void ScaleArray(T alpha, T[] x); + /// This result of the scaling. + /// This is similar to the SCAL BLAS routine. + void ScaleArray(T alpha, T[] x, T[] result); /// /// Computes the dot product of x and y. diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs index 763dc892..9dd23de1 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs @@ -36,13 +36,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra public partial class ManagedLinearAlgebraProvider { /// - /// Adds a scaled vector to another: y += alpha*x. + /// Adds a scaled vector to another: result = y + alpha*x. /// /// The vector to update. /// The value to scale by. /// The vector to add to . - /// This equivalent to the AXPY BLAS routine. - public virtual void AddVectorToScaledVector(Complex[] y, Complex alpha, Complex[] x) + /// The result of the addition. + /// This is similar to the AXPY BLAS routine. + public virtual void AddVectorToScaledVector(Complex[] y, Complex alpha, Complex[] x, Complex[] result) { if (y == null) { @@ -59,18 +60,22 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (alpha == 0.0) + if (y.Length != x.Length) { - return; + throw new ArgumentException(Resources.ArgumentVectorsSameLength); } - if (alpha == 1.0) + if (alpha.IsZero()) { - CommonParallel.For(0, y.Length, i => y[i] += x[i]); + CommonParallel.For(0, y.Length, index => result[index] = y[index]); + } + else if (alpha.IsOne()) + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); } else { - CommonParallel.For(0, y.Length, i => y[i] += alpha * x[i]); + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); } } @@ -79,20 +84,27 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra /// /// The scalar. /// The values to scale. - /// This is equivalent to the SCAL BLAS routine. - public virtual void ScaleArray(Complex alpha, Complex[] x) + /// This result of the scaling. + /// This is similar to the SCAL BLAS routine. + public virtual void ScaleArray(Complex alpha, Complex[] x, Complex[] result) { if (x == null) { throw new ArgumentNullException("x"); } - if (alpha.IsOne()) + if (alpha.IsZero()) { - return; + CommonParallel.For(0, x.Length, index => result[index] = Complex.Zero); + } + else if (alpha.IsOne()) + { + CommonParallel.For(0, x.Length, index => result[index] = x[index]); + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); } - - CommonParallel.For(0, x.Length, i => x[i] = alpha * x[i]); } /// diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs index e38b66e8..2acd6d20 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs @@ -35,13 +35,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra public partial class ManagedLinearAlgebraProvider { /// - /// Adds a scaled vector to another: y += alpha*x. + /// Adds a scaled vector to another: result = y + alpha*x. /// /// The vector to update. /// The value to scale by. /// The vector to add to . - /// This equivalent to the AXPY BLAS routine. - public virtual void AddVectorToScaledVector(Complex32[] y, Complex32 alpha, Complex32[] x) + /// The result of the addition. + /// This is similar to the AXPY BLAS routine. + public virtual void AddVectorToScaledVector(Complex32[] y, Complex32 alpha, Complex32[] x, Complex32[] result) { if (y == null) { @@ -63,13 +64,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra return; } - if (alpha == 1.0F) + if (alpha.IsZero()) + { + CommonParallel.For(0, y.Length, index => result[index] = y[index]); + } + else if (alpha.IsOne()) { - CommonParallel.For(0, y.Length, i => y[i] += x[i]); + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); } else { - CommonParallel.For(0, y.Length, i => y[i] += alpha * x[i]); + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); } } @@ -78,20 +83,27 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra /// /// The scalar. /// The values to scale. - /// This is equivalent to the SCAL BLAS routine. - public virtual void ScaleArray(Complex32 alpha, Complex32[] x) + /// This result of the scaling. + /// This is similar to the SCAL BLAS routine. + public virtual void ScaleArray(Complex32 alpha, Complex32[] x, Complex32[] result) { if (x == null) { throw new ArgumentNullException("x"); } - if (alpha.IsOne()) + if (alpha.IsZero()) { - return; + CommonParallel.For(0, x.Length, index => result[index] = Complex32.Zero); + } + else if (alpha.IsOne()) + { + CommonParallel.For(0, x.Length, index => result[index] = x[index]); + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); } - - CommonParallel.For(0, x.Length, i => x[i] = alpha * x[i]); } /// diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs index 88dfc4ba..7b81141f 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs @@ -35,13 +35,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra public partial class ManagedLinearAlgebraProvider : ILinearAlgebraProvider { /// - /// Adds a scaled vector to another: y += alpha*x. + /// Adds a scaled vector to another: result = y + alpha*x. /// /// The vector to update. /// The value to scale by. /// The vector to add to . - /// This equivalent to the AXPY BLAS routine. - public virtual void AddVectorToScaledVector(double[] y, double alpha, double[] x) + /// The result of the addition. + /// This is similar to the AXPY BLAS routine. + public virtual void AddVectorToScaledVector(double[] y, double alpha, double[] x, double[] result) { if (y == null) { @@ -60,16 +61,15 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra if (alpha == 0.0) { - return; + CommonParallel.For(0, y.Length, index => result[index] = y[index]); } - - if (alpha == 1.0) + else if (alpha == 1.0) { - CommonParallel.For(0, y.Length, index => { y[index] += x[index]; }); + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); } else { - CommonParallel.For(0, y.Length, index => { y[index] += alpha * x[index]; }); + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); } } @@ -78,20 +78,27 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra /// /// The scalar. /// The values to scale. - /// This is equivalent to the SCAL BLAS routine. - public virtual void ScaleArray(double alpha, double[] x) + /// This result of the scaling. + /// This is similar to the SCAL BLAS routine. + public virtual void ScaleArray(double alpha, double[] x, double[] result) { if (x == null) { throw new ArgumentNullException("x"); } - if (alpha == 1.0) + if (alpha == 0.0) { - return; + CommonParallel.For(0, x.Length, index => result[index] = 0.0); + } + else if (alpha == 1.0) + { + CommonParallel.For(0, x.Length, index => result[index] = x[index]); + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); } - - CommonParallel.For(0, x.Length, index => { x[index] = alpha * x[index]; }); } /// diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs index bb8ab9df..b9a62299 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs @@ -35,13 +35,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra public partial class ManagedLinearAlgebraProvider { /// - /// Adds a scaled vector to another: y += alpha*x. + /// Adds a scaled vector to another: result = y + alpha*x. /// /// The vector to update. /// The value to scale by. /// The vector to add to . - /// This equivalent to the AXPY BLAS routine. - public virtual void AddVectorToScaledVector(float[] y, float alpha, float[] x) + /// The result of the addition. + /// This is similar to the AXPY BLAS routine. + public virtual void AddVectorToScaledVector(float[] y, float alpha, float[] x, float[] result) { if (y == null) { @@ -63,13 +64,17 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra return; } - if (alpha == 1.0) + if (alpha == 0.0) { - CommonParallel.For(0, y.Length, i => y[i] += x[i]); + CommonParallel.For(0, y.Length, index => result[index] = y[index]); + } + else if (alpha == 1.0) + { + CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); } else { - CommonParallel.For(0, y.Length, i => y[i] += alpha * x[i]); + CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); } } @@ -78,20 +83,27 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra /// /// The scalar. /// The values to scale. - /// This is equivalent to the SCAL BLAS routine. - public virtual void ScaleArray(float alpha, float[] x) + /// This result of the scaling. + /// This is similar to the SCAL BLAS routine. + public virtual void ScaleArray(float alpha, float[] x, float[] result) { if (x == null) { throw new ArgumentNullException("x"); } - if (alpha == 1.0) + if (alpha == 0.0) { - return; + CommonParallel.For(0, x.Length, index => result[index] = 0.0f); + } + else if (alpha == 1.0) + { + CommonParallel.For(0, x.Length, index => result[index] = x[index]); + } + else + { + CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); } - - CommonParallel.For(0, x.Length, i => x[i] = alpha * x[i]); } /// diff --git a/src/Numerics/Algorithms/LinearAlgebra/native.generic.include b/src/Numerics/Algorithms/LinearAlgebra/native.generic.include index a5c9e7a8..47fad92c 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/native.generic.include +++ b/src/Numerics/Algorithms/LinearAlgebra/native.generic.include @@ -1,11 +1,12 @@  /// - /// Adds a scaled vector to another: y += alpha*x. + /// Adds a scaled vector to another: result = y + alpha*x. /// /// The vector to update. /// The value to scale by. /// The vector to add to . - /// This equivalent to the AXPY BLAS routine. - public override void AddVectorToScaledVector(<#=dataType#>[] y, <#=dataType#> alpha, <#=dataType#>[] x) + /// The result of the addition. + /// This is similar to the AXPY BLAS routine. + public override void AddVectorToScaledVector(<#=dataType#>[] y, <#=dataType#> alpha, <#=dataType#>[] x, <#=dataType#>[] result) { if (y == null) { @@ -27,7 +28,11 @@ return; } - SafeNativeMethods.<#=prefix#>_axpy(y.Length, <#=reff#>alpha, x, y); + if (!ReferenceEquals(y, result)){ + Array.Copy(y, 0, result, 0, y.Length * 8); + } + + SafeNativeMethods.<#=prefix#>_axpy(y.Length, <#=reff#>alpha, x, result); } /// @@ -35,8 +40,9 @@ /// /// The scalar. /// The values to scale. - /// This is equivalent to the SCAL BLAS routine. - public override void ScaleArray(<#=dataType#> alpha, <#=dataType#>[] x) + /// This result of the scaling. + /// This is similar to the SCAL BLAS routine. + public override void ScaleArray(<#=dataType#> alpha, <#=dataType#>[] x, <#=dataType#>[] result) { if (x == null) { @@ -48,7 +54,11 @@ return; } - SafeNativeMethods.<#=prefix#>_scale(x.Length, <#=reff#>alpha, x); + if (!ReferenceEquals(x, result)){ + Array.Copy(x, 0, result, 0, x.Length * 8); + } + + SafeNativeMethods.<#=prefix#>_scale(x.Length, <#=reff#>alpha, result); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index c42c2915..ec571014 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -340,8 +340,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } } @@ -483,8 +482,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } else { - Array.Copy(Data, denseResult.Data, Data.Length); - Control.LinearAlgebraProvider.ScaleArray(-1, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(-1, Data, denseResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index c9f2dc3a..b743b1a4 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -322,8 +322,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return base.Add(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, Complex.One, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex.One, denseVector.Data, copy.Data); return copy; } @@ -353,27 +353,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Add(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex.One, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, Complex.One, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] + other[index]); } } @@ -501,8 +492,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return base.Subtract(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -Complex.One, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex.One, denseVector.Data, copy.Data); return copy; } @@ -532,27 +523,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Subtract(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex.One, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, -Complex.One, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] - other[index]); } } @@ -628,8 +610,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return Clone(); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(complex, copy.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.ScaleArray(complex, Data, copy.Data); return copy; } diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index b174bf02..a52a0c63 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -488,13 +488,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The matrix to store the result of the multiplication. /// If the result matrix is . /// If the result matrix's dimensions are not the same as this matrix. - public override void Multiply(Complex scalar, Matrix result) + protected override void DoMultiply(Complex scalar, Matrix result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (scalar == 0.0) { result.Clear(); @@ -514,8 +509,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } else { - CopyTo(diagResult); - Control.LinearAlgebraProvider.ScaleArray(scalar, diagResult.Data); + if (!ReferenceEquals(this, result)) + { + CopyTo(diagResult); + } + + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, diagResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index df3d0c13..85957334 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -1238,8 +1238,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._nonZeroValues); + if (!ReferenceEquals(this, result)) + { + CopyTo(sparseResult); + } + + CommonParallel.For(0, NonZerosCount, index => sparseResult._nonZeroValues[index] *= scalar); } } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index cf1c0d94..551ca0b4 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -848,16 +848,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex 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; + return new SparseVector(Count); } + + var copy = new SparseVector(this); + Control.LinearAlgebraProvider.ScaleArray(complex, copy._nonZeroValues, copy._nonZeroValues); + return copy; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 33309ce5..67b0b02c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -340,8 +340,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } } @@ -483,8 +482,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } else { - Array.Copy(Data, denseResult.Data, Data.Length); - Control.LinearAlgebraProvider.ScaleArray(-1, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(-1, Data, denseResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 0ca97754..2214e708 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -323,8 +323,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return base.Add(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, Complex32.One, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex32.One, denseVector.Data, copy.Data); return copy; } @@ -354,27 +354,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Add(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex32.One, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, Complex32.One, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] + other[index]); } } @@ -502,8 +493,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return base.Subtract(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -Complex32.One, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex32.One, denseVector.Data, copy.Data); return copy; } @@ -533,27 +524,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Subtract(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex32.One, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, -Complex32.One, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] - other[index]); } } @@ -629,8 +611,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return Clone(); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(complex, copy.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.ScaleArray(complex, Data, copy.Data); return copy; } diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index c96f1e3f..46c6f130 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -493,13 +493,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The matrix to store the result of the multiplication. /// If the result matrix is . /// If the result matrix's dimensions are not the same as this matrix. - public override void Multiply(Complex32 scalar, Matrix result) + protected override void DoMultiply(Complex32 scalar, Matrix result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (scalar.IsZero()) { result.Clear(); @@ -519,8 +514,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } else { - CopyTo(diagResult); - Control.LinearAlgebraProvider.ScaleArray(scalar, diagResult.Data); + if (!ReferenceEquals(this, result)) + { + CopyTo(diagResult); + } + + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, diagResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 6868b6b4..1309f7b9 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -1238,8 +1238,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._nonZeroValues); + if (!ReferenceEquals(this, result)) + { + CopyTo(sparseResult); + } + + CommonParallel.For(0, NonZerosCount, index => sparseResult._nonZeroValues[index] *= scalar); } } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 46c677b2..3ceedb9f 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -848,16 +848,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 if (complex == Complex32.Zero) { - var copy = Clone(); - copy.Clear(); // Set array empty - return copy; - } - else - { - var copy = (SparseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(complex, copy._nonZeroValues); - return copy; + return new SparseVector(Count); } + + var copy = new SparseVector(this); + Control.LinearAlgebraProvider.ScaleArray(complex, copy._nonZeroValues, copy._nonZeroValues); + return copy; } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 03a600b5..19b3dd29 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -329,8 +329,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } } @@ -472,8 +471,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Buffer.BlockCopy(Data, 0, denseResult.Data, 0, Data.Length * Constants.SizeOfDouble); - Control.LinearAlgebraProvider.ScaleArray(-1, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(-1, Data, denseResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 95e0e97d..1a39a081 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -30,7 +30,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double using System.Collections.Generic; using System.Globalization; using System.Linq; - using Distributions; using Generic; using NumberTheory; using Properties; @@ -375,8 +374,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.Add(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, 1.0, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data, copy.Data); return copy; } @@ -406,27 +405,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Add(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, 1.0, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] + other[index]); } } @@ -554,8 +544,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.Subtract(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -1.0, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data, copy.Data); return copy; } @@ -585,27 +575,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Subtract(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, -1.0, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] - other[index]); } } @@ -681,8 +662,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double return Clone(); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, copy.Data); return copy; } diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index 22746c54..a4ab2317 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -487,13 +487,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The matrix to store the result of the multiplication. /// If the result matrix is . /// If the result matrix's dimensions are not the same as this matrix. - public override void Multiply(double scalar, Matrix result) + protected override void DoMultiply(double scalar, Matrix result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (scalar == 0.0) { result.Clear(); @@ -509,12 +504,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double var diagResult = result as DiagonalMatrix; if (diagResult == null) { - base.Multiply(scalar, result); + base.DoMultiply(scalar, result); } else { - CopyTo(diagResult); - Control.LinearAlgebraProvider.ScaleArray(scalar, diagResult.Data); + if (!ReferenceEquals(this, result)) + { + CopyTo(diagResult); + } + + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, diagResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 1a22e8d3..aac70cca 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -1237,8 +1237,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._nonZeroValues); + if (!ReferenceEquals(this, result)) + { + CopyTo(sparseResult); + } + + CommonParallel.For(0, NonZerosCount, index => sparseResult._nonZeroValues[index] *= scalar); } } diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 244a2eca..f80d0277 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -797,16 +797,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (scalar == 0) { - var copy = Clone(); - copy.Clear(); // Set array empty - return copy; - } - else - { - var copy = (SparseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy._nonZeroValues); - return copy; + return new SparseVector(Count); } + + var copy = new SparseVector(this); + Control.LinearAlgebraProvider.ScaleArray(scalar, copy._nonZeroValues, copy._nonZeroValues); + return copy; } /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index baa3eef4..55a4b3eb 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -329,8 +329,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } } @@ -472,8 +471,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - Buffer.BlockCopy(Data, 0, denseResult.Data, 0, Data.Length * Constants.SizeOfFloat); - Control.LinearAlgebraProvider.ScaleArray(-1, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(-1, Data, denseResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index b5d8c133..3f068bd5 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -30,7 +30,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single using System.Collections.Generic; using System.Globalization; using System.Linq; - using Distributions; using Generic; using NumberTheory; using Properties; @@ -375,8 +374,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single return base.Add(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, 1.0f, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0f, denseVector.Data, copy.Data); return copy; } @@ -406,27 +405,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Add(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0f, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, 1.0f, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] + other[index]); } } @@ -554,8 +544,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single return base.Subtract(other); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -1.0f, denseVector.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0f, denseVector.Data, copy.Data); return copy; } @@ -585,27 +575,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var rdense = result as DenseVector; + var odense = other as DenseVector; + if (rdense != null && odense != null) { - var tmp = Subtract(other); - tmp.CopyTo(result); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0f, odense.Data, rdense.Data); } else { - var rdense = result as DenseVector; - var odense = other as DenseVector; - if (rdense != null && odense != null) - { - CopyTo(result); - Control.LinearAlgebraProvider.AddVectorToScaledVector(rdense.Data, -1.0f, odense.Data); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => result[index] = Data[index] - other[index]); } } @@ -681,8 +662,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single return Clone(); } - var copy = (DenseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy.Data); + var copy = new DenseVector(Count); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, copy.Data); return copy; } diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 1555520b..29ae7424 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -487,13 +487,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The matrix to store the result of the multiplication. /// If the result matrix is . /// If the result matrix's dimensions are not the same as this matrix. - public override void Multiply(float scalar, Matrix result) + protected override void DoMultiply(float scalar, Matrix result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (scalar == 0.0) { result.Clear(); @@ -513,8 +508,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - CopyTo(diagResult); - Control.LinearAlgebraProvider.ScaleArray(scalar, diagResult.Data); + if (!ReferenceEquals(this, result)) + { + CopyTo(diagResult); + } + + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, diagResult.Data); } } diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index a8b74f5b..c50259eb 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -1237,8 +1237,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - CopyTo(result); - Control.LinearAlgebraProvider.ScaleArray(scalar, sparseResult._nonZeroValues); + if (!ReferenceEquals(this, result)) + { + CopyTo(sparseResult); + } + + CommonParallel.For(0, NonZerosCount, index => sparseResult._nonZeroValues[index] *= scalar); } } diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 3f73868a..4dbfd83d 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -797,16 +797,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single if (scalar == 0) { - var copy = Clone(); - copy.Clear(); // Set array empty - return copy; - } - else - { - var copy = (SparseVector)Clone(); - Control.LinearAlgebraProvider.ScaleArray(scalar, copy._nonZeroValues); - return copy; + return new SparseVector(Count); } + + var copy = new SparseVector(this); + Control.LinearAlgebraProvider.ScaleArray(scalar, copy._nonZeroValues, copy._nonZeroValues); + return copy; } ///