Browse Source

commented out CommonParallel.Aggregate modified the affected code

la-knuth
Marcus Cuda 16 years ago
parent
commit
2cf99d8184
  1. 16
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs
  2. 8
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs
  3. 28
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs
  4. 18
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs
  5. 8
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  6. 39
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  7. 13
      src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs
  8. 8
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  9. 37
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  10. 9
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  11. 46
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  12. 8
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  13. 39
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  14. 13
      src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs
  15. 8
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  16. 37
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  17. 9
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  18. 46
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  19. 8
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  20. 38
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  21. 13
      src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs
  22. 8
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  23. 37
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  24. 9
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  25. 46
      src/Numerics/LinearAlgebra/Double/Vector.cs
  26. 10
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  27. 8
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  28. 39
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  29. 13
      src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs
  30. 8
      src/Numerics/LinearAlgebra/Single/Matrix.cs
  31. 38
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  32. 9
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  33. 46
      src/Numerics/LinearAlgebra/Single/Vector.cs
  34. 4
      src/Numerics/Threading/CommonParallel.cs
  35. 6
      src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs

16
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs

@ -131,7 +131,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
return CommonParallel.Aggregate(0, y.Length, index => y[index] * x[index]); var dot = Complex.Zero;
for (var index = 0; index < y.Length; index++)
{
dot += y[index] * x[index];
}
return dot;
} }
/// <summary> /// <summary>
@ -1839,7 +1845,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i => i =>
{ {
var im = i * rowsR; var im = i * rowsR;
sol[jm + i] = CommonParallel.Aggregate(0, rowsR, k => q[im + k].Conjugate() * column[k]); var sum = Complex.Zero;
for (var k = 0; k < rowsR; k++)
{
sum += q[im + k].Conjugate() * column[k];
}
sol[jm + i] = sum;
}); });
} }

8
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs

@ -1845,7 +1845,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i => i =>
{ {
var im = i * rowsR; var im = i * rowsR;
sol[jm + i] = CommonParallel.Aggregate(0, rowsR, k => q[im + k].Conjugate() * column[k]); var sum = Complex32.Zero;
for (var k = 0; k < rowsR; k++)
{
sum += q[im + k].Conjugate() * column[k];
}
sol[jm + i] = sum;
}); });
} }

28
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs

@ -125,7 +125,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
return CommonParallel.Aggregate(0, y.Length, index => y[index] * x[index]); var sum = 0.0;
for (var index = 0; index < y.Length; index++)
{
sum += y[index] * x[index];
}
return sum;
} }
/// <summary> /// <summary>
@ -1820,7 +1827,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i => i =>
{ {
var im = i * rowsR; var im = i * rowsR;
sol[jm + i] = CommonParallel.Aggregate(0, rowsR, k => q[im + k] * column[k]);
var sum = 0.0;
for (var k = 0; k < rowsR; k++ )
{
sum += q[im + k] * column[k];
}
sol[jm + i] = sum;
}); });
} }
@ -2007,8 +2021,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
{ {
// Compute the transformation for the l-th column and // Compute the transformation for the l-th column and
// place the l-th diagonal in vector s[l]. // place the l-th diagonal in vector s[l].
var l1 = l;
stemp[l] = Math.Sqrt(CommonParallel.Aggregate(l, rowsA, i1 => (a[(l1 * rowsA) + i1] * a[(l1 * rowsA) + i1]))); var sum = 0.0;
for (var i1 = l; i1 < rowsA; i1++)
{
sum += a[(l * rowsA) + i1] * a[(l * rowsA) + i1];
}
stemp[l] = Math.Sqrt(sum);
if (stemp[l] != 0.0) if (stemp[l] != 0.0)
{ {

18
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs

@ -1828,7 +1828,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i => i =>
{ {
var im = i * rowsR; var im = i * rowsR;
sol[jm + i] = CommonParallel.Aggregate(0, rowsR, k => q[im + k] * column[k]);
var sum = 0.0f;
for (var k = 0; k < rowsR; k++)
{
sum += q[im + k] * column[k];
}
sol[jm + i] = sum;
}); });
} }
@ -2016,7 +2023,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
// Compute the transformation for the l-th column and // Compute the transformation for the l-th column and
// place the l-th diagonal in vector s[l]. // place the l-th diagonal in vector s[l].
var l1 = l; var l1 = l;
stemp[l] = (float)Math.Sqrt(CommonParallel.Aggregate(l, rowsA, i1 => (a[(l1 * rowsA) + i1] * a[(l1 * rowsA) + i1])));
var sum = 0.0f;
for (var i1 = l; i1 < rowsA; i1++)
{
sum += a[(l1 * rowsA) + i1] * a[(l1 * rowsA) + i1];
}
stemp[l] = (float)Math.Sqrt(sum);
if (stemp[l] != 0.0) if (stemp[l] != 0.0)
{ {

8
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -538,7 +538,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => Data[(i * RowCount) + i]); var sum = Complex.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += Data[(i * RowCount) + i];
}
return sum;
} }
} }
} }

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

