Browse Source

LA: matrix RemoveRow/RemoveColumn #207 #45

pull/202/merge
Christoph Ruegg 13 years ago
parent
commit
ca8c2846ca
  1. 74
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  2. 74
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  3. 74
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  4. 1
      src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs
  5. 46
      src/Numerics/LinearAlgebra/Matrix.cs
  6. 74
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  7. 1
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  8. 51
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs

74
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -930,80 +930,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return target;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>
/// <param name="columnIndex">The index of where to insert the column.</param>
/// <param name="column">The column to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt; the number of columns.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="column"/> != the number of rows.</exception>
public override Matrix<Complex> InsertColumn(int columnIndex, Vector<Complex> column)
{
if (columnIndex < 0 || columnIndex > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnIndex");
}
if (column.Count != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
var result = new SparseMatrix(RowCount, ColumnCount + 1);
for (var i = 0; i < columnIndex; i++)
{
result.SetColumn(i, Column(i));
}
result.SetColumn(columnIndex, column);
for (var i = columnIndex + 1; i < ColumnCount + 1; i++)
{
result.SetColumn(i, Column(i - 1));
}
return result;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index.
/// </summary>
/// <param name="rowIndex">The index of where to insert the row.</param>
/// <param name="row">The row to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception>
public override Matrix<Complex> InsertRow(int rowIndex, Vector<Complex> row)
{
if (rowIndex < 0 || rowIndex > RowCount)
{
throw new ArgumentOutOfRangeException("rowIndex");
}
if (row.Count != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
var result = new SparseMatrix(RowCount + 1, ColumnCount);
for (var i = 0; i < rowIndex; i++)
{
result.At(i, i, At(i, i));
}
result.SetRow(rowIndex, row);
for (var i = rowIndex + 1; i < result.RowCount; i++)
{
result.At(i, i - 1, At(i - 1, i - 1));
}
return result;
}
/// <summary>
/// Permute the columns of a matrix according to a permutation.
/// </summary>

74
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -924,80 +924,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return target;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>
/// <param name="columnIndex">The index of where to insert the column.</param>
/// <param name="column">The column to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt; the number of columns.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="column"/> != the number of rows.</exception>
public override Matrix<Complex32> InsertColumn(int columnIndex, Vector<Complex32> column)
{
if (columnIndex < 0 || columnIndex > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnIndex");
}
if (column.Count != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
var result = new SparseMatrix(RowCount, ColumnCount + 1);
for (var i = 0; i < columnIndex; i++)
{
result.SetColumn(i, Column(i));
}
result.SetColumn(columnIndex, column);
for (var i = columnIndex + 1; i < ColumnCount + 1; i++)
{
result.SetColumn(i, Column(i - 1));
}
return result;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index.
/// </summary>
/// <param name="rowIndex">The index of where to insert the row.</param>
/// <param name="row">The row to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception>
public override Matrix<Complex32> InsertRow(int rowIndex, Vector<Complex32> row)
{
if (rowIndex < 0 || rowIndex > RowCount)
{
throw new ArgumentOutOfRangeException("rowIndex");
}
if (row.Count != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
var result = new SparseMatrix(RowCount + 1, ColumnCount);
for (var i = 0; i < rowIndex; i++)
{
result.At(i, i, At(i, i));
}
result.SetRow(rowIndex, row);
for (var i = rowIndex + 1; i < result.RowCount; i++)
{
result.At(i, i - 1, At(i - 1, i - 1));
}
return result;
}
/// <summary>
/// Permute the columns of a matrix according to a permutation.
/// </summary>

74
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -774,80 +774,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return target;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>
/// <param name="columnIndex">The index of where to insert the column.</param>
/// <param name="column">The column to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt; the number of columns.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="column"/> != the number of rows.</exception>
public override Matrix<double> InsertColumn(int columnIndex, Vector<double> column)
{
if (columnIndex < 0 || columnIndex > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnIndex");
}
if (column.Count != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
var result = new SparseMatrix(RowCount, ColumnCount + 1);
for (var i = 0; i < columnIndex; i++)
{
result.SetColumn(i, Column(i));
}
result.SetColumn(columnIndex, column);
for (var i = columnIndex + 1; i < ColumnCount + 1; i++)
{
result.SetColumn(i, Column(i - 1));
}
return result;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index.
/// </summary>
/// <param name="rowIndex">The index of where to insert the row.</param>
/// <param name="row">The row to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception>
public override Matrix<double> InsertRow(int rowIndex, Vector<double> row)
{
if (rowIndex < 0 || rowIndex > RowCount)
{
throw new ArgumentOutOfRangeException("rowIndex");
}
if (row.Count != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
var result = new SparseMatrix(RowCount + 1, ColumnCount);
for (var i = 0; i < rowIndex; i++)
{
result.At(i, i, At(i, i));
}
result.SetRow(rowIndex, row);
for (var i = rowIndex + 1; i < result.RowCount; i++)
{
result.At(i, i - 1, At(i - 1, i - 1));
}
return result;
}
/// <summary>
/// Permute the columns of a matrix according to a permutation.
/// </summary>

1
src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs

@ -1380,6 +1380,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// Pointwise raise this matrix to an exponent.
/// </summary>
/// <param name="exponent">The exponent to raise this matrix values to.</param>
/// <param name="result">The matrix to store the result into.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwisePower(T exponent, Matrix<T> result)
{

46
src/Numerics/LinearAlgebra/Matrix.cs

@ -631,7 +631,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <exception cref="ArgumentNullException">If <paramref name="column "/> is <see langword="null" />. </exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt; the number of columns.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="column"/> != the number of rows.</exception>
public virtual Matrix<T> InsertColumn(int columnIndex, Vector<T> column)
public Matrix<T> InsertColumn(int columnIndex, Vector<T> column)
{
if (column == null)
{
@ -648,13 +648,32 @@ namespace MathNet.Numerics.LinearAlgebra
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
var result = Build.SameAs(this, RowCount, ColumnCount + 1);
var result = Build.SameAs(this, RowCount, ColumnCount + 1, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, columnIndex, skipClearing: true);
result.SetColumn(columnIndex, column);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, columnIndex, columnIndex + 1, ColumnCount - columnIndex, skipClearing: true);
return result;
}
/// <summary>
/// Creates a new matrix with the given column removed.
/// </summary>
/// <param name="columnIndex">The index of the column to remove.</param>
/// <returns>A new matrix without the chosen column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt; the number of columns.</exception>
public Matrix<T> RemoveColumn(int columnIndex)
{
if (columnIndex < 0 || columnIndex > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnIndex");
}
var result = Build.SameAs(this, RowCount, ColumnCount - 1, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, columnIndex, skipClearing: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, columnIndex + 1, columnIndex, ColumnCount - columnIndex - 1, skipClearing: true);
return result;
}
/// <summary>
/// Copies the values of the given Vector to the specified column.
/// </summary>
@ -728,7 +747,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <exception cref="ArgumentNullException">If <paramref name="row"/> is <see langword="null" />. </exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception>
public virtual Matrix<T> InsertRow(int rowIndex, Vector<T> row)
public Matrix<T> InsertRow(int rowIndex, Vector<T> row)
{
if (row == null)
{
@ -745,13 +764,32 @@ namespace MathNet.Numerics.LinearAlgebra
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
var result = Build.SameAs(this, RowCount + 1, ColumnCount);
var result = Build.SameAs(this, RowCount + 1, ColumnCount, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, rowIndex, 0, 0, ColumnCount, skipClearing: true);
result.SetRow(rowIndex, row);
Storage.CopySubMatrixTo(result.Storage, rowIndex, rowIndex+1, RowCount - rowIndex, 0, 0, ColumnCount, skipClearing: true);
return result;
}
/// <summary>
/// Creates a new matrix with the given row removed.
/// </summary>
/// <param name="rowIndex">The index of the row to remove.</param>
/// <returns>A new matrix without the chosen row.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
public Matrix<T> RemoveRow(int rowIndex)
{
if (rowIndex < 0 || rowIndex > ColumnCount)
{
throw new ArgumentOutOfRangeException("rowIndex");
}
var result = Build.SameAs(this, RowCount - 1, ColumnCount, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, rowIndex, 0, 0, ColumnCount, skipClearing: true);
Storage.CopySubMatrixTo(result.Storage, rowIndex + 1, rowIndex, RowCount - rowIndex - 1, 0, 0, ColumnCount, skipClearing: true);
return result;
}
/// <summary>
/// Copies the values of the given Vector to the specified row.
/// </summary>

74
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -774,80 +774,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return target;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>
/// <param name="columnIndex">The index of where to insert the column.</param>
/// <param name="column">The column to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="columnIndex"/> is &lt; zero or &gt; the number of columns.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="column"/> != the number of rows.</exception>
public override Matrix<float> InsertColumn(int columnIndex, Vector<float> column)
{
if (columnIndex < 0 || columnIndex > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnIndex");
}
if (column.Count != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column");
}
var result = new SparseMatrix(RowCount, ColumnCount + 1);
for (var i = 0; i < columnIndex; i++)
{
result.SetColumn(i, Column(i));
}
result.SetColumn(columnIndex, column);
for (var i = columnIndex + 1; i < ColumnCount + 1; i++)
{
result.SetColumn(i, Column(i - 1));
}
return result;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given row at the given index.
/// </summary>
/// <param name="rowIndex">The index of where to insert the row.</param>
/// <param name="row">The row to insert.</param>
/// <returns>A new <see cref="SparseMatrix"/> with the inserted column.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="rowIndex"/> is &lt; zero or &gt; the number of rows.</exception>
/// <exception cref="ArgumentException">If the size of <paramref name="row"/> != the number of columns.</exception>
public override Matrix<float> InsertRow(int rowIndex, Vector<float> row)
{
if (rowIndex < 0 || rowIndex > RowCount)
{
throw new ArgumentOutOfRangeException("rowIndex");
}
if (row.Count != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row");
}
var result = new SparseMatrix(RowCount + 1, ColumnCount);
for (var i = 0; i < rowIndex; i++)
{
result.At(i, i, At(i, i));
}
result.SetRow(rowIndex, row);
for (var i = rowIndex + 1; i < result.RowCount; i++)
{
result.At(i, i - 1, At(i - 1, i - 1));
}
return result;
}
/// <summary>
/// Permute the columns of a matrix according to a permutation.
/// </summary>

1
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -796,6 +796,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// Pointwise raise this vector to an exponent and store the result into the result vector.
/// </summary>
/// <param name="exponent">The exponent to raise this vector values to.</param>
/// <param name="result">The matrix to store the result into.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwisePower(T exponent, Vector<T> result)
{

51
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs

@ -131,6 +131,31 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.InsertRow(0, Vector<T>.Build.Dense(matrix.ColumnCount + 1)), Throws.ArgumentException);
}
[Theory]
public void CanRemoveRow(Matrix<T> matrix)
{
var row = Vector<T>.Build.Random(matrix.ColumnCount, 0);
for (var position = 0; position < matrix.RowCount; position++)
{
var result = matrix.RemoveRow(position);
Assert.That(result.RowCount, Is.EqualTo(matrix.RowCount - 1));
for (int ir = 0, im = 0; ir < result.RowCount; ir++, im++)
{
if (ir == position)
{
im++;
}
for (var j = 0; j < result.ColumnCount; j++)
{
Assert.That(result[ir, j], Is.EqualTo(matrix[im, j]), "A({0},{1}) for {2}", ir, j, matrix.GetType().FullName);
}
}
}
Assert.That(() => matrix.RemoveRow(-1), Throws.InstanceOf<ArgumentOutOfRangeException>());
Assert.That(() => matrix.RemoveRow(matrix.RowCount + 1), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory]
public void CanInsertColumn(Matrix<T> matrix)
{
@ -167,6 +192,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(() => matrix.InsertColumn(0, Vector<T>.Build.Dense(matrix.RowCount + 1)), Throws.ArgumentException);
}
[Theory]
public void CanRemoveColumn(Matrix<T> matrix)
{
var column = Vector<T>.Build.Random(matrix.RowCount, 0);
for (var position = 0; position < matrix.ColumnCount; position++)
{
var result = matrix.RemoveColumn(position);
Assert.That(result.ColumnCount, Is.EqualTo(matrix.ColumnCount - 1));
for (int jr = 0, jm = 0; jr < result.ColumnCount; jr++, jm++)
{
if (jr == position)
{
jm++;
}
for (var i = 0; i < result.RowCount; i++)
{
Assert.That(result[i, jr], Is.EqualTo(matrix[i, jm]));
}
}
}
// Invalid
Assert.That(() => matrix.RemoveColumn(-1), Throws.InstanceOf<ArgumentOutOfRangeException>());
Assert.That(() => matrix.RemoveColumn(matrix.ColumnCount + 1), Throws.InstanceOf<ArgumentOutOfRangeException>());
}
[Theory]
public void CanAppend(Matrix<T> left, Matrix<T> right)
{

Loading…
Cancel
Save