Browse Source

dense matrix performance tweak. work item: 5680

la-knuth
Marcus Cuda 16 years ago
parent
commit
9f2d5e5923
  1. 83
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 83
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  3. 86
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  4. 83
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

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

@ -37,6 +37,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </summary> /// </summary>
public class DenseMatrix : Matrix public class DenseMatrix : Matrix
{ {
/// <summary>
/// Number of rows.
/// </summary>
/// <remarks>Using this instead of the RowCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _rowCount;
/// <summary>
/// Number of columns.
/// </summary>
/// <remarks>Using this instead of the ColumnCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _columnCount;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size. /// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size.
/// </summary> /// </summary>
@ -47,6 +61,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DenseMatrix(int order) public DenseMatrix(int order)
: base(order) : base(order)
{ {
_rowCount = order;
_columnCount = order;
Data = new Complex[order * order]; Data = new Complex[order * order];
} }
@ -62,6 +78,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DenseMatrix(int rows, int columns) public DenseMatrix(int rows, int columns)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new Complex[rows * columns]; Data = new Complex[rows * columns];
} }
@ -78,6 +96,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DenseMatrix(int rows, int columns, Complex value) public DenseMatrix(int rows, int columns, Complex value)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new Complex[rows * columns]; Data = new Complex[rows * columns];
for (var i = 0; i < Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
@ -95,6 +115,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DenseMatrix(int rows, int columns, Complex[] array) public DenseMatrix(int rows, int columns, Complex[] array)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = array; Data = array;
} }
@ -106,14 +128,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DenseMatrix(Complex[,] array) public DenseMatrix(Complex[,] array)
: base(array.GetLength(0), array.GetLength(1)) : base(array.GetLength(0), array.GetLength(1))
{ {
var rows = array.GetLength(0); _rowCount = array.GetLength(0);
var columns = array.GetLength(1); _columnCount = array.GetLength(1);
Data = new Complex[rows * columns]; Data = new Complex[_rowCount * _columnCount];
for (var i = 0; i < rows; i++) for (var i = 0; i < _rowCount; i++)
{ {
for (var j = 0; j < columns; j++) for (var j = 0; j < _columnCount; j++)
{ {
Data[(j * rows) + i] = array[i, j]; Data[(j * _rowCount) + i] = array[i, j];
} }
} }
} }
@ -157,6 +179,51 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseVector(size); return new DenseVector(size);
} }
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>
/// <param name="row">
/// The row of the element.
/// </param>
/// <param name="column">
/// The column of the element.
/// </param>
/// <value>The value to get or set.</value>
/// <remarks>This method is ranged checked. <see cref="At(int,int)"/> and <see cref="At(int,int,Complex)"/>
/// to get and set values without range checking.</remarks>
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;
}
}
/// <summary> /// <summary>
/// Retrieves the requested element without range checking. /// Retrieves the requested element without range checking.
/// </summary> /// </summary>
@ -171,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </returns> /// </returns>
public override Complex At(int row, int column) public override Complex At(int row, int column)
{ {
return Data[(column * RowCount) + row]; return Data[(column * _rowCount) + row];
} }
/// <summary> /// <summary>
@ -188,7 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </param> /// </param>
public override void At(int row, int column, Complex value) public override void At(int row, int column, Complex value)
{ {
Data[(column * RowCount) + row] = value; Data[(column * _rowCount) + row] = value;
} }
/// <summary> /// <summary>

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

