10 changed files with 459 additions and 21 deletions
@ -0,0 +1,196 @@ |
|||||
|
// <copyright file="DenseVector.cs" company="Math.NET">
|
||||
|
// Math.NET Numerics, part of the Math.NET Project
|
||||
|
// http://mathnet.opensourcedotnet.info
|
||||
|
// Copyright (c) 2009 Math.NET
|
||||
|
// Permission is hereby granted, free of charge, to any person
|
||||
|
// obtaining a copy of this software and associated documentation
|
||||
|
// files (the "Software"), to deal in the Software without
|
||||
|
// restriction, including without limitation the rights to use,
|
||||
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
|
// copies of the Software, and to permit persons to whom the
|
||||
|
// Software is furnished to do so, subject to the following
|
||||
|
// conditions:
|
||||
|
// The above copyright notice and this permission notice shall be
|
||||
|
// included in all copies or substantial portions of the Software.
|
||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
// </copyright>
|
||||
|
namespace MathNet.Numerics.LinearAlgebra.Double |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
using Properties; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// A vector using dense storage.
|
||||
|
/// </summary>
|
||||
|
public class DenseVector : Vector |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DenseVector"/> class with a given size.
|
||||
|
/// </summary>
|
||||
|
/// <param name="size">
|
||||
|
/// the size of the vector.
|
||||
|
/// </param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// If <paramref name="size"/> is less than one.
|
||||
|
/// </exception>
|
||||
|
public DenseVector(int size) |
||||
|
: base(size) |
||||
|
{ |
||||
|
Data = new double[size]; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DenseVector"/> class with a given size
|
||||
|
/// and each element set to the given value;
|
||||
|
/// </summary>
|
||||
|
/// <param name="size">
|
||||
|
/// the size of the vector.
|
||||
|
/// </param>
|
||||
|
/// <param name="value">
|
||||
|
/// the value to set each element to.
|
||||
|
/// </param>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// If <paramref name="size"/> is less than one.
|
||||
|
/// </exception>
|
||||
|
public DenseVector(int size, double value) |
||||
|
: this(size) |
||||
|
{ |
||||
|
for (var index = 0; index < Data.Length; index++) |
||||
|
{ |
||||
|
Data[index] = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="DenseVector"/> class by
|
||||
|
/// copying the values from another.
|
||||
|
/// </summary>
|
||||
|
/// <param name="other">
|
||||
|
/// The matrix to create the new matrix from.
|
||||
|
/// </param>
|
||||
|
public DenseVector(Vector other) |
||||
|
: this(other.Count) |
||||
|
{ |
||||
|
var vector = other as DenseVector; |
||||
|
if (vector == null) |
||||
|
{ |
||||
|
// using enumerators since they will be more efficient for copying sparse matrices
|
||||
|
foreach (var item in other.GetIndexedEnumerator()) |
||||
|
{ |
||||
|
Data[item.Key] = item.Value; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Buffer.BlockCopy(vector.Data, 0, Data, 0, Data.Length * Constants.SizeOfDouble); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the vector's data.
|
||||
|
/// </summary>
|
||||
|
/// <value>The vector's data.</value>
|
||||
|
internal double[] Data |
||||
|
{ |
||||
|
get; |
||||
|
private set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>Gets or sets the value at the given <paramref name="index"/>.</summary>
|
||||
|
/// <param name="index">The index of the value to get or set.</param>
|
||||
|
/// <returns>The value of the vector at the given <paramref name="index"/>.</returns>
|
||||
|
/// <exception cref="IndexOutOfRangeException">If <paramref name="index"/> is negative or
|
||||
|
/// greater than the size of the vector.</exception>
|
||||
|
public override double this[int index] |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return Data[index]; |
||||
|
} |
||||
|
|
||||
|
set |
||||
|
{ |
||||
|
Data[index] = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a matrix with the given dimensions using the same storage type
|
||||
|
/// as this vector.
|
||||
|
/// </summary>
|
||||
|
/// <param name="rows">
|
||||
|
/// The number of rows.
|
||||
|
/// </param>
|
||||
|
/// <param name="columns">
|
||||
|
/// The number of columns.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// A matrix with the given dimensions.
|
||||
|
/// </returns>
|
||||
|
public override Matrix CreateMatrix(int rows, int columns) |
||||
|
{ |
||||
|
throw new NotImplementedException(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a <strong>Vector</strong> of the given size using the same storage type
|
||||
|
/// as this vector.
|
||||
|
/// </summary>
|
||||
|
/// <param name="size">
|
||||
|
/// The size of the <strong>Vector</strong> to create.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The new <c>Vector</c>.
|
||||
|
/// </returns>
|
||||
|
public override Vector CreateVector(int size) |
||||
|
{ |
||||
|
return new DenseVector(size); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Copies the values of this vector into the target vector.
|
||||
|
/// </summary>
|
||||
|
/// <param name="target">
|
||||
|
/// The vector to copy elements into.
|
||||
|
/// </param>
|
||||
|
/// <exception cref="ArgumentNullException">
|
||||
|
/// If <paramref name="target"/> is <see langword="null"/>.
|
||||
|
/// </exception>
|
||||
|
/// <exception cref="ArgumentException">
|
||||
|
/// If <paramref name="target"/> is not the same size as this vector.
|
||||
|
/// </exception>
|
||||
|
public override void CopyTo(Vector target) |
||||
|
{ |
||||
|
if (target == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException("target"); |
||||
|
} |
||||
|
|
||||
|
if (Count != target.Count) |
||||
|
{ |
||||
|
throw new ArgumentException("target", Resources.ArgumentVectorsSameLengths); |
||||
|
} |
||||
|
|
||||
|
var otherVector = target as DenseVector; |
||||
|
if (otherVector == null) |
||||
|
{ |
||||
|
for (var index = 0; index < Data.Length; index++) |
||||
|
{ |
||||
|
target[index] = Data[index]; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Buffer.BlockCopy(Data, 0, otherVector.Data, 0, Data.Length * Constants.SizeOfDouble); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
||||
|
{ |
||||
|
using LinearAlgebra.Double; |
||||
|
|
||||
|
public class DenseVectorTests : VectorTests |
||||
|
{ |
||||
|
protected override MathNet.Numerics.LinearAlgebra.Double.Vector CreateVector(int size) |
||||
|
{ |
||||
|
return new DenseVector(size); |
||||
|
} |
||||
|
|
||||
|
protected override Vector CreateVector(IList<double> data) |
||||
|
{ |
||||
|
var vector = new DenseVector(data.Count); |
||||
|
for(var index = 0; index < data.Count; index++) |
||||
|
{ |
||||
|
vector[index] = data[index]; |
||||
|
} |
||||
|
|
||||
|
return vector; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,194 @@ |
|||||
|
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
// <copyright file="VectorTests.cs" company="">
|
||||
|
// </copyright>
|
||||
|
// <summary>
|
||||
|
// vector tests.
|
||||
|
// </summary>
|
||||
|
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
|
||||
|
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
using MathNet.Numerics.LinearAlgebra.Double; |
||||
|
|
||||
|
using MbUnit.Framework; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The vector tests.
|
||||
|
/// </summary>
|
||||
|
public abstract class VectorTests |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The test data.
|
||||
|
/// </summary>
|
||||
|
private readonly double[] _data = { 1, 2, 3, 4, 5 }; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can clone vector.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
public void CanCloneVector() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var clone = vector.Clone(); |
||||
|
|
||||
|
Assert.AreEqual(vector.Count, clone.Count); |
||||
|
for (var index = 0; index < _data.Length; index++) |
||||
|
{ |
||||
|
Assert.AreEqual(vector[index], clone[index]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can convert vector to string.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
public void CanConvertVectorToString() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var str = vector.ToString(); |
||||
|
|
||||
|
Assert.AreEqual("1,2,3,4,5", str); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can copy partial vector to another.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[MultipleAsserts] |
||||
|
public void CanCopyPartialVectorToAnother() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var other = CreateVector(_data.Length); |
||||
|
|
||||
|
vector.CopyTo(other, 2, 2, 2); |
||||
|
|
||||
|
Assert.AreEqual(0.0, other[0]); |
||||
|
Assert.AreEqual(0.0, other[1]); |
||||
|
Assert.AreEqual(3.0, other[2]); |
||||
|
Assert.AreEqual(4.0, other[3]); |
||||
|
Assert.AreEqual(0.0, other[4]); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can copy vector to another.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[MultipleAsserts] |
||||
|
public void CanCopyVectorToAnother() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var other = CreateVector(_data.Length); |
||||
|
|
||||
|
vector.CopyTo(other); |
||||
|
|
||||
|
for (var index = 0; index < _data.Length; index++) |
||||
|
{ |
||||
|
Assert.AreEqual(vector[index], other[index]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can create matrix.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[Ignore] |
||||
|
public void CanCreateMatrix() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can create vector.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
public void CanCreateVector() |
||||
|
{ |
||||
|
var expected = CreateVector(5); |
||||
|
var actual = expected.CreateVector(5); |
||||
|
Assert.AreEqual(expected.GetType(), actual.GetType(), "vectors are same type."); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can enumerate over vector.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[MultipleAsserts] |
||||
|
public void CanEnumerateOverVector() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var index = 0; |
||||
|
|
||||
|
foreach (var element in vector) |
||||
|
{ |
||||
|
Assert.AreEqual(index + 1, element); |
||||
|
index++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can equate vectors.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[MultipleAsserts] |
||||
|
public void CanEquateVectors() |
||||
|
{ |
||||
|
var vector1 = CreateVector(_data); |
||||
|
var vector2 = CreateVector(_data); |
||||
|
var vector3 = CreateVector(4); |
||||
|
Assert.IsTrue(vector1.Equals(vector1)); |
||||
|
Assert.IsTrue(vector1.Equals(vector2)); |
||||
|
Assert.IsFalse(vector1.Equals(vector3)); |
||||
|
Assert.IsFalse(vector1.Equals(null)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can get indexed enumerator.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[MultipleAsserts] |
||||
|
public void CanGetIndexedEnumerator() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var index = 0; |
||||
|
|
||||
|
foreach (var pair in vector.GetIndexedEnumerator()) |
||||
|
{ |
||||
|
Assert.AreEqual(index, pair.Key); |
||||
|
Assert.AreEqual(++index, pair.Value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// can get indexed enumerator over range.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
[MultipleAsserts] |
||||
|
public void CanGetIndexedEnumeratorOverRange() |
||||
|
{ |
||||
|
var vector = CreateVector(_data); |
||||
|
var index = 2; |
||||
|
|
||||
|
foreach (var pair in vector.GetIndexedEnumerator(2, 2)) |
||||
|
{ |
||||
|
Assert.AreEqual(index, pair.Key); |
||||
|
Assert.AreEqual(++index, pair.Value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// throws argument exception if size is not positive.
|
||||
|
/// </summary>
|
||||
|
[Test] |
||||
|
public void ThrowsArgumentExceptionIfSizeIsNotPositive() |
||||
|
{ |
||||
|
Assert.Throws<ArgumentException>(() => CreateVector(-1)); |
||||
|
Assert.Throws<ArgumentException>(() => CreateVector(0)); |
||||
|
} |
||||
|
|
||||
|
protected abstract Vector CreateVector(int size); |
||||
|
|
||||
|
protected abstract Vector CreateVector(IList<double> data); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue