Browse Source

made parallel code serial code where it modifies a matrix, since we cannot guarantee subclasses are thread safe - in some cases making them thread safe hurts performance

la-knuth
Marcus Cuda 16 years ago
parent
commit
ecee6b3ae2
  1. 208
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  2. 208
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  3. 208
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  4. 34
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  5. 278
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  6. 208
      src/Numerics/LinearAlgebra/Single/Matrix.cs

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

@ -145,16 +145,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) + other.At(i, j));
}
});
result.At(i, j, At(i, j) + other.At(i, j));
}
}
}
/// <summary>
@ -166,16 +163,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - other.At(i, j));
}
});
result.At(i, j, At(i, j) - other.At(i, j));
}
}
}
/// <summary>
@ -185,16 +179,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(Complex scalar, Matrix<Complex> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) * scalar);
}
});
result.At(i, j, At(i, j) * scalar);
}
}
}
/// <summary>
@ -204,19 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{
CommonParallel.For(
0,
RowCount,
i =>
{
var s = new Complex();
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
});
for (var i = 0; i < RowCount; i++)
{
var s = Complex.Zero;
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
}
}
/// <summary>
@ -226,19 +214,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<Complex> leftSide, Vector<Complex> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
var s = Complex.Zero;
for (var i = 0; i != leftSide.Count; i++)
{
var s = new Complex();
for (var i = 0; i != leftSide.Count; i++)
{
s += leftSide[i] * At(i, j);
}
s += leftSide[i] * At(i, j);
}
result[j] = s;
});
result[j] = s;
}
}
/// <summary>
@ -248,22 +233,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i != other.ColumnCount; i++)
{
for (var i = 0; i != other.ColumnCount; i++)
var s = Complex.Zero;
for (var l = 0; l < ColumnCount; l++)
{
var s = new Complex();
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
s += At(j, l) * other.At(l, i);
}
});
result.At(j, i, s);
}
}
}
/// <summary>
@ -283,22 +265,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
var s = Complex.Zero;
for (var l = 0; l < ColumnCount; l++)
{
var s = new Complex();
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
s += At(i, l) * other.At(j, l);
}
});
result.At(i, j, s);
}
}
}
/// <summary>
@ -307,16 +286,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j != ColumnCount; j++)
{
for (var j = 0; j != ColumnCount; j++)
{
result[i, j] = -At(i, j);
}
});
result[i, j] = -At(i, j);
}
}
}
/// <summary>
@ -326,16 +302,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) * other.At(i, j));
}
});
result.At(i, j, At(i, j) * other.At(i, j));
}
}
}
/// <summary>
@ -345,16 +318,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<Complex> other, Matrix<Complex> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) / other.At(i, j));
}
});
result.At(i, j, At(i, j) / other.At(i, j));
}
}
}
/// <summary>
@ -379,16 +349,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex> matrix, IContinuousDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
});
matrix.At(i, j, distribution.Sample());
}
}
}
/// <summary>
@ -398,16 +365,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex> matrix, IDiscreteDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
});
matrix.At(i, j, distribution.Sample());
}
}
}
}
}

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

@ -145,16 +145,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) + other.At(i, j));
}
});
result.At(i, j, At(i, j) + other.At(i, j));
}
}
}
/// <summary>
@ -166,16 +163,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - other.At(i, j));
}
});
result.At(i, j, At(i, j) - other.At(i, j));
}
}
}
/// <summary>
@ -185,16 +179,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(Complex32 scalar, Matrix<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) * scalar);
}
});
result.At(i, j, At(i, j) * scalar);
}
}
}
/// <summary>
@ -204,19 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
i =>
{
var s = new Complex32();
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
});
for (var i = 0; i < RowCount; i++)
{
var s = Complex32.Zero;
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
}
}
/// <summary>
@ -236,19 +224,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<Complex32> leftSide, Vector<Complex32> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
var s = Complex32.Zero;
for (var i = 0; i != leftSide.Count; i++)
{
var s = new Complex32();
for (var i = 0; i != leftSide.Count; i++)
{
s += leftSide[i] * At(i, j);
}
s += leftSide[i] * At(i, j);
}
result[j] = s;
});
result[j] = s;
}
}
/// <summary>
@ -258,22 +243,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i != other.ColumnCount; i++)
{
for (var i = 0; i != other.ColumnCount; i++)
var s = Complex32.Zero;
for (var l = 0; l < ColumnCount; l++)
{
var s = new Complex32();
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
s += At(j, l) * other.At(l, i);
}
});
result.At(j, i, s);
}
}
}
/// <summary>
@ -283,22 +265,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
var s = Complex32.Zero;
for (var l = 0; l < ColumnCount; l++)
{
var s = new Complex32();
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
s += At(i, l) * other.At(j, l);
}
});
result.At(i, j, s);
}
}
}
/// <summary>
@ -307,16 +286,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j != ColumnCount; j++)
{
for (var j = 0; j != ColumnCount; j++)
{
result[i, j] = -At(i, j);
}
});
result[i, j] = -At(i, j);
}
}
}
/// <summary>
@ -326,16 +302,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) * other.At(i, j));
}
});
result.At(i, j, At(i, j) * other.At(i, j));
}
}
}
/// <summary>
@ -345,16 +318,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<Complex32> other, Matrix<Complex32> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) / other.At(i, j));
}
});
result.At(i, j, At(i, j) / other.At(i, j));
}
}
}
/// <summary>
@ -379,16 +349,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex32> matrix, IContinuousDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, Convert.ToSingle(distribution.Sample()));
}
});
matrix.At(i, j, Convert.ToSingle(distribution.Sample()));
}
}
}
/// <summary>
@ -398,16 +365,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex32> matrix, IDiscreteDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
});
matrix.At(i, j, distribution.Sample());
}
}
}
}
}

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

