From 81f454df70b20ac066ff9d83326b64476d52e452 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 1 Mar 2013 18:46:19 +0100 Subject: [PATCH] LA: Scalar addition/subtraction operators on generic vectors --- src/Numerics/LinearAlgebra/Generic/Vector.cs | 70 ++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 6f9bd692..a5d68ada 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -818,6 +818,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic 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); + } + /// /// Returns a Vector containing the negated values of . /// @@ -852,6 +886,42 @@ namespace MathNet.Numerics.LinearAlgebra.Generic 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"); + } + + var res = rightSide.Negate(); + res.Add(leftSide, res); + return res; + } + /// /// Multiplies a vector with a scalar. ///