From 4b64bbf24fb8f1275a43233fa4a11dc6b5b67d07 Mon Sep 17 00:00:00 2001 From: tibel Date: Thu, 23 May 2013 19:53:43 +0200 Subject: [PATCH] add Vector.SubtractFrom(scalar) --- src/Numerics/LinearAlgebra/Generic/Vector.cs | 49 ++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index a32d1260..2081d49f 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -191,6 +191,17 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// 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. /// @@ -391,6 +402,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic 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. /// @@ -948,9 +993,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentNullException("rightSide"); } - var res = rightSide.Negate(); - res.Add(leftSide, res); - return res; + return rightSide.SubtractFrom(leftSide); } ///