Browse Source

LA Storage: add dense vector storage

la-knuth
Christoph Ruegg 14 years ago
parent
commit
2b204badd7
  1. 136
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs
  2. 1
      src/Numerics/Numerics.csproj
  3. 3
      src/Portable/Portable.csproj

136
src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs

@ -0,0 +1,136 @@
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
public class DenseVectorStorage<T> : VectorStorage<T>
where T : struct, IEquatable<T>, IFormattable
{
// [ruegg] public fields are OK here
public readonly T[] Data;
internal DenseVectorStorage(int length)
: base(length)
{
Data = new T[length];
}
internal DenseVectorStorage(int length, T[] data)
: base(length)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
if (data.Length != length)
{
throw new ArgumentOutOfRangeException("data", string.Format(Resources.ArgumentArrayWrongLength, length));
}
Data = data;
}
/// <summary>
/// Retrieves the requested element without range checking.
/// </summary>
public override T At(int index)
{
return Data[index];
}
/// <summary>
/// Sets the element without range checking.
/// </summary>
public override void At(int index, T value)
{
Data[index] = value;
}
public override void Clear()
{
Array.Clear(Data, 0, Data.Length);
}
public override void Clear(int index, int count)
{
Array.Clear(Data, index, count);
}
/// <remarks>Parameters assumed to be validated already.</remarks>
public override void CopyTo(VectorStorage<T> target, bool skipClearing = false)
{
var denseTarget = target as DenseVectorStorage<T>;
if (denseTarget != null)
{
CopyTo(denseTarget);
return;
}
// FALL BACK
for (int i = 0; i < Length; i++)
{
target.At(i, Data[i]);
}
}
void CopyTo(DenseVectorStorage<T> target)
{
if (ReferenceEquals(this, target))
{
return;
}
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Length != target.Length)
{
var message = string.Format(Resources.ArgumentMatrixDimensions2, Length, target.Length);
throw new ArgumentException(message, "target");
}
//Buffer.BlockCopy(Data, 0, target.Data, 0, Data.Length * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)));
Array.Copy(Data, 0, target.Data, 0, Data.Length);
}
public override void CopySubVectorTo(VectorStorage<T> target,
int sourceIndex, int targetIndex, int count,
bool skipClearing = false)
{
var denseTarget = target as DenseVectorStorage<T>;
if (denseTarget != null)
{
CopySubVectorTo(denseTarget, sourceIndex, targetIndex, count);
return;
}
// FALL BACK
base.CopySubVectorTo(target, sourceIndex, targetIndex, count, skipClearing);
}
void CopySubVectorTo(DenseVectorStorage<T> target,
int sourceIndex, int targetIndex, int count)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (ReferenceEquals(this, target))
{
throw new NotSupportedException();
}
ValidateSubVectorRange(target, sourceIndex, targetIndex, count);
//Buffer.BlockCopy(Data, sourceIndex, target.Data, targetIndex, count * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)));
Array.Copy(Data, sourceIndex, target.Data, targetIndex, count);
}
}
}

1
src/Numerics/Numerics.csproj

@ -140,6 +140,7 @@
<Compile Include="Distributions\Multivariate\InverseWishart.cs" />
<Compile Include="Distributions\Multivariate\MatrixNormal.cs" />
<Compile Include="Distributions\Multivariate\Wishart.cs" />
<Compile Include="LinearAlgebra\Storage\DenseVectorStorage.cs" />
<Compile Include="LinearAlgebra\Storage\MatrixStorage.Validation.cs" />
<Compile Include="LinearAlgebra\Complex32\ExtensionMethods.cs" />
<Compile Include="LinearAlgebra\Complex32\Factorization\Cholesky.cs" />

3
src/Portable/Portable.csproj

@ -891,6 +891,9 @@
<Compile Include="..\Numerics\LinearAlgebra\Storage\DenseColumnMajorMatrixStorage.cs">
<Link>LinearAlgebra\Storage\DenseColumnMajorMatrixStorage.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Storage\DenseVectorStorage.cs">
<Link>LinearAlgebra\Storage\DenseVectorStorage.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Storage\DiagonalMatrixStorage.cs">
<Link>LinearAlgebra\Storage\DiagonalMatrixStorage.cs</Link>
</Compile>

Loading…
Cancel
Save