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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -335,7 +335,37 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// </summary> /// </summary>
/// <returns>The negated vector.</returns> /// <returns>The negated vector.</returns>
/// <remarks>Added as an alternative to the unary negation operator.</remarks> /// <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> /// <summary>
/// Subtracts another vector from this vector. /// Subtracts another vector from this vector.
@ -399,7 +429,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public Vector<T> Conjugate() public Vector<T> Conjugate()
{ {
var retrunVector = CreateVector(Count); var retrunVector = CreateVector(Count);
Conjugate(retrunVector); DoConjugate(retrunVector);
return retrunVector; return retrunVector;
} }

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

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

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

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

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

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

Loading…
Cancel
Save