@ -850,10 +850,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override Complex Sum() public override Complex Sum()
{ {
return CommonParallel.Aggregate( var sum = Complex.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => Data[i]); {
sum += Data[i];
}
return sum;
} }
/// <summary> /// <summary>
@ -862,10 +866,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex SumMagnitudes() public override Complex SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = Complex.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => Data[i].Magnitude); {
sum += Data[i].Magnitude;
}
return sum;
} }
/// <summary> /// <summary>
@ -1103,10 +1111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (1.0 == p) if (1.0 == p)
{ {
return CommonParallel.Aggregate( return SumMagnitudes();
0,
Count,
index => Data[index].Magnitude);
} }
if (2.0 == p) if (2.0 == p)
@ -1123,10 +1128,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Math.Max); Math.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var i = 0; i < Count; i++)
index => Math.Pow(Data[index].Magnitude, p)); {
sum += Math.Pow(Data[i].Magnitude, p);
}
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }

13
src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs

@ -34,7 +34,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
using System.Numerics; using System.Numerics;
using Generic; using Generic;
using Properties; using Properties;
using Threading;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para> /// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para>
@ -101,9 +100,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
for (var j = k + 1; j < columnsQ; j++) for (var j = k + 1; j < columnsQ; j++)
{ {
int k1 = k; var k1 = k;
int j1 = j; var j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index].Conjugate() * q[(j1 * rowsQ) + index]);
var dot = Complex.Zero;
for (var index = 0; index < rowsQ; index++)
{
dot += q[(k1 * rowsQ) + index].Conjugate() * q[(j1 * rowsQ) + index];
}
r[(j * columnsQ) + k] = dot; r[(j * columnsQ) + k] = dot;
for (var i = 0; i < rowsQ; i++) for (var i = 0; i < rowsQ; i++)
{ {

8
src/Numerics/LinearAlgebra/Complex/Matrix.cs

@ -339,7 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => At(i, i)); var sum = Complex.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
} }
/// <summary> /// <summary>

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

@ -1317,10 +1317,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{ {
// Multiply row of matrix A on column of matrix B // Multiply row of matrix A on column of matrix B
other.Column(column, columnVector); other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex, var sum = Complex.Zero;
endIndex, for (var index = startIndex; index < endIndex; index++)
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]); {
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum); result.At(row, column, sum);
} }
} }
@ -1343,10 +1346,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
continue; continue;
} }
var sum = CommonParallel.Aggregate( var sum = Complex.Zero;
startIndex, for (var index = startIndex; index < endIndex; index++)
endIndex, {
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]); sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum; result[row] = sum;
} }
} }
@ -1389,15 +1394,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
continue; continue;
} }
var i1 = i; var sum = Complex.Zero;
var sum = CommonParallel.Aggregate( for (var index = startIndexOther; index < endIndexOther; index++)
startIndexOther, {
endIndexOther, var ind = FindItem(i, otherSparse._columnIndices[index]);
index => if (ind >= 0)
{ {
var ind = FindItem(i1, otherSparse._columnIndices[index]); sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0; }
}); }
resultSparse.SetValueAt(i, j, sum + result.At(i, j)); resultSparse.SetValueAt(i, j, sum + result.At(i, j));
} }

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

