From 29471df10281d3a00e746b4d2a16e3602b21069c Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 15 Jul 2012 19:42:21 +0200 Subject: [PATCH] LA Storage: this = range checked, At = not checked --- .../LinearAlgebra/Complex/DenseMatrix.cs | 37 ++--------- .../LinearAlgebra/Complex/DiagonalMatrix.cs | 22 ++++++- .../LinearAlgebra/Complex/SparseMatrix.cs | 34 +++++++--- .../LinearAlgebra/Complex32/DenseMatrix.cs | 37 ++--------- .../LinearAlgebra/Complex32/DiagonalMatrix.cs | 22 ++++++- .../LinearAlgebra/Complex32/SparseMatrix.cs | 34 +++++++--- .../LinearAlgebra/Double/DenseMatrix.cs | 37 ++--------- .../LinearAlgebra/Double/DiagonalMatrix.cs | 22 ++++++- .../LinearAlgebra/Double/SparseMatrix.cs | 34 +++++++--- .../LinearAlgebra/Single/DenseMatrix.cs | 37 ++--------- .../LinearAlgebra/Single/DiagonalMatrix.cs | 22 ++++++- .../LinearAlgebra/Single/SparseMatrix.cs | 34 +++++++--- .../Storage/DenseColumnMajorMatrixStorage.cs | 59 +++++++++++++++++- .../SparseCompressedRowMatrixStorage.cs | 62 +++++++++++++++---- .../Storage/SparseDiagonalMatrixStorage.cs | 59 ++++++++++++++++-- 15 files changed, 366 insertions(+), 186 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 681787a3..5bcc8265 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -283,7 +283,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Gets or sets the value at the given row and column. + /// Gets or sets the value at the given row and column, with range checking. /// /// /// The row of the element. @@ -296,35 +296,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// to get and set values without range checking. public override Complex this[int row, int column] { - get - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - return _data[(column * _rowCount) + row]; - } - - set - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - _data[(column * _rowCount) + row] = value; - } + get { return _storage[row, column]; } + set { _storage[row, column] = value; } } /// @@ -341,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public override Complex At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -358,7 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public override void At(int row, int column, Complex value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index 9d4c16c6..3ebafe3b 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -149,6 +149,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex get { return _storage; } } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override Complex this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -165,7 +183,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// may be thrown if one of the indices is outside the dimensions of the matrix. public override Complex At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -185,7 +203,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// may be thrown if one of the indices is outside the dimensions of the matrix. public override void At(int row, int column, Complex value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 64b4eeaf..5c263e59 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, array[i + (j * rows)]); + _storage.At(i, j, array[i + (j * rows)]); } } } @@ -168,7 +168,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { for (var j = 0; j < _storage.ColumnCount; j++) { - _storage.SetValueAt(i, j, array[i, j]); + _storage.At(i, j, array[i, j]); } } } @@ -194,7 +194,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, matrix.At(i, j)); + _storage.At(i, j, matrix.At(i, j)); } } } @@ -448,7 +448,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex if ((columnIndices[j] >= columnIndex) && (columnIndices[j] < columnIndex + columnCount)) { var column = columnIndices[j] - columnIndex; - result._storage.SetValueAt(row, column, values[j]); + result._storage.At(row, column, values[j]); } } } @@ -617,6 +617,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return ret; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override Complex this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -631,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public override Complex At(int row, int column) { - return _storage.GetValueAt(row, column); + return _storage.At(row, column); } /// @@ -648,7 +666,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public override void At(int row, int column, Complex value) { - _storage.SetValueAt(row, column, value); + _storage.At(row, column, value); } /// @@ -760,7 +778,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex for (var j = startIndex; j < endIndex; j++) { - retStorage.SetValueAt(columnIndices[j], i, values[j]); + retStorage.At(columnIndices[j], i, values[j]); } } @@ -1354,7 +1372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } - resultSparse.Storage.SetValueAt(i, j, sum + result.At(i, j)); + resultSparse.Storage.At(i, j, sum + result.At(i, j)); } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 686fad85..3074f342 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -283,7 +283,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Gets or sets the value at the given row and column. + /// Gets or sets the value at the given row and column, with range checking. /// /// /// The row of the element. @@ -296,35 +296,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// to get and set values without range checking. public override Complex32 this[int row, int column] { - get - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - return _data[(column * _rowCount) + row]; - } - - set - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - _data[(column * _rowCount) + row] = value; - } + get { return _storage[row, column]; } + set { _storage[row, column] = value; } } /// @@ -341,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public override Complex32 At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -358,7 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public override void At(int row, int column, Complex32 value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index a9800b3f..c50f2dae 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -154,6 +154,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 get { return _storage; } } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override Complex32 this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -170,7 +188,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// may be thrown if one of the indices is outside the dimensions of the matrix. public override Complex32 At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -190,7 +208,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// may be thrown if one of the indices is outside the dimensions of the matrix. public override void At(int row, int column, Complex32 value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 76f09b49..27c047af 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, array[i + (j * rows)]); + _storage.At(i, j, array[i + (j * rows)]); } } } @@ -168,7 +168,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { for (var j = 0; j < _storage.ColumnCount; j++) { - _storage.SetValueAt(i, j, array[i, j]); + _storage.At(i, j, array[i, j]); } } } @@ -194,7 +194,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, matrix.At(i, j)); + _storage.At(i, j, matrix.At(i, j)); } } } @@ -448,7 +448,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 if ((columnIndices[j] >= columnIndex) && (columnIndices[j] < columnIndex + columnCount)) { var column = columnIndices[j] - columnIndex; - result._storage.SetValueAt(row, column, values[j]); + result._storage.At(row, column, values[j]); } } } @@ -617,6 +617,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return ret; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override Complex32 this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -631,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public override Complex32 At(int row, int column) { - return _storage.GetValueAt(row, column); + return _storage.At(row, column); } /// @@ -648,7 +666,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public override void At(int row, int column, Complex32 value) { - _storage.SetValueAt(row, column, value); + _storage.At(row, column, value); } /// @@ -761,7 +779,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 for (var j = startIndex; j < endIndex; j++) { - retStorage.SetValueAt(columnIndices[j], i, values[j]); + retStorage.At(columnIndices[j], i, values[j]); } } @@ -1354,7 +1372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } - resultSparse.Storage.SetValueAt(i, j, sum + result.At(i, j)); + resultSparse.Storage.At(i, j, sum + result.At(i, j)); } } } diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index a5262fbc..beb12775 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -283,7 +283,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Gets or sets the value at the given row and column. + /// Gets or sets the value at the given row and column, with range checking. /// /// /// The row of the element. @@ -296,35 +296,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// to get and set values without range checking. public override double this[int row, int column] { - get - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - return _data[(column * _rowCount) + row]; - } - - set - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - _data[(column * _rowCount) + row] = value; - } + get { return _storage[row, column]; } + set { _storage[row, column] = value; } } /// @@ -341,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override double At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -358,7 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override void At(int row, int column, double value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index 2ab9e782..be2f1357 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -148,6 +148,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double get { return _storage; } } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override double this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -164,7 +182,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// may be thrown if one of the indices is outside the dimensions of the matrix. public override double At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -184,7 +202,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// may be thrown if one of the indices is outside the dimensions of the matrix. public override void At(int row, int column, double value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 6644e608..406eede6 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, array[i + (j * rows)]); + _storage.At(i, j, array[i + (j * rows)]); } } } @@ -167,7 +167,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var j = 0; j < _storage.ColumnCount; j++) { - _storage.SetValueAt(i, j, array[i, j]); + _storage.At(i, j, array[i, j]); } } } @@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, matrix.At(i, j)); + _storage.At(i, j, matrix.At(i, j)); } } } @@ -447,7 +447,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double if ((columnIndices[j] >= columnIndex) && (columnIndices[j] < columnIndex + columnCount)) { var column = columnIndices[j] - columnIndex; - result._storage.SetValueAt(row, column, values[j]); + result._storage.At(row, column, values[j]); } } } @@ -616,6 +616,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double return ret; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override double this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -630,7 +648,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override double At(int row, int column) { - return _storage.GetValueAt(row, column); + return _storage.At(row, column); } /// @@ -647,7 +665,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override void At(int row, int column, double value) { - _storage.SetValueAt(row, column, value); + _storage.At(row, column, value); } /// @@ -759,7 +777,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (var j = startIndex; j < endIndex; j++) { - retStorage.SetValueAt(columnIndices[j], i, values[j]); + retStorage.At(columnIndices[j], i, values[j]); } } @@ -1352,7 +1370,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - resultSparse.Storage.SetValueAt(i, j, sum + result.At(i, j)); + resultSparse.Storage.At(i, j, sum + result.At(i, j)); } } } diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index fd0bf68f..bb7103f5 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -283,7 +283,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Gets or sets the value at the given row and column. + /// Gets or sets the value at the given row and column, with range checking. /// /// /// The row of the element. @@ -296,35 +296,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// to get and set values without range checking. public override float this[int row, int column] { - get - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - return _data[(column * _rowCount) + row]; - } - - set - { - if (row < 0 || row >= _rowCount) - { - throw new ArgumentOutOfRangeException("row"); - } - - if (column < 0 || column >= _columnCount) - { - throw new ArgumentOutOfRangeException("column"); - } - - _data[(column * _rowCount) + row] = value; - } + get { return _storage[row, column]; } + set { _storage[row, column] = value; } } /// @@ -341,7 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public override float At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -358,7 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public override void At(int row, int column, float value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 1c6b099c..94a48a8a 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -153,6 +153,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single get { return _storage; } } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override float this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -169,7 +187,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// may be thrown if one of the indices is outside the dimensions of the matrix. public override float At(int row, int column) { - return _storage[row, column]; + return _storage.At(row, column); } /// @@ -189,7 +207,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// may be thrown if one of the indices is outside the dimensions of the matrix. public override void At(int row, int column, float value) { - _storage[row, column] = value; + _storage.At(row, column, value); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 9bda0ac1..f251b6f6 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, array[i + (j * rows)]); + _storage.At(i, j, array[i + (j * rows)]); } } } @@ -167,7 +167,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single { for (var j = 0; j < _storage.ColumnCount; j++) { - _storage.SetValueAt(i, j, array[i, j]); + _storage.At(i, j, array[i, j]); } } } @@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single { for (var j = 0; j < columns; j++) { - _storage.SetValueAt(i, j, matrix.At(i, j)); + _storage.At(i, j, matrix.At(i, j)); } } } @@ -447,7 +447,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single if ((columnIndices[j] >= columnIndex) && (columnIndices[j] < columnIndex + columnCount)) { var column = columnIndices[j] - columnIndex; - result._storage.SetValueAt(row, column, values[j]); + result._storage.At(row, column, values[j]); } } } @@ -616,6 +616,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single return ret; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. + public override float this[int row, int column] + { + get { return _storage[row, column]; } + set { _storage[row, column] = value; } + } + /// /// Retrieves the requested element without range checking. /// @@ -630,7 +648,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public override float At(int row, int column) { - return _storage.GetValueAt(row, column); + return _storage.At(row, column); } /// @@ -647,7 +665,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public override void At(int row, int column, float value) { - _storage.SetValueAt(row, column, value); + _storage.At(row, column, value); } /// @@ -759,7 +777,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single for (var j = startIndex; j < endIndex; j++) { - retStorage.SetValueAt(columnIndices[j], i, values[j]); + retStorage.At(columnIndices[j], i, values[j]); } } @@ -1351,7 +1369,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - resultSparse.Storage.SetValueAt(i, j, sum + result.At(i, j)); + resultSparse.Storage.At(i, j, sum + result.At(i, j)); } } } diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index 9141a0fd..aba3e5a3 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -29,10 +29,65 @@ namespace MathNet.Numerics.LinearAlgebra.Storage Data = data; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. public T this[int row, int column] { - get { return Data[(column*RowCount) + row]; } - set { Data[(column*RowCount) + row] = value; } + get + { + if (row < 0 || row >= RowCount) + { + throw new ArgumentOutOfRangeException("row"); + } + + if (column < 0 || column >= ColumnCount) + { + throw new ArgumentOutOfRangeException("column"); + } + + return At(row, column); + } + + set + { + if (row < 0 || row >= RowCount) + { + throw new ArgumentOutOfRangeException("row"); + } + + if (column < 0 || column >= ColumnCount) + { + throw new ArgumentOutOfRangeException("column"); + } + + At(row, column, value); + } + } + + /// + /// Retrieves the requested element without range checking. + /// + public T At(int row, int column) + { + return Data[(column * RowCount) + row]; + } + + /// + /// Sets the element without range checking. + /// + public void At(int row, int column, T value) + { + Data[(column * RowCount) + row] = value; } public void Clear() diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 8d983193..ed1ae2aa 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -48,16 +48,49 @@ namespace MathNet.Numerics.LinearAlgebra.Storage ValueCount = 0; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. public T this[int row, int column] { - get { return GetValueAt(row, column); } - set { SetValueAt(row, column, value); } - } + get + { + if (row < 0 || row >= RowCount) + { + throw new ArgumentOutOfRangeException("row"); + } - public void Clear() - { - ValueCount = 0; - Array.Clear(RowPointers, 0, RowPointers.Length); + if (column < 0 || column >= ColumnCount) + { + throw new ArgumentOutOfRangeException("column"); + } + + return At(row, column); + } + + set + { + if (row < 0 || row >= RowCount) + { + throw new ArgumentOutOfRangeException("row"); + } + + if (column < 0 || column >= ColumnCount) + { + throw new ArgumentOutOfRangeException("column"); + } + + At(row, column, value); + } } /// @@ -72,20 +105,21 @@ namespace MathNet.Numerics.LinearAlgebra.Storage /// /// The requested element. /// - public T GetValueAt(int row, int column) + /// Not range-checked. + public T At(int row, int column) { var index = FindItem(row, column); return index >= 0 ? Values[index] : _zero; } /// - /// Created this method because we cannot call "virtual At" in constructor of the class, but we need to do it + /// Sets the element without range checking. /// /// The row of the element. /// The column of the element. /// The value to set the element to. - /// WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks - public void SetValueAt(int row, int column, T value) + /// WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks. + public void At(int row, int column, T value) { var index = FindItem(row, column); if (index >= 0) @@ -151,6 +185,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } + public void Clear() + { + ValueCount = 0; + Array.Clear(RowPointers, 0, RowPointers.Length); + } + /// /// Delete value from internal storage /// diff --git a/src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs index 5f6779a8..05964d9c 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs @@ -32,22 +32,71 @@ namespace MathNet.Numerics.LinearAlgebra.Storage Data = data; } + /// + /// Gets or sets the value at the given row and column, with range checking. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// The value to get or set. + /// This method is ranged checked. and + /// to get and set values without range checking. public T this[int row, int column] { get { - return row == column ? Data[row] : _zero; + if (row < 0 || row >= RowCount) + { + throw new ArgumentOutOfRangeException("row"); + } + + if (column < 0 || column >= ColumnCount) + { + throw new ArgumentOutOfRangeException("column"); + } + + return At(row, column); } + set { - if (row == column) + if (row < 0 || row >= RowCount) { - Data[row] = value; + throw new ArgumentOutOfRangeException("row"); } - else if (!_zero.Equals(value)) + + if (column < 0 || column >= ColumnCount) { - throw new IndexOutOfRangeException("Cannot set an off-diagonal element in a diagonal matrix."); + throw new ArgumentOutOfRangeException("column"); } + + At(row, column, value); + } + } + + /// + /// Retrieves the requested element without range checking. + /// + public T At(int row, int column) + { + return row == column ? Data[row] : _zero; + } + + /// + /// Sets the element without range checking. + /// + public void At(int row, int column, T value) + { + if (row == column) + { + Data[row] = value; + } + else if (!_zero.Equals(value)) + { + throw new IndexOutOfRangeException("Cannot set an off-diagonal element in a diagonal matrix."); } }