Browse Source

issue 5660: some of the optimized dense implementations were not be ing called. fixed

pull/36/head
Marcus Cuda 16 years ago
parent
commit
9f0c071e29
  1. 444
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  2. 445
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  3. 403
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  4. 403
      src/Numerics/LinearAlgebra/Single/DenseVector.cs

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

@ -244,87 +244,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Adds a complex to each element of the vector.
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to add.</param>
/// <returns>A copy of the vector with the complex added.</returns>
public override Vector<Complex> Add(Complex complex)
{
if (complex == Complex.Zero)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] += complex);
return copy;
}
/// <summary>
/// Adds a complex to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to add.</param>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Complex complex, Vector<Complex> result)
protected override void DoAdd(Complex scalar, Vector<Complex> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Add(complex, result);
base.DoAdd(scalar, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] + complex);
}
}
/// <summary>
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <returns>A new vector containing the sum of both vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex> Add(Vector<Complex> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
index => dense.Data[index] = Data[index] + scalar);
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Add(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex.One, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
@ -332,27 +269,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Vector<Complex> other, Vector<Complex> result)
protected override void DoAdd(Vector<Complex> other, Vector<Complex> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
@ -361,10 +279,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] + other[index]);
base.DoAdd(other, result);
}
}
@ -414,127 +329,42 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Subtracts a complex from each element of the vector.
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to subtract.</param>
/// <returns>A new vector containing the subtraction of this vector and the complex.</returns>
public override Vector<Complex> Subtract(Complex complex)
{
if (complex == Complex.Zero)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] -= complex);
return copy;
}
/// <summary>
/// Subtracts a complex from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to subtract.</param>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Complex complex, Vector<Complex> result)
protected override void DoSubtract(Complex scalar, Vector<Complex> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Subtract(complex, result);
base.DoSubtract(scalar, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] - complex);
index => dense.Data[index] = Data[index] - scalar);
}
}
/// <summary>
/// Subtracts another vector from this vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex> Subtract(Vector<Complex> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Subtract(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex.One, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Vector<Complex> other, Vector<Complex> result)
protected override void DoSubtract(Vector<Complex> other, Vector<Complex> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex.One, odense.Data, rdense.Data);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, odense.Data, rdense.Data);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] - other[index]);
base.DoSubtract(other, result);
}
}
@ -599,49 +429,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Multiplies a complex to each element of the vector.
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the complex.</returns>
public override Vector<Complex> Multiply(Complex complex)
/// <param name="scalar">The scalar to multiply.</param>
/// <param name="result">The vector to store the result of the multiplication.</param>
/// <remarks></remarks>
protected override void DoMultiply(Complex scalar, Vector<Complex> result)
{
if (complex == Complex.One)
var denseResult = result as DenseVector;
if (denseResult == null)
{
return Clone();
base.DoMultiply(scalar, result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.ScaleArray(complex, Data, copy.Data);
return copy;
}
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
public override Complex DotProduct(Vector<Complex> other)
/// <returns>s
/// The result of the addition.</returns>
protected override Complex DoDotProduct(Vector<Complex> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.DotProduct(other);
}
return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
}
/// <summary>
@ -877,124 +693,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Pointwise multiplies this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <returns>A new vector which is the pointwise multiplication of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex> PointwiseMultiply(Vector<Complex> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.PointwiseMultiply(other);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] *= other[index]);
return copy;
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseMultiply(Vector<Complex> other, Vector<Complex> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = PointwiseMultiply(other);
tmp.CopyTo(result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
}
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// Pointwise divide this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex> PointwiseDivide(Vector<Complex> other)
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<Complex> other, Vector<Complex> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
var dense = result as DenseVector;
if (dense == null)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
base.DoPointwiseMultiply(other, result);
}
var denseVector = other as DenseVector;
if (denseVector == null)
else
{
return base.PointwiseMultiply(other);
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] /= other[index]);
return copy;
}
/// <summary>
@ -1002,51 +718,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <param name="result">The vector to store the result of the pointwise division.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseDivide(Vector<Complex> other, Vector<Complex> result)
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<Complex> other, Vector<Complex> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
var dense = result as DenseVector;
if (dense == null)
{
var tmp = PointwiseDivide(other);
tmp.CopyTo(result);
base.DoPointwiseDivide(other, result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
}
@ -1309,39 +994,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Conjugates vector and save result to <paramref name="target"/>
/// </summary>
/// <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))
{
var tmp = CreateVector(Count);
Conjugate(tmp);
tmp.CopyTo(target);
}
var denseTarget = target as DenseVector;
var otherVector = target as DenseVector;
if (otherVector == null)
if (denseTarget == null)
{
base.Conjugate(target);
base.DoConjugate(target);
}
else
{
CommonParallel.For(
0,
Count,
index => otherVector.Data[index] = Data[index].Conjugate());
index => denseTarget.Data[index] = Data[index].Conjugate());
}
}
/// <summary>Gets the value at the given <paramref name="index"/>.</summary>
/// <param name="index">The index of the value to get or set.</param>
/// <returns>The value of the vector at the given <paramref name="index"/>.</returns>

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

