diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Vector.Arithmetic.cs new file mode 100644 index 00000000..bd6c444c --- /dev/null +++ b/src/Numerics/LinearAlgebra/Generic/Vector.Arithmetic.cs @@ -0,0 +1,951 @@ +// +// 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.Generic +{ + 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 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); + } + + /// + /// 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); + } + + /// + /// 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(); + } +} diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index ccdc7d25..b610fc62 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -157,928 +157,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// 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); - } - - /// - /// 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); - } - - /// - /// 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. /// diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 86ade787..df5ef4b6 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -110,6 +110,7 @@ +