@ -37,6 +37,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </summary> /// </summary>
public class DenseMatrix : Matrix public class DenseMatrix : Matrix
{ {
/// <summary>
/// Number of rows.
/// </summary>
/// <remarks>Using this instead of the RowCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _rowCount;
/// <summary>
/// Number of columns.
/// </summary>
/// <remarks>Using this instead of the ColumnCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _columnCount;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size. /// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size.
/// </summary> /// </summary>
@ -47,6 +61,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DenseMatrix(int order) public DenseMatrix(int order)
: base(order) : base(order)
{ {
_rowCount = order;
_columnCount = order;
Data = new Complex32[order * order]; Data = new Complex32[order * order];
} }
@ -62,6 +78,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DenseMatrix(int rows, int columns) public DenseMatrix(int rows, int columns)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new Complex32[rows * columns]; Data = new Complex32[rows * columns];
} }
@ -78,6 +96,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DenseMatrix(int rows, int columns, Complex32 value) public DenseMatrix(int rows, int columns, Complex32 value)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new Complex32[rows * columns]; Data = new Complex32[rows * columns];
for (var i = 0; i < Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
@ -95,6 +115,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DenseMatrix(int rows, int columns, Complex32[] array) public DenseMatrix(int rows, int columns, Complex32[] array)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = array; Data = array;
} }
@ -106,14 +128,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DenseMatrix(Complex32[,] array) public DenseMatrix(Complex32[,] array)
: base(array.GetLength(0), array.GetLength(1)) : base(array.GetLength(0), array.GetLength(1))
{ {
var rows = array.GetLength(0); _rowCount = array.GetLength(0);
var columns = array.GetLength(1); _columnCount = array.GetLength(1);
Data = new Complex32[rows * columns]; Data = new Complex32[_rowCount * _columnCount];
for (var i = 0; i < rows; i++) for (var i = 0; i < _rowCount; i++)
{ {
for (var j = 0; j < columns; j++) for (var j = 0; j < _columnCount; j++)
{ {
Data[(j * rows) + i] = array[i, j]; Data[(j * _rowCount) + i] = array[i, j];
} }
} }
} }
@ -157,6 +179,51 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseVector(size); return new DenseVector(size);
} }
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>
/// <param name="row">
/// The row of the element.
/// </param>
/// <param name="column">
/// The column of the element.
/// </param>
/// <value>The value to get or set.</value>
/// <remarks>This method is ranged checked. <see cref="At(int,int)"/> and <see cref="At(int,int,Complex32)"/>
/// to get and set values without range checking.</remarks>
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;
}
}
/// <summary> /// <summary>
/// Retrieves the requested element without range checking. /// Retrieves the requested element without range checking.
/// </summary> /// </summary>
@ -171,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </returns> /// </returns>
public override Complex32 At(int row, int column) public override Complex32 At(int row, int column)
{ {
return Data[(column * RowCount) + row]; return Data[(column * _rowCount) + row];
} }
/// <summary> /// <summary>
@ -188,7 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </param> /// </param>
public override void At(int row, int column, Complex32 value) public override void At(int row, int column, Complex32 value)
{ {
Data[(column * RowCount) + row] = value; Data[(column * _rowCount) + row] = value;
} }
/// <summary> /// <summary>

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

@ -37,6 +37,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary> /// </summary>
public class DenseMatrix : Matrix public class DenseMatrix : Matrix
{ {
/// <summary>
/// Number of rows.
/// </summary>
/// <remarks>Using this instead of the RowCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _rowCount;
/// <summary>
/// Number of columns.
/// </summary>
/// <remarks>Using this instead of the ColumnCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _columnCount;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size. /// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size.
/// </summary> /// </summary>
@ -47,6 +61,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int order) public DenseMatrix(int order)
: base(order) : base(order)
{ {
_rowCount = order;
_columnCount = order;
Data = new double[order * order]; Data = new double[order * order];
} }
@ -62,6 +78,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int rows, int columns) public DenseMatrix(int rows, int columns)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new double[rows * columns]; Data = new double[rows * columns];
} }
@ -78,6 +96,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int rows, int columns, double value) public DenseMatrix(int rows, int columns, double value)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new double[rows * columns]; Data = new double[rows * columns];
for (var i = 0; i < Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
@ -95,6 +115,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int rows, int columns, double[] array) public DenseMatrix(int rows, int columns, double[] array)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = array; Data = array;
} }
@ -106,14 +128,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(double[,] array) public DenseMatrix(double[,] array)
: base(array.GetLength(0), array.GetLength(1)) : base(array.GetLength(0), array.GetLength(1))
{ {
var rows = array.GetLength(0); _rowCount = array.GetLength(0);
var columns = array.GetLength(1); _columnCount = array.GetLength(1);
Data = new double[rows * columns];
for (var i = 0; i < rows; i++) Data = new double[_rowCount * _columnCount];
for (var i = 0; i < _rowCount; i++)
{ {
for (var j = 0; j < columns; j++) for (var j = 0; j < _columnCount; j++)
{ {
Data[(j * rows) + i] = array[i, j]; Data[(j * _rowCount) + i] = array[i, j];
} }
} }
} }
@ -157,6 +180,51 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseVector(size); return new DenseVector(size);
} }
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>
/// <param name="row">
/// The row of the element.
/// </param>
/// <param name="column">
/// The column of the element.
/// </param>
/// <value>The value to get or set.</value>
/// <remarks>This method is ranged checked. <see cref="At(int,int)"/> and <see cref="At(int,int,double)"/>
/// to get and set values without range checking.</remarks>
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;
}
}
/// <summary> /// <summary>
/// Retrieves the requested element without range checking. /// Retrieves the requested element without range checking.
/// </summary> /// </summary>
@ -171,9 +239,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public override double At(int row, int column) public override double At(int row, int column)
{ {
return Data[(column * RowCount) + row]; return Data[(column * _rowCount) + row];
} }
/// <summary> /// <summary>
/// Sets the value of the given element. /// Sets the value of the given element.
/// </summary> /// </summary>
@ -188,7 +256,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </param> /// </param>
public override void At(int row, int column, double value) public override void At(int row, int column, double value)
{ {
Data[(column * RowCount) + row] = value; Data[(column * _rowCount) + row] = value;
} }
/// <summary> /// <summary>

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

