Browse Source

LA: Perf, drop some range checks

v2
Christoph Ruegg 14 years ago
parent
commit
d0b93b247a
  1. 5
      src/Numerics/LinearAlgebra/Complex/Factorization/Cholesky.cs
  2. 2
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  3. 5
      src/Numerics/LinearAlgebra/Complex32/Factorization/Cholesky.cs
  4. 2
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  5. 5
      src/Numerics/LinearAlgebra/Double/Factorization/Cholesky.cs
  6. 2
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  7. 11
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  8. 13
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  9. 5
      src/Numerics/LinearAlgebra/Single/Factorization/Cholesky.cs
  10. 2
      src/Numerics/LinearAlgebra/Single/Matrix.cs

5
src/Numerics/LinearAlgebra/Complex/Factorization/Cholesky.cs

@ -54,7 +54,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
var det = Complex.One; var det = Complex.One;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det *= CholeskyFactor[j, j] * CholeskyFactor[j, j]; var d = CholeskyFactor.At(j, j);
det *= d * d;
} }
return det; return det;
@ -71,7 +72,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
var det = Complex.Zero; var det = Complex.Zero;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det += 2.0 * CholeskyFactor[j, j].NaturalLogarithm(); det += 2.0 * CholeskyFactor.At(j, j).NaturalLogarithm();
} }
return det; return det;

2
src/Numerics/LinearAlgebra/Complex/Matrix.cs

@ -302,7 +302,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{ {
for (var j = 0; j != ColumnCount; j++) for (var j = 0; j != ColumnCount; j++)
{ {
result[i, j] = -At(i, j); result.At(i, j, -At(i, j));
} }
} }
} }

5
src/Numerics/LinearAlgebra/Complex32/Factorization/Cholesky.cs

@ -54,7 +54,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
var det = Complex32.One; var det = Complex32.One;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det *= CholeskyFactor[j, j] * CholeskyFactor[j, j]; var d = CholeskyFactor.At(j, j);
det *= d * d;
} }
return det; return det;
@ -71,7 +72,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
var det = Complex32.Zero; var det = Complex32.Zero;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det += 2.0f * CholeskyFactor[j, j].NaturalLogarithm(); det += 2.0f * CholeskyFactor.At(j, j).NaturalLogarithm();
} }
return det; return det;

2
src/Numerics/LinearAlgebra/Complex32/Matrix.cs

@ -296,7 +296,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
for (var j = 0; j != ColumnCount; j++) for (var j = 0; j != ColumnCount; j++)
{ {
result[i, j] = -At(i, j); result.At(i, j, -At(i, j));
} }
} }
} }

5
src/Numerics/LinearAlgebra/Double/Factorization/Cholesky.cs

@ -54,7 +54,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
var det = 1.0; var det = 1.0;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det *= CholeskyFactor[j, j] * CholeskyFactor[j, j]; var d = CholeskyFactor.At(j, j);
det *= d * d;
} }
return det; return det;
@ -71,7 +72,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
var det = 0.0; var det = 0.0;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det += 2 * Math.Log(CholeskyFactor[j, j]); det += 2 * Math.Log(CholeskyFactor.At(j, j));
} }
return det; return det;

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

@ -286,7 +286,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
for (var j = 0; j != ColumnCount; j++) for (var j = 0; j != ColumnCount; j++)
{ {
result[i, j] = -At(i, j); result.At(i, j, -At(i, j));
} }
} }
} }

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

@ -29,6 +29,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.Runtime;
using System.Text; using System.Text;
using Factorization; using Factorization;
using Numerics; using Numerics;
@ -36,6 +37,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
using Storage; using Storage;
using Threading; using Threading;
/// <summary> /// <summary>
/// Defines the base class for <c>Matrix</c> classes. /// Defines the base class for <c>Matrix</c> classes.
/// </summary> /// </summary>
@ -166,7 +168,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// to get and set values without range checking.</remarks> /// to get and set values without range checking.</remarks>
public T this[int row, int column] public T this[int row, int column]
{ {
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
get { return Storage[row, column]; } get { return Storage[row, column]; }
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
set { Storage[row, column] = value; } set { Storage[row, column] = value; }
} }
@ -182,6 +189,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns> /// <returns>
/// The requested element. /// The requested element.
/// </returns> /// </returns>
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public T At(int row, int column) public T At(int row, int column)
{ {
return Storage.At(row, column); return Storage.At(row, column);
@ -199,6 +208,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="value"> /// <param name="value">
/// The value to set the element to. /// The value to set the element to.
/// </param> /// </param>
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public void At(int row, int column, T value) public void At(int row, int column, T value)
{ {
Storage.At(row, column, value); Storage.At(row, column, value);

13
src/Numerics/LinearAlgebra/Generic/Vector.cs

@ -30,8 +30,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.Runtime;
using System.Text; using System.Text;
using Distributions;
using Numerics; using Numerics;
using Properties; using Properties;
using Storage; using Storage;
@ -86,13 +86,20 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// greater than the size of the vector.</exception> /// greater than the size of the vector.</exception>
public T this[int index] public T this[int index]
{ {
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
get { return Storage[index]; } get { return Storage[index]; }
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
set { Storage[index] = value;} set { Storage[index] = value;}
} }
/// <summary>Gets the value at the given <paramref name="index"/> without range checking..</summary> /// <summary>Gets the value at the given <paramref name="index"/> without range checking..</summary>
/// <param name="index">The index of the value to get or set.</param> /// <param name="index">The index of the value to get or set.</param>
/// <returns>The value of the vector at the given <paramref name="index"/>.</returns> /// <returns>The value of the vector at the given <paramref name="index"/>.</returns>
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public T At(int index) public T At(int index)
{ {
return Storage.At(index); return Storage.At(index);
@ -101,6 +108,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <summary>Sets the <paramref name="value"/> at the given <paramref name="index"/> without range checking..</summary> /// <summary>Sets the <paramref name="value"/> at the given <paramref name="index"/> without range checking..</summary>
/// <param name="index">The index of the value to get or set.</param> /// <param name="index">The index of the value to get or set.</param>
/// <param name="value">The value to set.</param> /// <param name="value">The value to set.</param>
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public void At(int index, T value) public void At(int index, T value)
{ {
Storage.At(index, value); Storage.At(index, value);

5
src/Numerics/LinearAlgebra/Single/Factorization/Cholesky.cs

@ -54,7 +54,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
var det = 1.0f; var det = 1.0f;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det *= CholeskyFactor[j, j] * CholeskyFactor[j, j]; var d = CholeskyFactor.At(j, j);
det *= d * d;
} }
return det; return det;
@ -71,7 +72,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
var det = 0.0f; var det = 0.0f;
for (var j = 0; j < CholeskyFactor.RowCount; j++) for (var j = 0; j < CholeskyFactor.RowCount; j++)
{ {
det += 2.0f * Convert.ToSingle(Math.Log(CholeskyFactor[j, j])); det += 2.0f * Convert.ToSingle(Math.Log(CholeskyFactor.At(j, j)));
} }
return det; return det;

2
src/Numerics/LinearAlgebra/Single/Matrix.cs

@ -302,7 +302,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
for (var j = 0; j != ColumnCount; j++) for (var j = 0; j != ColumnCount; j++)
{ {
result[i, j] = -At(i, j); result.At(i, j, -At(i, j));
} }
} }
} }

Loading…
Cancel
Save