From 4b64bbf24fb8f1275a43233fa4a11dc6b5b67d07 Mon Sep 17 00:00:00 2001 From: tibel Date: Thu, 23 May 2013 19:53:43 +0200 Subject: [PATCH 1/3] 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); } /// From 62f745884b7c582e007be852c5f024b5ecc233ca Mon Sep 17 00:00:00 2001 From: tibel Date: Thu, 23 May 2013 20:19:18 +0200 Subject: [PATCH 2/3] add Vector.DivideByThis(scalar) --- src/Numerics/LinearAlgebra/Complex/Vector.cs | 13 ++++ .../LinearAlgebra/Complex32/Vector.cs | 13 ++++ src/Numerics/LinearAlgebra/Double/Vector.cs | 13 ++++ src/Numerics/LinearAlgebra/Generic/Vector.cs | 76 +++++++++++++++++++ src/Numerics/LinearAlgebra/Single/Vector.cs | 13 ++++ 5 files changed, 128 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index b32957c9..39514064 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -146,6 +146,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex DoMultiply(1 / scalar, 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 override void DoDivideByThis(Complex scalar, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, scalar / At(index)); + } + } + /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index 60a3ffc3..93aece30 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -146,6 +146,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 DoMultiply(1 / scalar, 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 override void DoDivideByThis(Complex32 scalar, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, scalar / At(index)); + } + } + /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 26510d32..f6427777 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -145,6 +145,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double DoMultiply(1 / scalar, 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 override void DoDivideByThis(double scalar, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, scalar / At(index)); + } + } + /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 2081d49f..b62b0c4e 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -230,6 +230,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// 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. /// @@ -668,6 +675,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic 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. /// @@ -1048,6 +1089,23 @@ namespace MathNet.Numerics.LinearAlgebra.Generic 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. /// @@ -1065,6 +1123,24 @@ namespace MathNet.Numerics.LinearAlgebra.Generic 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. /// diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index ed86d2f2..9996a41e 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -145,6 +145,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single DoMultiply(1 / scalar, 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 override void DoDivideByThis(float scalar, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, scalar / At(index)); + } + } + /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// From 274df148d434c37cb1de136d7ce97ed67c4bd32b Mon Sep 17 00:00:00 2001 From: tibel Date: Thu, 23 May 2013 21:10:24 +0200 Subject: [PATCH 3/3] use At() instead of indexer if possible --- src/Numerics/LinearAlgebra/Generic/Matrix.cs | 8 ++++---- src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs | 2 +- src/Numerics/LinearAlgebra/Generic/Vector.cs | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index 322ea3c8..96217271 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -110,7 +110,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (var i = 0; i < columnVectors[j].Count; i++) { - matrix.At(i, j, columnVectors[j][i]); + matrix.At(i, j, columnVectors[j].At(i)); } } @@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (var j = 0; j < rowVectors[i].Count; j++) { - matrix.At(i, j, rowVectors[i][j]); + matrix.At(i, j, rowVectors[i].At(j)); } } @@ -640,7 +640,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic for (var i = 0; i < min; i++) { - diagonal[i] = At(i, i); + diagonal.At(i, At(i, i)); } return diagonal; @@ -962,7 +962,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic for (var i = 0; i < min; i++) { - At(i, i, source[i]); + At(i, i, source.At(i)); } } diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs index 4a6c31c0..5484f060 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs @@ -96,7 +96,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (int i = 0; i < Count; ++i) { - if (this[i].Equals(item)) + if (At(i).Equals(item)) return i; } return -1; diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index b62b0c4e..cb2b69b3 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -881,7 +881,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic for (var i = 0; i < u.Count; i++) { - matrix.SetRow(i, v.Multiply(u[i])); + matrix.SetRow(i, v.Multiply(u.At(i))); } return matrix; @@ -1210,7 +1210,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The value of maximum element. public T Maximum() { - return this[MaximumIndex()]; + return At(MaximumIndex()); } /// @@ -1225,7 +1225,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The value of the minimum element. public T Minimum() { - return this[MinimumIndex()]; + return At(MinimumIndex()); } ///