@ -37,6 +37,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </summary> /// </summary>
public class DenseMatrix : Matrix public class DenseMatrix : Matrix
{ {
/// <summary>
/// Number of rows.
/// </summary>
/// <remarks>Using this instead of the RowCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _rowCount;
/// <summary>
/// Number of columns.
/// </summary>
/// <remarks>Using this instead of the ColumnCount property to speed up calculating
/// a matrix index in the data array.</remarks>
private readonly int _columnCount;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size. /// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size.
/// </summary> /// </summary>
@ -47,6 +61,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DenseMatrix(int order) public DenseMatrix(int order)
: base(order) : base(order)
{ {
_rowCount = order;
_columnCount = order;
Data = new float[order * order]; Data = new float[order * order];
} }
@ -62,6 +78,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DenseMatrix(int rows, int columns) public DenseMatrix(int rows, int columns)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new float[rows * columns]; Data = new float[rows * columns];
} }
@ -78,6 +96,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DenseMatrix(int rows, int columns, float value) public DenseMatrix(int rows, int columns, float value)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = new float[rows * columns]; Data = new float[rows * columns];
for (var i = 0; i < Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
@ -95,6 +115,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DenseMatrix(int rows, int columns, float[] array) public DenseMatrix(int rows, int columns, float[] array)
: base(rows, columns) : base(rows, columns)
{ {
_rowCount = rows;
_columnCount = columns;
Data = array; Data = array;
} }
@ -106,14 +128,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DenseMatrix(float[,] array) public DenseMatrix(float[,] array)
: base(array.GetLength(0), array.GetLength(1)) : base(array.GetLength(0), array.GetLength(1))
{ {
var rows = array.GetLength(0); _rowCount = array.GetLength(0);
var columns = array.GetLength(1); _columnCount = array.GetLength(1);
Data = new float[rows * columns]; Data = new float[_rowCount * _columnCount];
for (var i = 0; i < rows; i++) for (var i = 0; i < _rowCount; i++)
{ {
for (var j = 0; j < columns; j++) for (var j = 0; j < _columnCount; j++)
{ {
Data[(j * rows) + i] = array[i, j]; Data[(j * _rowCount) + i] = array[i, j];
} }
} }
} }
@ -157,6 +179,51 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseVector(size); return new DenseVector(size);
} }
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>
/// <param name="row">
/// The row of the element.
/// </param>
/// <param name="column">
/// The column of the element.
/// </param>
/// <value>The value to get or set.</value>
/// <remarks>This method is ranged checked. <see cref="At(int,int)"/> and <see cref="At(int,int,float)"/>
/// to get and set values without range checking.</remarks>
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;
}
}
/// <summary> /// <summary>
/// Retrieves the requested element without range checking. /// Retrieves the requested element without range checking.
/// </summary> /// </summary>
@ -171,7 +238,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </returns> /// </returns>
public override float At(int row, int column) public override float At(int row, int column)
{ {
return Data[(column * RowCount) + row]; return Data[(column * _rowCount) + row];
} }
/// <summary> /// <summary>
@ -188,7 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </param> /// </param>
public override void At(int row, int column, float value) public override void At(int row, int column, float value)
{ {
Data[(column * RowCount) + row] = value; Data[(column * _rowCount) + row] = value;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save