Browse Source

Linear Algebra: optimize range checking

ridge-regression
Christoph Ruegg 8 years ago
parent
commit
b625985d48
  1. 16
      src/Numerics/LinearAlgebra/Matrix.cs
  2. 28
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.Validation.cs
  3. 2
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  4. 22
      src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs

16
src/Numerics/LinearAlgebra/Matrix.cs

@ -161,7 +161,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary> /// </summary>
public void ClearRow(int rowIndex) public void ClearRow(int rowIndex)
{ {
if (rowIndex < 0 || rowIndex >= RowCount) if ((uint)rowIndex >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }
@ -174,7 +174,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary> /// </summary>
public void ClearColumn(int columnIndex) public void ClearColumn(int columnIndex)
{ {
if (columnIndex < 0 || columnIndex >= ColumnCount) if ((uint)columnIndex >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
@ -263,7 +263,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// or greater than or equal to the number of rows.</exception> /// or greater than or equal to the number of rows.</exception>
public Vector<T> Row(int index) public Vector<T> Row(int index)
{ {
if (index >= RowCount || index < 0) if ((uint)index >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
@ -348,7 +348,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// or greater than or equal to the number of columns.</exception> /// or greater than or equal to the number of columns.</exception>
public Vector<T> Column(int index) public Vector<T> Column(int index)
{ {
if (index >= ColumnCount || index < 0) if ((uint)index >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
@ -662,7 +662,7 @@ namespace MathNet.Numerics.LinearAlgebra
throw new ArgumentNullException(nameof(column)); throw new ArgumentNullException(nameof(column));
} }
if (columnIndex < 0 || columnIndex > ColumnCount) if ((uint)columnIndex > (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
@ -687,7 +687,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt;= the number of columns.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt;= the number of columns.</exception>
public Matrix<T> RemoveColumn(int columnIndex) public Matrix<T> RemoveColumn(int columnIndex)
{ {
if (columnIndex < 0 || columnIndex >= ColumnCount) if ((uint)columnIndex >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
@ -778,7 +778,7 @@ namespace MathNet.Numerics.LinearAlgebra
throw new ArgumentNullException(nameof(row)); throw new ArgumentNullException(nameof(row));
} }
if (rowIndex < 0 || rowIndex > RowCount) if ((uint)rowIndex > (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }
@ -803,7 +803,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt;= the number of rows.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt;= the number of rows.</exception>
public Matrix<T> RemoveRow(int rowIndex) public Matrix<T> RemoveRow(int rowIndex)
{ {
if (rowIndex < 0 || rowIndex >= RowCount) if ((uint)rowIndex >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }

28
src/Numerics/LinearAlgebra/Storage/MatrixStorage.Validation.cs

@ -38,12 +38,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
void ValidateRange(int row, int column) void ValidateRange(int row, int column)
{ {
if (row < 0 || row >= RowCount) if ((uint)row >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(row)); throw new ArgumentOutOfRangeException(nameof(row));
} }
if (column < 0 || column >= ColumnCount) if ((uint)column >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(column)); throw new ArgumentOutOfRangeException(nameof(column));
} }
@ -66,12 +66,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Source // Verify Source
if (sourceRowIndex >= RowCount || sourceRowIndex < 0) if ((uint)sourceRowIndex >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceRowIndex)); throw new ArgumentOutOfRangeException(nameof(sourceRowIndex));
} }
if (sourceColumnIndex >= ColumnCount || sourceColumnIndex < 0) if ((uint)sourceColumnIndex >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex)); throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex));
} }
@ -91,12 +91,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Target // Verify Target
if (targetRowIndex >= target.RowCount || targetRowIndex < 0) if ((uint)targetRowIndex >= (uint)target.RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(targetRowIndex)); throw new ArgumentOutOfRangeException(nameof(targetRowIndex));
} }
if (targetColumnIndex >= target.ColumnCount || targetColumnIndex < 0) if ((uint)targetColumnIndex >= (uint)target.ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(targetColumnIndex)); throw new ArgumentOutOfRangeException(nameof(targetColumnIndex));
} }
@ -118,7 +118,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
void ValidateRowRange<TU>(VectorStorage<TU> target, int rowIndex) void ValidateRowRange<TU>(VectorStorage<TU> target, int rowIndex)
where TU : struct, IEquatable<TU>, IFormattable where TU : struct, IEquatable<TU>, IFormattable
{ {
if (rowIndex >= RowCount || rowIndex < 0) if ((uint)rowIndex >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }
@ -132,7 +132,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
void ValidateColumnRange<TU>(VectorStorage<TU> target, int columnIndex) void ValidateColumnRange<TU>(VectorStorage<TU> target, int columnIndex)
where TU : struct, IEquatable<TU>, IFormattable where TU : struct, IEquatable<TU>, IFormattable
{ {
if (columnIndex >= ColumnCount || columnIndex < 0) if ((uint)columnIndex >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
@ -154,12 +154,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Source // Verify Source
if (rowIndex >= RowCount || rowIndex < 0) if ((uint)rowIndex >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }
if (sourceColumnIndex >= ColumnCount || sourceColumnIndex < 0) if ((uint)sourceColumnIndex >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex)); throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex));
} }
@ -171,7 +171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Target // Verify Target
if (targetColumnIndex >= target.Length || targetColumnIndex < 0) if ((uint)targetColumnIndex >= (uint)target.Length)
{ {
throw new ArgumentOutOfRangeException(nameof(targetColumnIndex)); throw new ArgumentOutOfRangeException(nameof(targetColumnIndex));
} }
@ -193,12 +193,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Source // Verify Source
if (columnIndex >= ColumnCount || columnIndex < 0) if ((uint)columnIndex >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
if (sourceRowIndex >= RowCount || sourceRowIndex < 0) if ((uint)sourceRowIndex >= (uint)RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceRowIndex)); throw new ArgumentOutOfRangeException(nameof(sourceRowIndex));
} }
@ -210,7 +210,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Target // Verify Target
if (targetRowIndex >= target.Length || targetRowIndex < 0) if ((uint)targetRowIndex >= (uint)target.Length)
{ {
throw new ArgumentOutOfRangeException(nameof(targetRowIndex)); throw new ArgumentOutOfRangeException(nameof(targetRowIndex));
} }

