From 2b204badd79020f3a12914d7830551d056a59d22 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 30 Aug 2012 00:02:43 +0200 Subject: [PATCH] LA Storage: add dense vector storage --- .../Storage/DenseVectorStorage.cs | 136 ++++++++++++++++++ src/Numerics/Numerics.csproj | 1 + src/Portable/Portable.csproj | 3 + 3 files changed, 140 insertions(+) create mode 100644 src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs new file mode 100644 index 00000000..ab7a5321 --- /dev/null +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -0,0 +1,136 @@ +using System; +using MathNet.Numerics.Properties; + +namespace MathNet.Numerics.LinearAlgebra.Storage +{ + public class DenseVectorStorage : VectorStorage + where T : struct, IEquatable, 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; + } + + /// + /// Retrieves the requested element without range checking. + /// + public override T At(int index) + { + return Data[index]; + } + + /// + /// Sets the element without range checking. + /// + 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); + } + + /// Parameters assumed to be validated already. + public override void CopyTo(VectorStorage target, bool skipClearing = false) + { + var denseTarget = target as DenseVectorStorage; + if (denseTarget != null) + { + CopyTo(denseTarget); + return; + } + + // FALL BACK + + for (int i = 0; i < Length; i++) + { + target.At(i, Data[i]); + } + } + + void CopyTo(DenseVectorStorage 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 target, + int sourceIndex, int targetIndex, int count, + bool skipClearing = false) + { + var denseTarget = target as DenseVectorStorage; + if (denseTarget != null) + { + CopySubVectorTo(denseTarget, sourceIndex, targetIndex, count); + return; + } + + // FALL BACK + + base.CopySubVectorTo(target, sourceIndex, targetIndex, count, skipClearing); + } + + void CopySubVectorTo(DenseVectorStorage 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); + } + } +} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 658891fc..18d2fdbb 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -140,6 +140,7 @@ + diff --git a/src/Portable/Portable.csproj b/src/Portable/Portable.csproj index db4fecad..68192d1f 100644 --- a/src/Portable/Portable.csproj +++ b/src/Portable/Portable.csproj @@ -891,6 +891,9 @@ LinearAlgebra\Storage\DenseColumnMajorMatrixStorage.cs + + LinearAlgebra\Storage\DenseVectorStorage.cs + LinearAlgebra\Storage\DiagonalMatrixStorage.cs