Browse Source

vector: Added conversion operators to DenseVector

vector: added ToArray, ToColumnMatrix, and ToRowMatrix methods to the Vector class

vector: corrected execption parameter order in DenseVector
la-knuth
Marcus Cuda 17 years ago
parent
commit
a6e980ec8e
  1. 1
      src/.gitignore
  2. 97
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  3. 47
      src/Numerics/LinearAlgebra/Double/Vector.cs
  4. 25
      src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs
  5. 42
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

1
src/.gitignore

@ -3,3 +3,4 @@ bin
obj
*.user
*.suo
*.vsdoc

97
src/Numerics/LinearAlgebra/Double/DenseVector.cs

@ -90,11 +90,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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;
// }
Parallel.For(0, Count, index => this[index] = other[index]);
}
else
@ -115,15 +110,81 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Gets the vector's data.
/// Gets the vector's internal data.
/// </summary>
/// <value>The vector's data.</value>
/// <value>The vector's internal data.</value>
/// <remarks>Changing values in the array also changes the corresponding value in vector. Use with care.</remarks>
internal double[] Data
{
get;
private set;
}
/// <summary>
/// Returns a reference to the internal data structure.
/// </summary>
/// <param name="vector">The DenseVector whose internal data we are
/// returning.</param>
/// <returns>
/// A reference to the internal date of the given vector.
/// </returns>
public static implicit operator double[](DenseVector vector)
{
if (vector == null)
{
throw new ArgumentNullException();
}
return vector.Data;
}
/// <summary>
/// Returns a vector bound directly to a reference of the provided array.
/// </summary>
/// <param name="array">The array to bind to the DenseVector object.</param>
/// <returns>
/// A DenseVector whose values are bound to the given array.
/// </returns>
public static implicit operator DenseVector(double[] array)
{
if (array == null)
{
throw new ArgumentNullException();
}
return new DenseVector(array);
}
/// <summary>
/// Create a matrix based on this vector in column form (one single column).
/// </summary>
/// <returns>This vector as a column matrix.</returns>
public override Matrix ToColumnMatrix()
{
var matrix = new DenseMatrix(Count, 1);
for (var i = 0; i < Data.Length; i++)
{
matrix[i, 0] = Data[i];
}
return matrix;
}
/// <summary>
/// Create a matrix based on this vector in row form (one single row).
/// </summary>
/// <returns>This vector as a row matrix.</returns>
public override Matrix ToRowMatrix()
{
var matrix = new DenseMatrix(1, Count);
for (var i = 0; i < Data.Length; i++)
{
matrix[0, i] = Data[i];
}
return matrix;
}
/// <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>
@ -196,7 +257,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != target.Count)
{
throw new ArgumentException("target", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
var otherVector = target as DenseVector;
@ -243,7 +304,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != result.Count)
{
throw new ArgumentException("result", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CopyTo(result);
@ -265,7 +326,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != other.Count)
{
throw new ArgumentException("other", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
@ -298,12 +359,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != other.Count)
{
throw new ArgumentException("other", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException("result", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
@ -358,7 +419,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException("rightSide", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
var ret = leftSide.Clone();
@ -396,7 +457,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != result.Count)
{
throw new ArgumentException("result", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CopyTo(result);
@ -418,7 +479,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != other.Count)
{
throw new ArgumentException("other", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
@ -451,12 +512,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != other.Count)
{
throw new ArgumentException("other", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException("result", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
@ -510,7 +571,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException("rightSide", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
var ret = leftSide.Clone();

47
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -719,7 +719,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != target.Count)
{
throw new ArgumentException("target", Resources.ArgumentVectorsSameLength);
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target))
@ -784,6 +784,51 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Returns the data contained in the vector as an array.
/// </summary>
/// <returns>The vector's data as an array.</returns>
public virtual double[] ToArray()
{
var ret = new double[Count];
for (var i = 0; i < ret.Length; i++)
{
ret[i] = this[i];
}
return ret;
}
/// <summary>
/// Create a matrix based on this vector in column form (one single column).
/// </summary>
/// <returns>This vector as a column matrix.</returns>
public virtual Matrix ToColumnMatrix()
{
var matrix = CreateMatrix(Count, 1);
for (var i = 0; i < Count; i++)
{
matrix[i, 0] = this[i];
}
return matrix;
}
/// <summary>
/// Create a matrix based on this vector in row form (one single row).
/// </summary>
/// <returns>This vector as a row matrix.</returns>
public virtual Matrix ToRowMatrix()
{
var matrix = CreateMatrix(1, Count);
for (var i = 0; i < Count; i++)
{
matrix[0, i] = this[i];
}
return matrix;
}
#endregion
#region Implemented Interfaces

25
src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs

@ -40,12 +40,35 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
[MultipleAsserts]
public void CanCreateMatrix()
public void CanCreateDenseMatrix()
{
var vector = new DenseVector(3);
var matrix = vector.CreateMatrix(2, 3);
Assert.AreEqual(2, matrix.RowCount);
Assert.AreEqual(3, matrix.ColumnCount);
}
[Test]
[MultipleAsserts]
public void CanConvertDenseVectorToArray()
{
var data = new[] { 0.0, 1.0, 2.0, 3.0, 4.0 };
var vector = new DenseVector(data);
var array = (double[])vector;
Assert.IsInstanceOfType(typeof(double[]), array);
Assert.AreSame(vector.Data, array);
Assert.AreElementsEqual(vector, array);
}
[Test]
[MultipleAsserts]
public void CanConvertArrayToDenseVector()
{
var array = new[] { 0.0, 1.0, 2.0, 3.0, 4.0 };
var vector = (DenseVector)array;
Assert.IsInstanceOfType(typeof(DenseVector), vector);
Assert.AreElementsEqual(array, array);
}
}
}

42
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -161,6 +161,48 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
[Test]
[MultipleAsserts]
public void CanConvertVectorToArray()
{
var vector = new DenseVector(_data);
var array = vector.ToArray();
Assert.AreNotSame(array, vector.Data);
Assert.AreElementsEqual(vector, array);
}
[Test]
[MultipleAsserts]
public void CanConvertVectorToColumnMatrix()
{
var vector = new DenseVector(_data);
var matrix = vector.ToColumnMatrix();
Assert.AreEqual(vector.Count, matrix.RowCount);
Assert.AreEqual(1, matrix.ColumnCount);
for(var i = 0; i < vector.Count; i++)
{
Assert.AreEqual(vector[i], matrix[i, 0]);
}
}
[Test]
[MultipleAsserts]
public void CanConvertVectorToRowMatrix()
{
var vector = new DenseVector(_data);
var matrix = vector.ToRowMatrix();
Assert.AreEqual(vector.Count, matrix.ColumnCount);
Assert.AreEqual(1, matrix.RowCount);
for (var i = 0; i < vector.Count; i++)
{
Assert.AreEqual(vector[i], matrix[0, i]);
}
}
protected abstract Vector CreateVector(int size);
protected abstract Vector CreateVector(IList<double> data);

Loading…
Cancel
Save