@ -135,18 +135,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) + other.At(i, j));
}
});
result.At(i, j, At(i, j) + other.At(i, j));
}
}
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>
@ -156,16 +153,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - other.At(i, j));
}
});
result.At(i, j, At(i, j) - other.At(i, j));
}
}
}
/// <summary>
@ -175,16 +169,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(double scalar, Matrix<double> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) * scalar);
}
});
result.At(i, j, At(i, j) * scalar);
}
}
}
/// <summary>
@ -194,19 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<double> rightSide, Vector<double> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
var s = 0.0;
for (var j = 0; j != ColumnCount; j++)
{
var s = 0.0;
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
});
s += At(i, j) * rightSide[j];
}
result[i] = s;
}
}
/// <summary>
@ -226,19 +214,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<double> leftSide, Vector<double> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
var s = 0.0;
for (var i = 0; i != leftSide.Count; i++)
{
var s = 0.0;
for (var i = 0; i != leftSide.Count; i++)
{
s += leftSide[i] * At(i, j);
}
s += leftSide[i] * At(i, j);
}
result[j] = s;
});
result[j] = s;
}
}
/// <summary>
@ -248,22 +233,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i != other.ColumnCount; i++)
{
for (var i = 0; i != other.ColumnCount; i++)
var s = 0.0;
for (var l = 0; l < ColumnCount; l++)
{
var s = 0.0;
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
s += At(j, l) * other.At(l, i);
}
});
result.At(j, i, s);
}
}
}
/// <summary>
@ -273,22 +255,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
var s = 0.0;
for (var l = 0; l < ColumnCount; l++)
{
var s = 0.0;
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
s += At(i, l) * other.At(j, l);
}
});
result.At(i, j, s);
}
}
}
/// <summary>
@ -297,16 +276,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j != ColumnCount; j++)
{
for (var j = 0; j != ColumnCount; j++)
{
result[i, j] = -At(i, j);
}
});
result[i, j] = -At(i, j);
}
}
}
/// <summary>
@ -316,16 +292,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<double> other, Matrix<double> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) * other.At(i, j));
}
});
result.At(i, j, At(i, j) * other.At(i, j));
}
}
}
/// <summary>
@ -335,16 +308,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<double> other, Matrix<double> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) / other.At(i, j));
}
});
result.At(i, j, At(i, j) / other.At(i, j));
}
}
}
/// <summary>
@ -369,16 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<double> matrix, IContinuousDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
});
matrix.At(i, j, distribution.Sample());
}
}
}
/// <summary>
@ -388,16 +355,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<double> matrix, IDiscreteDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
});
matrix.At(i, j, distribution.Sample());
}
}
}
}
}

34
src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs

@ -1096,16 +1096,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, At(i, j) * other);
}
});
result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, At(i, j) * other);
}
}
}
/// <summary>
@ -1122,10 +1119,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
}
var ret = Clone();
CommonParallel.For(
0,
ColumnCount,
i => ret.SetColumn(i, Column(i).Normalize(p)));
for (var index = 0; index < ColumnCount; index++)
{
ret.SetColumn(index, Column(index).Normalize(p));
}
return ret;
}
@ -1144,10 +1143,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
var ret = Clone();
CommonParallel.For(
0,
RowCount,
i => ret.SetRow(i, Row(i).Normalize(p)));
for (var index = 0; index < RowCount; index++)
{
ret.SetRow(index, Row(index).Normalize(p));
}
return ret;
}
}