@ -970,10 +970,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, _nonZeroValues[index].Magnitude), Math.Max); return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, _nonZeroValues[index].Magnitude), Math.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0, for (var index = 0; index < NonZerosCount; index++)
NonZerosCount, {
index => Math.Pow(_nonZeroValues[index].Magnitude, p)); sum += Math.Pow(_nonZeroValues[index].Magnitude, p);
}
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }

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

@ -185,10 +185,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </returns> /// </returns>
protected override Complex DoDotProduct(Vector<Complex> other) protected override Complex DoDotProduct(Vector<Complex> other)
{ {
return CommonParallel.Aggregate( var dot = Complex.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i) * other.At(i)); {
dot += At(i) * other.At(i);
}
return dot;
} }
/// <summary> /// <summary>
@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override Complex Sum() public override Complex Sum()
{ {
return CommonParallel.Aggregate( var sum = Complex.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i)); {
sum += At(i);
}
return sum;
} }
/// <summary> /// <summary>
@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex SumMagnitudes() public override Complex SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = Complex.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i).Magnitude); {
sum += At(i).Magnitude;
}
return sum;
} }
/// <summary> /// <summary>
@ -300,10 +312,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Math.Max); Math.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
index => Math.Pow(At(index).Magnitude, p)); {
sum += Math.Pow(At(index).Magnitude, p);
}
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }

8
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -538,7 +538,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => Data[(i * RowCount) + i]); var sum = Complex32.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += Data[(i * RowCount) + i];
}
return sum;
} }
} }
} }

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

@ -851,10 +851,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override Complex32 Sum() public override Complex32 Sum()
{ {
return CommonParallel.Aggregate( var sum = Complex32.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => Data[i]); {
sum += Data[i];
}
return sum;
} }
/// <summary> /// <summary>
@ -863,10 +867,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex32 SumMagnitudes() public override Complex32 SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = Complex32.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => Data[i].Magnitude); {
sum += Data[i].Magnitude;
}
return sum;
} }
/// <summary> /// <summary>
@ -1156,10 +1164,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (1.0 == p) if (1.0 == p)
{ {
return CommonParallel.Aggregate( return SumMagnitudes();
0,
Count,
index => Data[index].Magnitude);
} }
if (2.0 == p) if (2.0 == p)
@ -1176,10 +1181,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
Common.Max); Common.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var i = 0; i < Count; i++)
index => Math.Pow(Data[index].Magnitude, p)); {
sum += Math.Pow(Data[i].Magnitude, p);
}
return (float)Math.Pow(sum, 1.0 / p); return (float)Math.Pow(sum, 1.0 / p);
} }

13
src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs

@ -34,7 +34,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
using Generic; using Generic;
using Numerics; using Numerics;
using Properties; using Properties;
using Threading;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para> /// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para>
@ -101,9 +100,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
for (var j = k + 1; j < columnsQ; j++) for (var j = k + 1; j < columnsQ; j++)
{ {
int k1 = k; var k1 = k;
int j1 = j; var j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index].Conjugate() * q[(j1 * rowsQ) + index]);
var dot = Complex32.Zero;
for (var index = 0; index < rowsQ; index++)
{
dot += q[(k1 * rowsQ) + index].Conjugate() * q[(j1 * rowsQ) + index];
}
r[(j * columnsQ) + k] = dot; r[(j * columnsQ) + k] = dot;
for (var i = 0; i < rowsQ; i++) for (var i = 0; i < rowsQ; i++)
{ {

8
src/Numerics/LinearAlgebra/Complex32/Matrix.cs

@ -339,7 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => At(i, i)); var sum = Complex32.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
} }
/// <summary> /// <summary>

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

