Browse Source

sparse matrix is no longer thread safe to improve performance

pull/36/head
Marcus Cuda 16 years ago
parent
commit
407ba19669
  1. 129
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  2. 132
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  3. 126
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  4. 131
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  5. 4
      src/Numerics/Threading/CommonParallel.cs

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

@ -43,11 +43,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </summary>
public class SparseMatrix : Matrix
{
/// <summary>
/// Object for use in "lock"
/// </summary>
private readonly object _lockObject = new object();
/// <summary>
/// The array containing the row indices of the existing rows. Element "j" of the array gives the index of the
/// element in the <see cref="_nonZeroValues"/> array that is first non-zero element in a row "j"
@ -613,11 +608,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </returns>
public override Complex At(int row, int column)
{
lock (_lockObject)
{
var index = FindItem(row, column);
return index >= 0 ? _nonZeroValues[index] : 0.0;
}
var index = FindItem(row, column);
return index >= 0 ? _nonZeroValues[index] : 0.0;
}
/// <summary>
@ -634,10 +626,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </param>
public override void At(int row, int column, Complex value)
{
lock (_lockObject)
{
SetValueAt(row, column, value);
}
SetValueAt(row, column, value);
}
#region Internal methods - CRS storage implementation
@ -1276,7 +1265,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
{
base.DoMultiply(scalar, result);
result.Clear();
for (var row = 0; row < RowCount; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
result.At(row, column, _nonZeroValues[index] * scalar);
}
}
}
else
{
@ -1296,16 +1302,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var otherSparseMatrix = other as SparseMatrix;
var resultSparseMatrix = result as SparseMatrix;
if (otherSparseMatrix == null || resultSparseMatrix == null)
{
base.DoMultiply(other, result);
return;
}
resultSparseMatrix.Clear();
var columnVector = new DenseVector(otherSparseMatrix.RowCount);
var columnVector = new DenseVector(other.RowCount);
for (var row = 0; row < RowCount; row++)
{
// Get the begin / end index for the current row
@ -1316,15 +1313,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
continue;
}
for (var column = 0; column < otherSparseMatrix.ColumnCount; column++)
for (var column = 0; column < other.ColumnCount; column++)
{
// Multiply row of matrix A on column of matrix B
otherSparseMatrix.Column(column, columnVector);
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
resultSparseMatrix.SetValueAt(row, column, sum);
result.At(row, column, sum);
}
}
}
@ -1426,40 +1423,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}
@ -1474,40 +1449,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}

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

@ -43,11 +43,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </summary>
public class SparseMatrix : Matrix
{
/// <summary>
/// Object for use in "lock"
/// </summary>
private readonly object _lockObject = new object();
/// <summary>
/// The array containing the row indices of the existing rows. Element "j" of the array gives the index of the
/// element in the <see cref="_nonZeroValues"/> array that is first non-zero element in a row "j"
@ -608,13 +603,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </returns>
public override Complex32 At(int row, int column)
{
lock (_lockObject)
{
var index = FindItem(row, column);
return index >= 0 ? _nonZeroValues[index] : 0.0f;
}
var index = FindItem(row, column);
return index >= 0 ? _nonZeroValues[index] : 0.0f;
}
/// <summary>
/// Sets the value of the given element.
/// </summary>
@ -629,10 +621,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </param>
public override void At(int row, int column, Complex32 value)
{
lock (_lockObject)
{
SetValueAt(row, column, value);
}
SetValueAt(row, column, value);
}
#region Internal methods - CRS storage implementation
@ -1271,7 +1260,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
{
base.DoMultiply(scalar, result);
result.Clear();
for (var row = 0; row < RowCount; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
result.At(row, column, _nonZeroValues[index] * scalar);
}
}
}
else
{
@ -1291,16 +1297,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var otherSparseMatrix = other as SparseMatrix;
var resultSparseMatrix = result as SparseMatrix;
if (otherSparseMatrix == null || resultSparseMatrix == null)
{
base.DoMultiply(other, result);
return;
}
resultSparseMatrix.Clear();
var columnVector = new DenseVector(otherSparseMatrix.RowCount);
result.Clear();
var columnVector = new DenseVector(other.RowCount);
for (var row = 0; row < RowCount; row++)
{
// Get the begin / end index for the current row
@ -1311,15 +1309,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
continue;
}
for (var column = 0; column < otherSparseMatrix.ColumnCount; column++)
for (var column = 0; column < other.ColumnCount; column++)
{
// Multiply row of matrix A on column of matrix B
otherSparseMatrix.Column(column, columnVector);
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
resultSparseMatrix.SetValueAt(row, column, sum);
result.At(row, column, sum);
}
}
}
@ -1421,40 +1419,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0f)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0f)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0f)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}
@ -1469,40 +1445,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0f)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0f)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0f)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}

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

