//
// 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-2013 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.Runtime;
using Properties;
using Storage;
///
/// Defines the generic class for Vector classes.
///
/// Supported data types are double, single, , and .
[Serializable]
public abstract partial class Vector :
IFormattable, IEnumerable, IEquatable>, IList, IList
#if !PORTABLE
, ICloneable
#endif
where T : struct, IEquatable, IFormattable
{
///
/// The zero value for type T.
///
private static readonly T Zero = Common.ZeroOf();
///
/// The value of 1.0 for type T.
///
private static readonly T One = Common.OneOf();
///
/// 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);
}
///
/// 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);
///
/// Negates vector and save result to
///
/// Target vector
protected abstract void DoNegate(Vector result);
///
/// Complex conjugates vector and save result to
///
/// Target vector
protected abstract void DoConjugate(Vector 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);
///
/// 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 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);
///
/// Subtracts each element of the vector from a scalar and stores the result in the result vector.
///
/// The scalar to subtract from.
/// The vector to store the result of the subtraction.
protected virtual void DoSubtractFrom(T scalar, Vector result)
{
DoNegate(result);
result.DoAdd(scalar, 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 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.
/// The result of the addition.
protected abstract T DoDotProduct(Vector other);
///
/// 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);
///
/// Divides a scalar by each element of the vector and stores the result in the result vector.
///
/// The scalar to divide.
/// The vector to store the result of the division.
protected abstract void DoDivideByThis(T scalar, Vector 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 scalar, Vector result);
///
/// Computes the modulus for the given dividend for each element of the vector.
///
/// The dividend to use.
/// A vector to store the results in.
protected abstract void DoModulusByThis(T scalar, Vector 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 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);
///
/// Pointwise modulus this vector with another vector and stores the result into the result vector.
///
/// The vector to pointwise modulus this one by.
/// The result of the modulus.
protected abstract void DoPointwiseModulus(Vector other, Vector result);
///
/// Adds a scalar to each element of the vector.
///
/// The scalar to add.
/// A copy of the vector with the scalar added.
public 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 void Add(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (scalar.Equals(Zero))
{
CopyTo(result);
return;
}
DoAdd(scalar, result);
}
///
/// Returns a copy of this vector.
///
/// This vector.
///
/// Added as an alternative to the unary addition operator.
///
[Obsolete("Use Clone instead. Scheduled for removal in v3.0.")]
public 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 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 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);
}
///
/// 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 Vector Subtract(T scalar)
{
if (scalar.Equals(Zero))
{
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 void Subtract(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (scalar.Equals(Zero))
{
CopyTo(result);
return;
}
DoSubtract(scalar, result);
}
///
/// Subtracts each element of the vector from a scalar.
///
/// The scalar to subtract from.
/// A new vector containing the subtraction of the scalar and this vector.
public Vector SubtractFrom(T scalar)
{
var result = CreateVector(Count);
DoSubtractFrom(scalar, result);
return result;
}
///
/// Subtracts each element of the vector from a scalar and stores the result in the result vector.
///
/// The scalar to subtract from.
/// The vector to store the result of the subtraction.
/// If the result vector is .
/// If this vector and are not the same size.
public void SubtractFrom(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoSubtractFrom(scalar, result);
}
///
/// Returns a negated vector.
///
/// The negated vector.
/// Added as an alternative to the unary negation operator.
public Vector Negate()
{
var retrunVector = CreateVector(Count);
DoNegate(retrunVector);
return retrunVector;
}
///
/// Negates vector and save result to
///
/// Target vector
public void Negate(Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoNegate(result);
}
///
/// 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 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 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);
}
///
/// Return vector with complex conjugate values of the source vector
///
/// Conjugated vector
public Vector Conjugate()
{
var retrunVector = CreateVector(Count);
DoConjugate(retrunVector);
return retrunVector;
}
///
/// Complex conjugates vector and save result to
///
/// Target vector
public void Conjugate(Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoConjugate(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 Vector Multiply(T scalar)
{
if (scalar.Equals(One))
{
return Clone();
}
if (scalar.Equals(Zero))
{
return CreateVector(Count);
}
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 void Multiply(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (scalar.Equals(One))
{
CopyTo(result);
return;
}
if (scalar.Equals(Zero))
{
result.Clear();
return;
}
DoMultiply(scalar, result);
}
///
/// Computes the dot product between this vector and another vector.
///
/// The other vector to add.
/// The result of the addition.
/// If is not of the same size.
/// If is .
public T DotProduct(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
return DoDotProduct(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 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 void Divide(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (scalar.Equals(One))
{
CopyTo(result);
return;
}
DoDivide(scalar, result);
}
///
/// Divides a scalar by each element of the vector.
///
/// The scalar to divide.
/// A new vector that is the division of the vector and the scalar.
public Vector DevideByThis(T scalar)
{
var result = CreateVector(Count);
DoDivideByThis(scalar, result);
return result;
}
///
/// Divides a scalar by each element of the vector and stores the result in the result vector.
///
/// The scalar to divide.
/// The vector to store the result of the division.
/// If the result vector is .
/// If this vector and are not the same size.
public void DivideByThis(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoDivideByThis(scalar, result);
}
///
/// Computes the modulus for each element of the vector for the given divisor.
///
/// The divisor to use.
/// A vector containing the result.
public Vector Modulus(T scalar)
{
var result = CreateVector(Count);
DoModulus(scalar, 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 void Modulus(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoModulus(scalar, result);
}
///
/// Computes the modulus for the given dividend for each element of the vector.
///
/// The dividend to use.
/// A vector containing the result.
public Vector ModulusByThis(T scalar)
{
var result = CreateVector(Count);
DoModulusByThis(scalar, result);
return result;
}
///
/// Computes the modulus for the given dividend for each element of the vector.
///
/// The dividend to use.
/// A vector to store the results in.
public void ModulusByThis(T scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoModulusByThis(scalar, 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 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 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 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 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 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 modulus this vector with another vector.
///
/// The vector to pointwise modulus this one by.
/// A new vector which is the pointwise modulus of the two vectors.
/// If the other vector is .
/// If this vector and are not the same size.
public Vector PointwiseModulus(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var result = CreateVector(Count);
DoPointwiseModulus(other, result);
return result;
}
///
/// Pointwise modulus this vector with another vector and stores the result into the result vector.
///
/// The vector to pointwise modulus this one by.
/// The vector to store the result of the pointwise modulus.
/// 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 void PointwiseModulus(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");
}
DoPointwiseModulus(other, 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.At(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 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.Clone();
}
///
/// 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();
}
///
/// 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 (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Add(rightSide);
}
///
/// Adds a scalar to each element of a vector.
///
/// The vector to add to.
/// The scalar value to add.
/// The result of the addition.
/// If is .
public static Vector operator +(Vector leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Add(rightSide);
}
///
/// Adds a scalar to each element of a vector.
///
/// The scalar value to add.
/// The vector to add to.
/// The result of the addition.
/// If is .
public static Vector operator +(T leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Add(leftSide);
}
///
/// 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 (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Subtract(rightSide);
}
///
/// Subtracts a scalar from each element of a vector.
///
/// The vector to subtract from.
/// The scalar value to subtract.
/// The result of the subtraction.
/// If is .
public static Vector operator -(Vector leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Subtract(rightSide);
}
///
/// Substracts each element of a vector from a scalar.
///
/// The scalar value to subtract from.
/// The vector to subtract.
/// The result of the subtraction.
/// If is .
public static Vector operator -(T leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.SubtractFrom(leftSide);
}
///
/// 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 (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.DotProduct(rightSide);
}
///
/// Divides a scalar with a vector.
///
/// The scalar to divide.
/// The vector.
/// The result of the division.
/// If is .
public static Vector operator /(T leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.DevideByThis(leftSide);
}
///
/// 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);
}
///
/// Pointwise divides two Vectors.
///
/// The vector to divide.
/// The other vector.
/// The result of the division.
/// If and are not the same size.
/// If is .
public static Vector operator /(Vector leftSide, Vector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.PointwiseDivide(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);
}
///
/// Computes the modulus of the given dividend of each element of the vector.
///
/// The dividend we want to compute the modulus of.
/// The vector whose elements we want to use as divisor.
/// The result of the calculation
/// If is .
public static Vector operator %(T leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.ModulusByThis(leftSide);
}
///
/// Computes the pointwise modulus of each element of two vectors.
///
/// The vector whose elements we want to compute the modulus of.
/// The divisor to use.
/// The result of the calculation
/// If and are not the same size.
/// If is .
public static Vector operator %(Vector leftSide, Vector rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.PointwiseModulus(rightSide);
}
///
/// 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);
///
/// 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 T Maximum()
{
return At(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 T Minimum()
{
return At(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();
///
/// Returns a deep-copy clone of the vector.
///
/// A deep-copy clone of the vector.
public Vector Clone()
{
var result = CreateVector(Count);
Storage.CopyToUnchecked(result.Storage, skipClearing: true);
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 void SetValues(T[] values)
{
var source = new DenseVectorStorage(Count, values);
source.CopyTo(Storage);
}
///
/// 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");
}
Storage.CopyTo(target.Storage);
}
///
/// 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 Vector SubVector(int index, int count)
{
var target = CreateVector(count);
Storage.CopySubVectorTo(target.Storage, index, 0, count, skipClearing: true);
return target;
}
///
/// Copies the values of a given vector into a region in this vector.
///
/// The field to start copying to
/// The number of fields to cpy. Must be positive.
/// The sub-vector to copy from.
/// If is
public void SetSubVector(int index, int count, Vector subVector)
{
if (subVector == null)
{
throw new ArgumentNullException("subVector");
}
subVector.Storage.CopySubVectorTo(Storage, 0, index, count);
}
///
/// 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. Scheduled for removal in v3.0.")]
public void CopyTo(Vector destination, int sourceIndex, int targetIndex, int count)
{
CopySubVectorTo(destination, sourceIndex, targetIndex, count);
}
///
/// Returns the data contained in the vector as an array.
///
///
/// The vector's data as an array.
///
public T[] ToArray()
{
var result = new DenseVectorStorage(Count);
Storage.CopyToUnchecked(result, skipClearing: true);
return result.Data;
}
///
/// Create a matrix based on this vector in column form (one single column).
///
///
/// This vector as a column matrix.
///
public Matrix ToColumnMatrix()
{
var result = CreateMatrix(Count, 1);
Storage.CopyToColumnUnchecked(result.Storage, 0, skipClearing: true);
return result;
}
///
/// Create a matrix based on this vector in row form (one single row).
///
///
/// This vector as a row matrix.
///
public Matrix ToRowMatrix()
{
var result = CreateMatrix(1, Count);
Storage.CopyToRowUnchecked(result.Storage, 0, skipClearing: true);
return result;
}
///
/// Returns an enumerator that iterates through the collection.
///
///
/// A that can be used to iterate through the collection.
///
public IEnumerator GetEnumerator()
{
return Storage.Enumerate().GetEnumerator();
}
///
/// Returns an that contains the position and value of the element, for all non-zero elements.
///
///
/// 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.
/// The enumerator will exclude all elements with a zero value.
///
public IEnumerable> GetIndexedEnumerator()
{
return Storage.EnumerateNonZero();
}
///
/// Applies a function to each value of this vector and replaces the value with its result.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public void MapInplace(Func f, bool forceMapZeros = false)
{
Storage.MapInplace(f, forceMapZeros);
}
///
/// Applies a function to each value of this vector and replaces the value with its result.
/// The index of each value (zero-based) is passed as first argument to the function.
/// If forceMapZero is not set to true, zero values may or may not be skipped depending
/// on the actual data storage implementation (relevant mostly for sparse vectors).
///
public void MapIndexedInplace(Func f, bool forceMapZeros = false)
{
Storage.MapIndexedInplace(f, forceMapZeros);
}
}
}