Browse Source

Cosmetics: Matrix class structure (no code changes)

v2
Christoph Ruegg 14 years ago
parent
commit
5fbb3e3607
  1. 649
      src/Numerics/LinearAlgebra/Generic/Matrix.cs

649
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -35,8 +35,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
using Numerics; using Numerics;
using Properties; using Properties;
using Storage; using Storage;
using Threading;
/// <summary> /// <summary>
/// Defines the base class for <c>Matrix</c> classes. /// Defines the base class for <c>Matrix</c> classes.
@ -290,6 +288,19 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return result; return result;
} }
#if !PORTABLE
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
object ICloneable.Clone()
{
return Clone();
}
#endif
/// <summary> /// <summary>
/// Copies the elements of this matrix to the given matrix. /// Copies the elements of this matrix to the given matrix.
/// </summary> /// </summary>
@ -339,17 +350,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// </remarks> /// </remarks>
public abstract Vector<T> CreateVector(int size, bool fullyMutable = false); public abstract Vector<T> CreateVector(int size, bool fullyMutable = false);
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return ToString(null);
}
/// <summary> /// <summary>
/// Copies a row into an Vector. /// Copies a row into an Vector.
/// </summary> /// </summary>
@ -521,6 +521,25 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
Storage.CopySubColumnTo(result.Storage, columnIndex, rowIndex, 0, length); Storage.CopySubColumnTo(result.Storage, columnIndex, rowIndex, 0, length);
} }
/// <summary>
/// Returns a new matrix containing the upper triangle of this matrix.
/// </summary>
/// <returns>The upper triangle of this matrix.</returns>
public virtual Matrix<T> UpperTriangle()
{
var ret = CreateMatrix(RowCount, ColumnCount);
for (var row = 0; row < RowCount; row++)
{
for (var column = row; column < ColumnCount; column++)
{
ret.At(row, column, At(row, column));
}
}
return ret;
}
/// <summary> /// <summary>
/// Returns a new matrix containing the lower triangle of this matrix. /// Returns a new matrix containing the lower triangle of this matrix.
/// </summary> /// </summary>
@ -567,25 +586,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
} }
/// <summary>
/// Returns a new matrix containing the upper triangle of this matrix.
/// </summary>
/// <returns>The upper triangle of this matrix.</returns>
public virtual Matrix<T> UpperTriangle()
{
var ret = CreateMatrix(RowCount, ColumnCount);
for (var row = 0; row < RowCount; row++)
{
for (var column = row; column < ColumnCount; column++)
{
ret.At(row, column, At(row, column));
}
}
return ret;
}
/// <summary> /// <summary>
/// Puts the upper triangle of this matrix into the result matrix. /// Puts the upper triangle of this matrix into the result matrix.
/// </summary> /// </summary>
@ -636,105 +636,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return target; return target;
} }
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates over the matrix columns.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over the matrix columns</returns>
/// <seealso cref="IEnumerator{T}"/>
public virtual IEnumerable<Tuple<int, Vector<T>>> ColumnEnumerator()
{
for (var i = 0; i < ColumnCount; i++)
{
yield return new Tuple<int, Vector<T>>(i, Column(i));
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates the requested matrix columns.
/// </summary>
/// <param name="index">The column to start enumerating over.</param>
/// <param name="length">The number of columns to enumerating over.</param>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over requested matrix columns.</returns>
/// <seealso cref="IEnumerator{T}"/>
/// <exception cref="ArgumentOutOfRangeException">If:
/// <list><item><paramref name="index"/> is negative,
/// or greater than or equal to the number of columns.</item>
/// <item><c>(index + length) &gt;= Columns.</c></item></list>
/// </exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual IEnumerable<Tuple<int, Vector<T>>> ColumnEnumerator(int index, int length)
{
if (index >= ColumnCount || index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
if (index + length > ColumnCount)
{
throw new ArgumentOutOfRangeException("length");
}
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var maxIndex = index + length;
for (var i = index; i < maxIndex; i++)
{
yield return new Tuple<int, Vector<T>>(i, Column(i));
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates the requested matrix rows.
/// </summary>
/// <param name="index">The row to start enumerating over.</param>
/// <param name="length">The number of rows to enumerating over.</param>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over requested matrix rows.</returns>
/// <seealso cref="IEnumerator{T}"/>
/// <exception cref="ArgumentOutOfRangeException">If:
/// <list><item><paramref name="index"/> is negative,
/// or greater than or equal to the number of rows.</item>
/// <item><c>(index + length) &gt;= Rows.</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual IEnumerable<Tuple<int, Vector<T>>> RowEnumerator(int index, int length)
{
if (index >= RowCount || index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
if (index + length > RowCount)
{
throw new ArgumentOutOfRangeException("length");
}
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var maxi = index + length;
for (var i = index; i < maxi; i++)
{
yield return new Tuple<int, Vector<T>>(i, Row(i));
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates over the matrix rows.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over the matrix rows</returns>
/// <seealso cref="IEnumerator{T}"/>
public virtual IEnumerable<Tuple<int, Vector<T>>> RowEnumerator()
{
for (var i = 0; i < RowCount; i++)
{
yield return new Tuple<int, Vector<T>>(i, Row(i));
}
}
/// <summary> /// <summary>
/// Returns the elements of the diagonal in a Vector. /// Returns the elements of the diagonal in a Vector.
/// </summary> /// </summary>
@ -1157,217 +1058,67 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
/// <summary> /// <summary>
/// Returns this matrix as a multidimensional array. /// Returns the transpose of this matrix.
/// </summary> /// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns> /// <returns>The transpose of this matrix.</returns>
public T[,] ToArray() public virtual Matrix<T> Transpose()
{ {
return Storage.ToArray(); var ret = CreateMatrix(ColumnCount, RowCount);
} for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
ret.At(j, i, At(i, j));
}
}
/// <summary> return ret;
/// Returns the matrix's elements as an array with the data laid out column-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
public T[] ToColumnWiseArray()
{
return Storage.ToColumnMajorArray();
} }
/// <summary> /// <summary>
/// Returns the matrix's elements as an array with the data laid row-wise. /// Returns the conjugate transpose of this matrix.
/// </summary> /// </summary>
/// <example><pre> /// <returns>The conjugate transpose of this matrix.</returns>
/// 1, 2, 3 public abstract Matrix<T> ConjugateTranspose();
/// 4, 5, 6 will be returned as 1, 2, 3, 4, 5, 6, 7, 8, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
public T[] ToRowWiseArray()
{
return Storage.ToRowMajorArray();
}
#region Implemented Interfaces
#if !PORTABLE
#region ICloneable
/// <summary> /// <summary>
/// Creates a new object that is a copy of the current instance. /// Permute the rows of a matrix according to a permutation.
/// </summary> /// </summary>
/// <returns> /// <param name="p">The row permutation to apply to this matrix.</param>
/// A new object that is a copy of this instance. public virtual void PermuteRows(Permutation p)
/// </returns>
object ICloneable.Clone()
{ {
return Clone(); if (p.Dimension != RowCount)
} {
throw new ArgumentException(Resources.ArgumentArraysSameLength, "p");
#endregion }
#endif // Get a sequence of inversions from the permutation.
var inv = p.ToInversions();
#region IEquatable<Matrix<T>> for (var i = 0; i < inv.Length; i++)
{
if (inv[i] != i)
{
var q = inv[i];
for (var j = 0; j < ColumnCount; j++)
{
var temp = At(q, j);
At(q, j, At(i, j));
At(i, j, temp);
}
}
}
}
/// <summary> /// <summary>
/// Indicates whether the current object is equal to another object of the same type. /// Permute the columns of a matrix according to a permutation.
/// </summary> /// </summary>
/// <param name="other"> /// <param name="p">The column permutation to apply to this matrix.</param>
/// An object to compare with this object. public virtual void PermuteColumns(Permutation p)
/// </param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
/// </returns>
public bool Equals(Matrix<T> other)
{ {
if (other == null) if (p.Dimension != ColumnCount)
{ {
return false; throw new ArgumentException(Resources.ArgumentArraysSameLength, "p");
}
return Storage.Equals(other.Storage);
}
#endregion
#region IFormattable
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">
/// The format to use.
/// </param>
/// <param name="formatProvider">
/// The format provider to use.
/// </param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public virtual string ToString(string format, IFormatProvider formatProvider = null)
{
var stringBuilder = new StringBuilder();
for (var row = 0; row < RowCount; row++)
{
for (var column = 0; column < ColumnCount; column++)
{
stringBuilder.Append(At(row, column).ToString(format, formatProvider));
if (column != ColumnCount - 1)
{
stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator);
}
}
if (row != RowCount - 1)
{
stringBuilder.Append(Environment.NewLine);
}
}
return stringBuilder.ToString();
}
#endregion
#endregion
#region System.Object overrides
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
var other = obj as Matrix<T>;
return other != null && Storage.Equals(other.Storage);
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return Storage.GetHashCode();
}
#endregion
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
/// <returns>The transpose of this matrix.</returns>
public virtual Matrix<T> Transpose()
{
var ret = CreateMatrix(ColumnCount, RowCount);
for (var j = 0; j < ColumnCount; j++)
{
for (var i = 0; i < RowCount; i++)
{
ret.At(j, i, At(i, j));
}
}
return ret;
}
/// <summary>
/// Returns the conjugate transpose of this matrix.
/// </summary>
/// <returns>The conjugate transpose of this matrix.</returns>
public abstract Matrix<T> ConjugateTranspose();
/// <summary>
/// Permute the rows of a matrix according to a permutation.
/// </summary>
/// <param name="p">The row permutation to apply to this matrix.</param>
public virtual void PermuteRows(Permutation p)
{
if (p.Dimension != RowCount)
{
throw new ArgumentException(Resources.ArgumentArraysSameLength, "p");
}
// Get a sequence of inversions from the permutation.
var inv = p.ToInversions();
for (var i = 0; i < inv.Length; i++)
{
if (inv[i] != i)
{
var q = inv[i];
for (var j = 0; j < ColumnCount; j++)
{
var temp = At(q, j);
At(q, j, At(i, j));
At(i, j, temp);
}
}
}
}
/// <summary>
/// Permute the columns of a matrix according to a permutation.
/// </summary>
/// <param name="p">The column permutation to apply to this matrix.</param>
public virtual void PermuteColumns(Permutation p)
{
if (p.Dimension != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentArraysSameLength, "p");
} }
// Get a sequence of inversions from the permutation. // Get a sequence of inversions from the permutation.
@ -1571,21 +1322,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns>The infinity norm of this matrix.</returns> /// <returns>The infinity norm of this matrix.</returns>
public abstract T InfinityNorm(); public abstract T InfinityNorm();
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public virtual IEnumerable<Tuple<int, int, T>> IndexedEnumerator()
{
for (var row = 0; row < RowCount; row++)
{
for (var column = 0; column < ColumnCount; column++)
{
yield return new Tuple<int, int, T>(row, column, At(row, column));
}
}
}
/// <summary> /// <summary>
/// Gets a value indicating whether this matrix is symmetric. /// Gets a value indicating whether this matrix is symmetric.
/// </summary> /// </summary>
@ -1612,5 +1348,240 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return true; return true;
} }
} }
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates over the matrix columns.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over the matrix columns</returns>
/// <seealso cref="IEnumerator{T}"/>
public virtual IEnumerable<Tuple<int, Vector<T>>> ColumnEnumerator()
{
for (var i = 0; i < ColumnCount; i++)
{
yield return new Tuple<int, Vector<T>>(i, Column(i));
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates the requested matrix columns.
/// </summary>
/// <param name="index">The column to start enumerating over.</param>
/// <param name="length">The number of columns to enumerating over.</param>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over requested matrix columns.</returns>
/// <seealso cref="IEnumerator{T}"/>
/// <exception cref="ArgumentOutOfRangeException">If:
/// <list><item><paramref name="index"/> is negative,
/// or greater than or equal to the number of columns.</item>
/// <item><c>(index + length) &gt;= Columns.</c></item></list>
/// </exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual IEnumerable<Tuple<int, Vector<T>>> ColumnEnumerator(int index, int length)
{
if (index >= ColumnCount || index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
if (index + length > ColumnCount)
{
throw new ArgumentOutOfRangeException("length");
}
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var maxIndex = index + length;
for (var i = index; i < maxIndex; i++)
{
yield return new Tuple<int, Vector<T>>(i, Column(i));
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates the requested matrix rows.
/// </summary>
/// <param name="index">The row to start enumerating over.</param>
/// <param name="length">The number of rows to enumerating over.</param>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over requested matrix rows.</returns>
/// <seealso cref="IEnumerator{T}"/>
/// <exception cref="ArgumentOutOfRangeException">If:
/// <list><item><paramref name="index"/> is negative,
/// or greater than or equal to the number of rows.</item>
/// <item><c>(index + length) &gt;= Rows.</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual IEnumerable<Tuple<int, Vector<T>>> RowEnumerator(int index, int length)
{
if (index >= RowCount || index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
if (index + length > RowCount)
{
throw new ArgumentOutOfRangeException("length");
}
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var maxi = index + length;
for (var i = index; i < maxi; i++)
{
yield return new Tuple<int, Vector<T>>(i, Row(i));
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates over the matrix rows.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over the matrix rows</returns>
/// <seealso cref="IEnumerator{T}"/>
public virtual IEnumerable<Tuple<int, Vector<T>>> RowEnumerator()
{
for (var i = 0; i < RowCount; i++)
{
yield return new Tuple<int, Vector<T>>(i, Row(i));
}
}
/// <summary>
/// Iterates throw each element in the matrix (row-wise).
/// </summary>
/// <returns>The value at the current iteration along with its position (row, column, value).</returns>
public virtual IEnumerable<Tuple<int, int, T>> IndexedEnumerator()
{
for (var row = 0; row < RowCount; row++)
{
for (var column = 0; column < ColumnCount; column++)
{
yield return new Tuple<int, int, T>(row, column, At(row, column));
}
}
}
/// <summary>
/// Returns this matrix as a multidimensional array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
public T[,] ToArray()
{
return Storage.ToArray();
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid out column-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
public T[] ToColumnWiseArray()
{
return Storage.ToColumnMajorArray();
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid row-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 2, 3, 4, 5, 6, 7, 8, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
public T[] ToRowWiseArray()
{
return Storage.ToRowMajorArray();
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return ToString(null);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">
/// The format to use.
/// </param>
/// <param name="formatProvider">
/// The format provider to use.
/// </param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public virtual string ToString(string format, IFormatProvider formatProvider = null)
{
var stringBuilder = new StringBuilder();
for (var row = 0; row < RowCount; row++)
{
for (var column = 0; column < ColumnCount; column++)
{
stringBuilder.Append(At(row, column).ToString(format, formatProvider));
if (column != ColumnCount - 1)
{
stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator);
}
}
if (row != RowCount - 1)
{
stringBuilder.Append(Environment.NewLine);
}
}
return stringBuilder.ToString();
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return Storage.GetHashCode();
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
/// </returns>
public bool Equals(Matrix<T> other)
{
return other != null && Storage.Equals(other.Storage);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
var other = obj as Matrix<T>;
return other != null && Storage.Equals(other.Storage);
}
} }
} }

Loading…
Cancel
Save