Browse Source

vector: corrected pointwise methods

la-knuth
Marcus Cuda 17 years ago
parent
commit
8f8a23e7a2
  1. 177
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  2. 162
      src/Numerics/LinearAlgebra/Double/Vector.cs
  3. 120
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

177
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;
/// <summary>
@ -935,8 +934,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise multiplication of this vector and <paramref name="other"/>.</returns>
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;
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
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;
/// <summary>
/// Pointwise add this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise addition of this vector and <paramref name="other"/>.</returns>
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;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <param name="result">The vector to store the result of the pointwise addition.</param>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
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]);
}
/// <summary>
/// Pointwise subtarct this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise subtraction of this vector and <paramref name="other"/>.</returns>
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;
}
/// <summary>
/// Pointwise subtract this vector with another vector and stores the result into the result vector.
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <param name="result">The vector to store the result of the pointwise subtraction.</param>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
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;
/// <summary>
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise division of this vector and <paramref name="other"/>.</returns>
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;
}
/// <summary>
@ -1145,7 +1047,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
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);
}
}
/// <summary>

162
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -551,8 +551,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise multiplication of this vector and <paramref name="other"/>.</returns>
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]);
}
/// <summary>
@ -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]);
}
/// <summary>
/// Pointwise add this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise addition of this vector and <paramref name="other"/>.</returns>
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;
}
/// <summary>
/// Pointwise add this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <param name="result">The vector to store the result of the pointwise addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
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]);
}
/// <summary>
/// Pointwise subtarct this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise subtraction of this vector and <paramref name="other"/>.</returns>
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;
}
/// <summary>
/// Pointwise subtract this vector with another vector and stores the result into the result vector.
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <param name="result">The vector to store the result of the pointwise subtraction.</param>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
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]);
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise division of this vector and <paramref name="other"/>.</returns>
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]);
}
/// <summary>
@ -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);
}
}
/// <summary>

120
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]);
}
}

Loading…
Cancel
Save