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);
}
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>
@ -1839,7 +1845,13 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i =>
{
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 =>
{
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);
}
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>
@ -1820,7 +1827,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i =>
{
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
// 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)
{

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

@ -1828,7 +1828,14 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
i =>
{
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
// place the l-th diagonal in vector s[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)
{

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

@ -538,7 +538,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
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>
public override Complex Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => Data[i]);
var sum = Complex.Zero;
for (var i = 0; i < Count; i++)
{
sum += Data[i];
}
return sum;
}
/// <summary>
@ -862,10 +866,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => Data[i].Magnitude);
var sum = Complex.Zero;
for (var i = 0; i < Count; i++)
{
sum += Data[i].Magnitude;
}
return sum;
}
/// <summary>
@ -1103,10 +1111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (1.0 == p)
{
return CommonParallel.Aggregate(
0,
Count,
index => Data[index].Magnitude);
return SumMagnitudes();
}
if (2.0 == p)
@ -1123,10 +1128,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Math.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Data[index].Magnitude, p));
var sum = 0.0;
for (var i = 0; i < Count; i++)
{
sum += Math.Pow(Data[i].Magnitude, 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 Generic;
using Properties;
using Threading;
/// <summary>
/// <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++)
{
int k1 = k;
int j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index].Conjugate() * q[(j1 * rowsQ) + index]);
var k1 = k;
var j1 = j;
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;
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);
}
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>

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
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
var sum = Complex.Zero;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum);
}
}
@ -1343,10 +1346,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
continue;
}
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]);
var sum = Complex.Zero;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum;
}
}
@ -1389,15 +1394,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
continue;
}
var i1 = i;
var sum = CommonParallel.Aggregate(
startIndexOther,
endIndexOther,
index =>
var sum = Complex.Zero;
for (var index = startIndexOther; index < endIndexOther; index++)
{
var ind = FindItem(i, otherSparse._columnIndices[index]);
if (ind >= 0)
{
var ind = FindItem(i1, otherSparse._columnIndices[index]);
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0;
});
sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
}
}
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);
}
var sum = CommonParallel.Aggregate(
0,
NonZerosCount,
index => Math.Pow(_nonZeroValues[index].Magnitude, p));
var sum = 0.0;
for (var index = 0; index < NonZerosCount; index++)
{
sum += Math.Pow(_nonZeroValues[index].Magnitude, 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>
protected override Complex DoDotProduct(Vector<Complex> other)
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i) * other.At(i));
var dot = Complex.Zero;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}
/// <summary>
@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the vector's elements.</returns>
public override Complex Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i));
var sum = Complex.Zero;
for (var i = 0; i < Count; i++)
{
sum += At(i);
}
return sum;
}
/// <summary>
@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i).Magnitude);
var sum = Complex.Zero;
for (var i = 0; i < Count; i++)
{
sum += At(i).Magnitude;
}
return sum;
}
/// <summary>
@ -300,10 +312,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
Math.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(At(index).Magnitude, p));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(At(index).Magnitude, 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);
}
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>
public override Complex32 Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => Data[i]);
var sum = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
sum += Data[i];
}
return sum;
}
/// <summary>
@ -863,10 +867,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex32 SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => Data[i].Magnitude);
var sum = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
sum += Data[i].Magnitude;
}
return sum;
}
/// <summary>
@ -1156,10 +1164,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (1.0 == p)
{
return CommonParallel.Aggregate(
0,
Count,
index => Data[index].Magnitude);
return SumMagnitudes();
}
if (2.0 == p)
@ -1176,10 +1181,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
Common.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Data[index].Magnitude, p));
var sum = 0.0;
for (var i = 0; i < Count; i++)
{
sum += Math.Pow(Data[i].Magnitude, 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 Numerics;
using Properties;
using Threading;
/// <summary>
/// <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++)
{
int k1 = k;
int j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index].Conjugate() * q[(j1 * rowsQ) + index]);
var k1 = k;
var j1 = j;
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;
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);
}
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>

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
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
var sum = Complex32.Zero;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum);
}
}
@ -1339,10 +1342,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
continue;
}
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]);
var sum = Complex32.Zero;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum;
}
}
@ -1385,15 +1390,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
continue;
}
var i1 = i;
var sum = CommonParallel.Aggregate(
startIndexOther,
endIndexOther,
index =>
var sum = Complex32.Zero;
for (var index = startIndexOther; index < endIndexOther; index++)
{
var ind = FindItem(i, otherSparse._columnIndices[index]);
if (ind >= 0)
{
var ind = FindItem(i1, otherSparse._columnIndices[index]);
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0f;
});
sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
}
}
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);
}
var sum = CommonParallel.Aggregate(
0,
NonZerosCount,
index => Math.Pow(_nonZeroValues[index].Magnitude, p));
var sum = 0.0;
for (var index = 0; index < NonZerosCount; index++)
{
sum += Math.Pow(_nonZeroValues[index].Magnitude, 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>
protected override Complex32 DoDotProduct(Vector<Complex32> other)
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i) * other.At(i));
var dot = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}
/// <summary>
@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the vector's elements.</returns>
public override Complex32 Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i));
var sum = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
sum += At(i);
}
return sum;
}
/// <summary>
@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override Complex32 SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i).Magnitude);
var sum = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
sum += At(i).Magnitude;
}
return sum;
}
/// <summary>
@ -300,10 +312,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
Common.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(At(index).Magnitude, p));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(At(index).Magnitude, 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);
}
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>
public override double Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => Data[i]);
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Data[index];
}
return sum;
}
/// <summary>
@ -954,10 +958,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override double SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => Math.Abs(Data[i]));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Abs(Data[index]);
}
return sum;
}
/// <summary>
@ -1197,10 +1205,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (1.0 == p)
{
return CommonParallel.Aggregate(
0,
Count,
index => Math.Abs(Data[index]));
return SumMagnitudes();
}
if (2.0 == p)
@ -1217,10 +1222,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Math.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Math.Abs(Data[index]), p));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(Math.Abs(Data[index]), 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 Generic;
using Properties;
using Threading;
/// <summary>
/// <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++)
{
int k1 = k;
int j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index] * q[(j1 * rowsQ) + index]);
var k1 = k;
var j1 = j;
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;
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);
}
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>

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
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
var sum = 0.0;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum);
}
}
@ -1355,10 +1358,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
continue;
}
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]);
var sum = 0.0;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum;
}
}
@ -1401,15 +1406,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
continue;
}
var i1 = i;
var sum = CommonParallel.Aggregate(
startIndexOther,
endIndexOther,
index =>
var sum = 0.0;
for (var index = startIndexOther; index < endIndexOther; index++)
{
var ind = FindItem(i, otherSparse._columnIndices[index]);
if (ind >= 0)
{
var ind = FindItem(i1, otherSparse._columnIndices[index]);
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0;
});
sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
}
}
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);
}
var sum = CommonParallel.Aggregate(
0,
NonZerosCount,
index => Math.Pow(Math.Abs(_nonZeroValues[index]), p));
var sum = 0.0;
for (var index = 0; index < NonZerosCount; index++)
{
sum += Math.Pow(Math.Abs(_nonZeroValues[index]), 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>
protected override double DoDotProduct(Vector<double> other)
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i) * other.At(i));
var dot = 0.0;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}
/// <summary>
@ -256,10 +260,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the vector's elements.</returns>
public override double Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i));
var sum = 0.0;
for (var i = 0; i < Count; i++)
{
sum += At(i);
}
return sum;
}
/// <summary>
@ -268,10 +276,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override double SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => Math.Abs(At(i)));
var sum = 0.0;
for (var i = 0; i < Count; i++)
{
sum += Math.Abs(At(i));
}
return sum;
}
/// <summary>
@ -299,10 +311,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Math.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Math.Abs(At(index)), p));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(Math.Abs(At(index)), 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);
CommonParallel.For(
0,
u.Count,
i => matrix.SetRow(i, v.Multiply(u[i])));
for (var i = 0; i < u.Count; i++)
{
matrix.SetRow(i, v.Multiply(u[i]));
}
return matrix;
}

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

