//
// 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.
//
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra
{
public abstract partial class Vector
{
///
/// 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.
/// The sum of a[i]*b[i] for all i.
protected abstract T DoDotProduct(Vector other);
///
/// Computes the dot product between the conjugate of this vector and another vector.
///
/// The other vector.
/// The sum of conj(a[i])*b[i] for all i.
protected abstract T DoConjugateDotProduct(Vector other);
///
/// Divides each element of the vector by a scalar and stores the result in the result vector.
///
/// The scalar denominator to use.
/// The vector to store the result of the division.
protected abstract void DoDivide(T divisor, Vector result);
///
/// Divides a scalar by each element of the vector and stores the result in the result vector.
///
/// The scalar numerator to use.
/// The vector to store the result of the division.
protected abstract void DoDivideByThis(T dividend, Vector result);
///
/// Computes the modulus for each element of the vector for the given divisor.
///
/// The scalar denominator to use.
/// A vector to store the results in.
protected abstract void DoModulus(T divisor, Vector result);
///
/// Computes the modulus for the given dividend for each element of the vector.
///
/// The scalar numerator to use.
/// A vector to store the results in.
protected abstract void DoModulusByThis(T dividend, 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 pointwise denominator vector to use.
/// The result of the division.
protected abstract void DoPointwiseDivide(Vector divisor, Vector result);
///
/// Pointwise modulus this vector with another vector and stores the result into the result vector.
///
/// The pointwise denominator vector to use.
/// The result of the modulus.
protected abstract void DoPointwiseModulus(Vector divisor, 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 this vector and are not the same size.
public void Add(T scalar, Vector result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (scalar.Equals(Zero))
{
CopyTo(result);
return;
}
DoAdd(scalar, result);
}
///
/// Adds another vector to this vector.
///
/// The vector to add to this one.
/// A new vector containing the sum of both vectors.
/// If this vector and are not the same size.
public Vector Add(Vector 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 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 (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 this vector and are not the same size.
public void Subtract(T scalar, Vector 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 this vector and are not the same size.
public void SubtractFrom(T scalar, Vector 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 (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 this vector and are not the same size.
public Vector Subtract(Vector 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 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 (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 (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 this vector and are not the same size.
public void Multiply(T scalar, Vector 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.
/// The sum of a[i]*b[i] for all i.
/// If is not of the same size.
public T DotProduct(Vector other)
{
if (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
return DoDotProduct(other);
}
///
/// Computes the dot product between the conjugate of this vector and another vector.
///
/// The other vector.
/// The sum of conj(a[i])*b[i] for all i.
/// If is not of the same size.
/// If is .
public T ConjugateDotProduct(Vector other)
{
if (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
return DoConjugateDotProduct(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 this vector and are not the same size.
public void Divide(T scalar, Vector 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 this vector and are not the same size.
public void DivideByThis(T scalar, Vector result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoDivideByThis(scalar, result);
}
///
/// Computes the modulus (vector % divisor) for each element of the vector for the given divisor.
///
/// The scalar denominator to use.
/// A vector containing the result.
public Vector Modulus(T divisor)
{
var result = CreateVector(Count);
DoModulus(divisor, result);
return result;
}
///
/// Computes the modulus (vector % divisor) for each element of the vector for the given divisor.
///
/// The scalar denominator to use.
/// A vector to store the results in.
public void Modulus(T divisor, Vector result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoModulus(divisor, result);
}
///
/// Computes the modulus (dividend % vector) for the given dividend for each element of the vector.
///
/// The scalar numerator to use.
/// A vector containing the result.
public Vector ModulusByThis(T dividend)
{
var result = CreateVector(Count);
DoModulusByThis(dividend, result);
return result;
}
///
/// Computes the modulus (dividend % vector) for the given dividend for each element of the vector.
///
/// The scalar numerator to use.
/// A vector to store the results in.
public void ModulusByThis(T dividend, Vector result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoModulusByThis(dividend, 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 this vector and are not the same size.
public Vector PointwiseMultiply(Vector 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 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 (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 pointwise denominator vector to use.
/// A new vector which is the pointwise division of the two vectors.
/// If this vector and are not the same size.
public Vector PointwiseDivide(Vector divisor)
{
if (Count != divisor.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor");
}
var result = CreateVector(Count);
DoPointwiseDivide(divisor, result);
return result;
}
///
/// Pointwise divide this vector with another vector and stores the result into the result vector.
///
/// The pointwise denominator vector to use.
/// The vector to store the result of the pointwise division.
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public void PointwiseDivide(Vector divisor, Vector result)
{
if (Count != divisor.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseDivide(divisor, result);
}
///
/// Pointwise modulus this vector with another vector.
///
/// The pointwise denominator vector to use.
/// A new vector which is the pointwise modulus of the two vectors.
/// If this vector and are not the same size.
public Vector PointwiseModulus(Vector divisor)
{
if (Count != divisor.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor");
}
var result = CreateVector(Count);
DoPointwiseModulus(divisor, result);
return result;
}
///
/// Pointwise modulus this vector with another vector and stores the result into the result vector.
///
/// The pointwise denominator vector to use.
/// The vector to store the result of the pointwise modulus.
/// If this vector and are not the same size.
/// If this vector and are not the same size.
public void PointwiseModulus(Vector divisor, Vector result)
{
if (Count != divisor.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseModulus(divisor, result);
}
///
/// Outer product of two vectors
///
/// First vector
/// Second vector
/// Matrix M[i,j] = u[i]*v[j]
public static Matrix OuterProduct(Vector u, Vector 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);
}
///
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
///
/// The sum of the absolute values.
public abstract T L1Norm();
///
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
///
/// The square root of the sum of the squared values.
public abstract T L2Norm();
///
/// Calculates the infinity norm of the vector.
///
/// The square root of the sum of the squared values.
public abstract T InfinityNorm();
///
/// 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 T SumMagnitudes()
{
return L1Norm();
}
}
}