278
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -470,16 +470,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> LowerTriangle()
{
var ret = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = j; i < RowCount; i++)
{
for (var i = j; i < RowCount; i++)
{
ret.At(i, j, At(i, j));
}
});
ret.At(i, j, At(i, j));
}
}
return ret;
}
@ -501,16 +499,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, i >= j ? At(i, j) : default(T));
}
});
result.At(i, j, i >= j ? At(i, j) : default(T));
}
}
}
/// <summary>
@ -520,10 +515,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> UpperTriangle()
{
var ret = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
{
for (var i = 0; i < RowCount; i++)
{
@ -532,7 +526,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
ret.At(i, j, At(i, j));
}
}
});
}
}
return ret;
}
@ -554,16 +550,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, i <= j ? At(i, j) : default(T));
}
});
result.At(i, j, i <= j ? At(i, j) : default(T));
}
}
}
/// <summary>
@ -619,16 +612,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
var result = CreateMatrix(rowLength, columnLength);
CommonParallel.For(
columnIndex,
colMax,
j =>
for (var j = columnIndex; j < colMax; j++)
{
for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++)
{
for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++)
{
result.At(ii, j - columnIndex, At(i, j));
}
});
result.At(ii, j - columnIndex, At(i, j));
}
}
return result;
}
@ -741,10 +732,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{
var min = Math.Min(RowCount, ColumnCount);
var diagonal = CreateVector(min);
CommonParallel.For(
0,
min,
i => { diagonal[i] = At(i, i); });
for (var i = 0; i < min; i++)
{
diagonal[i] = At(i, i);
}
return diagonal;
}
@ -756,19 +749,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> StrictlyLowerTriangle()
{
var result = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
if (i > j)
{
if (i > j)
{
result.At(i, j, At(i, j));
}
result.At(i, j, At(i, j));
}
});
}
}
return result;
}
@ -790,16 +782,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, i > j ? At(i, j) : default(T));
}
});
result.At(i, j, i > j ? At(i, j) : default(T));
}
}
}
/// <summary>
@ -810,19 +799,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> StrictlyUpperTriangle()
{
var result = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
if (i < j)
{
if (i < j)
{
result.At(i, j, At(i, j));
}
result.At(i, j, At(i, j));
}
});
}
}
return result;
}
@ -844,16 +832,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, i < j ? At(i, j) : default(T));
}
});
result.At(i, j, i < j ? At(i, j) : default(T));
}
}
}
/// <summary>
@ -928,10 +913,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
CommonParallel.For(
0,
RowCount,
i => At(i, columnIndex, column[i]));
for (var i = 0; i < RowCount; i++)
{
At(i, columnIndex, column[i]);
}
}
/// <summary>
@ -961,10 +946,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
CommonParallel.For(
0,
RowCount,
i => At(i, columnIndex, column[i]));
for (var i = 0; i < RowCount; i++)
{
At(i, columnIndex, column[i]);
}
}
/// <summary>
@ -1037,10 +1022,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
CommonParallel.For(
0,
ColumnCount,
i => At(rowIndex, i, row[i]));
for (var i = 0; i < ColumnCount; i++)
{
At(rowIndex, i, row[i]);
}
}
/// <summary>
@ -1070,10 +1055,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
CommonParallel.For(
0,
ColumnCount,
i => At(rowIndex, i, row[i]));
for (var i = 0; i < ColumnCount; i++)
{
At(rowIndex, i, row[i]);
}
}
/// <summary>
@ -1144,16 +1129,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentOutOfRangeException("columnLength");
}
CommonParallel.For(
columnIndex,
colMax,
j =>
for (var j = columnIndex; j < colMax; j++)
{
for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++)
{
for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++)
{
At(i, j, subMatrix[ii, j - columnIndex]);
}
});
At(i, j, subMatrix[ii, j - columnIndex]);
}
}
}
/// <summary>
@ -1180,10 +1162,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "source");
}
CommonParallel.For(
0,
min,
i => At(i, i, source[i]));
for (var i = 0; i < min; i++)
{
At(i, i, source[i]);
}
}
/// <summary>
@ -1210,10 +1192,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentArraysSameLength, "source");
}
CommonParallel.For(
0,
min,
i => At(i, i, source[i]));
for (var i = 0; i < min; i++)
{
At(i, i, source[i]);
}
}
/// <summary>
@ -1677,27 +1659,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.Invoke(
() =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j));
}
}
},
() =>
result.At(i, j, At(i, j));
}
}
for (var i = 0; i < lower.RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < lower.RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i + RowCount, j, lower.At(i, j));
}
}
});
result.At(i + RowCount, j, lower.At(i, j));
}
}
}
/// <summary>
@ -1745,27 +1721,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.Invoke(
() =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j));
}
}
},
() =>
result.At(i, j, At(i, j));
}
}
for (var i = 0; i < lower.RowCount; i++)
{
for (var j = 0; j < lower.ColumnCount; j++)
{
for (var i = 0; i < lower.RowCount; i++)
{
for (var j = 0; j < lower.ColumnCount; j++)
{
result.At(i + RowCount, j + ColumnCount, lower.At(i, j));
}
}
});
result.At(i + RowCount, j + ColumnCount, lower.At(i, j));
}
}
}
/// <summary>Calculates the L1 norm.</summary>

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

