Browse Source

LA Storage: add vector storage base

pull/47/merge
Christoph Ruegg 14 years ago
parent
commit
4e7d54b2da
  1. 11
      src/Numerics/LinearAlgebra/Generic/Common.cs
  2. 4
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  3. 55
      src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs
  4. 199
      src/Numerics/LinearAlgebra/Storage/VectorStorage.cs
  5. 4
      src/Numerics/Numerics.csproj
  6. 6
      src/Portable/Portable.csproj

11
src/Numerics/LinearAlgebra/Generic/Common.cs

@ -33,17 +33,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// </summary>
internal static class Common
{
/// <summary>
/// Returns the maximum value.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <returns>The maximum value.</returns>
public static float Max(float a, float b)
{
return Math.Max(a, b);
}
/// <summary>
/// Sets the value of <c>1.0</c> for type T.
/// </summary>

4
src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs

@ -89,7 +89,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary>
/// True if the specified field can be set to any value.
/// Fall if the field is fixed, like an off-diagonal field on a diagonal matrix.
/// False if the field is fixed, like an off-diagonal field on a diagonal matrix.
/// </summary>
public virtual bool IsMutable(int row, int column)
{
@ -166,7 +166,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param>
public override sealed bool Equals(object obj)
{
return Equals(obj as MatrixStorage<T>);

55
src/Numerics/LinearAlgebra/Storage/VectorStorage.Validation.cs

@ -0,0 +1,55 @@
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
// ReSharper disable UnusedParameter.Global
public partial class VectorStorage<T>
{
protected void ValidateRange(int index)
{
if (index < 0 || index >= Length)
{
throw new ArgumentOutOfRangeException("index");
}
}
protected void ValidateSubVectorRange(VectorStorage<T> target,
int sourceIndex, int targetIndex, int count)
{
if (count < 1)
{
throw new ArgumentOutOfRangeException("count", Resources.ArgumentMustBePositive);
}
// Verify Source
if (sourceIndex >= Length || sourceIndex < 0)
{
throw new ArgumentOutOfRangeException("sourceIndex");
}
var sourceMax = sourceIndex + count;
if (sourceMax > Length)
{
throw new ArgumentOutOfRangeException("count");
}
// Verify Target
if (targetIndex >= target.Length || targetIndex < 0)
{
throw new ArgumentOutOfRangeException("targetIndex");
}
var targetMax = targetIndex + count;
if (targetMax > target.Length)
{
throw new ArgumentOutOfRangeException("count");
}
}
}
// ReSharper restore UnusedParameter.Global
}

199
src/Numerics/LinearAlgebra/Storage/VectorStorage.cs

@ -0,0 +1,199 @@
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage
{
public abstract partial class VectorStorage<T> : IEquatable<VectorStorage<T>>
where T : struct, IEquatable<T>, IFormattable
{
// [ruegg] public fields are OK here
public readonly int Length;
protected VectorStorage(int length)
{
if (length <= 0)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMustBePositive);
}
Length = length;
}
/// <summary>
/// Gets or sets the value at the given index, with range checking.
/// </summary>
/// <param name="index">
/// The index of the element.
/// </param>
/// <value>The value to get or set.</value>
/// <remarks>This method is ranged checked. <see cref="At(int)"/> and <see cref="At(int,T)"/>
/// to get and set values without range checking.</remarks>
public T this[int index]
{
get
{
ValidateRange(index);
return At(index);
}
set
{
ValidateRange(index);
At(index, value);
}
}
/// <summary>
/// Retrieves the requested element without range checking.
/// </summary>
/// <param name="index">The index of the element.</param>
/// <returns>The requested element.</returns>
/// <remarks>Not range-checked.</remarks>
public abstract T At(int index);
/// <summary>
/// Sets the element without range checking.
/// </summary>
/// <param name="index">The index 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>
public abstract void At(int index, T value);
/// <summary>
/// True if all fields of this vector can be set to any value.
/// False if some fields are fixed.
/// </summary>
public virtual bool IsFullyMutable
{
get { return true; }
}
/// <summary>
/// True if the specified field can be set to any value.
/// False if the field is fixed.
/// </summary>
public virtual bool IsMutable(int index)
{
return true;
}
public virtual void Clear()
{
for (var i = 0; i < Length; i++)
{
At(i, default(T));
}
}
public virtual void Clear(int index, int count)
{
for (var i = index; i < index + count; i++)
{
At(i, default(T));
}
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
/// </returns>
public virtual bool Equals(VectorStorage<T> other)
{
// Reject equality when the argument is null or has a different shape.
if (other == null)
{
return false;
}
if (Length != other.Length)
{
return false;
}
// Accept if the argument is the same object as this.
if (ReferenceEquals(this, other))
{
return true;
}
// If all else fails, perform element wise comparison.
for (var index = 0; index < Length; index++)
{
if (!At(index).Equals(other.At(index)))
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param>
public override sealed bool Equals(object obj)
{
return Equals(obj as MatrixStorage<T>);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
public override int GetHashCode()
{
var hashNum = Math.Min(Length, 25);
int hash = 17;
unchecked
{
for (var i = 0; i < hashNum; i++)
{
hash = hash*31 + At(i).GetHashCode();
}
}
return hash;
}
/// <remarks>Parameters assumed to be validated already.</remarks>
public virtual void CopyTo(VectorStorage<T> target, bool skipClearing = false)
{
for (int i = 0; i < Length; i++)
{
target.At(i, At(i));
}
}
public virtual void CopySubVectorTo(VectorStorage<T> target,
int sourceIndex, int targetIndex, int count,
bool skipClearing = false)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (ReferenceEquals(this, target))
{
throw new NotSupportedException();
}
ValidateSubVectorRange(target, sourceIndex, targetIndex, count);
for (int i = sourceIndex, ii = targetIndex; i < sourceIndex + count; i++, ii++)
{
target.At(ii, At(i));
}
}
}
}

4
src/Numerics/Numerics.csproj

@ -353,6 +353,8 @@
<Compile Include="LinearAlgebra\Storage\MatrixStorage.cs" />
<Compile Include="LinearAlgebra\Storage\SparseCompressedRowMatrixStorage.cs" />
<Compile Include="LinearAlgebra\Storage\DiagonalMatrixStorage.cs" />
<Compile Include="LinearAlgebra\Storage\VectorStorage.cs" />
<Compile Include="LinearAlgebra\Storage\VectorStorage.Validation.cs" />
<Compile Include="Permutation.cs" />
<Compile Include="Distributions\Continuous\Beta.cs" />
<Compile Include="Distributions\Continuous\ContinuousUniform.cs" />
@ -489,4 +491,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

6
src/Portable/Portable.csproj

@ -903,6 +903,12 @@
<Compile Include="..\Numerics\LinearAlgebra\Storage\SparseCompressedRowMatrixStorage.cs">
<Link>LinearAlgebra\Storage\SparseCompressedRowMatrixStorage.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Storage\VectorStorage.cs">
<Link>LinearAlgebra\Storage\VectorStorage.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Storage\VectorStorage.Validation.cs">
<Link>LinearAlgebra\Storage\VectorStorage.Validation.cs</Link>
</Compile>
<Compile Include="..\Numerics\NumberTheory\IntegerTheory.cs">
<Link>NumberTheory\IntegerTheory.cs</Link>
</Compile>

Loading…
Cancel
Save