@ -1313,10 +1313,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
// Multiply row of matrix A on column of matrix B // Multiply row of matrix A on column of matrix B
other.Column(column, columnVector); other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex, var sum = Complex32.Zero;
endIndex, for (var index = startIndex; index < endIndex; index++)
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]); {
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum); result.At(row, column, sum);
} }
} }
@ -1339,10 +1342,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
continue; continue;
} }
var sum = CommonParallel.Aggregate( var sum = Complex32.Zero;
startIndex, for (var index = startIndex; index < endIndex; index++)
endIndex, {
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]); sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum; result[row] = sum;
} }
} }
@ -1385,15 +1390,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
continue; continue;
} }
var i1 = i; var sum = Complex32.Zero;
var sum = CommonParallel.Aggregate( for (var index = startIndexOther; index < endIndexOther; index++)
startIndexOther, {
endIndexOther, var ind = FindItem(i, otherSparse._columnIndices[index]);
index => if (ind >= 0)
{ {
var ind = FindItem(i1, otherSparse._columnIndices[index]); sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0f; }
}); }
resultSparse.SetValueAt(i, j, sum + result.At(i, j)); resultSparse.SetValueAt(i, j, sum + result.At(i, j));
} }

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

@ -1000,10 +1000,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, _nonZeroValues[index].Magnitude), Common.Max); return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, _nonZeroValues[index].Magnitude), Common.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0, for (var index = 0; index < NonZerosCount; index++)
NonZerosCount, {
index => Math.Pow(_nonZeroValues[index].Magnitude, p)); sum += Math.Pow(_nonZeroValues[index].Magnitude, p);
}
return (float)Math.Pow(sum, 1.0 / p); return (float)Math.Pow(sum, 1.0 / p);
} }

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

@ -185,10 +185,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </returns> /// </returns>
protected override Complex32 DoDotProduct(Vector<Complex32> other) protected override Complex32 DoDotProduct(Vector<Complex32> other)
{ {
return CommonParallel.Aggregate( var dot = Complex32.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i) * other.At(i)); {
dot += At(i) * other.At(i);
}
return dot;
} }
/// <summary> /// <summary>
@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override Complex32 Sum() public override Complex32 Sum()
{ {
return CommonParallel.Aggregate( var sum = Complex32.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i)); {
sum += At(i);
}
return sum;
} }
/// <summary> /// <summary>
@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex32 SumMagnitudes() public override Complex32 SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = Complex32.Zero;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i).Magnitude); {
sum += At(i).Magnitude;
}
return sum;
} }
/// <summary> /// <summary>
@ -300,10 +312,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
Common.Max); Common.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
index => Math.Pow(At(index).Magnitude, p)); {
sum += Math.Pow(At(index).Magnitude, p);
}
return (float)Math.Pow(sum, 1.0 / p); return (float)Math.Pow(sum, 1.0 / p);
} }

8
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -527,7 +527,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => Data[(i * RowCount) + i]); var sum = 0.0;
for (var i = 0; i < RowCount; i++)
{
sum += Data[(i * RowCount) + i];
}
return sum;
} }
} }
} }

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

@ -942,10 +942,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override double Sum() public override double Sum()
{ {
return CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
i => Data[i]); {
sum += Data[index];
}
return sum;
} }
/// <summary> /// <summary>
@ -954,10 +958,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override double SumMagnitudes() public override double SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
i => Math.Abs(Data[i])); {
sum += Math.Abs(Data[index]);
}
return sum;
} }
/// <summary> /// <summary>
@ -1197,10 +1205,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (1.0 == p) if (1.0 == p)
{ {
return CommonParallel.Aggregate( return SumMagnitudes();
0,
Count,
index => Math.Abs(Data[index]));
} }
if (2.0 == p) if (2.0 == p)
@ -1217,10 +1222,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Math.Max); Math.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0, for (var index = 0; index < Count; index++)
Count, {
index => Math.Pow(Math.Abs(Data[index]), p)); sum += Math.Pow(Math.Abs(Data[index]), p);
}
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }

