Author: patrick
Date: 2006-01-25 07:03:12 -0500 (Wed, 25 Jan 2006)
New Revision: 189
Modified:
trunk/LinearAlgebra/DenseMatrix.cs
trunk/LinearAlgebra/Matrix.cs
trunk/LinearAlgebra/MatrixBuilder.cs
trunk/LinearAlgebra/Solvers/BicgStabSolver.cs
trunk/LinearAlgebra/Solvers/ConvergenceMonitor.cs
trunk/LinearAlgebra/Solvers/IIterativeMatrixSolver.cs
trunk/LinearAlgebra/Solvers/IPreconditioner.cs
trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs
trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs
trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs
trunk/LinearAlgebra/Solvers/Preconditioners/IlutpHeapSorter.cs
trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs
trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs
trunk/LinearAlgebra/SparseMatrix.cs
trunk/LinearAlgebra/SparseVector.cs
trunk/NumericalLibrary.csproj
trunk/Properties/AssemblyInfo.cs
trunk/UnitTests/LinearAlgebra/SparseMatrixTest.cs
trunk/UnitTests/Properties/AssemblyInfo.cs
trunk/UnitTests/UnitTests.csproj
Log:
- Put the iterative solvers back into the vs project. All compile now with the new Vector and Matrix base classes.
- Fixing the SparseMatrix.
- Fixed errors in the SparseVector.GetEnumerator methods.
Modified: trunk/LinearAlgebra/DenseMatrix.cs
===================================================================
--- trunk/LinearAlgebra/DenseMatrix.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/DenseMatrix.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -210,7 +210,7 @@
/// <returns>The value at the given indices.</returns>
/// <exception cref="IndexOutOfRangeException">If <paramref name="row"/> * <paramref name="column"/> is
/// greater than <c>DenseMatrix.Rows * DenseMatrix.Columns</c></exception>.
- public override double ValueAt(int row, int column)
+ protected internal override double ValueAt(int row, int column)
{
return data[column * Rows + row];
}
Modified: trunk/LinearAlgebra/Matrix.cs
===================================================================
--- trunk/LinearAlgebra/Matrix.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Matrix.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -80,7 +80,7 @@
/// <returns>The value at the given indices.</returns>
/// <exception cref="IndexOutOfRangeException">Depending on the implementation, an <see cref="IndexOutOfRangeException"/>
/// may be thrown if one of the indices is outside the dimensions of the matrix.</exception>
- public abstract double ValueAt(int row, int column);
+ protected internal abstract double ValueAt(int row, int column);
/// <summary>
/// Set the value at the given indices to the given value. Note: This method is not ranged checked. If the
Modified: trunk/LinearAlgebra/MatrixBuilder.cs
===================================================================
--- trunk/LinearAlgebra/MatrixBuilder.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/MatrixBuilder.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -1,6 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Text;
using dnAnalytics.LinearAlgebra.Native;
namespace dnAnalytics.LinearAlgebra
@@ -157,7 +155,8 @@
ret = new DenseMatrix(rows, columns, useNativeLibrary);
break;
case MatrixType.Sparse:
- throw new NotImplementedException();
+ ret = new SparseMatrix(rows, columns);
+ break;
}
return ret;
}
@@ -249,7 +248,15 @@
}
break;
case MatrixType.Sparse:
- throw new NotImplementedException();
+ ret = new SparseMatrix(rows, columns);
+ for (int i = 0; i < rows; i++)
+ {
+ for (int j = 0; j < columns; j++)
+ {
+ ret.ValueAt(i, j, value);
+ }
+ }
+ break;
}
return ret;
}
@@ -260,7 +267,7 @@
/// </summary>
/// <param name="order">The order of the <see cref="Matrix"/> .</param>
/// <returns>An identity <see cref="Matrix"/> of the given order.</returns>
- [Obsolete("This is a temporary methid and will be replaced.", false)]
+ [Obsolete("This is a temporary method and will be replaced.", false)]
public static Matrix CreateIdentityMatrix(int order)
{
return new DenseMatrix(order, false);
Modified: trunk/LinearAlgebra/Solvers/BicgStabSolver.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/BicgStabSolver.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/BicgStabSolver.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -161,9 +161,9 @@
/// left hand side vector can be reused. If so it would be better to
/// use the overloaded <c>Solve</c> method.
/// </remarks>
- public IVector Solve(IVector vector)
+ public Vector Solve(Vector vector)
{
- IVector result = new DenseVector(mCoefficients.Rows);
+ Vector result = VectorBuilder.CreateVector(mCoefficients.Rows, VectorType.Dense);
Solve(vector, result);
return result;
}
@@ -173,7 +173,7 @@
/// </summary>
/// <param name="vector">The left hand side vector, <code>x</code>.</param>
/// <param name="result">The right hand side vector, <code>b</code>.</param>
- public void Solve(IVector vector, IVector result)
+ public void Solve(Vector vector, Vector result)
{
// Set the convergence monitor
if (mConvergenceMonitor == null)
@@ -190,30 +190,28 @@
// Compute r_0 = b - Ax_0 for some initial guess x_0
// In this case we take x_0 = vector
// This is basically a SAXPY so it could be made a lot faster
- IVector residuals = new DenseVector(mCoefficients.Rows);
+ Vector residuals = VectorBuilder.CreateVector(mCoefficients.Rows, VectorType.Dense);
mCoefficients.Multiply(result, residuals);
residuals.Multiply(-1);
residuals.Add(vector);
//Choose r~ (for example, r~ = r_0)
- IVector tempResiduals = new DenseVector(residuals.Count);
+ Vector tempResiduals = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
residuals.CopyTo(tempResiduals);
// create five temporary vectors needed to hold temporary
// coefficients. All vectors are mangled in each iteration.
// These are defined here to prevent stressing the garbage collector
- IVector p = new DenseVector(residuals.Count);
- IVector pDash = new DenseVector(residuals.Count);
- IVector nu = new DenseVector(residuals.Count);
- IVector s = new DenseVector(residuals.Count);
- IVector sDash = new DenseVector(residuals.Count);
- IVector t = new DenseVector(residuals.Count);
+ Vector p = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
+ Vector pDash = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
+ Vector nu = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
+ Vector s = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
+ Vector sDash = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
+ Vector t = VectorBuilder.CreateVector(residuals.Count, VectorType.Dense);
// create some temporary double variables that are needed
// to hold values in between iterations
double currentRho = 0;
- double oldRho = 0;
- double beta = 0;
double alpha = 0;
double omega = 0;
@@ -221,7 +219,7 @@
while (mConvergenceMonitor.ShouldCalculationContinue(iterationNumber, result, residuals))
{
// rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1)
- oldRho = currentRho;
+ double oldRho = currentRho;
currentRho = tempResiduals.DotProduct(residuals);
// if (rho_(i-1) == 0) // METHOD FAILS
@@ -238,7 +236,7 @@
else
{
// beta_(i-1) = (rho_(i-1)/rho_(i-2))(alpha_(i-1)/omega(i-1))
- beta = (currentRho/oldRho)*(alpha/omega);
+ double beta = (currentRho/oldRho)*(alpha/omega);
// p_i = r_(i-1) + beta_(i-1)(p_(i-1) - omega_(i-1) * nu_(i-1))
nu.Multiply(omega);
p.Subtract(nu);
Modified: trunk/LinearAlgebra/Solvers/ConvergenceMonitor.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/ConvergenceMonitor.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/ConvergenceMonitor.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -105,7 +105,7 @@
/// <param name="solutionVector">The current solution vector.</param>
/// <param name="residualVector">The current residual vector.</param>
/// <returns><c>true</c> if the iterative process may continue; otherwise, <c>false</c>.</returns>
- public bool ShouldCalculationContinue(int currentIterationNumber, IVector solutionVector, IVector residualVector)
+ public bool ShouldCalculationContinue(int currentIterationNumber, Vector solutionVector, Vector residualVector)
{
// check the iteration count. If we are over the maximum number of
// iterations we stop
Modified: trunk/LinearAlgebra/Solvers/IIterativeMatrixSolver.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/IIterativeMatrixSolver.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/IIterativeMatrixSolver.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -28,13 +28,13 @@
/// </remarks>
/// <param name="vector">The right hand side vector, <code>b</code>.</param>
/// <returns>The left hand side vector, <code>x</code>.</returns>
- IVector Solve(IVector vector);
+ Vector Solve(Vector vector);
/// <summary>
/// Solves the standard matrix equation <code>Ax = b</code>.
/// </summary>
/// <param name="vector">The left hand side vector, <code>x</code>.</param>
/// <param name="result">The right hand side vector, <code>b</code>.</param>
- void Solve(IVector vector, IVector result);
+ void Solve(Vector vector, Vector result);
}
}
\ No newline at end of file
Modified: trunk/LinearAlgebra/Solvers/IPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/IPreconditioner.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/IPreconditioner.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -37,13 +37,13 @@
/// </remarks>
/// <param name="vector">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
- IVector Solve(IVector vector);
+ Vector Solve(Vector vector);
/// <summary>
/// Solves the matrix equation <code>Ax = b</code>.
/// </summary>
/// <param name="vector">The right hand side vector.</param>
/// <param name="result">The left hand side vector.</param>
- void Solve(IVector vector, IVector result);
+ void Solve(Vector vector, Vector result);
}
}
\ No newline at end of file
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalIluFactorization.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -178,7 +178,7 @@
/// </summary>
/// <param name="vector">The right hand side vector.</param>
/// <param name="result">The left hand side vector.</param>
- public void Solve(IVector vector, IVector result)
+ public void Solve(Vector vector, Vector result)
{
/*
Allow to solve system Mx = y
@@ -225,7 +225,7 @@
z_i = d_ii^-1 * (y_i - SUM_(j<i) l_ij * z_j)
}
*/
- IVector rowVector = new SparseVector(mCoefficientMatrix.Rows);
+ Vector rowVector = VectorBuilder.CreateVector(mCoefficientMatrix.Rows, VectorType.Sparse);
for (int i = 0; i < mCoefficientMatrix.Rows; i++)
{
mL.Row(i, rowVector);
@@ -272,7 +272,7 @@
/// </remarks>
/// <param name="vector">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
- public IVector Solve(IVector vector)
+ public Vector Solve(Vector vector)
{
if (vector == null)
{
@@ -284,7 +284,7 @@
throw new NotConformableException();
}
- IVector result = new DenseVector(vector.Count);
+ Vector result = VectorBuilder.CreateVector(vector.Count, VectorType.Dense);
Solve(vector, result);
return result;
}
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/DiagonalPreconditioner.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -75,7 +75,7 @@
/// </summary>
/// <param name="vector">The right hand side vector.</param>
/// <param name="result">The left hand side vector.</param>
- public void Solve(IVector vector, IVector result)
+ public void Solve(Vector vector, Vector result)
{
// Solve Dx = y
// Since we stored the inverses of the diagonal values we can do:
@@ -117,7 +117,7 @@
/// </remarks>
/// <param name="vector">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
- public IVector Solve(IVector vector)
+ public Vector Solve(Vector vector)
{
if (vector == null)
{
@@ -129,7 +129,7 @@
throw new NotConformableException();
}
- IVector result = new DenseVector(vector.Count);
+ Vector result = VectorBuilder.CreateVector(vector.Count, VectorType.Dense);
Solve(vector, result);
return result;
}
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/IluFactorization.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -137,7 +137,7 @@
/// </summary>
/// <param name="vector">The right hand side vector.</param>
/// <param name="result">The left hand side vector.</param>
- public void Solve(IVector vector, IVector result)
+ public void Solve(Vector vector, Vector result)
{
if (vector == null)
{
@@ -169,7 +169,7 @@
}
NOTE: l_ii should be 1 because u_ii has to be the value
*/
- IVector rowValues = new DenseVector(mLuDecomposition.Rows);
+ Vector rowValues = VectorBuilder.CreateVector(mLuDecomposition.Rows, VectorType.Dense);
for (int i = 0; i < mLuDecomposition.Rows; i++)
{
mLuDecomposition.Row(i, rowValues);
@@ -216,7 +216,7 @@
/// </remarks>
/// <param name="vector">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
- public IVector Solve(IVector vector)
+ public Vector Solve(Vector vector)
{
if (vector == null)
{
@@ -228,7 +228,7 @@
throw new NotConformableException();
}
- IVector result = new DenseVector(vector.Count);
+ Vector result = VectorBuilder.CreateVector(vector.Count, VectorType.Dense);
Solve(vector, result);
return result;
}
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/IlutpHeapSorter.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/IlutpHeapSorter.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/IlutpHeapSorter.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -11,7 +11,7 @@
internal static class IlutpHeapSorter
{
public static void SortDoubleIndicesDecreasing(int lowerBound, int upperBound,
- int[] sortedIndices, IVector values)
+ int[] sortedIndices, Vector values)
{
// Move all the indices that we're interested in forward
// Ignore the rest
@@ -30,7 +30,7 @@
}
- private static void HeapSortDoublesIndices(int lowerBound, int upperBound, int[] sortedIndices, IVector values)
+ private static void HeapSortDoublesIndices(int lowerBound, int upperBound, int[] sortedIndices, Vector values)
{
int start = (upperBound - lowerBound + 1)/2 - 1 + lowerBound;
int end = (upperBound - lowerBound + 1) - 1 + lowerBound;
@@ -45,7 +45,7 @@
}
}
- private static void BuildDoubleIndexHeap(int start, int count, int[] sortedIndices, IVector values)
+ private static void BuildDoubleIndexHeap(int start, int count, int[] sortedIndices, Vector values)
{
while (start >= 0)
{
@@ -54,7 +54,7 @@
}
}
- private static void SiftDoubleIndices(int[] sortedIndices, IVector values, int begin, int count)
+ private static void SiftDoubleIndices(int[] sortedIndices, Vector values, int begin, int count)
{
int root = begin;
int child;
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/IlutpPreconditioner.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -239,8 +239,8 @@
{
mPivots[i] = i;
}
- IVector workVector = new DenseVector(mCoefficientMatrix.Rows);
- IVector rowVector = new DenseVector(mCoefficientMatrix.Columns);
+ Vector workVector = VectorBuilder.CreateVector(mCoefficientMatrix.Rows, VectorType.Dense);
+ Vector rowVector = VectorBuilder.CreateVector(mCoefficientMatrix.Columns, VectorType.Dense);
int[] indexSorting = new int[mCoefficientMatrix.Rows];
// spaceLeft = lfilNnz * nnz(A)
int spaceLeft = (int) mFillLevel*mCoefficientMatrix.NonZeros;
@@ -373,7 +373,7 @@
mWasPreconditionerCreated = true;
}
- private void PivotRow(IVector row)
+ private void PivotRow(Vector row)
{
Hashtable knownPivots = new Hashtable();
// pivot the row
@@ -412,7 +412,7 @@
return false;
}
- private static void SwapColumns(IMatrix matrix, int firstColumn, int secondColumn)
+ private static void SwapColumns(Matrix matrix, int firstColumn, int secondColumn)
{
for (int i = 0; i < matrix.Rows; i++)
{
@@ -422,7 +422,7 @@
}
}
- private static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, IVector values)
+ private static void FindLargestItems(int lowerBound, int upperBound, int[] sortedIndices, Vector values)
{
// Copy the indices for the values into the array
for (int i = 0; i < upperBound + 1 - lowerBound; i++)
@@ -445,7 +445,7 @@
/// </summary>
/// <param name="vector">The right hand side vector.</param>
/// <param name="result">The left hand side vector.</param>
- public void Solve(IVector vector, IVector result)
+ public void Solve(Vector vector, Vector result)
{
if (vector == null)
{
@@ -469,7 +469,7 @@
// Solve equation here
// Pivot(vector, result);
// Solve L*Y = B(piv,:)
- IVector rowValues = new DenseVector(mL.Rows);
+ Vector rowValues = VectorBuilder.CreateVector(mL.Rows, VectorType.Dense);
for (int i = 0; i < mL.Rows; i++)
{
mL.Row(i, rowValues);
@@ -499,11 +499,11 @@
// We have a column pivot so we only need to pivot the
// end result not the incomming right hand side vector
- IVector temp = new DenseVector(result);
+ Vector temp = result.Clone();
Pivot(temp, result);
}
- private void Pivot(IVector vector, IVector result)
+ private void Pivot(Vector vector, Vector result)
{
for (int i = 0; i < mPivots.Length; i++)
{
@@ -521,7 +521,7 @@
/// </remarks>
/// <param name="vector">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
- public IVector Solve(IVector vector)
+ public Vector Solve(Vector vector)
{
if (vector == null)
{
@@ -533,7 +533,7 @@
throw new NotConformableException();
}
- IVector result = new DenseVector(vector.Count);
+ Vector result = VectorBuilder.CreateVector(vector.Count,VectorType.Dense);
Solve(vector, result);
return result;
}
Modified: trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs
===================================================================
--- trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/Solvers/Preconditioners/UnitPreconditioner.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -55,7 +55,7 @@
/// </summary>
/// <param name="vector">The right hand side vector.</param>
/// <param name="result">The left hand side vector.</param>
- public void Solve(IVector vector, IVector result)
+ public void Solve(Vector vector, Vector result)
{
if (vector == null)
{
@@ -85,7 +85,7 @@
/// </remarks>
/// <param name="vector">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
- public IVector Solve(IVector vector)
+ public Vector Solve(Vector vector)
{
if (vector == null)
{
@@ -97,7 +97,7 @@
throw new NotConformableException();
}
- IVector result = new DenseVector(vector.Count);
+ Vector result = VectorBuilder.CreateVector(vector.Count, VectorType.Dense);
Solve(vector, result);
return result;
}
Modified: trunk/LinearAlgebra/SparseMatrix.cs
===================================================================
--- trunk/LinearAlgebra/SparseMatrix.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/SparseMatrix.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -68,7 +68,7 @@
/// <param name="dimension">The number of rows and columns in the matrix.</param>
public SparseMatrix(int dimension) : this(dimension, dimension)
{
- // Redirects to CDoubleSparseMatrix(int rows, int columns)
+ // Redirects to SparseMatrix(int rows, int columns)
}
/// <summary>
@@ -471,9 +471,9 @@
/// <returns>The value at the given indices.</returns>
/// <exception cref="IndexOutOfRangeException">Depending on the implementation, an <see cref="IndexOutOfRangeException"/>
/// may be thrown if one of the indices is outside the dimensions of the matrix.</exception>
- public override double ValueAt(int row, int column)
+ protected internal override double ValueAt(int row, int column)
{
- throw new NotImplementedException();
+ return this[row, column];
}
/// <summary>
Modified: trunk/LinearAlgebra/SparseVector.cs
===================================================================
--- trunk/LinearAlgebra/SparseVector.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/LinearAlgebra/SparseVector.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -670,21 +670,9 @@
public override IEnumerator<KeyValuePair<int,double>> GetEnumerator()
{
// Iterate over the vector size and return each value.
- for (int i = 0; i < mSize; i++)
+ for (int i = 0; i < mValueCount; i++)
{
- // See if the item exists. If so then return the value otherwise
- // return zero.
- // NOTE: we could potentially keep track of the last 'real' index
- // that we used. That way we wouldn't have to do a search.
- int internalIndex;
- if (FindItem(i, out internalIndex))
- {
- yield return new KeyValuePair<int, double>(i, mValues[internalIndex]);
- }
- else
- {
- yield return new KeyValuePair<int, double>(i, 0.0);
- }
+ yield return new KeyValuePair<int, double>(mIndices[i], mValues[i]);
}
}
@@ -703,18 +691,25 @@
{
throw new ArgumentOutOfRangeException("range");
}
-
- for (int i = range.Start; i <= range.End; i += range.Stride)
+
+ // Find the start
+ int begin;
+ FindItem(range.Start, out begin);
+
+ // Find the end
+ int end;
+ FindItem(range.End, out end);
+
+ // for each item between start and end
+ for (int i = begin; i <= end; i++)
{
- int internalIndex;
- if (FindItem(i, out internalIndex))
+ int difference = mIndices[i] - range.Start;
+ if (difference % range.Stride != 0)
{
- yield return new KeyValuePair<int, double>(i, mValues[internalIndex]);
+ continue;
}
- else
- {
- yield return new KeyValuePair<int, double>(i, 0.0);
- }
+
+ yield return new KeyValuePair<int, double>(mIndices[i], mValues[i]);
}
}
Modified: trunk/NumericalLibrary.csproj
===================================================================
--- trunk/NumericalLibrary.csproj 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/NumericalLibrary.csproj 2006-01-25 12:03:12 UTC (rev 189)
@@ -117,8 +117,19 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="LinearAlgebra\Range.cs" />
+ <Compile Include="LinearAlgebra\Solvers\BicgStabSolver.cs" />
+ <Compile Include="LinearAlgebra\Solvers\ConvergenceMonitor.cs" />
+ <Compile Include="LinearAlgebra\Solvers\IIterativeMatrixSolver.cs" />
+ <Compile Include="LinearAlgebra\Solvers\IPreconditioner.cs" />
<Compile Include="LinearAlgebra\Solvers\ISolver.cs" />
<Compile Include="LinearAlgebra\Decomposition\Svd.cs" />
+ <Compile Include="LinearAlgebra\Solvers\Preconditioners\DiagonalIluFactorization.cs" />
+ <Compile Include="LinearAlgebra\Solvers\Preconditioners\DiagonalPreconditioner.cs" />
+ <Compile Include="LinearAlgebra\Solvers\Preconditioners\IluFactorization.cs" />
+ <Compile Include="LinearAlgebra\Solvers\Preconditioners\IlutpHeapSorter.cs" />
+ <Compile Include="LinearAlgebra\Solvers\Preconditioners\IlutpPreconditioner.cs" />
+ <Compile Include="LinearAlgebra\Solvers\Preconditioners\UnitPreconditioner.cs" />
+ <Compile Include="LinearAlgebra\Solvers\SolutionStatus.cs" />
<Compile Include="LinearAlgebra\SparseMatrix.cs" />
<Compile Include="LinearAlgebra\SparseVector.cs">
<SubType>Code</SubType>
@@ -152,9 +163,6 @@
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
- <ItemGroup>
- <Folder Include="LinearAlgebra\Solvers\Preconditioners\" />
- </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Modified: trunk/Properties/AssemblyInfo.cs
===================================================================
--- trunk/Properties/AssemblyInfo.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/Properties/AssemblyInfo.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -24,9 +24,6 @@
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
-// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("62f04261-c566-4563-9ab2-e87dbbd5a605")]
-
// Version information for an assembly consists of the following four values:
//
// Major Version
Modified: trunk/UnitTests/LinearAlgebra/SparseMatrixTest.cs
===================================================================
--- trunk/UnitTests/LinearAlgebra/SparseMatrixTest.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/UnitTests/LinearAlgebra/SparseMatrixTest.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -1,247 +1,45 @@
+using dnAnalytics.LinearAlgebra;
using NUnit.Framework;
-namespace dnA.Math
+namespace dnAnalytics.UnitTests.LinearAlgebra
{
[TestFixture]
- public sealed class CDoubleSparseMatrixTest : AbstractDoubleMatrixTest
+ public sealed class CDoubleSparseMatrixTest : AbstractMatrixTest
{
- [SetUp]
- public void SetUp()
+ public override Matrix GetMatrix(int order)
{
- // Empty for now
+ return MatrixBuilder.CreateMatrix(order, MatrixType.Sparse);
}
- [TearDown]
- public void TearDown()
+ public override Matrix GetMatrix(int m, int n)
{
- // Empty for now
+ return MatrixBuilder.CreateMatrix(m, n, MatrixType.Sparse);
}
- protected override AbstractDoubleMatrix CreateMatrix(int rowCount, int columnCount)
+ public override Matrix GetMatrix(double[,] data)
{
- return new DoubleSparseMatrix(rowCount, columnCount);
- }
-
- [Test]
- public void GetNonExistingValue()
- {
- int rowCount = 10;
- int columnCount = 10;
- DoubleSparseMatrix matrix = new DoubleSparseMatrix(rowCount, columnCount);
-
- Assert.AreEqual(0, matrix.NonZeros, "#01");
-
- // Set some values. Use multiple per row to check the search
- // algorithm that the matrix class uses
- for (int i = 0; i < matrix.Rows; i++)
+ int rows = data.GetLength(0);
+ int cols = data.GetLength(1);
+ Matrix ret = MatrixBuilder.CreateMatrix(rows, cols, MatrixType.Sparse);
+ for (int i = 0; i < rows; i++)
{
- if (UtilityFunctions.IsEven(i))
+ for (int j = 0; j < cols; j++)
{
- // place values at the even j numbers
- // Order is garbled to check that that algoritm
- // actually doesn't care about the order in which
- // elements are added
- matrix[i, 6] = 10 * (i + 1) + (6 + 1);
- matrix[i, 4] = 10 * (i + 1) + (4 + 1);
- matrix[i, 2] = 10 * (i + 1) + (2 + 1);
- matrix[i, 8] = 10 * (i + 1) + (8 + 1);
- matrix[i, 0] = 10 * (i + 1) + (0 + 1);
+ ret[i, j] = data[i, j];
}
- else
- {
- // place values at the even j numbers
- matrix[i, 1] = 10 * (i + 1) + (1 + 1);
- matrix[i, 3] = 10 * (i + 1) + (3 + 1);
- matrix[i, 5] = 10 * (i + 1) + (5 + 1);
- matrix[i, 7] = 10 * (i + 1) + (7 + 1);
- matrix[i, 9] = 10 * (i + 1) + (9 + 1);
- }
}
- Assert.AreEqual(rowCount * (columnCount / 2), matrix.NonZeros, "#01");
-
- // Check that all numbers in the matrix are equal to zero
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if ((UtilityFunctions.IsEven(i) && UtilityFunctions.IsEven(j)) ||
- (UtilityFunctions.IsOdd(i) && UtilityFunctions.IsOdd(j)))
- {
- Assert.AreEqual(10 * (i + 1) + (j + 1), matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
+ return ret;
}
- [Test]
- public void SetExistingValueToZero()
+ public override Vector GetVector(int size)
{
- int rowCount = 10;
- int columnCount = 10;
- DoubleSparseMatrix matrix = new DoubleSparseMatrix(rowCount, columnCount);
-
- // now set the diagonal values and check if we can retrieve them
- // again.
- for (int i = 0; i < matrix.Rows; i++)
- {
- matrix[i, i] = i + 1;
- }
-
- Assert.AreEqual(matrix.Rows, matrix.NonZeros, "#01");
-
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if (i != j)
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(i + 1, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
-
- // Now set one of the diagonal values to zero and check that it is
- // actually gone
- matrix[5, 5] = 0.0;
-
- Assert.AreEqual(matrix.Rows - 1, matrix.NonZeros, "#03");
-
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if ((i != j) || ((i == 5) && (j == 5)))
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(i + 1, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
+ return VectorBuilder.CreateVector(size, VectorType.Dense);
}
- [Test]
- public void SetNonExistingValueToZero()
+ public override Vector GetVector(double[] source)
{
- int rowCount = 10;
- int columnCount = 10;
- DoubleSparseMatrix matrix = new DoubleSparseMatrix(rowCount, columnCount);
-
- // now set the diagonal values and check if we can retrieve them
- // again.
- for (int i = 0; i < matrix.Rows; i++)
- {
- matrix[i, i] = i + 1;
- }
-
- Assert.AreEqual(matrix.Rows, matrix.NonZeros, "#01");
-
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if (i != j)
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(i + 1, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
-
- // Now set one of the zero values to zero and check that nothing
- // has changed
- matrix[5, 6] = 0.0;
-
- Assert.AreEqual(matrix.Rows, matrix.NonZeros, "#03");
-
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if (i != j)
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(i + 1, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
+ return VectorBuilder.CreateVector(source, VectorType.Dense);
}
-
- [Test]
- public void SetNonExistingValue()
- {
- int rowCount = 10;
- int columnCount = 10;
- DoubleSparseMatrix matrix = new DoubleSparseMatrix(rowCount, columnCount);
-
- // now set the diagonal values and check if we can retrieve them
- // again.
- for (int i = 0; i < matrix.Rows; i++)
- {
- matrix[i, i] = i + 1;
- }
-
- Assert.AreEqual(matrix.Rows, matrix.NonZeros, "#01");
-
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if (i != j)
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(i + 1, matrix[i, j], epsilon, "#02-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
-
- // Now set one of the zero values to zero and check that nothing
- // has changed
- matrix[5, 6] = 56.0;
-
- Assert.AreEqual(matrix.Rows + 1, matrix.NonZeros, "#03");
-
- for (int i = 0; i < matrix.Rows; i++)
- {
- for (int j = 0; j < matrix.Columns; j++)
- {
- if ((i != j) && ((i != 5) || (j != 6)))
- {
- Assert.AreEqual(0.0, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- if (i == j)
- {
- Assert.AreEqual(i + 1, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- else
- {
- Assert.AreEqual(56, matrix[i, j], epsilon, "#04-(" + i.ToString() + ", " + j.ToString() + ")");
- }
- }
- }
- }
- }
}
}
\ No newline at end of file
Modified: trunk/UnitTests/Properties/AssemblyInfo.cs
===================================================================
--- trunk/UnitTests/Properties/AssemblyInfo.cs 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/UnitTests/Properties/AssemblyInfo.cs 2006-01-25 12:03:12 UTC (rev 189)
@@ -21,9 +21,6 @@
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
-// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("2615366c-8aa4-415f-b74b-c54d3d8abcf5")]
-
// Version information for an assembly consists of the following four values:
//
// Major Version
Modified: trunk/UnitTests/UnitTests.csproj
===================================================================
--- trunk/UnitTests/UnitTests.csproj 2006-01-25 11:53:12 UTC (rev 188)
+++ trunk/UnitTests/UnitTests.csproj 2006-01-25 12:03:12 UTC (rev 189)
@@ -65,6 +65,10 @@
<Compile Include="LinearAlgebra\IO\DelimitedMatrixWriter.cs" />
<Compile Include="LinearAlgebra\IO\MatrixMarketReader.cs" />
<Compile Include="LinearAlgebra\IO\MatrixMarketWriter.cs" />
+ <Compile Include="LinearAlgebra\SparseMatrixTest.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="LinearAlgebra\SparseVectorTest.cs" />
<Compile Include="Math\Complex.cs" />
<Compile Include="Math\ComplexMath.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
|