Browse Source

LA: Vectors should override DoX instead of X: Conjugate #95

v2
Christoph Ruegg 14 years ago
parent
commit
cfe807fe52
  1. 37
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 37
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  3. 2
      src/Numerics/LinearAlgebra/Generic/Vector.cs

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

@ -208,50 +208,39 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseVector(size); return new SparseVector(size);
} }
#region Operators and supplementary functions
/// <summary> /// <summary>
/// Conjugates vector and save result to <paramref name="target"/> /// Conjugates vector and save result to <paramref name="target"/>
/// </summary> /// </summary>
/// <param name="target">Target vector</param> /// <param name="target">Target vector</param>
public override void Conjugate(Vector<Complex> target) protected override void DoConjugate(Vector<Complex> target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target)) if (ReferenceEquals(this, target))
{ {
var tmp = CreateVector(Count); var tmp = CreateVector(Count);
Conjugate(tmp); DoConjugate(tmp);
tmp.CopyTo(target); tmp.CopyTo(target);
} }
var otherVector = target as SparseVector; var targetSparse = target as SparseVector;
if (otherVector == null) if (targetSparse == null)
{ {
base.Conjugate(target); base.Conjugate(target);
return;
} }
else
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
otherVector._storage.Values = new Complex[_storage.ValueCount]; targetSparse._storage.Values = new Complex[_storage.ValueCount];
otherVector._storage.Indices = new int[_storage.ValueCount]; targetSparse._storage.Indices = new int[_storage.ValueCount];
otherVector._storage.ValueCount = _storage.ValueCount; targetSparse._storage.ValueCount = _storage.ValueCount;
if (_storage.ValueCount != 0) if (_storage.ValueCount != 0)
{ {
CommonParallel.For(0, _storage.ValueCount, index => otherVector._storage.Values[index] = _storage.Values[index].Conjugate()); CommonParallel.For(0, _storage.ValueCount, index => targetSparse._storage.Values[index] = _storage.Values[index].Conjugate());
Buffer.BlockCopy(_storage.Indices, 0, otherVector._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); Buffer.BlockCopy(_storage.Indices, 0, targetSparse._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
} }
} }
}
#region Operators and supplementary functions
/// <summary> /// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector. /// Adds a scalar to each element of the vector and stores the result in the result vector.

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

@ -208,50 +208,39 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseVector(size); return new SparseVector(size);
} }
#region Operators and supplementary functions
/// <summary> /// <summary>
/// Conjugates vector and save result to <paramref name="target"/> /// Conjugates vector and save result to <paramref name="target"/>
/// </summary> /// </summary>
/// <param name="target">Target vector</param> /// <param name="target">Target vector</param>
public override void Conjugate(Vector<Complex32> target) protected override void DoConjugate(Vector<Complex32> target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target)) if (ReferenceEquals(this, target))
{ {
var tmp = CreateVector(Count); var tmp = CreateVector(Count);
Conjugate(tmp); DoConjugate(tmp);
tmp.CopyTo(target); tmp.CopyTo(target);
} }
var otherVector = target as SparseVector; var targetSparse = target as SparseVector;
if (otherVector == null) if (targetSparse == null)
{ {
base.Conjugate(target); base.Conjugate(target);
return;
} }
else
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
otherVector._storage.Values = new Complex32[_storage.ValueCount]; targetSparse._storage.Values = new Complex32[_storage.ValueCount];
otherVector._storage.Indices = new int[_storage.ValueCount]; targetSparse._storage.Indices = new int[_storage.ValueCount];
otherVector._storage.ValueCount = _storage.ValueCount; targetSparse._storage.ValueCount = _storage.ValueCount;
if (_storage.ValueCount != 0) if (_storage.ValueCount != 0)
{ {
CommonParallel.For(0, _storage.ValueCount, index => otherVector._storage.Values[index] = _storage.Values[index].Conjugate()); CommonParallel.For(0, _storage.ValueCount, index => targetSparse._storage.Values[index] = _storage.Values[index].Conjugate());
Buffer.BlockCopy(_storage.Indices, 0, otherVector._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); Buffer.BlockCopy(_storage.Indices, 0, targetSparse._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
} }
} }
}
#region Operators and supplementary functions
/// <summary> /// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector. /// Adds a scalar to each element of the vector and stores the result in the result vector.

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

@ -1246,7 +1246,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// Conjugates vector and save result to <paramref name="target"/> /// Conjugates vector and save result to <paramref name="target"/>
/// </summary> /// </summary>
/// <param name="target">Target vector</param> /// <param name="target">Target vector</param>
public virtual void Conjugate(Vector<T> target) public void Conjugate(Vector<T> target)
{ {
if (target == null) if (target == null)
{ {

Loading…
Cancel
Save