@ -42,11 +42,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary>
public class SparseMatrix : Matrix
{
/// <summary>
/// Object for use in "lock"
/// </summary>
private readonly object _lockObject = new object();
/// <summary>
/// The array containing the row indices of the existing rows. Element "j" of the array gives the index of the
/// element in the <see cref="_nonZeroValues"/> array that is first non-zero element in a row "j"
@ -606,10 +601,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns>
public override double At(int row, int column)
{
lock (_lockObject)
{
return GetValueAt(row, column);
}
}
/// <summary>
@ -626,10 +618,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </param>
public override void At(int row, int column, double value)
{
lock (_lockObject)
{
SetValueAt(row, column, value);
}
}
#region Internal methods - CRS storage implementation
@ -1287,7 +1276,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
{
base.DoMultiply(scalar, result);
result.Clear();
for (var row = 0; row < RowCount; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
result.At(row, column, _nonZeroValues[index] * scalar);
}
}
}
else
{
@ -1307,16 +1313,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{
var otherSparseMatrix = other as SparseMatrix;
var resultSparseMatrix = result as SparseMatrix;
if (otherSparseMatrix == null || resultSparseMatrix == null)
{
base.DoMultiply(other, result);
return;
}
resultSparseMatrix.Clear();
var columnVector = new DenseVector(otherSparseMatrix.RowCount);
result.Clear();
var columnVector = new DenseVector(other.RowCount);
for (var row = 0; row < RowCount; row++)
{
// Get the begin / end index for the current row
@ -1327,15 +1325,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
continue;
}
for (var column = 0; column < otherSparseMatrix.ColumnCount; column++)
for (var column = 0; column < other.ColumnCount; column++)
{
// Multiply row of matrix A on column of matrix B
otherSparseMatrix.Column(column, columnVector);
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
resultSparseMatrix.SetValueAt(row, column, sum);
result.At(row, column, sum);
}
}
}
@ -1437,40 +1435,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}
@ -1485,45 +1461,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}
}
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>

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

@ -42,11 +42,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </summary>
public class SparseMatrix : Matrix
{
/// <summary>
/// Object for use in "lock"
/// </summary>
private readonly object _lockObject = new object();
/// <summary>
/// The array containing the row indices of the existing rows. Element "j" of the array gives the index of the
/// element in the <see cref="_nonZeroValues"/> array that is first non-zero element in a row "j"
@ -607,13 +602,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </returns>
public override float At(int row, int column)
{
lock (_lockObject)
{
var index = FindItem(row, column);
return index >= 0 ? _nonZeroValues[index] : 0.0f;
}
var index = FindItem(row, column);
return index >= 0 ? _nonZeroValues[index] : 0.0f;
}
/// <summary>
/// Sets the value of the given element.
/// </summary>
@ -628,10 +620,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </param>
public override void At(int row, int column, float value)
{
lock (_lockObject)
{
SetValueAt(row, column, value);
}
SetValueAt(row, column, value);
}
#region Internal methods - CRS storage implementation
@ -1270,7 +1259,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
{
base.DoMultiply(scalar, result);
result.Clear();
for (var row = 0; row < RowCount; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
result.At(row, column, _nonZeroValues[index] * scalar);
}
}
}
else
{
@ -1290,16 +1296,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{
var otherSparseMatrix = other as SparseMatrix;
var resultSparseMatrix = result as SparseMatrix;
if (otherSparseMatrix == null || resultSparseMatrix == null)
{
base.DoMultiply(other, result);
return;
}
resultSparseMatrix.Clear();
var columnVector = new DenseVector(otherSparseMatrix.RowCount);
result.Clear();
var columnVector = new DenseVector(other.RowCount);
for (var row = 0; row < RowCount; row++)
{
// Get the begin / end index for the current row
@ -1310,15 +1309,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single
continue;
}
for (var column = 0; column < otherSparseMatrix.ColumnCount; column++)
for (var column = 0; column < other.ColumnCount; column++)
{
// Multiply row of matrix A on column of matrix B
otherSparseMatrix.Column(column, columnVector);
other.Column(column, columnVector);
var sum = CommonParallel.Aggregate(
startIndex,
endIndex,
index => _nonZeroValues[index] * columnVector[_columnIndices[index]]);
resultSparseMatrix.SetValueAt(row, column, sum);
result.At(row, column, sum);
}
}
}
@ -1420,40 +1419,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
var resVal = _nonZeroValues[j] * other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}
@ -1468,40 +1445,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{
result.Clear();
var sparseResult = result as SparseMatrix;
if (sparseResult == null)
for (var i = 0; i < other.RowCount; i++)
{
for (var i = 0; i < other.RowCount; i++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
result.At(i, _columnIndices[j], resVal);
}
}
}
}
else
{
for (var i = 0; i < other.RowCount; i++)
for (var j = startIndex; j < endIndex; j++)
{
// Get the begin / end index for the current row
var startIndex = _rowIndex[i];
var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount;
for (var j = startIndex; j < endIndex; j++)
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
var resVal = _nonZeroValues[j] / other.At(i, _columnIndices[j]);
if (resVal != 0.0)
{
sparseResult.SetValueAt(i, _columnIndices[j], resVal);
}
result.At(i, _columnIndices[j], resVal);
}
}
}

4
src/Numerics/Threading/CommonParallel.cs

@ -41,7 +41,7 @@ namespace MathNet.Numerics.Threading
/// <summary>
/// Used to simplify parallel code, particularly between the .NET 4.0 and Silverlight Code.
/// </summary>
internal static class CommonParallel
public static class CommonParallel
{
/// <summary>
/// Executes a for loop in which iterations may run in parallel.
@ -49,7 +49,7 @@ namespace MathNet.Numerics.Threading
/// <param name="fromInclusive">The start index, inclusive.</param>
/// <param name="toExclusive">The end index, exclusive.</param>
/// <param name="body">The body to be invoked for each iteration.</param>
/// <exception cref="ArgumentNullException">The <paramref name="body"/> argument is null.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="body"/> argument is <c>null</c>.</exception>
/// <exception cref="AggregateException">At least one invocation of the body threw an exception.</exception>
public static void For(int fromInclusive, int toExclusive, Action<int> body)
{

Loading…
Cancel
Save