diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs index 9dd23de1..a8246018 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs +++ b/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; } /// @@ -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; }); } diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs index 2acd6d20..40ba3cc6 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs +++ b/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; }); } diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs index 7b81141f..4201a22d 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs +++ b/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; } /// @@ -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) { diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs index b9a62299..9358cc19 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs +++ b/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) { diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index ec571014..c8bf954b 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/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; } } } diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 630c778c..44d90b8c 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -850,10 +850,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The sum of the vector's elements. 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; } /// @@ -862,10 +866,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs b/src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs index eeee8398..0e850de6 100644 --- a/src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs +++ b/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; /// /// A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization. @@ -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++) { diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index 1e6b4679..657daa2e 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/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; } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 454b474f..ded2c932 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/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)); } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 4b740ff1..380c8fb5 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/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); } diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index 39c3ca9f..14ba0aaa 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -185,10 +185,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// protected override Complex DoDotProduct(Vector 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; } /// @@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The sum of the vector's elements. 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; } /// @@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 67b0b02c..1332cc55 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/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; } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 158f6888..07873273 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -851,10 +851,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The sum of the vector's elements. 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; } /// @@ -863,10 +867,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs b/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs index 5f86ddbb..4df3e34e 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs +++ b/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; /// /// A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization. @@ -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++) { diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index 8ef9d8d8..833f12b3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/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; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 28f73412..94b00ef3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/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)); } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 40c5ff7d..a5ede8b9 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/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); } diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index 9db2acf2..32769f3b 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -185,10 +185,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// protected override Complex32 DoDotProduct(Vector 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; } /// @@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The sum of the vector's elements. 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; } /// @@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 19b3dd29..73332c9c 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/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; } } } diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index d3003ff0..df813470 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -942,10 +942,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The sum of the vector's elements. 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; } /// @@ -954,10 +958,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs b/src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs index e749680f..140bd8e8 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs +++ b/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; /// /// A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization. @@ -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++) { diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 76e5ab04..65a0aace 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/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; } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index c1aa5299..a9bc40ff 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/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)); } diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index f2def50e..8f97c05c 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/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); } diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index dff69988..8dd27dd9 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -184,10 +184,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// protected override double DoDotProduct(Vector 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; } /// @@ -256,10 +260,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The sum of the vector's elements. 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; } /// @@ -268,10 +276,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index d5d70e0a..d3723990 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/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; } diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 55a4b3eb..0b4d0027 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/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; } } } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index b3a8f7fc..ad61340d 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -942,10 +942,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The sum of the vector's elements. 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; } /// @@ -954,10 +958,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs b/src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs index 4b4fb787..5a02ae42 100644 --- a/src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs +++ b/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; /// /// A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization. @@ -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++) { diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index ab42692d..998046c5 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/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; } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 9efb5617..cfaedaea 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -1296,7 +1296,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The result of the multiplication. protected override void DoMultiply(Matrix other, Matrix 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)); } diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 0ccfc7ab..ceefd77b 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/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); } diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index e3ab2be0..e191cb18 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -185,10 +185,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// protected override float DoDotProduct(Vector 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; } /// @@ -257,10 +261,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The sum of the vector's elements. 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; } /// @@ -269,10 +277,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The sum of the absolute value of the vector's elements. 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; } /// @@ -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); } diff --git a/src/Numerics/Threading/CommonParallel.cs b/src/Numerics/Threading/CommonParallel.cs index dd03b6aa..ae29598d 100644 --- a/src/Numerics/Threading/CommonParallel.cs +++ b/src/Numerics/Threading/CommonParallel.cs @@ -79,7 +79,7 @@ namespace MathNet.Numerics.Threading #endif } - /// +/* /// /// Aggregates a function over a loop. /// /// Starting index of the loop. @@ -321,7 +321,7 @@ namespace MathNet.Numerics.Threading } #endif return sum; - } + }*/ /// /// Executes each of the provided actions inside a discrete, asynchronous task. diff --git a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs index 5611fc6b..fee7e769 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs +++ b/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); } ///