Browse Source

LA: Vector.Negate should implement common pattern

v2
Christoph Ruegg 14 years ago
parent
commit
6a085491ef
  1. 23
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  2. 35
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  3. 17
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  4. 23
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  5. 35
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  6. 17
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  7. 23
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  8. 35
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  9. 17
      src/Numerics/LinearAlgebra/Double/Vector.cs
  10. 34
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  11. 23
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  12. 35
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  13. 17
      src/Numerics/LinearAlgebra/Single/Vector.cs

23
src/Numerics/LinearAlgebra/Complex/DenseVector.cs

@ -369,19 +369,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<Complex> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<Complex> target)
{
var result = new DenseVector(_length);
CommonParallel.For(
0,
_values.Length,
index => result[index] = -_values[index]);
return result;
var denseResult = target as DenseVector;
if (denseResult == null)
{
base.DoNegate(target);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-Complex.One, _values, denseResult.Values);
}
}
/// <summary>

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

@ -448,24 +448,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<Complex> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<Complex> target)
{
var result = new SparseVectorStorage<Complex>(Count);
var valueCount = result.ValueCount = _storage.ValueCount;
var indices = result.Indices = new int[valueCount];
var values = result.Values = new Complex[valueCount];
if (valueCount != 0)
var sparseResult = target as SparseVector;
if (sparseResult == null)
{
CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]);
Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt);
target.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
target.At(_storage.Indices[index], -_storage.Values[index]);
}
}
else
{
if (!ReferenceEquals(this, target))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
sparseResult._storage.Values = new Complex[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
return new SparseVector(result);
Control.LinearAlgebraProvider.ScaleArray(-Complex.One, sparseResult._storage.Values, sparseResult._storage.Values);
}
}
/// <summary>

17
src/Numerics/LinearAlgebra/Complex/Vector.cs

@ -339,24 +339,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>
/// The negated vector.
/// </returns>
/// <remarks>
/// Added as an alternative to the unary negation operator.
/// </remarks>
public override Vector<Complex> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<Complex> target)
{
var result = CreateVector(Count);
for (var index = 0; index < Count; index++)
{
result.At(index, -At(index));
target.At(index, -At(index));
}
return result;
}
/// <summary>

23
src/Numerics/LinearAlgebra/Complex32/DenseVector.cs

@ -369,19 +369,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<Complex32> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<Complex32> target)
{
var result = new DenseVector(_length);
CommonParallel.For(
0,
_values.Length,
index => result[index] = -_values[index]);
return result;
var denseResult = target as DenseVector;
if (denseResult == null)
{
base.DoNegate(target);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, _values, denseResult.Values);
}
}
/// <summary>

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

@ -448,24 +448,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<Complex32> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<Complex32> target)
{
var result = new SparseVectorStorage<Complex32>(Count);
var valueCount = result.ValueCount = _storage.ValueCount;
var indices = result.Indices = new int[valueCount];
var values = result.Values = new Complex32[valueCount];
if (valueCount != 0)
var sparseResult = target as SparseVector;
if (sparseResult == null)
{
CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]);
Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt);
target.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
target.At(_storage.Indices[index], -_storage.Values[index]);
}
}
else
{
if (!ReferenceEquals(this, target))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
sparseResult._storage.Values = new Complex32[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
return new SparseVector(result);
Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, sparseResult._storage.Values, sparseResult._storage.Values);
}
}
/// <summary>

17
src/Numerics/LinearAlgebra/Complex32/Vector.cs

@ -339,24 +339,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>
/// The negated vector.
/// </returns>
/// <remarks>
/// Added as an alternative to the unary negation operator.
/// </remarks>
public override Vector<Complex32> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<Complex32> target)
{
var result = CreateVector(Count);
for (var index = 0; index < Count; index++)
{
result.At(index, -At(index));
target.At(index, -At(index));
}
return result;
}
/// <summary>

23
src/Numerics/LinearAlgebra/Double/DenseVector.cs

