|
From: <in...@dn...> - 2006-01-25 13:59:32
|
Author: patrick
Date: 2006-01-25 08:59:09 -0500 (Wed, 25 Jan 2006)
New Revision: 193
Modified:
trunk/LinearAlgebra/Matrix.cs
trunk/LinearAlgebra/Solvers/BicgStabSolver.cs
trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs
trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs
trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs
trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs
trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs
trunk/LinearAlgebra/SparseMatrix.cs
trunk/LinearAlgebra/SparseVector.cs
Log:
- Working on the SparseMatrix class.
* Need to check the results of the unit tests for both the SparseMatrix and the SparseVector
Modified: trunk/LinearAlgebra/Matrix.cs
===================================================================
--- trunk/LinearAlgebra/Matrix.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Matrix.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -164,7 +164,7 @@
/// <param name="index">The column to copy.</param>
/// <param name="range">The <see cref="Range"/> of elements to copy from the requested column.</param>
/// <returns>A <see cref="Vector"/> containing the copied elements.</returns>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="Range"/> specifies a range
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range
/// outside the column's range. </exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negitive,
/// or greater than or equal to the number of columns.</exception>
@@ -176,7 +176,7 @@
/// <param name="index">The column to copy.</param>
/// <param name="range">The <see cref="Range"/> of elements to copy from the requested column.</param>
/// <param name="result">The <see cref="Vector"/> to copy the column into.</param>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="Range"/> specifies a range
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range
/// outside the column's range. </exception>
/// <exception cref="ArgumentNullException">If the result matrix is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negitive,
@@ -205,11 +205,11 @@
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates the columns of this matrix.
/// </summary>
- /// <param name="range">The <paramref name="Range"/> of columns to enumerate over.</param>
+ /// <param name="range">The <paramref name="range"/> of columns to enumerate over.</param>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over a given <see cref="Range"/> of columns of this matrix.</returns>
/// <remarks>The enumerator returns a <seealso cref="KeyValuePair{T,K}"/> with the key being the column index and the value
/// being the that column as a <see cref="Vector"/>.</remarks>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="Range"/> specifies a range of columns that is
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range of columns that is
/// outside the number of matrix columns. </exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public abstract IEnumerator<KeyValuePair<int, Vector>> ColumnEnumerator(Range range);
@@ -272,7 +272,7 @@
/// <param name="index">The column to copy.</param>
/// <param name="range">The <see cref="Range"/> of elements to copy.</param>
/// <returns>A <see cref="Vector"/> containing the copied elements.</returns>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="Range"/> specifies a range
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range
/// outside the row's range. </exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negitive,
/// or greater than or equal to the number of rows.</exception>
@@ -287,7 +287,7 @@
/// <returns>A <see cref="Vector"/> containing the copied elements.</returns>
/// <exception cref="ArgumentNullException">if the result matrix is <c>null</c>.</exception>
/// <exception cref="NotConformableException">If <c>result.Count != (range.End - range.Start) / range.Stride + 1</c>.</exception>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="Range"/> specifies a range
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range
/// outside the row's range. </exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negitive,
/// or greater than or equal to the number of rows.</exception>
@@ -316,11 +316,11 @@
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that enumerates the rows of this matrix.
/// </summary>
- /// <param name="range">The <paramref name="Range"/> of rows to enumerate over.</param>
+ /// <param name="range">The <paramref name="range"/> of rows to enumerate over.</param>
/// <returns>An <see cref="IEnumerator{T}"/> that enumerates over a given <see cref="Range"/> of rows of this matrix.</returns>
/// <remarks>The enumerator returns a <seealso cref="KeyValuePair{T,K}"/> with the key being the row index and the value
/// being the that row as a <see cref="Vector"/>.</remarks>
- /// <exception cref="ArgumentOutOfRangeException">If <paramref name="Range"/> specifies a range of rows that is
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range of rows that is
/// outside the number of matrix rows. </exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public abstract IEnumerator<KeyValuePair<int, Vector>> RowEnumerator(Range range);
Modified: trunk/LinearAlgebra/Solvers/BicgStabSolver.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/BicgStabSolver.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Solvers/BicgStabSolver.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -37,7 +37,7 @@
/// </remarks>
public sealed class BiConjugateGradientStabilizedSolver : IIterativeMatrixSolver
{
- private SparseMatrix mCoefficients = null;
+ private Matrix mCoefficients = null;
private IPreconditioner mPreconditioner = null;
private ConvergenceMonitor mConvergenceMonitor = null;
@@ -51,7 +51,7 @@
/// <param name="coefficientMatrix">The coefficient matrix.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="coefficientMatrix"/> is <c>null</c>.</exception>
/// <exception cref="NotSquareMatrixException">If the <paramref name="coefficientMatrix"/> is not square.</exception>
- public BiConjugateGradientStabilizedSolver(SparseMatrix coefficientMatrix) :
+ public BiConjugateGradientStabilizedSolver(Matrix coefficientMatrix) :
this(coefficientMatrix, null, null)
{}
@@ -66,7 +66,7 @@
/// monitor the iterative process.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="coefficientMatrix"/> is <c>null</c>.</exception>
/// <exception cref="NotSquareMatrixException">If the <paramref name="coefficientMatrix"/> is not square.</exception>
- public BiConjugateGradientStabilizedSolver(SparseMatrix coefficientMatrix, ConvergenceMonitor monitor)
+ public BiConjugateGradientStabilizedSolver(Matrix coefficientMatrix, ConvergenceMonitor monitor)
: this(coefficientMatrix, null, monitor)
{}
@@ -82,7 +82,7 @@
/// matrix equation.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="coefficientMatrix"/> is <c>null</c>.</exception>
/// <exception cref="NotSquareMatrixException">If the <paramref name="coefficientMatrix"/> is not square.</exception>
- public BiConjugateGradientStabilizedSolver(SparseMatrix coefficientMatrix, IPreconditioner preconditioner)
+ public BiConjugateGradientStabilizedSolver(Matrix coefficientMatrix, IPreconditioner preconditioner)
: this(coefficientMatrix, preconditioner, null)
{}
@@ -96,7 +96,7 @@
/// monitor the iterative process.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="coefficientMatrix"/> is <c>null</c>.</exception>
/// <exception cref="NotSquareMatrixException">If the <paramref name="coefficientMatrix"/> is not square.</exception>
- public BiConjugateGradientStabilizedSolver(SparseMatrix coefficientMatrix, IPreconditioner preconditioner,
+ public BiConjugateGradientStabilizedSolver(Matrix coefficientMatrix, IPreconditioner preconditioner,
ConvergenceMonitor monitor)
{
if (coefficientMatrix == null)
@@ -117,7 +117,7 @@
/// <summary>
/// Returns the <see cref="SparseMatrix"/> on which the solver works.
/// </summary>
- internal SparseMatrix Matrix
+ internal Matrix Matrix
{
get
{
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -54,7 +54,7 @@
/// </summary>
/// <param name="coefficientMatrix">The <see cref="SparseMatrix"/> upon which this
/// preconditioner is based.</param>
- public DiagonalIncompleteLUFactorization(SparseMatrix coefficientMatrix)
+ public DiagonalIncompleteLUFactorization(Matrix coefficientMatrix)
{
if (coefficientMatrix == null)
{
@@ -66,7 +66,7 @@
throw new NotSquareMatrixException();
}
- mCoefficientMatrix = coefficientMatrix;
+ mCoefficientMatrix = new SparseMatrix(coefficientMatrix);
}
/// <summary>
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -35,7 +35,7 @@
/// </summary>
/// <param name="coefficientMatrix">The <see cref="SparseMatrix"/> upon which this
/// preconditioner is based.</param>
- public DiagonalPreconditioner(SparseMatrix coefficientMatrix)
+ public DiagonalPreconditioner(Matrix coefficientMatrix)
{
if (coefficientMatrix == null)
{
@@ -47,7 +47,7 @@
throw new NotSquareMatrixException();
}
- mCoefficientMatrix = coefficientMatrix;
+ mCoefficientMatrix = new SparseMatrix(coefficientMatrix);
}
/// <summary>
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -42,7 +42,7 @@
/// <param name="coefficientMatrix">
/// The <see cref="SparseMatrix"/> upon which the preconditioner is based.
/// </param>
- public IncompleteLUFactorization(SparseMatrix coefficientMatrix)
+ public IncompleteLUFactorization(Matrix coefficientMatrix)
{
if (coefficientMatrix == null)
{
@@ -54,7 +54,7 @@
throw new NotSquareMatrixException();
}
- mCoefficientMatrix = coefficientMatrix;
+ mCoefficientMatrix = new SparseMatrix(coefficientMatrix);
}
/// <summary>
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -71,7 +71,7 @@
/// </summary>
/// <param name="coefficientMatrix">The <see cref="SparseMatrix"/> upon which this
/// preconditioner is based.</param>
- public IlutpPreconditioner(SparseMatrix coefficientMatrix)
+ public IlutpPreconditioner(Matrix coefficientMatrix)
{
if (coefficientMatrix == null)
{
@@ -83,7 +83,7 @@
throw new NotSquareMatrixException();
}
- mCoefficientMatrix = coefficientMatrix;
+ mCoefficientMatrix = new SparseMatrix(coefficientMatrix);
}
/// <summary>
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -25,7 +25,7 @@
/// Initializes a new instance of the <c>UnitPreconditioner</c> class.
/// </summary>
/// <param name="coefficientMatrix">The coefficient matrix to use.</param>
- public UnitPreconditioner(SparseMatrix coefficientMatrix)
+ public UnitPreconditioner(Matrix coefficientMatrix)
{
if (coefficientMatrix == null)
{
@@ -37,7 +37,7 @@
throw new NotSquareMatrixException();
}
- mCoefficientMatrix = coefficientMatrix;
+ mCoefficientMatrix = new SparseMatrix(coefficientMatrix);
}
/// <summary>
Modified: trunk/LinearAlgebra/SparseMatrix.cs
===================================================================
--- trunk/LinearAlgebra/SparseMatrix.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/SparseMatrix.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -8,6 +8,7 @@
using System.Collections.Generic;
using dnAnalytics.Exceptions;
using dnAnalytics.LinearAlgebra;
+using dnAnalytics.Resources;
namespace dnAnalytics.LinearAlgebra
{
@@ -18,7 +19,7 @@
/// In the current implementation the <c>SparseMatrix</c> uses a
/// compressed row storage system.
/// </remarks>
- public sealed class SparseMatrix : Matrix
+ internal sealed class SparseMatrix : Matrix
{
// UPGRADE NOTE: Could create an iternal class that
// holds the sparse data structure. That way we can interchange
@@ -226,12 +227,21 @@
// because most of the time matrices are accessed sequentially which means
// that we can figure out what item to pick without having to actually
// search for it.
+
+ /// <summary>
+ /// Finds either the item indicated by the given indices (returns <c>true</c>) or the
+ /// insert position (returns <c>false</c>) in the values array.
+ /// </summary>
+ /// <param name="rowIndex"></param>
+ /// <param name="columnIndex"></param>
+ /// <param name="itemIndex"></param>
+ /// <returns></returns>
private bool FindItem(int rowIndex, int columnIndex, out int itemIndex)
{
// First find the start and end indices for the row
int startIndex;
int endIndex;
- FindColumnStartAndEnd(rowIndex, out startIndex, out endIndex);
+ FindRowStartAndEnd(rowIndex, out startIndex, out endIndex);
// Check if startIndex and endIndex are equal. If so then
// the item does not exist
@@ -294,7 +304,7 @@
return false;
}
- private void FindColumnStartAndEnd(int rowIndex, out int startIndex, out int endIndex)
+ private void FindRowStartAndEnd(int rowIndex, out int startIndex, out int endIndex)
{
// First find the start and end indices for the row
startIndex = mRowIndices[rowIndex];
@@ -473,6 +483,8 @@
/// may be thrown if one of the indices is outside the dimensions of the matrix.</exception>
protected internal override double ValueAt(int row, int column)
{
+ // In this case we just use the indexer because there is no way we can leave out the
+ // checks. These are essential to ensure that the search algorithm actually terminates!
return this[row, column];
}
@@ -488,7 +500,9 @@
/// may be thrown if one of the indices is outside the dimensions of the matrix.</exception>
public override void ValueAt(int row, int column, double value)
{
- throw new NotImplementedException();
+ // In this case we just use the indexer because there is no way we can leave out the
+ // checks. These are essential to ensure that the search algorithm actually terminates!
+ this[row, column] = value;
}
/// <summary>
@@ -546,7 +560,9 @@
/// <returns>A vector containing the copied elements.</returns>
public override Vector Column(int index)
{
- throw new NotImplementedException();
+ Vector result = VectorBuilder.CreateVector(Rows, VectorType.Sparse);
+ Column(index, result);
+ return result;
}
/// <summary>
@@ -555,9 +571,36 @@
/// <param name="index">The column to copy.</param>
/// <param name="result">The vector to copy the column into.</param>
/// <exception cref="ArgumentNullException">If the result matrix is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negative,
+ /// or greater than or equal to the number of columns.</exception>
+ /// <exception cref="NotConformableException">If the result vector is of the incorrect size.</exception>
public override void Column(int index, Vector result)
{
- throw new NotImplementedException();
+ if (result == null)
+ {
+ throw new ArgumentNullException();
+ }
+
+ if (index >= Columns || index < 0)
+ {
+ throw new ArgumentOutOfRangeException("index", Strings.OutOfRangeException);
+ }
+
+ if (result.Count != Rows)
+ {
+ throw new NotConformableException();
+ }
+
+ for (int i = 0; i < Rows; i++)
+ {
+ int itemIndex;
+ if (!FindItem(i, index, out itemIndex))
+ {
+ continue;
+ }
+
+ result[i] = mValues[itemIndex];
+ }
}
/// <summary>
@@ -568,17 +611,71 @@
/// <returns>A vector containing the copied elements.</returns>
public override Vector Column(int index, Range range)
{
- throw new NotImplementedException();
+ // the actual size of the vector is the number of elements in the
+ // range. This should be the difference between start and end + 1 (<-- off by one thing)
+ // and then the number of times the stride fits in that difference.
+ int size = range.End - range.Start;
+ size = size / range.Stride + 1;
+
+ Vector result = VectorBuilder.CreateVector(size, VectorType.Sparse);
+ Column(index, range, result);
+ return result;
}
/// <summary>
+ /// Copies the given <see cref="Range"/> of a column into the given vector.
+ /// </summary>
+ /// <param name="index">The column to copy.</param>
+ /// <param name="range">The <see cref="Range"/> of elements to copy from the requested column.</param>
+ /// <param name="result">The <see cref="Vector"/> to copy the column into.</param>
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range
+ /// outside the column's range. </exception>
+ /// <exception cref="ArgumentNullException">If the result matrix is <c>null</c>.</exception>
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negative,
+ /// or greater than or equal to the number of columns.</exception>
+ /// <exception cref="NotConformableException">If <c>result.Count != (range.End - range.Start) / range.Stride + 1</c>.</exception>
+ public override void Column(int index, Range range, Vector result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (index >= Columns || index < 0)
+ {
+ throw new ArgumentOutOfRangeException("index", Strings.OutOfRangeException);
+ }
+ if (range.Start >= Rows || range.End >= Rows)
+ {
+ throw new ArgumentOutOfRangeException("range", Strings.OutOfRangeException);
+ }
+
+ for (int i = range.Start; i < range.End; i += range.Stride)
+ {
+ int itemIndex;
+ if (!FindItem(i, index, out itemIndex))
+ {
+ continue;
+ }
+
+ result[i] = mValues[itemIndex];
+ }
+ }
+
+ /// <summary>
/// Returns an <see cref="IEnumerator"/> that enumerates the columns of this matrix.
/// </summary>
/// <param name="range">The <see cref="Range"/> of columns to enumerate over.</param>
/// <returns>An <see cref="IEnumerator"/> that enumerates over a given <see cref="Range"/> of columns of this matrix.</returns>
public override IEnumerator<KeyValuePair<int, Vector>> ColumnEnumerator(Range range)
{
- throw new NotImplementedException();
+ if (range.Start >= Columns || range.End >= Columns)
+ {
+ throw new ArgumentOutOfRangeException("range", Strings.OutOfRangeException);
+ }
+ for (int i = range.Start; i <= range.End; i += range.Stride)
+ {
+ yield return new KeyValuePair<int, Vector>(i, Column(i));
+ }
}
/// <summary>
@@ -587,7 +684,10 @@
/// <returns>An <see cref="IEnumerator"/> that enumerates over the columns of this matrix.</returns>
public override IEnumerator<KeyValuePair<int, Vector>> ColumnEnumerator()
{
- throw new NotImplementedException();
+ for (int i = 0; i <= Columns; i++)
+ {
+ yield return new KeyValuePair<int, Vector>(i, Column(i));
+ }
}
/// <summary>
@@ -597,7 +697,9 @@
/// <returns>A vector containing the copied elements.</returns>
public override Vector Row(int index)
{
- throw new NotImplementedException();
+ Vector result = VectorBuilder.CreateVector(Rows, VectorType.Sparse);
+ Row(index, result);
+ return result;
}
/// <summary>
@@ -605,10 +707,38 @@
/// </summary>
/// <param name="index">The row to copy.</param>
/// <param name="result">The vector to copy the row into.</param>
- /// <exception cref="ArgumentNullException">If the result vector is <c>null</c>.</exception>
+ /// <exception cref="ArgumentNullException">If the result matrix is null.</exception>
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negative,
+ /// or greater than or equal to the number of rows.</exception>
+ /// <exception cref="NotConformableException">If the result vector is of the incorrect size.</exception>
public override void Row(int index, Vector result)
{
- throw new NotImplementedException();
+ if (result == null)
+ {
+ throw new ArgumentNullException();
+ }
+
+ if (index >= Rows || index < 0)
+ {
+ throw new ArgumentOutOfRangeException("index", Strings.OutOfRangeException);
+ }
+
+ if (result.Count != Columns)
+ {
+ throw new NotConformableException();
+ }
+
+ // The data is stored in compressed row storage so all we have to do is find the
+ // begin and end of the row and we can quickly iterate over that.
+ int begin;
+ int end;
+ FindRowStartAndEnd(index, out begin,out end);
+
+ for (int i = begin; i < end + 1; i++)
+ {
+ int insertPosition = mColumnIndices[i];
+ result[insertPosition] = mValues[i];
+ }
}
/// <summary>
@@ -619,17 +749,72 @@
/// <returns>a vector containing the copied elements.</returns>
public override Vector Row(int index, Range range)
{
- throw new NotImplementedException();
+ // the actual size of the vector is the number of elements in the
+ // range. This should be the difference between start and end + 1 (<-- off by one thing)
+ // and then the number of times the stride fits in that difference.
+ int size = range.End - range.Start;
+ size = size / range.Stride + 1;
+
+ Vector result = VectorBuilder.CreateVector(size, VectorType.Sparse);
+ Row(index, range, result);
+ return result;
}
/// <summary>
+ /// Copies the given <see cref="Range"/> of a row into an <see cref="Vector"/>.
+ /// </summary>
+ /// <param name="index">The column to copy.</param>
+ /// <param name="range">The <see cref="Range"/> of elements to copy.</param>
+ /// <param name="result">The <see cref="Vector"/> to copy the row into.</param>
+ /// <returns>A <see cref="Vector"/> containing the copied elements.</returns>
+ /// <exception cref="ArgumentNullException">if the result matrix is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <c>result.Count != (range.End - range.Start) / range.Stride + 1</c>.</exception>
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="range"/> specifies a range
+ /// outside the row's range. </exception>
+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="index"/> is negitive,
+ /// or greater than or equal to the number of rows.</exception>
+ public override void Row(int index, Range range, Vector result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (index >= Rows || index < 0)
+ {
+ throw new ArgumentOutOfRangeException("index", Strings.OutOfRangeException);
+ }
+ if (range.Start >= Columns || range.End >= Columns)
+ {
+ throw new ArgumentOutOfRangeException("range", Strings.OutOfRangeException);
+ }
+
+ for (int i = range.Start; i < range.End; i += range.Stride)
+ {
+ int itemIndex;
+ if (!FindItem(index, i, out itemIndex))
+ {
+ continue;
+ }
+
+ result[i] = mValues[itemIndex];
+ }
+ }
+
+ /// <summary>
/// Returns an <see cref="IEnumerator"/> that enumerates the rows of this matrix.
/// </summary>
/// <param name="range">the <see cref="Range"/> of rows to enumerate over.</param>
/// <returns>an <see cref="IEnumerator"/> that enumerates over a given <see cref="Range"/> of rowss of this matrix.</returns>
public override IEnumerator<KeyValuePair<int, Vector>> RowEnumerator(Range range)
{
- throw new NotImplementedException();
+ if (range.Start >= Rows || range.End >= Rows)
+ {
+ throw new ArgumentOutOfRangeException("range", Strings.OutOfRangeException);
+ }
+ for (int i = range.Start; i <= range.End; i += range.Stride)
+ {
+ yield return new KeyValuePair<int, Vector>(i, Row(i));
+ }
}
/// <summary>
@@ -638,7 +823,10 @@
/// <returns>an <see cref="IEnumerator"/> that enumerates over the rows of this matrix.</returns>
public override IEnumerator<KeyValuePair<int, Vector>> RowEnumerator()
{
- throw new NotImplementedException();
+ for (int i = 0; i <= Rows; i++)
+ {
+ yield return new KeyValuePair<int, Vector>(i, Row(i));
+ }
}
///<summary>Calculates the condition number of this matrix.</summary>
Modified: trunk/LinearAlgebra/SparseVector.cs
===================================================================
--- trunk/LinearAlgebra/SparseVector.cs 2006-01-25 12:50:47 UTC (rev 192)
+++ trunk/LinearAlgebra/SparseVector.cs 2006-01-25 13:59:09 UTC (rev 193)
@@ -9,7 +9,7 @@
/// <summary>
/// A vector class that only stores non-zero values.
/// </summary>
- public sealed class SparseVector : Vector
+ internal sealed class SparseVector : Vector
{
/// <summary>
/// The array containing the actual values. Only the non-zero values are stored
|