@ -135,16 +135,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) + other.At(i, j));
}
});
result.At(i, j, At(i, j) + other.At(i, j));
}
}
}
/// <summary>
@ -156,16 +153,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - other.At(i, j));
}
});
result.At(i, j, At(i, j) - other.At(i, j));
}
}
}
/// <summary>
@ -175,16 +169,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(float scalar, Matrix<float> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) * scalar);
}
});
result.At(i, j, At(i, j) * scalar);
}
}
}
/// <summary>
@ -194,19 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<float> rightSide, Vector<float> result)
{
CommonParallel.For(
0,
RowCount,
i =>
{
var s = 0.0f;
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
});
for (var i = 0; i < RowCount; i++)
{
var s = 0.0f;
for (var j = 0; j != ColumnCount; j++)
{
s += At(i, j) * rightSide[j];
}
result[i] = s;
}
}
/// <summary>
@ -216,19 +204,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<float> leftSide, Vector<float> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
var s = 0.0f;
for (var i = 0; i != leftSide.Count; i++)
{
var s = 0.0f;
for (var i = 0; i != leftSide.Count; i++)
{
s += leftSide[i] * At(i, j);
}
s += leftSide[i] * At(i, j);
}
result[j] = s;
});
result[j] = s;
}
}
/// <summary>
@ -238,22 +223,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i != other.ColumnCount; i++)
{
for (var i = 0; i != other.ColumnCount; i++)
var s = 0.0f;
for (var l = 0; l < ColumnCount; l++)
{
var s = 0.0f;
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
s += At(j, l) * other.At(l, i);
}
});
result.At(j, i, s);
}
}
}
/// <summary>
@ -273,22 +255,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result)
{
CommonParallel.For(
0,
RowCount,
j =>
for (var j = 0; j < RowCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
var s = 0.0f;
for (var l = 0; l < ColumnCount; l++)
{
var s = 0.0f;
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
s += At(i, l) * other.At(j, l);
}
});
result.At(i, j, s);
}
}
}
/// <summary>
@ -297,16 +276,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
CommonParallel.For(
0,
RowCount,
i =>
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j != ColumnCount; j++)
{
for (var j = 0; j != ColumnCount; j++)
{
result[i, j] = -At(i, j);
}
});
result[i, j] = -At(i, j);
}
}
}
/// <summary>
@ -316,16 +292,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<float> other, Matrix<float> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) * other.At(i, j));
}
});
result.At(i, j, At(i, j) * other.At(i, j));
}
}
}
/// <summary>
@ -335,16 +308,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<float> other, Matrix<float> result)
{
CommonParallel.For(
0,
ColumnCount,
j =>
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
for (var i = 0; i < RowCount; i++)
{
result.At(i, j, At(i, j) / other.At(i, j));
}
});
result.At(i, j, At(i, j) / other.At(i, j));
}
}
}
/// <summary>
@ -369,16 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<float> matrix, IContinuousDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, Convert.ToSingle(distribution.Sample()));
}
});
matrix.At(i, j, Convert.ToSingle(distribution.Sample()));
}
}
}
/// <summary>
@ -388,16 +355,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<float> matrix, IDiscreteDistribution distribution)
{
CommonParallel.For(
0,
matrix.RowCount,
i =>
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
});
matrix.At(i, j, distribution.Sample());
}
}
}
}
}

Loading…
Cancel
Save