@ -245,87 +245,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Adds a complex to each element of the vector.
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to add.</param>
/// <returns>A copy of the vector with the complex added.</returns>
public override Vector<Complex32> Add(Complex32 complex)
{
if (complex == Complex32.Zero)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] += complex);
return copy;
}
/// <summary>
/// Adds a complex to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to add.</param>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Complex32 complex, Vector<Complex32> result)
protected override void DoAdd(Complex32 scalar, Vector<Complex32> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Add(complex, result);
base.DoAdd(scalar, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] + complex);
}
}
/// <summary>
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <returns>A new vector containing the sum of both vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex32> Add(Vector<Complex32> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
index => dense.Data[index] = Data[index] + scalar);
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Add(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex32.One, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
@ -333,27 +270,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Vector<Complex32> other, Vector<Complex32> result)
protected override void DoAdd(Vector<Complex32> other, Vector<Complex32> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
@ -362,10 +280,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] + other[index]);
base.DoAdd(other, result);
}
}
@ -415,130 +330,45 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Subtracts a complex from each element of the vector.
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to subtract.</param>
/// <returns>A new vector containing the subtraction of this vector and the complex.</returns>
public override Vector<Complex32> Subtract(Complex32 complex)
{
if (complex == Complex32.Zero)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] -= complex);
return copy;
}
/// <summary>
/// Subtracts a complex from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to subtract.</param>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Complex32 complex, Vector<Complex32> result)
protected override void DoSubtract(Complex32 scalar, Vector<Complex32> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Subtract(complex, result);
base.DoSubtract(scalar, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] - complex);
index => dense.Data[index] = Data[index] - scalar);
}
}
/// <summary>
/// Subtracts another vector from this vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex32> Subtract(Vector<Complex32> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Subtract(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex32.One, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Vector<Complex32> other, Vector<Complex32> result)
protected override void DoSubtract(Vector<Complex32> other, Vector<Complex32> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex32.One, odense.Data, rdense.Data);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0f, odense.Data, rdense.Data);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] - other[index]);
base.DoSubtract(other, result);
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
@ -600,49 +430,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Multiplies a complex to each element of the vector.
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="complex">The complex to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the complex.</returns>
public override Vector<Complex32> Multiply(Complex32 complex)
/// <param name="scalar">The scalar to multiply.</param>
/// <param name="result">The vector to store the result of the multiplication.</param>
/// <remarks></remarks>
protected override void DoMultiply(Complex32 scalar, Vector<Complex32> result)
{
if (complex == Complex32.One)
var denseResult = result as DenseVector;
if (denseResult == null)
{
return Clone();
base.DoMultiply(scalar, result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.ScaleArray(complex, Data, copy.Data);
return copy;
}
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
public override Complex32 DotProduct(Vector<Complex32> other)
/// <returns>s
/// The result of the addition.</returns>
protected override Complex32 DoDotProduct(Vector<Complex32> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.DotProduct(other);
}
return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
}
/// <summary>
@ -878,124 +694,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Pointwise multiplies this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <returns>A new vector which is the pointwise multiplication of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex32> PointwiseMultiply(Vector<Complex32> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.PointwiseMultiply(other);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] *= other[index]);
return copy;
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseMultiply(Vector<Complex32> other, Vector<Complex32> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = PointwiseMultiply(other);
tmp.CopyTo(result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
}
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// Pointwise divide this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<Complex32> PointwiseDivide(Vector<Complex32> other)
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<Complex32> other, Vector<Complex32> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
var dense = result as DenseVector;
if (dense == null)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
base.DoPointwiseMultiply(other, result);
}
var denseVector = other as DenseVector;
if (denseVector == null)
else
{
return base.PointwiseMultiply(other);
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] /= other[index]);
return copy;
}
/// <summary>
@ -1003,51 +719,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <param name="result">The vector to store the result of the pointwise division.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseDivide(Vector<Complex32> other, Vector<Complex32> result)
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<Complex32> other, Vector<Complex32> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
var dense = result as DenseVector;
if (dense == null)
{
var tmp = PointwiseDivide(other);
tmp.CopyTo(result);
base.DoPointwiseDivide(other, result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
}
@ -1362,36 +1047,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Conjugates vector and save result to <paramref name="target"/>
/// </summary>
/// <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))
{
var tmp = CreateVector(Count);
Conjugate(tmp);
tmp.CopyTo(target);
}
var denseTarget = target as DenseVector;
var otherVector = target as DenseVector;
if (otherVector == null)
if (denseTarget == null)
{
base.Conjugate(target);
base.DoConjugate(target);
}
else
{
CommonParallel.For(
0,
Count,
index => otherVector.Data[index] = Data[index].Conjugate());
index => denseTarget.Data[index] = Data[index].Conjugate());
}
}

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