13
src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs

@ -33,7 +33,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
using System; using System;
using Generic; using Generic;
using Properties; using Properties;
using Threading;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para> /// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para>
@ -100,9 +99,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
for (var j = k + 1; j < columnsQ; j++) for (var j = k + 1; j < columnsQ; j++)
{ {
int k1 = k; var k1 = k;
int j1 = j; var j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index] * q[(j1 * rowsQ) + index]);
var dot = 0.0;
for (var index = 0; index < rowsQ; index++)
{
dot += q[(k1 * rowsQ) + index] * q[(j1 * rowsQ) + index];
}
r[(j * columnsQ) + k] = dot; r[(j * columnsQ) + k] = dot;
for (var i = 0; i < rowsQ; i++) for (var i = 0; i < rowsQ; i++)
{ {

8
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -329,7 +329,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => At(i, i)); var sum = 0.0;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
} }
/// <summary> /// <summary>

37
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -1329,10 +1329,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
// Multiply row of matrix A on column of matrix B // Multiply row of matrix A on column of matrix B
other.Column(column, columnVector); other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex, var sum = 0.0;
endIndex, for (var index = startIndex; index < endIndex; index++)
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]); {
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum); result.At(row, column, sum);
} }
} }
@ -1355,10 +1358,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
continue; continue;
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
startIndex, for (var index = startIndex; index < endIndex; index++)
endIndex, {
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]); sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum; result[row] = sum;
} }
} }
@ -1401,15 +1406,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
continue; continue;
} }
var i1 = i; var sum = 0.0;
var sum = CommonParallel.Aggregate( for (var index = startIndexOther; index < endIndexOther; index++)
startIndexOther, {
endIndexOther, var ind = FindItem(i, otherSparse._columnIndices[index]);
index => if (ind >= 0)
{ {
var ind = FindItem(i1, otherSparse._columnIndices[index]); sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0; }
}); }
resultSparse.SetValueAt(i, j, sum + result.At(i, j)); resultSparse.SetValueAt(i, j, sum + result.At(i, j));
} }

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

@ -991,10 +991,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, Math.Abs(_nonZeroValues[index])), Math.Max); return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, Math.Abs(_nonZeroValues[index])), Math.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0, for (var index = 0; index < NonZerosCount; index++)
NonZerosCount, {
index => Math.Pow(Math.Abs(_nonZeroValues[index]), p)); sum += Math.Pow(Math.Abs(_nonZeroValues[index]), p);
}
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }

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

@ -184,10 +184,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
protected override double DoDotProduct(Vector<double> other) protected override double DoDotProduct(Vector<double> other)
{ {
return CommonParallel.Aggregate( var dot = 0.0;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i) * other.At(i)); {
dot += At(i) * other.At(i);
}
return dot;
} }
/// <summary> /// <summary>
@ -256,10 +260,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override double Sum() public override double Sum()
{ {
return CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i)); {
sum += At(i);
}
return sum;
} }
/// <summary> /// <summary>
@ -268,10 +276,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override double SumMagnitudes() public override double SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var i = 0; i < Count; i++)
i => Math.Abs(At(i))); {
sum += Math.Abs(At(i));
}
return sum;
} }
/// <summary> /// <summary>
@ -299,10 +311,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Math.Max); Math.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
index => Math.Pow(Math.Abs(At(index)), p)); {
sum += Math.Pow(Math.Abs(At(index)), p);
}
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }

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

@ -760,10 +760,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
var matrix = u.CreateMatrix(u.Count, v.Count); var matrix = u.CreateMatrix(u.Count, v.Count);
CommonParallel.For(
0, for (var i = 0; i < u.Count; i++)
u.Count, {
i => matrix.SetRow(i, v.Multiply(u[i]))); matrix.SetRow(i, v.Multiply(u[i]));
}
return matrix; return matrix;
} }

