From 9f0c071e292379e8d905543774ad82300d64ba97 Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Sat, 22 Jan 2011 20:33:03 +0800 Subject: [PATCH] issue 5660: some of the optimized dense implementations were not be ing called. fixed --- .../LinearAlgebra/Complex/DenseVector.cs | 444 +++-------------- .../LinearAlgebra/Complex32/DenseVector.cs | 445 +++--------------- .../LinearAlgebra/Double/DenseVector.cs | 403 ++-------------- .../LinearAlgebra/Single/DenseVector.cs | 403 ++-------------- 4 files changed, 201 insertions(+), 1494 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 44d90b8c..0cf9c43a 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -244,87 +244,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Adds a complex to each element of the vector. + /// Adds a scalar to each element of the vector and stores the result in the result vector. /// - /// The complex to add. - /// A copy of the vector with the complex added. - public override Vector Add(Complex complex) - { - if (complex == Complex.Zero) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] += complex); - return copy; - } - - /// - /// Adds a complex to each element of the vector and stores the result in the result vector. - /// - /// The complex to add. + /// 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 override void Add(Complex complex, Vector result) + protected override void DoAdd(Complex scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Add(complex, result); + base.DoAdd(scalar, result); } else { CommonParallel.For( 0, Data.Length, - index => dense.Data[index] = Data[index] + complex); - } - } - - /// - /// 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 override Vector Add(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); + index => dense.Data[index] = Data[index] + scalar); } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Add(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex.One, denseVector.Data, copy.Data); - return copy; } /// @@ -332,27 +269,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// 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 override void Add(Vector other, Vector result) + protected override void DoAdd(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) @@ -361,10 +279,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); + base.DoAdd(other, result); } } @@ -414,127 +329,42 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Subtracts a complex from each element of the vector. + /// Subtracts a scalar from each element of the vector and stores the result in the result vector. /// - /// The complex to subtract. - /// A new vector containing the subtraction of this vector and the complex. - public override Vector Subtract(Complex complex) - { - if (complex == Complex.Zero) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] -= complex); - return copy; - } - - /// - /// Subtracts a complex from each element of the vector and stores the result in the result vector. - /// - /// The complex to subtract. + /// 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 override void Subtract(Complex complex, Vector result) + protected override void DoSubtract(Complex scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Subtract(complex, result); + base.DoSubtract(scalar, result); } else { CommonParallel.For( 0, Data.Length, - index => dense.Data[index] = Data[index] - complex); + index => dense.Data[index] = Data[index] - scalar); } } - /// - /// 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 override Vector Subtract(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Subtract(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex.One, denseVector.Data, copy.Data); - return copy; - } - /// /// 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 override void Subtract(Vector other, Vector result) + protected override void DoSubtract(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) { - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex.One, odense.Data, rdense.Data); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, odense.Data, rdense.Data); } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); + base.DoSubtract(other, result); } } @@ -599,49 +429,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Multiplies a complex to each element of the vector. + /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// - /// The complex to multiply. - /// A new vector that is the multiplication of the vector and the complex. - public override Vector Multiply(Complex complex) + /// The scalar to multiply. + /// The vector to store the result of the multiplication. + /// + protected override void DoMultiply(Complex scalar, Vector result) { - if (complex == Complex.One) + var denseResult = result as DenseVector; + if (denseResult == null) { - return Clone(); + base.DoMultiply(scalar, result); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.ScaleArray(complex, Data, copy.Data); - return copy; } /// /// 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 override Complex DotProduct(Vector other) + /// s + /// The result of the addition. + protected override Complex DoDotProduct(Vector other) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - var denseVector = other as DenseVector; - if (denseVector == null) - { - return base.DotProduct(other); - } - - return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); + return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); } /// @@ -877,124 +693,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// 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 override Vector PointwiseMultiply(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.PointwiseMultiply(other); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] *= other[index]); - return copy; - } - - /// - /// 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 override 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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) - { - var tmp = PointwiseMultiply(other); - tmp.CopyTo(result); - } - else - { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseMultiply(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] * other[index]); - } - } - } - - /// - /// Pointwise divide this vector with another vector. + /// Pointwise divide this vector with another vector and stores the result into the result 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 override Vector PointwiseDivide(Vector other) + /// The vector to store the result of the pointwise division. + protected override void DoPointwiseMultiply(Vector other, Vector result) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) + var dense = result as DenseVector; + if (dense == null) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + base.DoPointwiseMultiply(other, result); } - - var denseVector = other as DenseVector; - - if (denseVector == null) + else { - return base.PointwiseMultiply(other); + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] * other[index]); } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] /= other[index]); - return copy; } /// @@ -1002,51 +718,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// 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 override void PointwiseDivide(Vector other, Vector result) + /// + protected override void DoPointwiseDivide(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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var dense = result as DenseVector; + if (dense == null) { - var tmp = PointwiseDivide(other); - tmp.CopyTo(result); + base.DoPointwiseDivide(other, result); } else { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseDivide(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] / other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] / other[index]); } } @@ -1309,39 +994,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// Conjugates vector and save result to /// /// Target vector - public override void Conjugate(Vector target) + protected override void DoConjugate(Vector target) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (Count != target.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); - } - - if (ReferenceEquals(this, target)) - { - var tmp = CreateVector(Count); - Conjugate(tmp); - tmp.CopyTo(target); - } + var denseTarget = target as DenseVector; - var otherVector = target as DenseVector; - if (otherVector == null) + if (denseTarget == null) { - base.Conjugate(target); + base.DoConjugate(target); } else { CommonParallel.For( 0, Count, - index => otherVector.Data[index] = Data[index].Conjugate()); + index => denseTarget.Data[index] = Data[index].Conjugate()); } } - /// Gets the value at the given . /// The index of the value to get or set. /// The value of the vector at the given . diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 07873273..7e506585 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -245,87 +245,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Adds a complex to each element of the vector. + /// Adds a scalar to each element of the vector and stores the result in the result vector. /// - /// The complex to add. - /// A copy of the vector with the complex added. - public override Vector Add(Complex32 complex) - { - if (complex == Complex32.Zero) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] += complex); - return copy; - } - - /// - /// Adds a complex to each element of the vector and stores the result in the result vector. - /// - /// The complex to add. + /// 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 override void Add(Complex32 complex, Vector result) + protected override void DoAdd(Complex32 scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Add(complex, result); + base.DoAdd(scalar, result); } else { CommonParallel.For( 0, Data.Length, - index => dense.Data[index] = Data[index] + complex); - } - } - - /// - /// 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 override Vector Add(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); + index => dense.Data[index] = Data[index] + scalar); } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Add(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, Complex32.One, denseVector.Data, copy.Data); - return copy; } /// @@ -333,27 +270,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// 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 override void Add(Vector other, Vector result) + protected override void DoAdd(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) @@ -362,10 +280,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); + base.DoAdd(other, result); } } @@ -415,130 +330,45 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Subtracts a complex from each element of the vector. + /// Subtracts a scalar from each element of the vector and stores the result in the result vector. /// - /// The complex to subtract. - /// A new vector containing the subtraction of this vector and the complex. - public override Vector Subtract(Complex32 complex) - { - if (complex == Complex32.Zero) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] -= complex); - return copy; - } - - /// - /// Subtracts a complex from each element of the vector and stores the result in the result vector. - /// - /// The complex to subtract. + /// 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 override void Subtract(Complex32 complex, Vector result) + protected override void DoSubtract(Complex32 scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Subtract(complex, result); + base.DoSubtract(scalar, result); } else { CommonParallel.For( 0, Data.Length, - index => dense.Data[index] = Data[index] - complex); + index => dense.Data[index] = Data[index] - scalar); } } - /// - /// 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 override Vector Subtract(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Subtract(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex32.One, denseVector.Data, copy.Data); - return copy; - } - /// /// 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 override void Subtract(Vector other, Vector result) + protected override void DoSubtract(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) { - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -Complex32.One, odense.Data, rdense.Data); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0f, odense.Data, rdense.Data); } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); + base.DoSubtract(other, result); } } - + /// /// Returns a Vector containing the negated values of . /// @@ -600,49 +430,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Multiplies a complex to each element of the vector. + /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// - /// The complex to multiply. - /// A new vector that is the multiplication of the vector and the complex. - public override Vector Multiply(Complex32 complex) + /// The scalar to multiply. + /// The vector to store the result of the multiplication. + /// + protected override void DoMultiply(Complex32 scalar, Vector result) { - if (complex == Complex32.One) + var denseResult = result as DenseVector; + if (denseResult == null) { - return Clone(); + base.DoMultiply(scalar, result); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.ScaleArray(complex, Data, copy.Data); - return copy; } /// /// 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 override Complex32 DotProduct(Vector other) + /// s + /// The result of the addition. + protected override Complex32 DoDotProduct(Vector other) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - var denseVector = other as DenseVector; - if (denseVector == null) - { - return base.DotProduct(other); - } - - return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); + return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); } /// @@ -878,124 +694,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// 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 override Vector PointwiseMultiply(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.PointwiseMultiply(other); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] *= other[index]); - return copy; - } - - /// - /// 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 override 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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) - { - var tmp = PointwiseMultiply(other); - tmp.CopyTo(result); - } - else - { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseMultiply(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] * other[index]); - } - } - } - - /// - /// Pointwise divide this vector with another vector. + /// Pointwise divide this vector with another vector and stores the result into the result 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 override Vector PointwiseDivide(Vector other) + /// The vector to store the result of the pointwise division. + protected override void DoPointwiseMultiply(Vector other, Vector result) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) + var dense = result as DenseVector; + if (dense == null) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + base.DoPointwiseMultiply(other, result); } - - var denseVector = other as DenseVector; - - if (denseVector == null) + else { - return base.PointwiseMultiply(other); + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] * other[index]); } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] /= other[index]); - return copy; } /// @@ -1003,51 +719,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// 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 override void PointwiseDivide(Vector other, Vector result) + /// + protected override void DoPointwiseDivide(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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var dense = result as DenseVector; + if (dense == null) { - var tmp = PointwiseDivide(other); - tmp.CopyTo(result); + base.DoPointwiseDivide(other, result); } else { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseDivide(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] / other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] / other[index]); } } @@ -1362,36 +1047,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// Conjugates vector and save result to /// /// Target vector - public override void Conjugate(Vector target) + protected override void DoConjugate(Vector target) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (Count != target.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); - } - - if (ReferenceEquals(this, target)) - { - var tmp = CreateVector(Count); - Conjugate(tmp); - tmp.CopyTo(target); - } + var denseTarget = target as DenseVector; - var otherVector = target as DenseVector; - if (otherVector == null) + if (denseTarget == null) { - base.Conjugate(target); + base.DoConjugate(target); } else { CommonParallel.For( 0, Count, - index => otherVector.Data[index] = Data[index].Conjugate()); + index => denseTarget.Data[index] = Data[index].Conjugate()); } } diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index df813470..a573a618 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -295,49 +295,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// Adds a scalar to each element of the vector. - /// - /// The scalar to add. - /// A copy of the vector with the scalar added. - public override Vector Add(double scalar) - { - if (scalar == 0.0) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] += scalar); - return copy; - } - /// /// 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 override void Add(double scalar, Vector result) + protected override void DoAdd(double scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Add(scalar, result); + base.DoAdd(scalar, result); } else { @@ -348,63 +316,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// 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 override Vector Add(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Add(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data, copy.Data); - return copy; - } - /// /// 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 override void Add(Vector other, Vector result) + protected override void DoAdd(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) @@ -413,10 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); + base.DoAdd(other, result); } } @@ -465,49 +380,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double return leftSide.Add(rightSide); } - /// - /// 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 override Vector Subtract(double scalar) - { - if (scalar == 0.0) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] -= scalar); - return copy; - } - /// /// 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 override void Subtract(double scalar, Vector result) + protected override void DoSubtract(double scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Subtract(scalar, result); + base.DoSubtract(scalar, result); } else { @@ -518,63 +401,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// 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 override Vector Subtract(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Subtract(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data, copy.Data); - return copy; - } - /// /// 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 override void Subtract(Vector other, Vector result) + protected override void DoSubtract(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) @@ -583,13 +416,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); + base.DoSubtract(other, result); } } - + /// /// Returns a Vector containing the negated values of . /// @@ -651,49 +481,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Multiplies a scalar to each element of the vector. + /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// /// The scalar to multiply. - /// A new vector that is the multiplication of the vector and the scalar. - public override Vector Multiply(double scalar) + /// The vector to store the result of the multiplication. + /// + protected override void DoMultiply(double scalar, Vector result) { - if (scalar == 1.0) + var denseResult = result as DenseVector; + if (denseResult == null) { - return Clone(); + base.DoMultiply(scalar, result); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.ScaleArray(scalar, Data, copy.Data); - return copy; } /// /// 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 override double DotProduct(Vector other) + /// s + /// The result of the addition. + protected override double DoDotProduct(Vector other) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - var denseVector = other as DenseVector; - if (denseVector == null) - { - return base.DotProduct(other); - } - - return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); + return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); } /// @@ -969,124 +785,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// 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 override Vector PointwiseMultiply(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.PointwiseMultiply(other); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] *= other[index]); - return copy; - } - - /// - /// 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 override 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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) - { - var tmp = PointwiseMultiply(other); - tmp.CopyTo(result); - } - else - { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseMultiply(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] * other[index]); - } - } - } - - /// - /// Pointwise divide this vector with another vector. + /// Pointwise divide this vector with another vector and stores the result into the result 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 override Vector PointwiseDivide(Vector other) + /// The vector to store the result of the pointwise division. + protected override void DoPointwiseMultiply(Vector other, Vector result) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) + var dense = result as DenseVector; + if (dense == null) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + base.DoPointwiseMultiply(other, result); } - - var denseVector = other as DenseVector; - - if (denseVector == null) + else { - return base.PointwiseMultiply(other); + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] * other[index]); } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] /= other[index]); - return copy; } /// @@ -1094,51 +810,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// 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 override void PointwiseDivide(Vector other, Vector result) + /// + protected override void DoPointwiseDivide(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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var dense = result as DenseVector; + if (dense == null) { - var tmp = PointwiseDivide(other); - tmp.CopyTo(result); + base.DoPointwiseDivide(other, result); } else { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseDivide(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] / other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] / other[index]); } } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index ad61340d..d5ea42e2 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -295,49 +295,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// Adds a scalar to each element of the vector. - /// - /// The scalar to add. - /// A copy of the vector with the scalar added. - public override Vector Add(float scalar) - { - if (scalar == 0.0) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] += scalar); - return copy; - } - /// /// 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 override void Add(float scalar, Vector result) + protected override void DoAdd(float scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Add(scalar, result); + base.DoAdd(scalar, result); } else { @@ -348,63 +316,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// 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 override Vector Add(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Add(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0f, denseVector.Data, copy.Data); - return copy; - } - /// /// 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 override void Add(Vector other, Vector result) + protected override void DoAdd(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) @@ -413,10 +331,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] + other[index]); + base.DoAdd(other, result); } } @@ -465,49 +380,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single return leftSide.Add(rightSide); } - /// - /// 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 override Vector Subtract(float scalar) - { - if (scalar == 0.0) - { - return Clone(); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Data.Length, - index => copy.Data[index] -= scalar); - return copy; - } - /// /// 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 override void Subtract(float scalar, Vector result) + protected override void DoSubtract(float scalar, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var dense = result as DenseVector; if (dense == null) { - base.Subtract(scalar, result); + base.DoSubtract(scalar, result); } else { @@ -518,63 +401,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// 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 override Vector Subtract(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.Subtract(other); - } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0f, denseVector.Data, copy.Data); - return copy; - } - /// /// 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 override void Subtract(Vector other, Vector result) + protected override void DoSubtract(Vector other, Vector result) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - var rdense = result as DenseVector; var odense = other as DenseVector; if (rdense != null && odense != null) @@ -583,13 +416,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - CommonParallel.For( - 0, - Data.Length, - index => result[index] = Data[index] - other[index]); + base.DoSubtract(other, result); } } - + /// /// Returns a Vector containing the negated values of . /// @@ -651,49 +481,35 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Multiplies a scalar to each element of the vector. + /// Multiplies a scalar to each element of the vector and stores the result in the result vector. /// /// The scalar to multiply. - /// A new vector that is the multiplication of the vector and the scalar. - public override Vector Multiply(float scalar) + /// The vector to store the result of the multiplication. + /// + protected override void DoMultiply(float scalar, Vector result) { - if (scalar == 1.0) + var denseResult = result as DenseVector; + if (denseResult == null) { - return Clone(); + base.DoMultiply(scalar, result); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); } - - var copy = new DenseVector(Count); - Control.LinearAlgebraProvider.ScaleArray(scalar, Data, copy.Data); - return copy; } /// /// 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 override float DotProduct(Vector other) + /// s + /// The result of the addition. + protected override float DoDotProduct(Vector other) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - var denseVector = other as DenseVector; - if (denseVector == null) - { - return base.DotProduct(other); - } - - return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); + return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); } /// @@ -969,124 +785,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// 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 override Vector PointwiseMultiply(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var denseVector = other as DenseVector; - - if (denseVector == null) - { - return base.PointwiseMultiply(other); - } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] *= other[index]); - return copy; - } - - /// - /// 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 override 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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) - { - var tmp = PointwiseMultiply(other); - tmp.CopyTo(result); - } - else - { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseMultiply(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] * other[index]); - } - } - } - - /// - /// Pointwise divide this vector with another vector. + /// Pointwise divide this vector with another vector and stores the result into the result 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 override Vector PointwiseDivide(Vector other) + /// The vector to store the result of the pointwise division. + protected override void DoPointwiseMultiply(Vector other, Vector result) { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (Count != other.Count) + var dense = result as DenseVector; + if (dense == null) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + base.DoPointwiseMultiply(other, result); } - - var denseVector = other as DenseVector; - - if (denseVector == null) + else { - return base.PointwiseMultiply(other); + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] * other[index]); } - - var copy = (DenseVector)Clone(); - CommonParallel.For( - 0, - Count, - index => copy[index] /= other[index]); - return copy; } /// @@ -1094,51 +810,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// 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 override void PointwiseDivide(Vector other, Vector result) + /// + protected override void DoPointwiseDivide(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"); - } - - if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + var dense = result as DenseVector; + if (dense == null) { - var tmp = PointwiseDivide(other); - tmp.CopyTo(result); + base.DoPointwiseDivide(other, result); } else { - var dense = result as DenseVector; - if (dense == null) - { - base.PointwiseDivide(other, result); - } - else - { - CommonParallel.For( - 0, - Data.Length, - index => dense.Data[index] = Data[index] / other[index]); - } + CommonParallel.For( + 0, + Data.Length, + index => dense.Data[index] = Data[index] / other[index]); } }