@ -295,49 +295,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <returns>A copy of the vector with the scalar added.</returns>
public override Vector<double> Add(double scalar)
{
if (scalar == 0.0)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] += scalar);
return copy;
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(double scalar, Vector<double> result)
protected override void DoAdd(double scalar, Vector<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Add(scalar, result);
base.DoAdd(scalar, result);
}
else
{
@ -348,63 +316,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <returns>A new vector containing the sum of both vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<double> Add(Vector<double> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Add(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
/// Adds another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Vector<double> other, Vector<double> result)
protected override void DoAdd(Vector<double> other, Vector<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
@ -413,10 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] + other[index]);
base.DoAdd(other, result);
}
}
@ -465,49 +380,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return leftSide.Add(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <returns>A new vector containing the subtraction of this vector and the scalar.</returns>
public override Vector<double> Subtract(double scalar)
{
if (scalar == 0.0)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] -= scalar);
return copy;
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(double scalar, Vector<double> result)
protected override void DoSubtract(double scalar, Vector<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Subtract(scalar, result);
base.DoSubtract(scalar, result);
}
else
{
@ -518,63 +401,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Subtracts another vector from this vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<double> Subtract(Vector<double> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Subtract(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Vector<double> other, Vector<double> result)
protected override void DoSubtract(Vector<double> other, Vector<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
@ -583,13 +416,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] - other[index]);
base.DoSubtract(other, result);
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
@ -651,49 +481,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Multiplies a scalar to each element of the vector.
/// Multiplies a scalar to each element of the vector and stores the result in the result 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)
/// <param name="result">The vector to store the result of the multiplication.</param>
/// <remarks></remarks>
protected override void DoMultiply(double scalar, Vector<double> result)
{
if (scalar == 1.0)
var denseResult = result as DenseVector;
if (denseResult == null)
{
return Clone();
base.DoMultiply(scalar, result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.ScaleArray(scalar, Data, copy.Data);
return copy;
}
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
public override double DotProduct(Vector<double> other)
/// <returns>s
/// The result of the addition.</returns>
protected override double DoDotProduct(Vector<double> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.DotProduct(other);
}
return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
}
/// <summary>
@ -969,124 +785,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Pointwise multiplies this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <returns>A new vector which is the pointwise multiplication of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<double> PointwiseMultiply(Vector<double> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.PointwiseMultiply(other);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] *= other[index]);
return copy;
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseMultiply(Vector<double> other, Vector<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = PointwiseMultiply(other);
tmp.CopyTo(result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
}
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// Pointwise divide this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<double> PointwiseDivide(Vector<double> other)
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<double> other, Vector<double> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
var dense = result as DenseVector;
if (dense == null)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
base.DoPointwiseMultiply(other, result);
}
var denseVector = other as DenseVector;
if (denseVector == null)
else
{
return base.PointwiseMultiply(other);
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] /= other[index]);
return copy;
}
/// <summary>
@ -1094,51 +810,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <param name="result">The vector to store the result of the pointwise division.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseDivide(Vector<double> other, Vector<double> result)
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<double> other, Vector<double> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
var dense = result as DenseVector;
if (dense == null)
{
var tmp = PointwiseDivide(other);
tmp.CopyTo(result);
base.DoPointwiseDivide(other, result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
}

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

@ -295,49 +295,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <returns>A copy of the vector with the scalar added.</returns>
public override Vector<float> Add(float scalar)
{
if (scalar == 0.0)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] += scalar);
return copy;
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(float scalar, Vector<float> result)
protected override void DoAdd(float scalar, Vector<float> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Add(scalar, result);
base.DoAdd(scalar, result);
}
else
{
@ -348,63 +316,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <returns>A new vector containing the sum of both vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<float> Add(Vector<float> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Add(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0f, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
/// Adds another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Vector<float> other, Vector<float> result)
protected override void DoAdd(Vector<float> other, Vector<float> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
@ -413,10 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] + other[index]);
base.DoAdd(other, result);
}
}
@ -465,49 +380,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return leftSide.Add(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <returns>A new vector containing the subtraction of this vector and the scalar.</returns>
public override Vector<float> Subtract(float scalar)
{
if (scalar == 0.0)
{
return Clone();
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Data.Length,
index => copy.Data[index] -= scalar);
return copy;
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(float scalar, Vector<float> result)
protected override void DoSubtract(float scalar, Vector<float> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var dense = result as DenseVector;
if (dense == null)
{
base.Subtract(scalar, result);
base.DoSubtract(scalar, result);
}
else
{
@ -518,63 +401,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Subtracts another vector from this vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<float> Subtract(Vector<float> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.Subtract(other);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0f, denseVector.Data, copy.Data);
return copy;
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Subtract(Vector<float> other, Vector<float> result)
protected override void DoSubtract(Vector<float> other, Vector<float> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
@ -583,13 +416,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
else
{
CommonParallel.For(
0,
Data.Length,
index => result[index] = Data[index] - other[index]);
base.DoSubtract(other, result);
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary>
@ -651,49 +481,35 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Multiplies a scalar to each element of the vector.
/// Multiplies a scalar to each element of the vector and stores the result in the result 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)
/// <param name="result">The vector to store the result of the multiplication.</param>
/// <remarks></remarks>
protected override void DoMultiply(float scalar, Vector<float> result)
{
if (scalar == 1.0)
var denseResult = result as DenseVector;
if (denseResult == null)
{
return Clone();
base.DoMultiply(scalar, result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data);
}
var copy = new DenseVector(Count);
Control.LinearAlgebraProvider.ScaleArray(scalar, Data, copy.Data);
return copy;
}
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
public override float DotProduct(Vector<float> other)
/// <returns>s
/// The result of the addition.</returns>
protected override float DoDotProduct(Vector<float> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.DotProduct(other);
}
return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
}
/// <summary>
@ -969,124 +785,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Pointwise multiplies this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <returns>A new vector which is the pointwise multiplication of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<float> PointwiseMultiply(Vector<float> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.PointwiseMultiply(other);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] *= other[index]);
return copy;
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseMultiply(Vector<float> other, Vector<float> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = PointwiseMultiply(other);
tmp.CopyTo(result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
}
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// Pointwise divide this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector<float> PointwiseDivide(Vector<float> other)
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<float> other, Vector<float> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
var dense = result as DenseVector;
if (dense == null)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
base.DoPointwiseMultiply(other, result);
}
var denseVector = other as DenseVector;
if (denseVector == null)
else
{
return base.PointwiseMultiply(other);
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] * other[index]);
}
var copy = (DenseVector)Clone();
CommonParallel.For(
0,
Count,
index => copy[index] /= other[index]);
return copy;
}
/// <summary>
@ -1094,51 +810,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <param name="result">The vector to store the result of the pointwise division.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointwiseDivide(Vector<float> other, Vector<float> result)
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<float> other, Vector<float> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
var dense = result as DenseVector;
if (dense == null)
{
var tmp = PointwiseDivide(other);
tmp.CopyTo(result);
base.DoPointwiseDivide(other, result);
}
else
{
var dense = result as DenseVector;
if (dense == null)
{
base.PointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
CommonParallel.For(
0,
Data.Length,
index => dense.Data[index] = Data[index] / other[index]);
}
}

Loading…
Cancel
Save