Browse Source

LA: Remove redundant Vector.Multiply overrides #94

v2
Christoph Ruegg 14 years ago
parent
commit
4e4b264940
  1. 38
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 38
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  3. 40
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  4. 31
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  5. 40
      src/Numerics/LinearAlgebra/Single/SparseVector.cs

38
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -605,28 +605,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseVector(result);
}
/// <summary>
/// Multiplies a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public override Vector<Complex> 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;
}
/// <summary>
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
@ -638,22 +616,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </param>
protected override void DoMultiply(Complex scalar, Vector<Complex> 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)
{

38
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -605,28 +605,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseVector(result);
}
/// <summary>
/// Multiplies a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public override Vector<Complex32> 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;
}
/// <summary>
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
@ -638,22 +616,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </param>
protected override void DoMultiply(Complex32 scalar, Vector<Complex32> 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)
{

40
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -568,28 +568,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new SparseVector(result);
}
/// <summary>
/// Multiplies a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public override Vector<double> 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;
}
/// <summary>
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
@ -601,22 +579,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </param>
protected override void DoMultiply(double scalar, Vector<double> 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);

31
src/Numerics/LinearAlgebra/Generic/Vector.cs

@ -543,13 +543,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// The scalar to multiply.
/// </param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public virtual Vector<T> Multiply(T scalar)
public Vector<T> 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
/// <exception cref="ArgumentException">
/// If this vector and <paramref name="result"/> are not the same size.
/// </exception>
public virtual void Multiply(T scalar, Vector<T> result)
public void Multiply(T scalar, Vector<T> 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.
/// </param>
/// <returns>A new vector that is the division of the vector and the scalar.</returns>
public virtual Vector<T> Divide(T scalar)
public Vector<T> Divide(T scalar)
{
if (scalar.Equals(One))
{
@ -671,7 +688,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <exception cref="ArgumentException">
/// If this vector and <paramref name="result"/> are not the same size.
/// </exception>
public virtual void Divide(T scalar, Vector<T> result)
public void Divide(T scalar, Vector<T> 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);
}

40
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -563,28 +563,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new SparseVector(result);
}
/// <summary>
/// Multiplies a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public override Vector<float> 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;
}
/// <summary>
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
@ -596,22 +574,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </param>
protected override void DoMultiply(float scalar, Vector<float> 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);

Loading…
Cancel
Save