8
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -527,7 +527,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => Data[(i * RowCount) + i]); var sum = 0.0f;
for (var i = 0; i < RowCount; i++)
{
sum += Data[(i * RowCount) + i];
}
return sum;
} }
} }
} }

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

@ -942,10 +942,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override float Sum() public override float Sum()
{ {
return CommonParallel.Aggregate( var sum = 0.0f;
0,
Count, for (var i = 0; i < Count; i++)
i => Data[i]); {
sum += Data[i];
}
return sum;
} }
/// <summary> /// <summary>
@ -954,10 +958,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override float SumMagnitudes() public override float SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = 0.0f;
0,
Count, for (var i = 0; i < Count; i++)
i => Math.Abs(Data[i])); {
sum += Math.Abs(Data[i]);
}
return sum;
} }
/// <summary> /// <summary>
@ -1197,10 +1205,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (1.0 == p) if (1.0 == p)
{ {
return CommonParallel.Aggregate( return SumMagnitudes();
0,
Count,
index => Math.Abs(Data[index]));
} }
if (2.0 == p) if (2.0 == p)
@ -1217,10 +1222,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
Common.Max); Common.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
index => Math.Pow(Math.Abs(Data[index]), p)); {
sum += Math.Pow(Math.Abs(Data[index]), p);
}
return (float)Math.Pow(sum, 1.0 / p); return (float)Math.Pow(sum, 1.0 / p);
} }

13
src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs

@ -33,7 +33,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
using System; using System;
using Generic; using Generic;
using Properties; using Properties;
using Threading;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para> /// <para>A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization.</para>
@ -100,9 +99,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
for (var j = k + 1; j < columnsQ; j++) for (var j = k + 1; j < columnsQ; j++)
{ {
int k1 = k; var k1 = k;
int j1 = j; var j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index] * q[(j1 * rowsQ) + index]);
var dot = 0.0f;
for (var index = 0; index < rowsQ; index++)
{
dot += q[(k1 * rowsQ) + index] * q[(j1 * rowsQ) + index];
}
r[(j * columnsQ) + k] = dot; r[(j * columnsQ) + k] = dot;
for (var i = 0; i < rowsQ; i++) for (var i = 0; i < rowsQ; i++)
{ {

8
src/Numerics/LinearAlgebra/Single/Matrix.cs

@ -329,7 +329,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, RowCount, i => At(i, i)); var sum = 0.0f;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
} }
/// <summary> /// <summary>

38
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -1296,7 +1296,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result) protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{ {
result.Clear(); result.Clear();
var columnVector = new DenseVector(other.RowCount); var columnVector = new DenseVector(other.RowCount);
for (var row = 0; row < RowCount; row++) for (var row = 0; row < RowCount; row++)
@ -1313,10 +1312,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
// Multiply row of matrix A on column of matrix B // Multiply row of matrix A on column of matrix B
other.Column(column, columnVector); other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex, var sum = 0.0f;
endIndex, for (var index = startIndex; index < endIndex; index++)
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]); {
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum); result.At(row, column, sum);
} }
} }
@ -1339,10 +1341,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
continue; continue;
} }
var sum = CommonParallel.Aggregate( var sum = 0.0f;
startIndex, for (var index = startIndex; index < endIndex; index++)
endIndex, {
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]); sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum; result[row] = sum;
} }
} }
@ -1385,15 +1389,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single
continue; continue;
} }
var i1 = i; var sum = 0.0f;
var sum = CommonParallel.Aggregate( for (var index = startIndexOther; index < endIndexOther; index++)
startIndexOther, {
endIndexOther, var ind = FindItem(i, otherSparse._columnIndices[index]);
index => if (ind >= 0)
{ {
var ind = FindItem(i1, otherSparse._columnIndices[index]); sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0f; }
}); }
resultSparse.SetValueAt(i, j, sum + result.At(i, j)); resultSparse.SetValueAt(i, j, sum + result.At(i, j));
} }

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

