Browse Source

Fixed design of the matrix methods.

la-knuth
Jurgen Van Gael 17 years ago
parent
commit
b340fc958c
  1. 56
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  2. 18
      src/Numerics/LinearAlgebra/Double/Matrix.cs

56
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -198,6 +198,25 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the other matrix is <see langword="null" />.</exception>
/// <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>
public override void Add(Matrix other) public override void Add(Matrix other)
{
DenseMatrix m = other as DenseMatrix;
if (m == null)
{
base.Add(other);
}
else
{
Add(other);
}
}
/// <summary>
/// Adds another <see cref="DenseMatrix"/> to this matrix. The result will be written into this matrix.
/// </summary>
/// <param name="other">The <see cref="DenseMatrix"/> to add to this matrix.</param>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
public void Add(DenseMatrix other)
{ {
if (other == null) if (other == null)
{ {
@ -209,26 +228,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions);
} }
Control.LinearAlgebraProvider.AddArrays(Data, other.Data, Data);
}
/// <summary>
/// Subtracts another matrix from this matrix. The result will be written into this matrix.
/// </summary>
/// <param name="other">The matrix to subtract.</param>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
public override void Subtract(Matrix other)
{
DenseMatrix m = other as DenseMatrix; DenseMatrix m = other as DenseMatrix;
if (m == null) if (m == null)
{ {
Parallel.For(0, RowCount, i => base.Subtract(other);
Parallel.For(0, ColumnCount, j =>
At(i, j, At(i, j) + other.At(i, j))));
} }
else else
{ {
Control.LinearAlgebraProvider.AddArrays(Data, m.Data, Data); Subtract(m);
} }
} }
/// <summary> /// <summary>
/// Subtracts another matrix from this matrix. The result will be written into this matrix. /// Subtracts another <see cref="DenseMatrix"/> from this matrix. The result will be written into this matrix.
/// </summary> /// </summary>
/// <param name="other">The matrix to subtract.</param> /// <param name="other">The <see cref="DenseMatrix"/> to subtract.</param>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the other matrix is <see langword="null" />.</exception>
/// <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>
public override void Subtract(Matrix other) public void Subtract(DenseMatrix other)
{ {
if (other == null) if (other == null)
{ {
@ -239,18 +267,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions);
} }
DenseMatrix m = other as DenseMatrix; Control.LinearAlgebraProvider.SubtractArrays(Data, other.Data, Data);
if (m == null)
{
Parallel.For(0, RowCount, i =>
Parallel.For(0, ColumnCount, j =>
At(i, j, At(i, j) - other.At(i, j))));
}
else
{
Control.LinearAlgebraProvider.SubtractArrays(Data, m.Data, Data);
}
} }
#endregion #endregion
} }

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

@ -446,9 +446,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions);
} }
Parallel.For(0, RowCount, i => Parallel.For(0, RowCount, i =>
Parallel.For(0, ColumnCount, j => {
At(i, j, At(i,j) + other.At(i,j)))); for (int j = 0; j < ColumnCount; j++)
{
At(i, j, At(i, j) + other.At(i, j));
}
});
} }
/// <summary> /// <summary>
@ -502,8 +506,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
Parallel.For(0, RowCount, i => Parallel.For(0, RowCount, i =>
Parallel.For(0, ColumnCount, j => {
At(i, j, At(i, j) - other.At(i, j)))); for(int j = 0; j < ColumnCount; j++)
{
At(i, j, At(i, j) - other.At(i, j));
}
});
} }
/// <summary> /// <summary>

Loading…
Cancel
Save