@ -527,7 +527,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
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>
public override float Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => Data[i]);
var sum = 0.0f;
for (var i = 0; i < Count; i++)
{
sum += Data[i];
}
return sum;
}
/// <summary>
@ -954,10 +958,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override float SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => Math.Abs(Data[i]));
var sum = 0.0f;
for (var i = 0; i < Count; i++)
{
sum += Math.Abs(Data[i]);
}
return sum;
}
/// <summary>
@ -1197,10 +1205,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (1.0 == p)
{
return CommonParallel.Aggregate(
0,
Count,
index => Math.Abs(Data[index]));
return SumMagnitudes();
}
if (2.0 == p)
@ -1217,10 +1222,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
Common.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Math.Abs(Data[index]), p));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(Math.Abs(Data[index]), 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 Generic;
using Properties;
using Threading;
/// <summary>
/// <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++)
{
int k1 = k;
int j1 = j;
var dot = CommonParallel.Aggregate(0, rowsQ, index => q[(k1 * rowsQ) + index] * q[(j1 * rowsQ) + index]);
var k1 = k;
var j1 = j;
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;
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);
}
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>

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>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{
result.Clear();
var columnVector = new DenseVector(other.RowCount);
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
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
var sum = 0.0f;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * columnVector[_columnIndices[index]];
}
result.At(row, column, sum);
}
}
@ -1339,10 +1341,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
continue;
}
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * rightSide[_columnIndices[index]]);
var sum = 0.0f;
for (var index = startIndex; index < endIndex; index++)
{
sum += _nonZeroValues[index] * rightSide[_columnIndices[index]];
}
result[row] = sum;
}
}
@ -1385,15 +1389,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single
continue;
}
var i1 = i;
var sum = CommonParallel.Aggregate(
startIndexOther,
endIndexOther,
index =>
var sum = 0.0f;
for (var index = startIndexOther; index < endIndexOther; index++)
{
var ind = FindItem(i, otherSparse._columnIndices[index]);
if (ind >= 0)
{
var ind = FindItem(i1, otherSparse._columnIndices[index]);
return ind >= 0 ? otherSparse._nonZeroValues[index] * _nonZeroValues[ind] : 0.0f;
});
sum += otherSparse._nonZeroValues[index] * _nonZeroValues[ind];
}
}
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);
}
var sum = CommonParallel.Aggregate(
0,
NonZerosCount,
index => Math.Pow(Math.Abs(_nonZeroValues[index]), p));
var sum = 0.0;
for (var index = 0; index < NonZerosCount; index++)
{
sum += Math.Pow(Math.Abs(_nonZeroValues[index]), 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>
protected override float DoDotProduct(Vector<float> other)
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i) * other.At(i));
var dot = 0.0f;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}
/// <summary>
@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the vector's elements.</returns>
public override float Sum()
{
return CommonParallel.Aggregate(
0,
Count,
i => At(i));
var sum = 0.0f;
for (var i = 0; i < Count; i++)
{
sum += At(i);
}
return sum;
}
/// <summary>
@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override float SumMagnitudes()
{
return CommonParallel.Aggregate(
0,
Count,
i => Math.Abs(At(i)));
var sum = 0.0f;
for (var i = 0; i < Count; i++)
{
sum += Math.Abs(At(i));
}
return sum;
}
/// <summary>
@ -300,10 +312,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
Common.Max);
}
var sum = CommonParallel.Aggregate(
0,
Count,
index => Math.Pow(Math.Abs(At(index)), p));
var sum = 0.0;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(Math.Abs(At(index)), p);
}
return (float)Math.Pow(sum, 1.0 / p);
}

