Browse Source

removed redundant copyto's

la-knuth
Marcus Cuda 16 years ago
parent
commit
289dad9181
  1. 37
      src/MathNet.Numerics.5.1.ReSharper
  2. 15
      src/NUnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs
  3. 12
      src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs
  4. 40
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs
  5. 36
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs
  6. 35
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs
  7. 36
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs
  8. 24
      src/Numerics/Algorithms/LinearAlgebra/native.generic.include
  9. 6
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  10. 62
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  11. 15
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  12. 8
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  13. 14
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  14. 6
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  15. 62
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  16. 15
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  17. 8
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  18. 14
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  19. 6
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  20. 63
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  21. 17
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  22. 8
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  23. 14
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  24. 6
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  25. 63
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  26. 15
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  27. 8
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  28. 14
      src/Numerics/LinearAlgebra/Single/SparseVector.cs

37
src/MathNet.Numerics.5.1.ReSharper

@ -10,35 +10,7 @@
<CustomDictionary>
<Name>en-US</Name>
<CaseSensitive>false</CaseSensitive>
<UserWords>Wishart
Wikipedia
Marsaglia
Xorshift
λ
Matlab
Matlab
Matlab
Endian
indices
&amp;lt
&amp;gt
Frobenius
Pointwise
multipcation
kronecker
Cholesky
Eigen
mxn
nxn
Nist
NIST's
Excel's
ipiv
blocksize
Dont
dll
Criterium
inline</UserWords>
<UserWords />
</CustomDictionary>
</Dictionaries>
</CustomDictionaries>
@ -56,6 +28,7 @@ inline</UserWords>
<ParamDirection>Any</ParamDirection>
</Match>
</CommentMatch>
<CommentNotMatch />
<SuppressIfBaseHasComment>false</SuppressIfBaseHasComment>
<MaxLineLength>80</MaxLineLength>
</CommentsSettings>
@ -139,7 +112,7 @@ inline</UserWords>
<MustNotHaveSuffixes />
</NamingConventionRule>
<NamingConventionRule>
<IsDisabled>false</IsDisabled>
<IsDisabled>true</IsDisabled>
<Matches>
<Match>
<AccessLevel>Private</AccessLevel>
@ -363,7 +336,7 @@ inline</UserWords>
<Regex>(?&lt;remove&gt;_)</Regex>
</NamingConventionRule>
<NamingConventionRule>
<IsDisabled>false</IsDisabled>
<IsDisabled>true</IsDisabled>
<Matches>
<Match>
<AccessLevel>Any</AccessLevel>
@ -386,7 +359,7 @@ inline</UserWords>
<MustNotHaveSuffixes />
</NamingConventionRule>
<NamingConventionRule>
<IsDisabled>false</IsDisabled>
<IsDisabled>true</IsDisabled>
<Matches>
<Match>
<AccessLevel>Any</AccessLevel>

15
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]);

12
src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebraProviderOfT.cs

@ -94,21 +94,23 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
int QueryWorkspaceBlockSize(string methodName);*/
/// <summary>
/// Adds a scaled vector to another: <c>y += alpha*x</c>.
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <remarks>This is equivalent to the AXPY BLAS routine.</remarks>
void AddVectorToScaledVector(T[] y, T alpha, T[] x);
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
void AddVectorToScaledVector(T[] y, T alpha, T[] x, T[] result);
/// <summary>
/// Scales an array. Can be used to scale a vector and a matrix.
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <remarks>This is equivalent to the SCAL BLAS routine.</remarks>
void ScaleArray(T alpha, T[] x);
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
void ScaleArray(T alpha, T[] x, T[] result);
/// <summary>
/// Computes the dot product of x and y.

40
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs

@ -36,13 +36,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
public partial class ManagedLinearAlgebraProvider
{
/// <summary>
/// Adds a scaled vector to another: <c>y += alpha*x</c>.
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <remarks>This equivalent to the AXPY BLAS routine.</remarks>
public virtual void AddVectorToScaledVector(Complex[] y, Complex alpha, Complex[] x)
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
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
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <remarks>This is equivalent to the SCAL BLAS routine.</remarks>
public virtual void ScaleArray(Complex alpha, Complex[] x)
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
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]);
}
/// <summary>