2
src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs

@ -274,7 +274,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
for (int k = 0; k < columnIndices.Length; k++) for (int k = 0; k < columnIndices.Length; k++)
{ {
if (columnIndices[k] < 0 || columnIndices[k] >= ColumnCount) if ((uint)columnIndices[k] >= (uint)ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndices)); throw new ArgumentOutOfRangeException(nameof(columnIndices));
} }

22
src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs

@ -37,7 +37,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
void ValidateRange(int index) void ValidateRange(int index)
{ {
if (index < 0 || index >= Length) if ((uint)index >= (uint)Length)
{ {
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
} }
@ -53,7 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Source // Verify Source
if (sourceIndex >= Length || sourceIndex < 0) if ((uint)sourceIndex >= (uint)Length)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceIndex)); throw new ArgumentOutOfRangeException(nameof(sourceIndex));
} }
@ -67,7 +67,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Target // Verify Target
if (targetIndex >= target.Length || targetIndex < 0) if ((uint)targetIndex >= (uint)target.Length)
{ {
throw new ArgumentOutOfRangeException(nameof(targetIndex)); throw new ArgumentOutOfRangeException(nameof(targetIndex));
} }
@ -82,7 +82,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
void ValidateRowRange(MatrixStorage<T> target, int rowIndex) void ValidateRowRange(MatrixStorage<T> target, int rowIndex)
{ {
if (rowIndex >= target.RowCount || rowIndex < 0) if ((uint)rowIndex >= (uint)target.RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }
@ -95,7 +95,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
void ValidateColumnRange(MatrixStorage<T> target, int columnIndex) void ValidateColumnRange(MatrixStorage<T> target, int columnIndex)
{ {
if (columnIndex >= target.ColumnCount || columnIndex < 0) if ((uint)columnIndex >= (uint)target.ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Source // Verify Source
if (sourceColumnIndex >= Length || sourceColumnIndex < 0) if ((uint)sourceColumnIndex >= (uint)Length)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex)); throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex));
} }
@ -128,12 +128,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Target // Verify Target
if (rowIndex >= target.RowCount || rowIndex < 0) if ((uint)rowIndex >= (uint)target.RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(rowIndex)); throw new ArgumentOutOfRangeException(nameof(rowIndex));
} }
if (targetColumnIndex >= target.ColumnCount || targetColumnIndex < 0) if ((uint)targetColumnIndex >= (uint)target.ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(targetColumnIndex)); throw new ArgumentOutOfRangeException(nameof(targetColumnIndex));
} }
@ -154,7 +154,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Source // Verify Source
if (sourceRowIndex >= Length || sourceRowIndex < 0) if ((uint)sourceRowIndex >= (uint)Length)
{ {
throw new ArgumentOutOfRangeException(nameof(sourceRowIndex)); throw new ArgumentOutOfRangeException(nameof(sourceRowIndex));
} }
@ -166,12 +166,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
// Verify Target // Verify Target
if (columnIndex >= target.ColumnCount || columnIndex < 0) if ((uint)columnIndex >= (uint)target.ColumnCount)
{ {
throw new ArgumentOutOfRangeException(nameof(columnIndex)); throw new ArgumentOutOfRangeException(nameof(columnIndex));
} }
if (targetRowIndex >= target.RowCount || targetRowIndex < 0) if ((uint)targetRowIndex >= (uint)target.RowCount)
{ {
throw new ArgumentOutOfRangeException(nameof(targetRowIndex)); throw new ArgumentOutOfRangeException(nameof(targetRowIndex));
} }

Loading…
Cancel
Save