diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs
index 0fe2ee50..a1d38288 100644
--- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs
+++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs
@@ -341,6 +341,7 @@ 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)
@@ -359,11 +360,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
- base.Add(other);
+ return base.Add(other);
}
else
{
- Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data);
+ var copy = (DenseVector)this.Clone();
+ Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, 1.0, denseVector.Data);
+ return copy;
}
}
@@ -457,17 +460,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// 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;
+ return this.Clone();
}
+ var copy = (DenseVector)this.Clone();
CommonParallel.For(
0,
Data.Length,
- index => Data[index] -= scalar);
+ index => copy.Data[index] -= scalar);
+ return copy;
}
///
@@ -497,6 +503,7 @@ 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)
@@ -515,11 +522,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
- base.Subtract(other);
+ return base.Subtract(other);
}
else
{
- Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data);
+ var copy = (DenseVector)this.Clone();
+ Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -1.0, denseVector.Data);
+ return copy;
}
}
@@ -628,14 +637,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Multiplies a scalar to each element of the vector.
///
/// The scalar to multiply.
+ /// A new vector that is the multiplication of the vector and the scalar.
public override Vector Multiply(double scalar)
{
if (scalar == 1.0)
{
- return;
+ return this.Clone();
}
- Control.LinearAlgebraProvider.ScaleArray(scalar, Data);
+ var copy = (DenseVector)this.Clone();
+ Control.LinearAlgebraProvider.ScaleArray(scalar, copy.Data);
+ return copy;
}
///
@@ -966,7 +978,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
- base.PointwiseMultiply(other);
+ return base.PointwiseMultiply(other);
}
else
{
@@ -1027,6 +1039,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Pointwise divide this vector with another 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)
@@ -1045,14 +1058,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
- base.PointwiseMultiply(other);
+ return base.PointwiseMultiply(other);
}
else
{
+ var copy = (DenseVector)this.Clone();
CommonParallel.For(
0,
- Count,
- index => this[index] /= other[index]);
+ Count,
+ index => copy[index] /= other[index]);
+ return copy;
}
}
diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs
index e50a416f..4ef3de1e 100644
--- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs
+++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs
@@ -389,6 +389,7 @@ 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)
@@ -407,11 +408,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (sparseVector == null)
{
- base.Add(other);
+ return base.Add(other);
}
else
{
- AddScaledSparceVector(1.0, sparseVector);
+ var copy = (SparseVector)this.Clone();
+ copy.AddScaledSparceVector(1.0, sparseVector);
+ return copy;
}
}
@@ -574,17 +577,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// 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;
+ return this.Clone();
}
+ var copy = (SparseVector)this.Clone();
for (var i = 0; i < Count; i++)
{
- this[i] -= scalar;
+ copy[i] -= scalar;
}
+ return copy;
}
///
@@ -614,6 +620,7 @@ 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)
@@ -632,11 +639,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (sparseVector == null)
{
- base.Subtract(other);
+ return base.Subtract(other);
}
else
{
- AddScaledSparceVector(-1.0, sparseVector);
+ var copy = (SparseVector)this.Clone();
+ copy.AddScaledSparceVector(-1.0, sparseVector);
+ return copy;
}
}
@@ -753,20 +762,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Multiplies a scalar to each element of the vector.
///
/// The scalar to multiply.
+ /// A new vector that is the multiplication of the vector and the scalar.
public override Vector Multiply(double scalar)
{
if (scalar == 1.0)
{
- return;
+ return this.Clone();
}
-
- if (scalar == 0)
+ else if (scalar == 0)
{
- Clear(); // Set array empty
- return;
+ var copy = this.Clone();
+ copy.Clear(); // Set array empty
+ return copy;
}
+ else
+ {
- Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues);
+ var copy = (SparseVector)this.Clone();
+ Control.LinearAlgebraProvider.ScaleArray(scalar, copy._nonZeroValues);
+ return copy;
+ }
}
///
@@ -1128,6 +1143,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Pointwise divide this vector with another 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)
@@ -1142,11 +1158,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
- // base implementation iterates though all elements, but we need only take non-zeros
+ // base implementation iterates though all elements, but we need only take non-zeros
+ var copy = (SparseVector)this.Clone();
for (var i = 0; i < NonZerosCount; i++)
{
- this[_nonZeroIndices[i]] /= other[_nonZeroIndices[i]];
+ copy[_nonZeroIndices[i]] /= other[_nonZeroIndices[i]];
}
+ return copy;
}
///
diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
index f3e2dca8..0935c711 100644
--- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
+++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
@@ -60,8 +60,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanAddScalarToVector()
{
- var vector = this.CreateVector(this._data);
- vector.Add(2.0);
+ var copy = this.CreateVector(this._data);
+ var vector = copy.Add(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@@ -166,9 +166,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanAddTwoVectors()
{
- var vector = this.CreateVector(this._data);
+ var copy = this.CreateVector(this._data);
var other = this.CreateVector(this._data);
- vector.Add(other);
+ var vector = copy.Add(other);
for (var i = 0; i < this._data.Length; i++)
{
@@ -212,8 +212,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanAddVectorToItself()
{
- var vector = this.CreateVector(this._data);
- vector.Add(vector);
+ var copy = this.CreateVector(this._data);
+ var vector = copy.Add(copy);
for (var i = 0; i < this._data.Length; i++)
{
@@ -285,8 +285,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanSubtractScalarFromVector()
{
- var vector = this.CreateVector(this._data);
- vector.Subtract(2.0);
+ var copy = this.CreateVector(this._data);
+ var vector = copy.Subtract(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@@ -391,9 +391,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanSubtractTwoVectors()
{
- var vector = this.CreateVector(this._data);
+ var copy = this.CreateVector(this._data);
var other = this.CreateVector(this._data);
- vector.Subtract(other);
+ var vector = copy.Subtract(other);
for (var i = 0; i < this._data.Length; i++)
{
@@ -437,8 +437,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanSubtractVectorFromItself()
{
- var vector = this.CreateVector(this._data);
- vector.Subtract(vector);
+ var copy = this.CreateVector(this._data);
+ var vector = copy.Subtract(copy);
for (var i = 0; i < this._data.Length; i++)
{
@@ -480,8 +480,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanDivideVectorByScalar()
{
- var vector = this.CreateVector(this._data);
- vector.Divide(2.0);
+ var copy = this.CreateVector(this._data);
+ var vector = copy.Divide(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@@ -520,8 +520,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanMultiplyVectorByScalar()
{
- var vector = this.CreateVector(this._data);
- vector.Multiply(2.0);
+ var copy = this.CreateVector(this._data);
+ var vector = copy.Multiply(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@@ -725,6 +725,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void PointwiseMultiply()
+ {
+ var vector1 = this.CreateVector(this._data);
+ var vector2 = vector1.Clone();
+ var result = vector1.PointwiseMultiply(vector2);
+ for (var i = 0; i < vector1.Count; i++)
+ {
+ Assert.AreEqual(this._data[i] * this._data[i], result[i]);
+ }
+ }
+
+ [Test]
+ public void PointwiseMultiplyUsingResultVector()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
@@ -767,19 +779,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
[Test]
- public void PointWiseMultiplyWithResult()
+ public void PointWiseDivide()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
- vector1.PointwiseMultiply(vector2);
+ var result = vector1.PointwiseDivide(vector2);
for (var i = 0; i < vector1.Count; i++)
{
- Assert.AreEqual(this._data[i] * this._data[i], vector1[i]);
+ Assert.AreEqual(this._data[i] / this._data[i], result[i]);
}
}
[Test]
- public void PointWiseDivide()
+ public void PointWiseDivideUsingResultVector()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
@@ -821,18 +833,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
vector1.PointwiseDivide(vector2, result);
}
- [Test]
- public void PointwiseDivideWithResult()
- {
- var vector1 = this.CreateVector(this._data);
- var vector2 = vector1.Clone();
- vector1.PointwiseDivide(vector2);
- for (var i = 0; i < vector1.Count; i++)
- {
- Assert.AreEqual(this._data[i] / this._data[i], vector1[i]);
- }
- }
-
[Test]
public void CanCalculateOuterProduct()
{