@ -998,10 +998,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, Math.Abs(_nonZeroValues[index])), Common.Max); return CommonParallel.Select(0, NonZerosCount, (index, localData) => Math.Max(localData, Math.Abs(_nonZeroValues[index])), Common.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0, for (var index = 0; index < NonZerosCount; index++)
NonZerosCount, {
index => Math.Pow(Math.Abs(_nonZeroValues[index]), p)); sum += Math.Pow(Math.Abs(_nonZeroValues[index]), p);
}
return (float)Math.Pow(sum, 1.0 / p); return (float)Math.Pow(sum, 1.0 / p);
} }

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

@ -185,10 +185,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </returns> /// </returns>
protected override float DoDotProduct(Vector<float> other) protected override float DoDotProduct(Vector<float> other)
{ {
return CommonParallel.Aggregate( var dot = 0.0f;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i) * other.At(i)); {
dot += At(i) * other.At(i);
}
return dot;
} }
/// <summary> /// <summary>
@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the vector's elements.</returns> /// <returns>The sum of the vector's elements.</returns>
public override float Sum() public override float Sum()
{ {
return CommonParallel.Aggregate( var sum = 0.0f;
0,
Count, for (var i = 0; i < Count; i++)
i => At(i)); {
sum += At(i);
}
return sum;
} }
/// <summary> /// <summary>
@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the absolute value of the vector's elements.</returns> /// <returns>The sum of the absolute value of the vector's elements.</returns>
public override float SumMagnitudes() public override float SumMagnitudes()
{ {
return CommonParallel.Aggregate( var sum = 0.0f;
0,
Count, for (var i = 0; i < Count; i++)
i => Math.Abs(At(i))); {
sum += Math.Abs(At(i));
}
return sum;
} }
/// <summary> /// <summary>
@ -300,10 +312,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
Common.Max); Common.Max);
} }
var sum = CommonParallel.Aggregate( var sum = 0.0;
0,
Count, for (var index = 0; index < Count; index++)
index => Math.Pow(Math.Abs(At(index)), p)); {
sum += Math.Pow(Math.Abs(At(index)), p);
}
return (float)Math.Pow(sum, 1.0 / p); return (float)Math.Pow(sum, 1.0 / p);
} }

4
src/Numerics/Threading/CommonParallel.cs

@ -79,7 +79,7 @@ namespace MathNet.Numerics.Threading
#endif #endif
} }
/// <summary> /* /// <summary>
/// Aggregates a function over a loop. /// Aggregates a function over a loop.
/// </summary> /// </summary>
/// <param name="fromInclusive">Starting index of the loop.</param> /// <param name="fromInclusive">Starting index of the loop.</param>
@ -321,7 +321,7 @@ namespace MathNet.Numerics.Threading
} }
#endif #endif
return sum; return sum;
} }*/
/// <summary> /// <summary>
/// Executes each of the provided actions inside a discrete, asynchronous task. /// Executes each of the provided actions inside a discrete, asynchronous task.

6
src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs

@ -1721,12 +1721,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
public virtual void CanComputeL2Norm() public virtual void CanComputeL2Norm()
{ {
var matrix = TestMatrices["Square3x3"]; var matrix = TestMatrices["Square3x3"];
AssertHelpers.AlmostEqual(10.391347375312632f, matrix.L2Norm(), 7); AssertHelpers.AlmostEqual(10.391347375312632f, matrix.L2Norm(), 6);
matrix = TestMatrices["Wide2x3"]; matrix = TestMatrices["Wide2x3"];
AssertHelpers.AlmostEqual(4.7540849434107635f, matrix.L2Norm(), 7); AssertHelpers.AlmostEqual(4.7540849434107635f, matrix.L2Norm(), 6);
matrix = TestMatrices["Tall3x2"]; matrix = TestMatrices["Tall3x2"];
AssertHelpers.AlmostEqual(7.182727033856683f, matrix.L2Norm(), 7); AssertHelpers.AlmostEqual(7.182727033856683f, matrix.L2Norm(), 5);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save