Browse Source

LA: Use LA provider more in dense vector arithmetics #91

v2
Christoph Ruegg 14 years ago
parent
commit
4471ff6acf
  1. 46
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  2. 46
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  3. 44
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  4. 46
      src/Numerics/LinearAlgebra/Single/DenseVector.cs

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

@ -271,15 +271,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The vector to store the result of the addition.</param>
protected override void DoAdd(Vector<Complex> other, Vector<Complex> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, Complex.One, odense.Values, rdense.Values);
base.DoAdd(other, result);
}
else
{
base.DoAdd(other, result);
Control.LinearAlgebraProvider.AddArrays(_values, otherDense._values, resultDense._values);
}
}
@ -350,21 +351,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// Subtracts another vector from 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>
protected override void DoSubtract(Vector<Complex> other, Vector<Complex> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, -1.0, odense.Values, rdense.Values);
base.DoSubtract(other, result);
}
else
{
base.DoSubtract(other, result);
Control.LinearAlgebraProvider.SubtractArrays(_values, otherDense._values, resultDense._values);
}
}
@ -638,17 +640,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<Complex> other, Vector<Complex> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] * other[index]);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_values, denseOther._values, denseResult._values);
}
}
@ -660,17 +661,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<Complex> other, Vector<Complex> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] / other[index]);
Control.LinearAlgebraProvider.PointWiseDivideArrays(_values, denseOther._values, denseResult._values);
}
}

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

@ -271,15 +271,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The vector to store the result of the addition.</param>
protected override void DoAdd(Vector<Complex32> other, Vector<Complex32> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, Complex32.One, odense.Values, rdense.Values);
base.DoAdd(other, result);
}
else
{
base.DoAdd(other, result);
Control.LinearAlgebraProvider.AddArrays(_values, otherDense._values, resultDense._values);
}
}
@ -350,21 +351,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// Subtracts another vector from 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>
protected override void DoSubtract(Vector<Complex32> other, Vector<Complex32> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, -1.0f, odense.Values, rdense.Values);
base.DoSubtract(other, result);
}
else
{
base.DoSubtract(other, result);
Control.LinearAlgebraProvider.SubtractArrays(_values, otherDense._values, resultDense._values);
}
}
@ -638,17 +640,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<Complex32> other, Vector<Complex32> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] * other[index]);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_values, denseOther._values, denseResult._values);
}
}
@ -660,17 +661,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<Complex32> other, Vector<Complex32> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] / other[index]);
Control.LinearAlgebraProvider.PointWiseDivideArrays(_values, denseOther._values, denseResult._values);
}
}

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

@ -271,15 +271,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The vector to store the result of the addition.</param>
protected override void DoAdd(Vector<double> other, Vector<double> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, 1.0, odense.Values, rdense.Values);
base.DoAdd(other, result);
}
else
{
base.DoAdd(other, result);
Control.LinearAlgebraProvider.AddArrays(_values, otherDense._values, resultDense._values);
}
}
@ -356,15 +357,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The vector to store the result of the subtraction.</param>
protected override void DoSubtract(Vector<double> other, Vector<double> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, -1.0, odense.Values, rdense.Values);
base.DoSubtract(other, result);
}
else
{
base.DoSubtract(other, result);
Control.LinearAlgebraProvider.SubtractArrays(_values, otherDense._values, resultDense._values);
}
}
@ -719,17 +721,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<double> other, Vector<double> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] * other[index]);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_values, denseOther._values, denseResult._values);
}
}
@ -741,17 +742,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<double> other, Vector<double> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] / other[index]);
Control.LinearAlgebraProvider.PointWiseDivideArrays(_values, denseOther._values, denseResult._values);
}
}

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

@ -271,15 +271,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The vector to store the result of the addition.</param>
protected override void DoAdd(Vector<float> other, Vector<float> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, 1.0f, odense.Values, rdense.Values);
base.DoAdd(other, result);
}
else
{
base.DoAdd(other, result);
Control.LinearAlgebraProvider.AddArrays(_values, otherDense._values, resultDense._values);
}
}
@ -350,21 +351,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// Subtracts another vector from 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>
protected override void DoSubtract(Vector<float> other, Vector<float> result)
{
var rdense = result as DenseVector;
var odense = other as DenseVector;
if (rdense != null && odense != null)
var otherDense = other as DenseVector;
var resultDense = result as DenseVector;
if (otherDense == null || resultDense == null)
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(_values, -1.0f, odense.Values, rdense.Values);
base.DoSubtract(other, result);
}
else
{
base.DoSubtract(other, result);
Control.LinearAlgebraProvider.SubtractArrays(_values, otherDense._values, resultDense._values);
}
}
@ -719,17 +721,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The vector to store the result of the pointwise division.</param>
protected override void DoPointwiseMultiply(Vector<float> other, Vector<float> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseMultiply(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] * other[index]);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_values, denseOther._values, denseResult._values);
}
}
@ -741,17 +742,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <remarks></remarks>
protected override void DoPointwiseDivide(Vector<float> other, Vector<float> result)
{
var dense = result as DenseVector;
if (dense == null)
var denseOther = other as DenseVector;
var denseResult = result as DenseVector;
if (denseOther == null || denseResult == null)
{
base.DoPointwiseDivide(other, result);
}
else
{
CommonParallel.For(
0,
_values.Length,
index => dense._values[index] = _values[index] / other[index]);
Control.LinearAlgebraProvider.PointWiseDivideArrays(_values, denseOther._values, denseResult._values);
}
}

Loading…
Cancel
Save