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> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result) protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -166,16 +163,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result) protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -185,16 +179,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the multiplication.</param> /// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(Complex scalar, Matrix<Complex> result) protected override void DoMultiply(Complex scalar, Matrix<Complex> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
for (var j = 0; j < ColumnCount; j++) result.At(i, j, At(i, j) * scalar);
{ }
result.At(i, j, At(i, j) * scalar); }
}
});
} }
/// <summary> /// <summary>
@ -204,19 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<Complex> rightSide, Vector<Complex> result) protected override void DoMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, var s = Complex.Zero;
i => for (var j = 0; j != ColumnCount; j++)
{ {
var s = new Complex(); s += At(i, j) * rightSide[j];
for (var j = 0; j != ColumnCount; j++) }
{
s += At(i, j) * rightSide[j]; result[i] = s;
} }
result[i] = s;
});
} }
/// <summary> /// <summary>
@ -226,19 +214,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<Complex> leftSide, Vector<Complex> result) protected override void DoLeftMultiply(Vector<Complex> leftSide, Vector<Complex> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, var s = Complex.Zero;
j => for (var i = 0; i != leftSide.Count; i++)
{ {
var s = new Complex(); s += leftSide[i] * At(i, j);
for (var i = 0; i != leftSide.Count; i++) }
{
s += leftSide[i] * At(i, j);
}
result[j] = s; result[j] = s;
}); }
} }
/// <summary> /// <summary>
@ -248,22 +233,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i != other.ColumnCount; i++)
j =>
{ {
for (var i = 0; i != other.ColumnCount; i++) var s = Complex.Zero;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = new Complex(); s += At(j, l) * other.At(l, i);
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
} }
});
result.At(j, i, s);
}
}
} }
/// <summary> /// <summary>
@ -283,22 +265,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
for (var i = 0; i < RowCount; i++) var s = Complex.Zero;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = new Complex(); s += At(i, l) * other.At(j, l);
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
} }
});
result.At(i, j, s);
}
}
} }
/// <summary> /// <summary>
@ -307,16 +286,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the negation.</param> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result) protected override void DoNegate(Matrix<Complex> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j != ColumnCount; j++)
i =>
{ {
for (var j = 0; j != ColumnCount; j++) result[i, j] = -At(i, j);
{ }
result[i, j] = -At(i, j); }
}
});
} }
/// <summary> /// <summary>
@ -326,16 +302,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param> /// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoPointwiseMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -345,16 +318,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the pointwise division.</param> /// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<Complex> other, Matrix<Complex> result) protected override void DoPointwiseDivide(Matrix<Complex> other, Matrix<Complex> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -379,16 +349,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex> matrix, IContinuousDistribution distribution) protected override void DoRandom(Matrix<Complex> matrix, IContinuousDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
for (var j = 0; j < matrix.ColumnCount; j++) matrix.At(i, j, distribution.Sample());
{ }
matrix.At(i, j, distribution.Sample()); }
}
});
} }
/// <summary> /// <summary>
@ -398,16 +365,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex> matrix, IDiscreteDistribution distribution) protected override void DoRandom(Matrix<Complex> matrix, IDiscreteDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
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> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -166,16 +163,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -185,16 +179,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the multiplication.</param> /// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(Complex32 scalar, Matrix<Complex32> result) protected override void DoMultiply(Complex32 scalar, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
for (var j = 0; j < ColumnCount; j++) result.At(i, j, At(i, j) * scalar);
{ }
result.At(i, j, At(i, j) * scalar); }
}
});
} }
/// <summary> /// <summary>
@ -204,19 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<Complex32> rightSide, Vector<Complex32> result) protected override void DoMultiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, var s = Complex32.Zero;
i => for (var j = 0; j != ColumnCount; j++)
{ {
var s = new Complex32(); s += At(i, j) * rightSide[j];
for (var j = 0; j != ColumnCount; j++) }
{
s += At(i, j) * rightSide[j]; result[i] = s;
} }
result[i] = s;
});
} }
/// <summary> /// <summary>
@ -236,19 +224,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<Complex32> leftSide, Vector<Complex32> result) protected override void DoLeftMultiply(Vector<Complex32> leftSide, Vector<Complex32> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, var s = Complex32.Zero;
j => for (var i = 0; i != leftSide.Count; i++)
{ {
var s = new Complex32(); s += leftSide[i] * At(i, j);
for (var i = 0; i != leftSide.Count; i++) }
{
s += leftSide[i] * At(i, j);
}
result[j] = s; result[j] = s;
}); }
} }
/// <summary> /// <summary>
@ -258,22 +243,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i != other.ColumnCount; i++)
j =>
{ {
for (var i = 0; i != other.ColumnCount; i++) var s = Complex32.Zero;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = new Complex32(); s += At(j, l) * other.At(l, i);
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
} }
});
result.At(j, i, s);
}
}
} }
/// <summary> /// <summary>
@ -283,22 +265,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
for (var i = 0; i < RowCount; i++) var s = Complex32.Zero;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = new Complex32(); s += At(i, l) * other.At(j, l);
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
} }
});
result.At(i, j, s);
}
}
} }
/// <summary> /// <summary>
@ -307,16 +286,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the negation.</param> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result) protected override void DoNegate(Matrix<Complex32> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j != ColumnCount; j++)
i =>
{ {
for (var j = 0; j != ColumnCount; j++) result[i, j] = -At(i, j);
{ }
result[i, j] = -At(i, j); }
}
});
} }
/// <summary> /// <summary>
@ -326,16 +302,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param> /// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoPointwiseMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -345,16 +318,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the pointwise division.</param> /// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoPointwiseDivide(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -379,16 +349,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex32> matrix, IContinuousDistribution distribution) protected override void DoRandom(Matrix<Complex32> matrix, IContinuousDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -398,16 +365,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex32> matrix, IDiscreteDistribution distribution) protected override void DoRandom(Matrix<Complex32> matrix, IDiscreteDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
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> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<double> other, Matrix<double> result) protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
/// Subtracts another matrix from this matrix. /// Subtracts another matrix from this matrix.
/// </summary> /// </summary>
@ -156,16 +153,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<double> other, Matrix<double> result) protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -175,16 +169,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the multiplication.</param> /// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(double scalar, Matrix<double> result) protected override void DoMultiply(double scalar, Matrix<double> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
for (var j = 0; j < ColumnCount; j++) result.At(i, j, At(i, j) * scalar);
{ }
result.At(i, j, At(i, j) * scalar); }
}
});
} }
/// <summary> /// <summary>
@ -194,19 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<double> rightSide, Vector<double> result) protected override void DoMultiply(Vector<double> rightSide, Vector<double> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, var s = 0.0;
i => for (var j = 0; j != ColumnCount; j++)
{ {
var s = 0.0; s += At(i, j) * rightSide[j];
for (var j = 0; j != ColumnCount; j++) }
{
s += At(i, j) * rightSide[j]; result[i] = s;
} }
result[i] = s;
});
} }
/// <summary> /// <summary>
@ -226,19 +214,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<double> leftSide, Vector<double> result) protected override void DoLeftMultiply(Vector<double> leftSide, Vector<double> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, var s = 0.0;
j => for (var i = 0; i != leftSide.Count; i++)
{ {
var s = 0.0; s += leftSide[i] * At(i, j);
for (var i = 0; i != leftSide.Count; i++) }
{
s += leftSide[i] * At(i, j);
}
result[j] = s; result[j] = s;
}); }
} }
/// <summary> /// <summary>
@ -248,22 +233,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result) protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i != other.ColumnCount; i++)
j =>
{ {
for (var i = 0; i != other.ColumnCount; i++) var s = 0.0;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = 0.0; s += At(j, l) * other.At(l, i);
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
} }
});
result.At(j, i, s);
}
}
} }
/// <summary> /// <summary>
@ -273,22 +255,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result) protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
for (var i = 0; i < RowCount; i++) var s = 0.0;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = 0.0; s += At(i, l) * other.At(j, l);
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
} }
});
result.At(i, j, s);
}
}
} }
/// <summary> /// <summary>
@ -297,16 +276,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the negation.</param> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result) protected override void DoNegate(Matrix<double> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j != ColumnCount; j++)
i =>
{ {
for (var j = 0; j != ColumnCount; j++) result[i, j] = -At(i, j);
{ }
result[i, j] = -At(i, j); }
}
});
} }
/// <summary> /// <summary>
@ -316,16 +292,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param> /// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<double> other, Matrix<double> result) protected override void DoPointwiseMultiply(Matrix<double> other, Matrix<double> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -335,16 +308,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the pointwise division.</param> /// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<double> other, Matrix<double> result) protected override void DoPointwiseDivide(Matrix<double> other, Matrix<double> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -369,16 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<double> matrix, IContinuousDistribution distribution) protected override void DoRandom(Matrix<double> matrix, IContinuousDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
for (var j = 0; j < matrix.ColumnCount; j++) matrix.At(i, j, distribution.Sample());
{ }
matrix.At(i, j, distribution.Sample()); }
}
});
} }
/// <summary> /// <summary>
@ -388,16 +355,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<double> matrix, IDiscreteDistribution distribution) protected override void DoRandom(Matrix<double> matrix, IDiscreteDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
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"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -1122,10 +1119,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
var ret = Clone(); var ret = Clone();
CommonParallel.For(
0, for (var index = 0; index < ColumnCount; index++)
ColumnCount, {
i => ret.SetColumn(i, Column(i).Normalize(p))); ret.SetColumn(index, Column(index).Normalize(p));
}
return ret; return ret;
} }
@ -1144,10 +1143,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
var ret = Clone(); var ret = Clone();
CommonParallel.For( for (var index = 0; index < RowCount; index++)
0, {
RowCount, ret.SetRow(index, Row(index).Normalize(p));
i => ret.SetRow(i, Row(i).Normalize(p))); }
return ret; return ret;
} }
} }

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

