//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 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.
//
namespace MathNet.Numerics.LinearAlgebra.Generic
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime;
using System.Text;
using Numerics;
using Properties;
using Storage;
using Threading;
///
/// Defines the generic class for Vector classes.
///
/// Supported data types are double, single, , and .
[Serializable]
public abstract class Vector :
#if PORTABLE
IFormattable, IEnumerable, IEquatable>
#else
IFormattable, IEnumerable, IEquatable>, ICloneable
#endif
where T : struct, IEquatable, IFormattable
{
///
/// The zero value for type T.
///
private static readonly T Zero = default(T);
///
/// The value of 1.0 for type T.
///
private static readonly T One = Common.SetOne();
///
/// Initializes a new instance of the Vector class.
///
protected Vector(VectorStorage storage)
{
Storage = storage;
Count = storage.Length;
}
///
/// Gets the raw vector data storage.
///
public VectorStorage Storage { get; private set; }
///
/// Gets the number of items.
///
public int Count { get; private set; }
/// Gets or sets the value at the given .
/// The index of the value to get or set.
/// The value of the vector at the given .
/// If is negative or
/// greater than the size of the vector.
public T this[int index]
{
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
get { return Storage[index]; }
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
set { Storage[index] = value;}
}
/// Gets the value at the given without range checking..
/// The index of the value to get or set.
/// The value of the vector at the given .
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public T At(int index)
{
return Storage.At(index);
}
/// Sets the at the given without range checking..
/// The index of the value to get or set.
/// The value to set.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
//[MethodImpl(MethodImplOptions.AggressiveInlining)] .Net 4.5 only
public void At(int index, T value)
{
Storage.At(index, value);
}
///
/// Resets all values to zero.
///
public void Clear()
{
Storage.Clear();
}
///
/// Sets all values of a subvector to zero.
///
public void ClearSubVector(int index, int count)
{
if (count < 1)
{
throw new ArgumentOutOfRangeException("count", Resources.ArgumentMustBePositive);
}
if (index + count > Count || index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
Storage.Clear(index, count);
}
///
/// Returns a deep-copy clone of the vector.
///
///
/// A deep-copy clone of the vector.
///
public Vector Clone()
{
var result = CreateVector(Count);
Storage.CopyTo(result.Storage, skipClearing: true);
return result;
}
///
/// Copies the values of this vector into the target vector.
///
///
/// The vector to copy elements into.
///
///
/// If is .
///
///
/// If is not the same size as this vector.
///
public void CopyTo(Vector target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (ReferenceEquals(this, target) || ReferenceEquals(Storage, target.Storage))
{
return;
}
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
Storage.CopyTo(target.Storage);
}
///
/// Copies the requested elements from this vector to another.
///
///
/// The vector to copy the elements to.
///
///
/// The element to start copying from.
///
///
/// The element to start copying to.
///
///
/// The number of elements to copy.
///
public void CopySubVectorTo(Vector destination, int sourceIndex, int targetIndex, int count)
{
if (destination == null)
{
throw new ArgumentNullException("destination");
}
// TODO: refactor range checks
Storage.CopySubVectorTo(destination.Storage, sourceIndex, targetIndex, count);
}
[Obsolete("Use CopySubVectorTo instead.")]
public void CopyTo(Vector destination, int sourceIndex, int targetIndex, int count)
{
CopySubVectorTo(destination, sourceIndex, targetIndex, count);
}
///
/// Creates a matrix with the given dimensions using the same storage type
/// as this vector.
///
///
/// The number of rows.
///
///
/// The number of columns.
///
///
/// A matrix with the given dimensions.
///
public abstract Matrix CreateMatrix(int rows, int columns);
///
/// Creates a Vector of the given size using the same storage type
/// as this vector.
///
///
/// The size of the Vector to create.
///
///
/// The new Vector.
///
public abstract Vector CreateVector(int size);
#region Elementary operations
///
/// Adds a scalar to each element of the vector.
///
///
/// The scalar to add.
///
/// A copy of the vector with the scalar added.
public virtual Vector Add(T scalar)
{
if (scalar.Equals(Zero))
{
return Clone();
}
var result = CreateVector(Count);
DoAdd(scalar, result);
return result;
}
///
/// Adds a scalar to each element of the vector and stores the result in the result vector.
///
///
/// The scalar to add.
///
///
/// The vector to store the result of the addition.
///
///
/// If the result vector is .
///
///
/// If this vector and are not the same size.
///
public virtual void Add(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoAdd(scalar, result);
}
///
/// Adds a scalar to each element of the vector and stores the result in the result vector.
///
///
/// The scalar to add.
///
///
/// The vector to store the result of the addition.
///
protected abstract void DoAdd(T scalar, Vector result);
///
/// Returns a copy of this vector.
///
///
/// This vector.
///
///
/// Added as an alternative to the unary addition operator.
///
public virtual Vector Plus()
{
return Clone();
}
///
/// Adds another vector to this vector.
///
///
/// The vector to add to this one.
///
/// A new vector containing the sum of both vectors.
///
/// If the other vector is .
///
///
/// If this vector and are not the same size.
///
public virtual Vector Add(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var result = CreateVector(Count);
DoAdd(other, result);
return result;
}
///
/// Adds another vector to this vector and stores the result into the result vector.
///
///
/// The vector to add to this one.
///
///
/// The vector to store the result of the addition.
///
///
/// If the other vector is .
///
///
/// If the result vector is .
///
///
/// If this vector and are not the same size.
///
///
/// If this vector and are not the same size.
///
public virtual void Add(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoAdd(other, result);
}
///
/// Adds another vector to this vector and stores the result into the result vector.
///
///
/// The vector to add to this one.
///
///
/// The vector to store the result of the addition.
///
protected abstract void DoAdd(Vector other, Vector result);
///
/// Subtracts a scalar from each element of the vector.
///
///
/// The scalar to subtract.
///
/// A new vector containing the subtraction of this vector and the scalar.
public virtual Vector Subtract(T scalar)
{
if (scalar.Equals(default(T)))
{
return Clone();
}
var result = CreateVector(Count);
DoSubtract(scalar, result);
return result;
}
///
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
///
///
/// The scalar to subtract.
///
///
/// The vector to store the result of the subtraction.
///
///
/// If the result vector is .
///
///
/// If this vector and are not the same size.
///
public virtual void Subtract(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoSubtract(scalar, result);
}
///
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
///
///
/// The scalar to subtract.
///
///
/// The vector to store the result of the subtraction.
///
protected abstract void DoSubtract(T scalar, Vector result);
///
/// Returns a negated vector.
///
///
/// The negated vector.
///
///
/// Added as an alternative to the unary negation operator.
///
public abstract Vector Negate();
///
/// Subtracts another vector from this vector.
///
///
/// The vector to subtract from this one.
///
/// A new vector containing the subtraction of the the two vectors.
///
/// If the other vector is .
///
///
/// If this vector and are not the same size.
///
public virtual Vector Subtract(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var result = CreateVector(Count);
DoSubtract(other, result);
return result;
}
///
/// Subtracts another vector to this vector and stores the result into the result vector.
///
///
/// The vector to subtract from this one.
///
///
/// The vector to store the result of the subtraction.
///
///
/// If the other vector is .
///
///
/// If the result vector is .
///
///
/// If this vector and are not the same size.
///
///
/// If this vector and are not the same size.
///
public virtual void Subtract(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoSubtract(other, result);
}
///
/// Subtracts another vector to this vector and stores the result into the result vector.
///
///
/// The vector to subtract from this one.
///
///
/// The vector to store the result of the subtraction.
///
protected abstract void DoSubtract(Vector other, Vector result);
///
/// Multiplies a scalar to each element of the vector.
///
///
/// The scalar to multiply.
///
/// A new vector that is the multiplication of the vector and the scalar.
public virtual Vector Multiply(T scalar)
{
if (scalar.Equals(One))
{
return Clone();
}
var result = CreateVector(Count);
DoMultiply(scalar, result);
return result;
}
///
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
///
///
/// The scalar to multiply.
///
///
/// The vector to store the result of the multiplication.
///
///
/// If the result vector is .
///
///
/// If this vector and are not the same size.
///
public virtual void Multiply(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoMultiply(scalar, result);
}
///
/// Multiplies a scalar to each element of the vector and stores the result in the result vector.
///
///
/// The scalar to multiply.
///
///
/// The vector to store the result of the multiplication.
///
protected abstract void DoMultiply(T scalar, Vector result);
///
/// Computes the dot product between this vector and another vector.
///
///
/// The other vector to add.
///
/// s
/// The result of the addition.
///
///
/// If is not of the same size.
///
///
/// If is .
///
public virtual T DotProduct(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
return DoDotProduct(other);
}
///
/// Computes the dot product between this vector and another vector.
///
///
/// The other vector to add.
///
/// s
/// The result of the addition.
///
protected abstract T DoDotProduct(Vector other);
///
/// Divides each element of the vector by a scalar.
///
///
/// The scalar to divide with.
///
/// A new vector that is the division of the vector and the scalar.
public virtual Vector Divide(T scalar)
{
if (scalar.Equals(One))
{
return Clone();
}
var result = CreateVector(Count);
DoDivide(scalar, result);
return result;
}
///
/// Divides each element of the vector by a scalar and stores the result in the result vector.
///
///
/// The scalar to divide with.
///
///
/// The vector to store the result of the division.
///
///
/// If the result vector is .
///
///
/// If this vector and are not the same size.
///
public virtual void Divide(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoDivide(scalar, result);
}
///
/// Divides each element of the vector by a scalar and stores the result in the result vector.
///
///
/// The scalar to divide with.
///
///
/// The vector to store the result of the division.
///
protected abstract void DoDivide(T scalar, Vector result);
///
/// Pointwise multiplies this vector with another vector.
///
/// The vector to pointwise multiply with this one.
/// A new vector which is the pointwise multiplication of the two vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public virtual Vector PointwiseMultiply(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var result = CreateVector(Count);
DoPointwiseMultiply(other, result);
return result;
}
///
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
///
/// The vector to pointwise multiply with this one.
/// The vector to store the result of the pointwise multiplication.
/// If the other vector is .
/// If the result vector is .
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public virtual void PointwiseMultiply(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseMultiply(other, result);
}
///
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
///
/// The vector to pointwise multiply with this one.
/// The vector to store the result of the pointwise multiplication.
protected abstract void DoPointwiseMultiply(Vector other, Vector result);
///
/// Pointwise divide this vector with another vector.
///
/// The vector to pointwise divide this one by.
/// A new vector which is the pointwise division of the two vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public virtual Vector PointwiseDivide(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var result = CreateVector(Count);
DoPointwiseDivide(other, result);
return result;
}
///
/// Pointwise divide this vector with another vector and stores the result into the result vector.
///
/// The vector to pointwise divide this one by.
/// The vector to store the result of the pointwise division.
/// If the other vector is .
/// If the result vector is .
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public virtual void PointwiseDivide(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseDivide(other, result);
}
///
/// Pointwise divide this vector with another vector and stores the result into the result vector.
///
///
/// The vector to pointwise divide this one by.
///
///
/// The result of the division.
///
protected abstract void DoPointwiseDivide(Vector other, Vector result);
///
/// Outer product of two vectors
///
/// First vector
/// Second vector
/// Matrix M[i,j] = u[i]*v[j]
/// If the u vector is .
/// If the v vector is .
public static Matrix OuterProduct(Vector u, Vector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = u.CreateMatrix(u.Count, v.Count);
for (var i = 0; i < u.Count; i++)
{
matrix.SetRow(i, v.Multiply(u[i]));
}
return matrix;
}
///
/// Outer product of this and another vector.
///
/// The vector to operate on.
///
/// Matrix M[i,j] = this[i] * v[j].
///
///
public Matrix OuterProduct(Vector v)
{
return OuterProduct(this, v);
}
///
/// Returns the value of the absolute minimum element.
///
/// The value of the absolute minimum element.
public abstract T AbsoluteMinimum();
///
/// Returns the index of the absolute minimum element.
///
/// The index of absolute minimum element.
public abstract int AbsoluteMinimumIndex();
///
/// Returns the value of the absolute maximum element.
///
/// The value of the absolute maximum element.
public abstract T AbsoluteMaximum();
///
/// Returns the index of the absolute maximum element.
///
/// The index of absolute maximum element.
public abstract int AbsoluteMaximumIndex();
///
/// Returns the value of maximum element.
///
/// The value of maximum element.
public virtual T Maximum()
{
return this[MaximumIndex()];
}
///
/// Returns the index of the absolute maximum element.
///
/// The index of absolute maximum element.
public abstract int MaximumIndex();
///
/// Returns the value of the minimum element.
///
/// The value of the minimum element.
public virtual T Minimum()
{
return this[MinimumIndex()];
}
///
/// Returns the index of the minimum element.
///
/// The index of minimum element.
public abstract int MinimumIndex();
///
/// Computes the sum of the vector's elements.
///
/// The sum of the vector's elements.
public abstract T Sum();
///
/// Computes the sum of the absolute value of the vector's elements.
///
/// The sum of the absolute value of the vector's elements.
public abstract T SumMagnitudes();
///
/// Computes the modulus for each element of the vector for the given divisor.
///
/// The divisor to use.
/// A vector containing the result.
public virtual Vector Modulus(T divisor)
{
var result = CreateVector(Count);
Modulus(divisor, result);
return result;
}
///
/// Computes the modulus for each element of the vector for the given divisor.
///
/// The divisor to use.
/// A vector to store the results in.
public virtual void Modulus(T divisor, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoModulus(divisor, result);
}
///
/// Computes the modulus for each element of the vector for the given divisor.
///
/// The divisor to use.
/// A vector to store the results in.
protected abstract void DoModulus(T divisor, Vector result);
#endregion
#region Arithmetic Operator Overloading
///
/// Returns a Vector containing the same values of .
///
/// This method is included for completeness.
/// The vector to get the values from.
/// A vector containing the same values as .
/// If is .
public static Vector operator +(Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Plus();
}
///
/// Adds two Vectors together and returns the results.
///
/// One of the vectors to add.
/// The other vector to add.
/// The result of the addition.
/// If and are not the same size.
/// If or is .
public static Vector operator +(Vector leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return leftSide.Add(rightSide);
}
///
/// Returns a Vector containing the negated values of .
///
/// The vector to get the values from.
/// A vector containing the negated values as .
/// If is .
public static Vector operator -(Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Negate();
}
///
/// Subtracts two Vectors and returns the results.
///
/// The vector to subtract from.
/// The vector to subtract.
/// The result of the subtraction.
/// If and are not the same size.
/// If or is .
public static Vector operator -(Vector leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return leftSide.Subtract(rightSide);
}
///
/// Multiplies a vector with a scalar.
///
/// The vector to scale.
/// The scalar value.
/// The result of the multiplication.
/// If is .
public static Vector operator *(Vector leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Multiply(rightSide);
}
///
/// Multiplies a vector with a scalar.
///
/// The scalar value.
/// The vector to scale.
/// The result of the multiplication.
/// If is .
public static Vector operator *(T leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Multiply(leftSide);
}
///
/// Computes the dot product between two Vectors.
///
/// The left row vector.
/// The right column vector.
/// The dot product between the two vectors.
/// If and are not the same size.
/// If or is .
public static T operator *(Vector leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return leftSide.DotProduct(rightSide);
}
///
/// Divides a vector with a scalar.
///
/// The vector to divide.
/// The scalar value.
/// The result of the division.
/// If is .
public static Vector operator /(Vector leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Divide(rightSide);
}
///
/// Computes the modulus of each element of the vector of the given divisor.
///
/// The vector whose elements we want to compute the modulus of.
/// The divisor to use,
/// The result of the calculation
/// If is .
public static Vector operator %(Vector leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Modulus(rightSide);
}
#endregion
#region Vector Norms
///
/// Computes the p-Norm.
///
///
/// The p value.
///
///
/// Scalar ret = (sum(abs(this[i])^p))^(1/p)
///
public abstract T Norm(double p);
///
/// Normalizes this vector to a unit vector with respect to the p-norm.
///
///
/// The p value.
///
///
/// This vector normalized to a unit vector with respect to the p-norm.
///
public abstract Vector Normalize(double p);
#endregion
///
/// Return vector with conjugate values of the source vector
///
/// Conjugated vector
public Vector Conjugate()
{
var retrunVector = CreateVector(Count);
Conjugate(retrunVector);
return retrunVector;
}
///
/// Conjugates vector and save result to
///
/// Target vector
public virtual void Conjugate(Vector target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
DoConjugate(target);
}
///
/// Conjugates vector and save result to
///
/// Target vector
protected abstract void DoConjugate(Vector target);
#region Copying and Conversion
///
/// Returns the data contained in the vector as an array.
///
///
/// The vector's data as an array.
///
public virtual T[] ToArray()
{
var ret = new T[Count];
for (var i = 0; i < ret.Length; i++)
{
ret[i] = At(i);
}
return ret;
}
///
/// Create a matrix based on this vector in column form (one single column).
///
///
/// This vector as a column matrix.
///
public virtual Matrix ToColumnMatrix()
{
var matrix = CreateMatrix(Count, 1);
for (var i = 0; i < Count; i++)
{
matrix.At(i, 0, this[i]);
}
return matrix;
}
///
/// Create a matrix based on this vector in row form (one single row).
///
///
/// This vector as a row matrix.
///
public virtual Matrix ToRowMatrix()
{
var matrix = CreateMatrix(1, Count);
for (var i = 0; i < Count; i++)
{
matrix.At(0, i, this[i]);
}
return matrix;
}
///
/// Creates a vector containing specified elements.
///
/// The first element to begin copying from.
/// The number of elements to copy.
/// A vector containing a copy of the specified elements.
/// - If is not positive or
/// greater than or equal to the size of the vector.
/// - If + is greater than or equal to the size of the vector.
///
/// If is not positive.
public virtual Vector SubVector(int index, int length)
{
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException("index");
}
if (length <= 0)
{
throw new ArgumentOutOfRangeException("length");
}
if (index + length > Count)
{
throw new ArgumentOutOfRangeException("index");
}
var result = CreateVector(length);
CommonParallel.For(
index,
index + length,
i => result[i - index] = this[i]);
return result;
}
///
/// Set the values of this vector to the given values.
///
/// The array containing the values to use.
/// If is .
/// If is not the same size as this vector.
public virtual void SetValues(T[] values)
{
if (values == null)
{
throw new ArgumentNullException("values");
}
if (values.Length != Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
}
CommonParallel.For(
0,
values.Length,
i => this[i] = values[i]);
}
#endregion
#region Implemented Interfaces
#if !PORTABLE
#region ICloneable
///
/// Creates a new object that is a copy of the current instance.
///
///
/// A new object that is a copy of this instance.
///
object ICloneable.Clone()
{
return Clone();
}
#endregion
#endif
#region IEnumerable
///
/// Returns an enumerator that iterates through a collection.
///
///
/// An object that can be used to iterate through the collection.
///
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region IEnumerable
///
/// Returns an enumerator that iterates through the collection.
///
///
/// A that can be used to iterate through the collection.
///
public virtual IEnumerator GetEnumerator()
{
for (var index = 0; index < Count; index++)
{
yield return this[index];
}
}
#endregion
///
/// Returns an that contains the position and value of the element.
///
///
/// An over this vector that contains the position and value of each
/// element.
///
///
/// The enumerator returns a
///
/// with the first value being the element index and the second value
/// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements
/// with a zero value.
///
public virtual IEnumerable> GetIndexedEnumerator()
{
for (var i = 0; i < Count; i++)
{
yield return new Tuple(i, this[i]);
}
}
#region IEquatable
///
/// Indicates whether the current object is equal to another object of the same type.
///
///
/// An object to compare with this object.
///
///
/// true if the current object is equal to the parameter; otherwise, false.
///
public virtual bool Equals(Vector other)
{
// Reject equality when the argument is null or has a different length.
if (other == null)
{
return false;
}
if (Count != other.Count)
{
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 < Count; index++)
{
if (!this[index].Equals(other[index]))
{
return false;
}
}
return true;
}
#endregion
#region IFormattable
///
/// Returns a that represents this instance.
///
///
/// An that supplies culture-specific formatting information.
///
///
/// A that represents this instance.
///
public string ToString(IFormatProvider formatProvider)
{
return ToString(null, formatProvider);
}
///
/// Returns a that represents this instance.
///
///
/// The format to use.
///
///
/// An that supplies culture-specific formatting information.
///
///
/// A that represents this instance.
///
public virtual string ToString(string format, IFormatProvider formatProvider)
{
var stringBuilder = new StringBuilder();
for (var index = 0; index < Count; index++)
{
stringBuilder.Append(this[index].ToString(format, formatProvider));
if (index != Count - 1)
{
stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator);
}
}
return stringBuilder.ToString();
}
#endregion
#endregion
#region System.Object overrides
///
/// Determines whether the specified is equal to this instance.
///
///
/// The to compare with this instance.
///
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object obj)
{
return Equals(obj as Vector);
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode()
{
var hashNum = Math.Min(Count, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if PORTABLE
hash ^= Precision.DoubleToInt64Bits(this[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(this[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return ToString(null, null);
}
#endregion
}
}