4
src/Numerics/Threading/CommonParallel.cs

@ -79,7 +79,7 @@ namespace MathNet.Numerics.Threading
#endif
}
/// <summary>
/* /// <summary>
/// Aggregates a function over a loop.
/// </summary>
/// <param name="fromInclusive">Starting index of the loop.</param>
@ -321,7 +321,7 @@ namespace MathNet.Numerics.Threading
}
#endif
return sum;
}
}*/
/// <summary>
/// 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()
{
var matrix = TestMatrices["Square3x3"];
AssertHelpers.AlmostEqual(10.391347375312632f, matrix.L2Norm(), 7);
AssertHelpers.AlmostEqual(10.391347375312632f, matrix.L2Norm(), 6);
matrix = TestMatrices["Wide2x3"];
AssertHelpers.AlmostEqual(4.7540849434107635f, matrix.L2Norm(), 7);
AssertHelpers.AlmostEqual(4.7540849434107635f, matrix.L2Norm(), 6);
matrix = TestMatrices["Tall3x2"];
AssertHelpers.AlmostEqual(7.182727033856683f, matrix.L2Norm(), 7);
AssertHelpers.AlmostEqual(7.182727033856683f, matrix.L2Norm(), 5);
}
/// <summary>

Loading…
Cancel
Save