@ -470,16 +470,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> LowerTriangle() public virtual Matrix<T> LowerTriangle()
{ {
var ret = CreateMatrix(RowCount, ColumnCount); var ret = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = j; i < RowCount; i++)
j =>
{ {
for (var i = j; i < RowCount; i++) ret.At(i, j, At(i, j));
{ }
ret.At(i, j, At(i, j)); }
}
});
return ret; return ret;
} }
@ -501,16 +499,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -520,10 +515,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> UpperTriangle() public virtual Matrix<T> UpperTriangle()
{ {
var ret = CreateMatrix(RowCount, ColumnCount); var ret = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0, for (var j = 0; j < ColumnCount; j++)
ColumnCount, {
j =>
{ {
for (var i = 0; i < RowCount; i++) for (var i = 0; i < RowCount; i++)
{ {
@ -532,7 +526,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
ret.At(i, j, At(i, j)); ret.At(i, j, At(i, j));
} }
} }
}); }
}
return ret; return ret;
} }
@ -554,16 +550,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -619,16 +612,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
var result = CreateMatrix(rowLength, columnLength); var result = CreateMatrix(rowLength, columnLength);
CommonParallel.For( for (var j = columnIndex; j < colMax; j++)
columnIndex, {
colMax, for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++)
j =>
{ {
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; return result;
} }
@ -741,10 +732,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{ {
var min = Math.Min(RowCount, ColumnCount); var min = Math.Min(RowCount, ColumnCount);
var diagonal = CreateVector(min); var diagonal = CreateVector(min);
CommonParallel.For(
0, for (var i = 0; i < min; i++)
min, {
i => { diagonal[i] = At(i, i); }); diagonal[i] = At(i, i);
}
return diagonal; return diagonal;
} }
@ -756,19 +749,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> StrictlyLowerTriangle() public virtual Matrix<T> StrictlyLowerTriangle()
{ {
var result = CreateMatrix(RowCount, ColumnCount); var result = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0, for (var i = 0; i < RowCount; 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; return result;
} }
@ -790,16 +782,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -810,19 +799,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> StrictlyUpperTriangle() public virtual Matrix<T> StrictlyUpperTriangle()
{ {
var result = CreateMatrix(RowCount, ColumnCount); var result = CreateMatrix(RowCount, ColumnCount);
CommonParallel.For(
0, for (var i = 0; i < RowCount; 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; return result;
} }
@ -844,16 +832,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -928,10 +913,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
} }
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, At(i, columnIndex, column[i]);
i => At(i, columnIndex, column[i])); }
} }
/// <summary> /// <summary>
@ -961,10 +946,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
} }
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, At(i, columnIndex, column[i]);
i => At(i, columnIndex, column[i])); }
} }
/// <summary> /// <summary>
@ -1037,10 +1022,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
} }
CommonParallel.For( for (var i = 0; i < ColumnCount; i++)
0, {
ColumnCount, At(rowIndex, i, row[i]);
i => At(rowIndex, i, row[i])); }
} }
/// <summary> /// <summary>
@ -1070,10 +1055,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
} }
CommonParallel.For( for (var i = 0; i < ColumnCount; i++)
0, {
ColumnCount, At(rowIndex, i, row[i]);
i => At(rowIndex, i, row[i])); }
} }
/// <summary> /// <summary>
@ -1144,16 +1129,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentOutOfRangeException("columnLength"); throw new ArgumentOutOfRangeException("columnLength");
} }
CommonParallel.For( for (var j = columnIndex; j < colMax; j++)
columnIndex, {
colMax, for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++)
j =>
{ {
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> /// <summary>
@ -1180,10 +1162,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "source"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "source");
} }
CommonParallel.For( for (var i = 0; i < min; i++)
0, {
min, At(i, i, source[i]);
i => At(i, i, source[i])); }
} }
/// <summary> /// <summary>
@ -1210,10 +1192,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentArraysSameLength, "source"); throw new ArgumentException(Resources.ArgumentArraysSameLength, "source");
} }
CommonParallel.For( for (var i = 0; i < min; i++)
0, {
min, At(i, i, source[i]);
i => At(i, i, source[i])); }
} }
/// <summary> /// <summary>
@ -1677,27 +1659,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); 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++) result.At(i, j, At(i, j));
{ }
for (var j = 0; j < ColumnCount; 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++) result.At(i + RowCount, j, lower.At(i, j));
{ }
for (var j = 0; j < ColumnCount; j++) }
{
result.At(i + RowCount, j, lower.At(i, j));
}
}
});
} }
/// <summary> /// <summary>
@ -1745,27 +1721,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); 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++) result.At(i, j, At(i, j));
{ }
for (var j = 0; j < ColumnCount; 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++) result.At(i + RowCount, j + ColumnCount, lower.At(i, j));
{ }
for (var j = 0; j < lower.ColumnCount; j++) }
{
result.At(i + RowCount, j + ColumnCount, lower.At(i, j));
}
}
});
} }
/// <summary>Calculates the L1 norm.</summary> /// <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> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoAdd(Matrix<float> other, Matrix<float> result) protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -156,16 +153,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception> /// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected override void DoSubtract(Matrix<float> other, Matrix<float> result) protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -175,16 +169,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the multiplication.</param> /// <param name="result">The matrix to store the result of the multiplication.</param>
protected override void DoMultiply(float scalar, Matrix<float> result) protected override void DoMultiply(float scalar, Matrix<float> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j < ColumnCount; j++)
i =>
{ {
for (var j = 0; j < ColumnCount; j++) result.At(i, j, At(i, j) * scalar);
{ }
result.At(i, j, At(i, j) * scalar); }
}
});
} }
/// <summary> /// <summary>
@ -194,19 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<float> rightSide, Vector<float> result) protected override void DoMultiply(Vector<float> rightSide, Vector<float> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, var s = 0.0f;
i => for (var j = 0; j != ColumnCount; j++)
{ {
var s = 0.0f; s += At(i, j) * rightSide[j];
for (var j = 0; j != ColumnCount; j++) }
{
s += At(i, j) * rightSide[j]; result[i] = s;
} }
result[i] = s;
});
} }
/// <summary> /// <summary>
@ -216,19 +204,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoLeftMultiply(Vector<float> leftSide, Vector<float> result) protected override void DoLeftMultiply(Vector<float> leftSide, Vector<float> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, var s = 0.0f;
j => for (var i = 0; i != leftSide.Count; i++)
{ {
var s = 0.0f; s += leftSide[i] * At(i, j);
for (var i = 0; i != leftSide.Count; i++) }
{
s += leftSide[i] * At(i, j);
}
result[j] = s; result[j] = s;
}); }
} }
/// <summary> /// <summary>
@ -238,22 +223,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result) protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i != other.ColumnCount; i++)
j =>
{ {
for (var i = 0; i != other.ColumnCount; i++) var s = 0.0f;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = 0.0f; s += At(j, l) * other.At(l, i);
for (var l = 0; l < ColumnCount; l++)
{
s += At(j, l) * other.At(l, i);
}
result.At(j, i, s);
} }
});
result.At(j, i, s);
}
}
} }
/// <summary> /// <summary>
@ -273,22 +255,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result) protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result)
{ {
CommonParallel.For( for (var j = 0; j < RowCount; j++)
0, {
RowCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
for (var i = 0; i < RowCount; i++) var s = 0.0f;
for (var l = 0; l < ColumnCount; l++)
{ {
var s = 0.0f; s += At(i, l) * other.At(j, l);
for (var l = 0; l < ColumnCount; l++)
{
s += At(i, l) * other.At(j, l);
}
result.At(i, j, s);
} }
});
result.At(i, j, s);
}
}
} }
/// <summary> /// <summary>
@ -297,16 +276,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the negation.</param> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result) protected override void DoNegate(Matrix<float> result)
{ {
CommonParallel.For( for (var i = 0; i < RowCount; i++)
0, {
RowCount, for (var j = 0; j != ColumnCount; j++)
i =>
{ {
for (var j = 0; j != ColumnCount; j++) result[i, j] = -At(i, j);
{ }
result[i, j] = -At(i, j); }
}
});
} }
/// <summary> /// <summary>
@ -316,16 +292,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the pointwise multiplication.</param> /// <param name="result">The matrix to store the result of the pointwise multiplication.</param>
protected override void DoPointwiseMultiply(Matrix<float> other, Matrix<float> result) protected override void DoPointwiseMultiply(Matrix<float> other, Matrix<float> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -335,16 +308,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the pointwise division.</param> /// <param name="result">The matrix to store the result of the pointwise division.</param>
protected override void DoPointwiseDivide(Matrix<float> other, Matrix<float> result) protected override void DoPointwiseDivide(Matrix<float> other, Matrix<float> result)
{ {
CommonParallel.For( for (var j = 0; j < ColumnCount; j++)
0, {
ColumnCount, for (var i = 0; i < RowCount; i++)
j =>
{ {
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> /// <summary>
@ -369,16 +339,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<float> matrix, IContinuousDistribution distribution) protected override void DoRandom(Matrix<float> matrix, IContinuousDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
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> /// <summary>
@ -388,16 +355,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<float> matrix, IDiscreteDistribution distribution) protected override void DoRandom(Matrix<float> matrix, IDiscreteDistribution distribution)
{ {
CommonParallel.For( for (var i = 0; i < matrix.RowCount; i++)
0, {
matrix.RowCount, for (var j = 0; j < matrix.ColumnCount; j++)
i =>
{ {
for (var j = 0; j < matrix.ColumnCount; j++) matrix.At(i, j, distribution.Sample());
{ }
matrix.At(i, j, distribution.Sample()); }
}
});
} }
} }
} }

Loading…
Cancel
Save