diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index c1249437..530684f7 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -32,7 +32,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double using Distributions; using NumberTheory; using Properties; - using Random; using Threading; /// @@ -935,8 +934,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to pointwise multiply with this one. /// If the other vector is . /// If this vector and are not the same size. - /// A new vector that is the pointwise multiplication of this vector and . - public virtual DenseVector PointWiseMultiply(DenseVector other) + public override void PointWiseMultiply(Vector other) { if (other == null) { @@ -948,82 +946,31 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - var result = new DenseVector(this.Count); - this.PointWiseMultiply(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 virtual void PointWiseMultiply(DenseVector other, DenseVector result) - { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (this.Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (this.Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - - CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] * other.Data[index]); - } + var denseVector = other as DenseVector; - /// - /// Pointwise add this vector with another vector. - /// - /// The vector to pointwise add with this one. - /// If the other vector is . - /// If this vector and are not the same size. - /// A new vector that is the pointwise addition of this vector and . - public virtual DenseVector PointWiseAdd(DenseVector other) - { - if (other == null) + if (denseVector == null) { - throw new ArgumentNullException("other"); + base.PointWiseMultiply(other); } - - if (this.Count != other.Count) + else { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + CommonParallel.For( + 0, + this.Count, + index => this[index] *= other[index]); } - - var result = new DenseVector(this.Count); - this.PointWiseAdd(other, result); - return result; } /// - /// Pointwise add this vector with another vector and stores the result into the result vector. + /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// - /// The vector to pointwise add with this one. - /// The vector to store the result of the pointwise addition. + /// 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 virtual void PointWiseAdd(DenseVector other, DenseVector result) + public override void PointWiseMultiply(Vector other, Vector result) { if (result == null) { @@ -1045,52 +992,27 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] + other.Data[index]); - } - - /// - /// Pointwise subtarct this vector with another vector. - /// - /// The vector to pointwise subtract from this one. - /// If the other vector is . - /// If this vector and are not the same size. - /// A new vector that is the pointwise subtraction of this vector and . - public virtual DenseVector PointWiseSubtract(DenseVector other) - { - if (other == null) + if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { - throw new ArgumentNullException("other"); + var tmp = result.CreateVector(result.Count); + this.PointWiseMultiply(other, tmp); + tmp.CopyTo(result); } - - if (this.Count != other.Count) + else { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + this.CopyTo(result); + result.PointWiseMultiply(other); } - - var result = new DenseVector(this.Count); - this.PointWiseSubtract(other, result); - return result; } /// - /// Pointwise subtract this vector with another vector and stores the result into the result vector. + /// Pointwise divide this vector with another vector. /// - /// The vector to pointwise subtract from this one. - /// The vector to store the result of the pointwise subtraction. + /// The vector to pointwise divide this one by. /// 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 virtual void PointWiseSubtract(DenseVector other, DenseVector result) + public override void PointWiseDivide(Vector other) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (other == null) { throw new ArgumentNullException("other"); @@ -1101,39 +1023,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - - CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] - other.Data[index]); - } + var denseVector = other as DenseVector; - /// - /// Pointwise divide this vector with another vector. - /// - /// The vector to pointwise divide this one by. - /// If the other vector is . - /// If this vector and are not the same size. - /// A new vector that is the pointwise division of this vector and . - public virtual DenseVector PointWiseDivide(DenseVector other) - { - if (other == null) + if (denseVector == null) { - throw new ArgumentNullException("other"); + base.PointWiseMultiply(other); } - - if (this.Count != other.Count) + else { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + CommonParallel.For( + 0, + this.Count, + index => this[index] /= other[index]); } - - var result = new DenseVector(this.Count); - this.PointWiseDivide(other, result); - return result; } /// @@ -1145,7 +1047,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - public virtual void PointWiseDivide(DenseVector other, DenseVector result) + public override void PointWiseDivide(Vector other, Vector result) { if (result == null) { @@ -1167,10 +1069,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] / other.Data[index]); + if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + { + var tmp = result.CreateVector(result.Count); + this.PointWiseDivide(other, tmp); + tmp.CopyTo(result); + } + else + { + this.CopyTo(result); + result.PointWiseDivide(other); + } } /// diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 42ff098b..91a354f9 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -551,8 +551,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to pointwise multiply with this one. /// If the other vector is . /// If this vector and are not the same size. - /// A new vector that is the pointwise multiplication of this vector and . - public virtual Vector PointWiseMultiply(Vector other) + public virtual void PointWiseMultiply(Vector other) { if (other == null) { @@ -564,9 +563,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - var result = this.CreateVector(this.Count); - this.PointWiseMultiply(other, result); - return result; + CommonParallel.For( + 0, + this.Count, + index => this[index] *= other[index]); } /// @@ -600,113 +600,27 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] * other[index]); - } - - /// - /// Pointwise add this vector with another vector. - /// - /// The vector to pointwise add with this one. - /// If the other vector is . - /// If this vector and are not the same size. - /// A new vector that is the pointwise addition of this vector and . - public virtual Vector PointWiseAdd(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (this.Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var result = this.CreateVector(this.Count); - this.PointWiseAdd(other, result); - return result; - } - - /// - /// Pointwise add this vector with another vector and stores the result into the result vector. - /// - /// The vector to pointwise add with this one. - /// The vector to store the result of the pointwise 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 virtual void PointWiseAdd(Vector other, Vector result) - { - if (result == null) - { - throw new ArgumentNullException("result"); - } - - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (this.Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - if (this.Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - - CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] + other[index]); - } - - /// - /// Pointwise subtarct this vector with another vector. - /// - /// The vector to pointwise subtract from this one. - /// If the other vector is . - /// If this vector and are not the same size. - /// A new vector that is the pointwise subtraction of this vector and . - public virtual Vector PointWiseSubtract(Vector other) - { - if (other == null) + if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { - throw new ArgumentNullException("other"); + var tmp = result.CreateVector(result.Count); + this.PointWiseMultiply(other, tmp); + tmp.CopyTo(result); } - - if (this.Count != other.Count) + else { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + this.CopyTo(result); + result.PointWiseMultiply(other); } - - var result = this.CreateVector(this.Count); - this.PointWiseSubtract(other, result); - return result; } /// - /// Pointwise subtract this vector with another vector and stores the result into the result vector. + /// Pointwise divide this vector with another vector. /// - /// The vector to pointwise subtract from this one. - /// The vector to store the result of the pointwise subtraction. + /// The vector to pointwise divide this one by. /// 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 virtual void PointWiseSubtract(Vector other, Vector result) + public virtual void PointWiseDivide(Vector other) { - if (result == null) - { - throw new ArgumentNullException("result"); - } - if (other == null) { throw new ArgumentNullException("other"); @@ -717,39 +631,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); - } - CommonParallel.For( 0, this.Count, - index => result[index] = this[index] - other[index]); - } - - /// - /// Pointwise divide this vector with another vector. - /// - /// The vector to pointwise divide this one by. - /// If the other vector is . - /// If this vector and are not the same size. - /// A new vector that is the pointwise division of this vector and . - public virtual Vector PointWiseDivide(Vector other) - { - if (other == null) - { - throw new ArgumentNullException("other"); - } - - if (this.Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } - - var result = this.CreateVector(this.Count); - this.PointWiseDivide(other, result); - return result; + index => this[index] /= other[index]); } /// @@ -783,10 +668,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] / other[index]); + if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) + { + var tmp = result.CreateVector(result.Count); + this.PointWiseDivide(other, tmp); + tmp.CopyTo(result); + } + else + { + this.CopyTo(result); + result.PointWiseDivide(other); + } } /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs index f13f6792..dae3835e 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs @@ -771,123 +771,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { var vector1 = this.CreateVector(this._data); var vector2 = vector1.Clone(); - var result = vector1.PointWiseMultiply(vector2); + vector1.PointWiseMultiply(vector2); for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(this._data[i] * this._data[i], result[i]); - } - } - - [Test] - public void PointWiseAdd() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - var result = CreateVector(vector1.Count); - vector1.PointWiseAdd(vector2, result); - for (var i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(this._data[i] + this._data[i], result[i]); + Assert.AreEqual(this._data[i] * this._data[i], vector1[i]); } } - - [Test] - [ExpectedArgumentNullException] - public void PointWiseAddWithOtherNullShouldThrowException() - { - var vector1 = this.CreateVector(this._data); - Vector vector2 = null; - var result = CreateVector(vector1.Count); - vector1.PointWiseAdd(vector2, result); - } - - [Test] - [ExpectedArgumentNullException] - public void PointWiseAddWithResultNullShouldThrowException() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - Vector result = null; - vector1.PointWiseAdd(vector2, result); - } - - [Test] - [ExpectedArgumentException] - public void PointWiseAddWithInvalidResultLengthShouldThrowException() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - var result = this.CreateVector(vector1.Count + 1); - vector1.PointWiseAdd(vector2, result); - } - - [Test] - public void PointWiseAddWithResult() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - var result = vector1.PointWiseAdd(vector2); - for (var i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(this._data[i] + this._data[i], result[i]); - } - } - - [Test] - public void PointWiseSubtract() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - var result = CreateVector(vector1.Count); - vector1.PointWiseSubtract(vector2, result); - for (var i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(this._data[i] - this._data[i], result[i]); - } - } - - [Test] - [ExpectedArgumentNullException] - public void PointWiseSubtractWithOtherNullShouldThrowException() - { - var vector1 = this.CreateVector(this._data); - Vector vector2 = null; - var result = CreateVector(vector1.Count); - vector1.PointWiseSubtract(vector2, result); - } - - [Test] - [ExpectedArgumentNullException] - public void PointWiseSubtractWithResultNullShouldThrowException() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - Vector result = null; - vector1.PointWiseSubtract(vector2, result); - } - - [Test] - [ExpectedArgumentException] - public void PointWiseSubtractWithInvalidResultLengthShouldThrowException() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - var result = this.CreateVector(vector1.Count + 1); - vector1.PointWiseSubtract(vector2, result); - } - - [Test] - public void PointWiseSubtractWithResult() - { - var vector1 = this.CreateVector(this._data); - var vector2 = vector1.Clone(); - var result = vector1.PointWiseSubtract(vector2); - for (var i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(this._data[i] - this._data[i], result[i]); - } - } - + [Test] public void PointWiseDivide() { @@ -936,10 +826,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { var vector1 = this.CreateVector(this._data); var vector2 = vector1.Clone(); - var result = vector1.PointWiseDivide(vector2); + vector1.PointWiseDivide(vector2); for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(this._data[i] / this._data[i], result[i]); + Assert.AreEqual(this._data[i] / this._data[i], vector1[i]); } }