Browse Source

LA: diagonal-dense Add Subtract

optimization-3
Christoph Ruegg 13 years ago
parent
commit
241336e1e4
  1. 4
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 26
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  3. 4
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  4. 26
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  5. 4
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  6. 26
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  7. 4
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  8. 26
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

4
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -506,11 +506,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null) if (diagonalOther != null)
{ {
CopyTo(result); Storage.CopyToUnchecked(result.Storage);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
for (int i = 0; i < diagonal.Length; i++) for (int i = 0; i < diagonal.Length; i++)
{ {

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

@ -202,16 +202,19 @@ 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)
{ {
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoAdd(other, result); Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.CopyTo(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }
@ -223,16 +226,19 @@ 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)
{ {
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoSubtract(other, result); Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.Negate(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }

4
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -501,11 +501,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null) if (diagonalOther != null)
{ {
CopyTo(result); Storage.CopyToUnchecked(result.Storage);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
for (int i = 0; i < diagonal.Length; i++) for (int i = 0; i < diagonal.Length; i++)
{ {

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

@ -197,16 +197,19 @@ 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)
{ {
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoAdd(other, result); Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.CopyTo(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }
@ -218,16 +221,19 @@ 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)
{ {
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoSubtract(other, result); Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.Negate(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }

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

@ -481,11 +481,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null) if (diagonalOther != null)
{ {
CopyTo(result); Storage.CopyToUnchecked(result.Storage);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
for (int i = 0; i < diagonal.Length; i++) for (int i = 0; i < diagonal.Length; i++)
{ {

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

@ -196,16 +196,19 @@ 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)
{ {
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoAdd(other, result); Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.CopyTo(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }
@ -217,16 +220,19 @@ 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)
{ {
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoSubtract(other, result); Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.Negate(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }

4
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -481,11 +481,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null) if (diagonalOther != null)
{ {
CopyTo(result); Storage.CopyToUnchecked(result.Storage);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
for (int i = 0; i < diagonal.Length; i++) for (int i = 0; i < diagonal.Length; i++)
{ {

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

@ -196,16 +196,19 @@ 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)
{ {
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoAdd(other, result); Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.CopyTo(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.AddArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }
@ -217,16 +220,19 @@ 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)
{ {
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix; var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (diagOther == null || diagResult == null)
{ {
base.DoSubtract(other, result); Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
} }
else
other.Negate(result);
for (int i = 0; i < _data.Length; i++)
{ {
Control.LinearAlgebraProvider.SubtractArrays(_data, diagOther._data, diagResult._data); result.At(i, i, result.At(i, i) + _data[i]);
} }
} }

Loading…
Cancel
Save