@ -379,19 +379,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<double> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<double> target)
{
var result = new DenseVector(_length);
CommonParallel.For(
0,
_values.Length,
index => result[index] = -_values[index]);
return result;
var denseResult = target as DenseVector;
if (denseResult == null)
{
base.DoNegate(target);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1.0d, _values, denseResult.Values);
}
}
/// <summary>

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

@ -416,24 +416,33 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<double> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<double> target)
{
var result = new SparseVectorStorage<double>(Count);
var valueCount = result.ValueCount = _storage.ValueCount;
var indices = result.Indices = new int[valueCount];
var values = result.Values = new double[valueCount];
if (valueCount != 0)
var sparseResult = target as SparseVector;
if (sparseResult == null)
{
CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]);
Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt);
target.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
target.At(_storage.Indices[index], -_storage.Values[index]);
}
}
else
{
if (!ReferenceEquals(this, target))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
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];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
return new SparseVector(result);
Control.LinearAlgebraProvider.ScaleArray(-1.0d, sparseResult._storage.Values, sparseResult._storage.Values);
}
}
/// <summary>

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

@ -343,24 +343,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>
/// The negated vector.
/// </returns>
/// <remarks>
/// Added as an alternative to the unary negation operator.
/// </remarks>
public override Vector<double> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<double> target)
{
var result = CreateVector(Count);
for (var index = 0; index < Count; index++)
{
result.At(index, -At(index));
target.At(index, -At(index));
}
return result;
}
/// <summary>

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

@ -335,7 +335,37 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public abstract Vector<T> Negate();
public Vector<T> Negate()
{
var retrunVector = CreateVector(Count);
DoNegate(retrunVector);
return retrunVector;
}
/// <summary>
/// Negates vector and save result to <paramref name="target"/>
/// </summary>
/// <param name="target">Target vector</param>
public void Negate(Vector<T> target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
DoNegate(target);
}
/// <summary>
/// Negates vector and save result to <paramref name="target"/>
/// </summary>
/// <param name="target">Target vector</param>
protected abstract void DoNegate(Vector<T> target);
/// <summary>
/// Subtracts another vector from this vector.
@ -399,7 +429,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public Vector<T> Conjugate()
{
var retrunVector = CreateVector(Count);
Conjugate(retrunVector);
DoConjugate(retrunVector);
return retrunVector;
}

23
src/Numerics/LinearAlgebra/Single/DenseVector.cs

@ -369,19 +369,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<float> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<float> target)
{
var result = new DenseVector(_length);
CommonParallel.For(
0,
_values.Length,
index => result[index] = -_values[index]);
return result;
var denseResult = target as DenseVector;
if (denseResult == null)
{
base.DoNegate(target);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1.0f, _values, denseResult.Values);
}
}
/// <summary>

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

@ -417,24 +417,33 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector<float> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<float> target)
{
var result = new SparseVectorStorage<float>(Count);
var valueCount = result.ValueCount = _storage.ValueCount;
var indices = result.Indices = new int[valueCount];
var values = result.Values = new float[valueCount];
if (valueCount != 0)
var sparseResult = target as SparseVector;
if (sparseResult == null)
{
CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]);
Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt);
target.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
target.At(_storage.Indices[index], -_storage.Values[index]);
}
}
else
{
if (!ReferenceEquals(this, target))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
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];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
return new SparseVector(result);
Control.LinearAlgebraProvider.ScaleArray(-1.0f, sparseResult._storage.Values, sparseResult._storage.Values);
}
}
/// <summary>

17
src/Numerics/LinearAlgebra/Single/Vector.cs

@ -343,24 +343,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Returns a negated vector.
/// Negates vector and saves result to <paramref name="target"/>
/// </summary>
/// <returns>
/// The negated vector.
/// </returns>
/// <remarks>
/// Added as an alternative to the unary negation operator.
/// </remarks>
public override Vector<float> Negate()
/// <param name="target">Target vector</param>
protected override void DoNegate(Vector<float> target)
{
var result = CreateVector(Count);
for (var index = 0; index < Count; index++)
{
result.At(index, -At(index));
target.At(index, -At(index));
}
return result;
}
/// <summary>

Loading…
Cancel
Save