From b625985d48ac998676d8f9a00e9f236ad0395d18 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 4 Nov 2018 17:50:39 +0100 Subject: [PATCH] Linear Algebra: optimize range checking --- src/Numerics/LinearAlgebra/Matrix.cs | 16 +++++------ .../Storage/MatrixStorage.Validation.cs | 28 +++++++++---------- .../LinearAlgebra/Storage/MatrixStorage.cs | 2 +- .../Storage/VectorStorage.Validation.cs | 22 +++++++-------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index 1b14cfec..64a847dd 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -161,7 +161,7 @@ namespace MathNet.Numerics.LinearAlgebra /// public void ClearRow(int rowIndex) { - if (rowIndex < 0 || rowIndex >= RowCount) + if ((uint)rowIndex >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } @@ -174,7 +174,7 @@ namespace MathNet.Numerics.LinearAlgebra /// public void ClearColumn(int columnIndex) { - if (columnIndex < 0 || columnIndex >= ColumnCount) + if ((uint)columnIndex >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } @@ -263,7 +263,7 @@ namespace MathNet.Numerics.LinearAlgebra /// or greater than or equal to the number of rows. public Vector Row(int index) { - if (index >= RowCount || index < 0) + if ((uint)index >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(index)); } @@ -348,7 +348,7 @@ namespace MathNet.Numerics.LinearAlgebra /// or greater than or equal to the number of columns. public Vector Column(int index) { - if (index >= ColumnCount || index < 0) + if ((uint)index >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(index)); } @@ -662,7 +662,7 @@ namespace MathNet.Numerics.LinearAlgebra throw new ArgumentNullException(nameof(column)); } - if (columnIndex < 0 || columnIndex > ColumnCount) + if ((uint)columnIndex > (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } @@ -687,7 +687,7 @@ namespace MathNet.Numerics.LinearAlgebra /// If is < zero or >= the number of columns. public Matrix RemoveColumn(int columnIndex) { - if (columnIndex < 0 || columnIndex >= ColumnCount) + if ((uint)columnIndex >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } @@ -778,7 +778,7 @@ namespace MathNet.Numerics.LinearAlgebra throw new ArgumentNullException(nameof(row)); } - if (rowIndex < 0 || rowIndex > RowCount) + if ((uint)rowIndex > (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } @@ -803,7 +803,7 @@ namespace MathNet.Numerics.LinearAlgebra /// If is < zero or >= the number of rows. public Matrix RemoveRow(int rowIndex) { - if (rowIndex < 0 || rowIndex >= RowCount) + if ((uint)rowIndex >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.Validation.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.Validation.cs index 646d1775..6cbe4939 100644 --- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.Validation.cs +++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.Validation.cs @@ -38,12 +38,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { void ValidateRange(int row, int column) { - if (row < 0 || row >= RowCount) + if ((uint)row >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(row)); } - if (column < 0 || column >= ColumnCount) + if ((uint)column >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(column)); } @@ -66,12 +66,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Source - if (sourceRowIndex >= RowCount || sourceRowIndex < 0) + if ((uint)sourceRowIndex >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(sourceRowIndex)); } - if (sourceColumnIndex >= ColumnCount || sourceColumnIndex < 0) + if ((uint)sourceColumnIndex >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex)); } @@ -91,12 +91,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Target - if (targetRowIndex >= target.RowCount || targetRowIndex < 0) + if ((uint)targetRowIndex >= (uint)target.RowCount) { throw new ArgumentOutOfRangeException(nameof(targetRowIndex)); } - if (targetColumnIndex >= target.ColumnCount || targetColumnIndex < 0) + if ((uint)targetColumnIndex >= (uint)target.ColumnCount) { throw new ArgumentOutOfRangeException(nameof(targetColumnIndex)); } @@ -118,7 +118,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage void ValidateRowRange(VectorStorage target, int rowIndex) where TU : struct, IEquatable, IFormattable { - if (rowIndex >= RowCount || rowIndex < 0) + if ((uint)rowIndex >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } @@ -132,7 +132,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage void ValidateColumnRange(VectorStorage target, int columnIndex) where TU : struct, IEquatable, IFormattable { - if (columnIndex >= ColumnCount || columnIndex < 0) + if ((uint)columnIndex >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } @@ -154,12 +154,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Source - if (rowIndex >= RowCount || rowIndex < 0) + if ((uint)rowIndex >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } - if (sourceColumnIndex >= ColumnCount || sourceColumnIndex < 0) + if ((uint)sourceColumnIndex >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex)); } @@ -171,7 +171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Target - if (targetColumnIndex >= target.Length || targetColumnIndex < 0) + if ((uint)targetColumnIndex >= (uint)target.Length) { throw new ArgumentOutOfRangeException(nameof(targetColumnIndex)); } @@ -193,12 +193,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Source - if (columnIndex >= ColumnCount || columnIndex < 0) + if ((uint)columnIndex >= (uint)ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } - if (sourceRowIndex >= RowCount || sourceRowIndex < 0) + if ((uint)sourceRowIndex >= (uint)RowCount) { throw new ArgumentOutOfRangeException(nameof(sourceRowIndex)); } @@ -210,7 +210,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Target - if (targetRowIndex >= target.Length || targetRowIndex < 0) + if ((uint)targetRowIndex >= (uint)target.Length) { throw new ArgumentOutOfRangeException(nameof(targetRowIndex)); } diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs index cdf4b4ab..9459747d 100644 --- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs @@ -274,7 +274,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage 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)); } diff --git a/src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs b/src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs index 1bf1e9d6..ff4e5f4a 100644 --- a/src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs +++ b/src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs @@ -37,7 +37,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { void ValidateRange(int index) { - if (index < 0 || index >= Length) + if ((uint)index >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(index)); } @@ -53,7 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Source - if (sourceIndex >= Length || sourceIndex < 0) + if ((uint)sourceIndex >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(sourceIndex)); } @@ -67,7 +67,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Target - if (targetIndex >= target.Length || targetIndex < 0) + if ((uint)targetIndex >= (uint)target.Length) { throw new ArgumentOutOfRangeException(nameof(targetIndex)); } @@ -82,7 +82,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage void ValidateRowRange(MatrixStorage target, int rowIndex) { - if (rowIndex >= target.RowCount || rowIndex < 0) + if ((uint)rowIndex >= (uint)target.RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } @@ -95,7 +95,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage void ValidateColumnRange(MatrixStorage target, int columnIndex) { - if (columnIndex >= target.ColumnCount || columnIndex < 0) + if ((uint)columnIndex >= (uint)target.ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } @@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Source - if (sourceColumnIndex >= Length || sourceColumnIndex < 0) + if ((uint)sourceColumnIndex >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(sourceColumnIndex)); } @@ -128,12 +128,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Target - if (rowIndex >= target.RowCount || rowIndex < 0) + if ((uint)rowIndex >= (uint)target.RowCount) { throw new ArgumentOutOfRangeException(nameof(rowIndex)); } - if (targetColumnIndex >= target.ColumnCount || targetColumnIndex < 0) + if ((uint)targetColumnIndex >= (uint)target.ColumnCount) { throw new ArgumentOutOfRangeException(nameof(targetColumnIndex)); } @@ -154,7 +154,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Source - if (sourceRowIndex >= Length || sourceRowIndex < 0) + if ((uint)sourceRowIndex >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(sourceRowIndex)); } @@ -166,12 +166,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // Verify Target - if (columnIndex >= target.ColumnCount || columnIndex < 0) + if ((uint)columnIndex >= (uint)target.ColumnCount) { throw new ArgumentOutOfRangeException(nameof(columnIndex)); } - if (targetRowIndex >= target.RowCount || targetRowIndex < 0) + if ((uint)targetRowIndex >= (uint)target.RowCount) { throw new ArgumentOutOfRangeException(nameof(targetRowIndex)); }