Browse Source

LA Storage: IMatrixStorage

la-knuth
Christoph Ruegg 14 years ago
parent
commit
3fd92a7e1d
  1. 49
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  2. 51
      src/Numerics/LinearAlgebra/Storage/IMatrixStorage.cs
  3. 60
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
  4. 59
      src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs
  5. 1
      src/Numerics/Numerics.csproj
  6. 3
      src/Portable/Portable.csproj

49
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -1,9 +1,10 @@
using System;
using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
internal class DenseColumnMajorMatrixStorage<T>
internal class DenseColumnMajorMatrixStorage<T> : IMatrixStorage<T>
where T : struct, IEquatable<T>, IFormattable
{
// [ruegg] public fields are OK here
@ -95,6 +96,42 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
Array.Clear(Data, 0, Data.Length);
}
public void CopyTo(IMatrixStorage<T> target, bool skipClearing = false)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
{
CopyTo(denseTarget);
return;
}
// FALL BACK
if (ReferenceEquals(this, target))
{
return;
}
if (target == null)
{
throw new ArgumentNullException("target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount);
throw new ArgumentException(message, "target");
}
for (int j = 0, offset = 0; j < ColumnCount; j++, offset += RowCount)
{
for (int i = 0; i < RowCount; i++)
{
target.At(i, j, Data[i + offset]);
}
}
}
public void CopyTo(DenseColumnMajorMatrixStorage<T> target)
{
if (ReferenceEquals(this, target))
@ -142,5 +179,15 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
Array.Copy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount);
}
}
int IMatrixStorage<T>.RowCount
{
get { return RowCount; }
}
int IMatrixStorage<T>.ColumnCount
{
get { return ColumnCount; }
}
}
}

51
src/Numerics/LinearAlgebra/Storage/IMatrixStorage.cs

@ -0,0 +1,51 @@
using System;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
public interface IMatrixStorage<T> where T : struct, IEquatable<T>, IFormattable
{
int RowCount { get; }
int ColumnCount { get; }
/// <summary>
/// Gets or sets the value at the given row and column, with range checking.
/// </summary>
/// <param name="row">
/// The row of the element.
/// </param>
/// <param name="column">
/// The column of the element.
/// </param>
/// <value>The value to get or set.</value>
/// <remarks>This method is ranged checked. <see cref="At(int,int)"/> and <see cref="At(int,int,T)"/>
/// to get and set values without range checking.</remarks>
T this[int row, int column] { get; set; }
/// <summary>
/// Retrieves the requested element without range checking.
/// </summary>
/// <param name="row">
/// The row of the element.
/// </param>
/// <param name="column">
/// The column of the element.
/// </param>
/// <returns>
/// The requested element.
/// </returns>
/// <remarks>Not range-checked.</remarks>
T At(int row, int column);
/// <summary>
/// Sets the element without range checking.
/// </summary>
/// <param name="row"> The row of the element. </param>
/// <param name="column"> The column of the element. </param>
/// <param name="value"> The value to set the element to. </param>
/// <remarks>WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks.</remarks>
void At(int row, int column, T value);
void Clear();
void CopyTo(IMatrixStorage<T> target, bool skipClearing = false);
}
}

60
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -3,7 +3,7 @@ using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
internal class SparseCompressedRowMatrixStorage<T>
internal class SparseCompressedRowMatrixStorage<T> : IMatrixStorage<T>
where T : struct, IEquatable<T>, IFormattable
{
// [ruegg] public fields are OK here
@ -266,6 +266,54 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return delta;
}
public void CopyTo(IMatrixStorage<T> target, bool skipClearing = false)
{
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
{
CopyTo(sparseTarget);
return;
}
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
{
CopyTo(denseTarget, skipClearing);
return;
}
// FALL BACK
if (target == null)
{
throw new ArgumentNullException("target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount);
throw new ArgumentException(message, "target");
}
if (!skipClearing)
{
target.Clear();
}
if (ValueCount != 0)
{
for (int row = 0; row < RowCount; row++)
{
var startIndex = RowPointers[row];
var endIndex = row < RowPointers.Length - 1 ? RowPointers[row + 1] : ValueCount;
for (var j = startIndex; j < endIndex; j++)
{
target.At(row, ColumnIndices[j], Values[j]);
}
}
}
}
public void CopyTo(SparseCompressedRowMatrixStorage<T> target)
{
if (ReferenceEquals(this, target))
@ -327,5 +375,15 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
}
}
int IMatrixStorage<T>.RowCount
{
get { return RowCount; }
}
int IMatrixStorage<T>.ColumnCount
{
get { return ColumnCount; }
}
}
}

59
src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs

@ -3,7 +3,7 @@ using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
internal class SparseDiagonalMatrixStorage<T>
internal class SparseDiagonalMatrixStorage<T> : IMatrixStorage<T>
where T : struct, IEquatable<T>, IFormattable
{
// [ruegg] public fields are OK here
@ -105,6 +105,53 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
Array.Clear(Data, 0, Data.Length);
}
public void CopyTo(IMatrixStorage<T> target, bool skipClearing = false)
{
var diagonalTarget = target as SparseDiagonalMatrixStorage<T>;
if (diagonalTarget != null)
{
CopyTo(diagonalTarget);
return;
}
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
{
CopyTo(denseTarget, skipClearing);
return;
}
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
{
CopyTo(sparseTarget, skipClearing);
return;
}
// FALL BACK
if (target == null)
{
throw new ArgumentNullException("target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount);
throw new ArgumentException(message, "target");
}
if (!skipClearing)
{
target.Clear();
}
for (int i = 0; i < Data.Length; i++)
{
target.At(i, i, Data[i]);
}
}
public void CopyTo(SparseDiagonalMatrixStorage<T> target)
{
if (ReferenceEquals(this, target))
@ -282,5 +329,15 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
}
}
int IMatrixStorage<T>.RowCount
{
get { return RowCount; }
}
int IMatrixStorage<T>.ColumnCount
{
get { return ColumnCount; }
}
}
}

1
src/Numerics/Numerics.csproj

@ -332,6 +332,7 @@
<Compile Include="LinearAlgebra\Generic\Vector.cs" />
<Compile Include="LinearAlgebra\Single\Vector.cs" />
<Compile Include="LinearAlgebra\Storage\DenseColumnMajorMatrixStorage.cs" />
<Compile Include="LinearAlgebra\Storage\IMatrixStorage.cs" />
<Compile Include="LinearAlgebra\Storage\SparseCompressedRowMatrixStorage.cs" />
<Compile Include="LinearAlgebra\Storage\SparseDiagonalMatrixStorage.cs" />
<Compile Include="Permutation.cs" />

3
src/Portable/Portable.csproj

@ -894,6 +894,9 @@
<Compile Include="..\Numerics\LinearAlgebra\Storage\DenseColumnMajorMatrixStorage.cs">
<Link>LinearAlgebra\Storage\DenseColumnMajorMatrixStorage.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Storage\IMatrixStorage.cs">
<Link>LinearAlgebra\Storage\IMatrixStorage.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Storage\SparseCompressedRowMatrixStorage.cs">
<Link>LinearAlgebra\Storage\SparseCompressedRowMatrixStorage.cs</Link>
</Compile>

Loading…
Cancel
Save