36
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs

@ -35,13 +35,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
public partial class ManagedLinearAlgebraProvider
{
/// <summary>
/// Adds a scaled vector to another: <c>y += alpha*x</c>.
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <remarks>This equivalent to the AXPY BLAS routine.</remarks>
public virtual void AddVectorToScaledVector(Complex32[] y, Complex32 alpha, Complex32[] x)
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
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
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <remarks>This is equivalent to the SCAL BLAS routine.</remarks>
public virtual void ScaleArray(Complex32 alpha, Complex32[] x)
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
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]);
}
/// <summary>

35
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs

@ -35,13 +35,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
public partial class ManagedLinearAlgebraProvider : ILinearAlgebraProvider
{
/// <summary>
/// Adds a scaled vector to another: <c>y += alpha*x</c>.
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <remarks>This equivalent to the AXPY BLAS routine.</remarks>
public virtual void AddVectorToScaledVector(double[] y, double alpha, double[] x)
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
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
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <remarks>This is equivalent to the SCAL BLAS routine.</remarks>
public virtual void ScaleArray(double alpha, double[] x)
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
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]; });
}
/// <summary>

36
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs

@ -35,13 +35,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
public partial class ManagedLinearAlgebraProvider
{
/// <summary>
/// Adds a scaled vector to another: <c>y += alpha*x</c>.
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <remarks>This equivalent to the AXPY BLAS routine.</remarks>
public virtual void AddVectorToScaledVector(float[] y, float alpha, float[] x)
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
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
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <remarks>This is equivalent to the SCAL BLAS routine.</remarks>
public virtual void ScaleArray(float alpha, float[] x)
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
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]);
}
/// <summary>

24
src/Numerics/Algorithms/LinearAlgebra/native.generic.include

@ -1,11 +1,12 @@
 /// <summary>
/// Adds a scaled vector to another: <c>y += alpha*x</c>.
/// Adds a scaled vector to another: <c>result = y + alpha*x</c>.
/// </summary>
/// <param name="y">The vector to update.</param>
/// <param name="alpha">The value to scale <paramref name="x"/> by.</param>
/// <param name="x">The vector to add to <paramref name="y"/>.</param>
/// <remarks>This equivalent to the AXPY BLAS routine.</remarks>
public override void AddVectorToScaledVector(<#=dataType#>[] y, <#=dataType#> alpha, <#=dataType#>[] x)
/// <param name="result">The result of the addition.</param>
/// <remarks>This is similar to the AXPY BLAS routine.</remarks>
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);
}
/// <summary>
@ -35,8 +40,9 @@
/// </summary>
/// <param name="alpha">The scalar.</param>
/// <param name="x">The values to scale.</param>
/// <remarks>This is equivalent to the SCAL BLAS routine.</remarks>
public override void ScaleArray(<#=dataType#> alpha, <#=dataType#>[] x)
/// <param name="result">This result of the scaling.</param>
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
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);
}
/// <summary>

6
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);
}
}

62
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;
}

15
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -488,13 +488,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void Multiply(Complex scalar, Matrix<Complex> result)
protected override void DoMultiply(Complex scalar, Matrix<Complex> 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);
}
}

8
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);
}
}

14
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;
}
/// <summary>

6
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);
}
}

62
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;
}

15
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -493,13 +493,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void Multiply(Complex32 scalar, Matrix<Complex32> result)
protected override void DoMultiply(Complex32 scalar, Matrix<Complex32> 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);
}
}

8
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);
}
}

14
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;
}
/// <summary>

6
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);
}
}

63
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;
}

17
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -487,13 +487,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void Multiply(double scalar, Matrix<double> result)
protected override void DoMultiply(double scalar, Matrix<double> 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);
}
}

8
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);
}
}

14
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;
}
/// <summary>

6
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);
}
}

63
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;
}

15
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -487,13 +487,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
public override void Multiply(float scalar, Matrix<float> result)
protected override void DoMultiply(float scalar, Matrix<float> 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);
}
}

8
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);
}
}

14
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;
}
/// <summary>

Loading…
Cancel
Save