Browse Source

LA: Matrix arithmetics design pattern: Add, Subtract

v2
Christoph Ruegg 14 years ago
parent
commit
4daf5ec83e
  1. 48
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  2. 48
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  3. 50
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  4. 21
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  5. 48
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

48
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -228,34 +228,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the addition.</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 Add(Matrix<Complex> other, Matrix<Complex> result)
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Add(other, result);
base.DoAdd(other, result);
}
else
{
@ -303,34 +283,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the subtraction.</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<Complex> other, Matrix<Complex> result)
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Subtract(other, result);
base.DoSubtract(other, result);
}
else
{

48
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -228,34 +228,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the addition.</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 Add(Matrix<Complex32> other, Matrix<Complex32> result)
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Add(other, result);
base.DoAdd(other, result);
}
else
{
@ -303,34 +283,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the subtraction.</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<Complex32> other, Matrix<Complex32> result)
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "result");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Subtract(other, result);
base.DoSubtract(other, result);
}
else
{

50
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -227,38 +227,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the addition.</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 Add(Matrix<double> other, Matrix<double> result)
protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Add(other, result);
base.DoAdd(other, result);
}
else
{
Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data);
Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data);
}
}
@ -302,34 +282,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the subtraction.</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<double> other, Matrix<double> result)
protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Subtract(other, result);
base.DoSubtract(other, result);
}
else
{

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

@ -81,13 +81,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The matrix to store the result of the addition.</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 virtual void Add(Matrix<T> other, Matrix<T> result)
public void Add(Matrix<T> other, Matrix<T> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
@ -141,16 +146,26 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The matrix to store the result of the subtraction.</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 virtual void Subtract(Matrix<T> other, Matrix<T> result)
public void Subtract(Matrix<T> other, Matrix<T> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other);
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
DoSubtract(other, result);

48
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -227,34 +227,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the addition.</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 Add(Matrix<float> other, Matrix<float> result)
protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Add(other, result);
base.DoAdd(other, result);
}
else
{
@ -302,34 +282,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the subtraction.</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<float> other, Matrix<float> result)
protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, other, "other");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther == null || diagResult == null)
{
base.Subtract(other, result);
base.DoSubtract(other, result);
}
else
{

Loading…
Cancel
Save