diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 3c7575e1..a0b94c59 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -369,19 +369,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new DenseVector(_length); - CommonParallel.For( - 0, - _values.Length, - index => result[index] = -_values[index]); - - return result; + var denseResult = target as DenseVector; + if (denseResult == null) + { + base.DoNegate(target); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(-Complex.One, _values, denseResult.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 85dd9747..a298ba79 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -448,24 +448,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new SparseVectorStorage(Count); - var valueCount = result.ValueCount = _storage.ValueCount; - var indices = result.Indices = new int[valueCount]; - var values = result.Values = new Complex[valueCount]; - - if (valueCount != 0) + var sparseResult = target as SparseVector; + if (sparseResult == null) { - CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]); - Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt); + target.Clear(); + for (var index = 0; index < _storage.ValueCount; index++) + { + target.At(_storage.Indices[index], -_storage.Values[index]); + } } + else + { + if (!ReferenceEquals(this, target)) + { + sparseResult._storage.ValueCount = _storage.ValueCount; + sparseResult._storage.Indices = new int[_storage.ValueCount]; + Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); + sparseResult._storage.Values = new Complex[_storage.ValueCount]; + Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); + } - return new SparseVector(result); + Control.LinearAlgebraProvider.ScaleArray(-Complex.One, sparseResult._storage.Values, sparseResult._storage.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index 7e585b1b..186b3882 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -339,24 +339,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// - /// The negated vector. - /// - /// - /// Added as an alternative to the unary negation operator. - /// - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = CreateVector(Count); - for (var index = 0; index < Count; index++) { - result.At(index, -At(index)); + target.At(index, -At(index)); } - - return result; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 605169c0..d5444ada 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -369,19 +369,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new DenseVector(_length); - CommonParallel.For( - 0, - _values.Length, - index => result[index] = -_values[index]); - - return result; + var denseResult = target as DenseVector; + if (denseResult == null) + { + base.DoNegate(target); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, _values, denseResult.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 08792875..9803b7cc 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -448,24 +448,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new SparseVectorStorage(Count); - var valueCount = result.ValueCount = _storage.ValueCount; - var indices = result.Indices = new int[valueCount]; - var values = result.Values = new Complex32[valueCount]; - - if (valueCount != 0) + var sparseResult = target as SparseVector; + if (sparseResult == null) { - CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]); - Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt); + target.Clear(); + for (var index = 0; index < _storage.ValueCount; index++) + { + target.At(_storage.Indices[index], -_storage.Values[index]); + } } + else + { + if (!ReferenceEquals(this, target)) + { + sparseResult._storage.ValueCount = _storage.ValueCount; + sparseResult._storage.Indices = new int[_storage.ValueCount]; + Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); + sparseResult._storage.Values = new Complex32[_storage.ValueCount]; + Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); + } - return new SparseVector(result); + Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, sparseResult._storage.Values, sparseResult._storage.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index 3c2dcc3f..31ba9311 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -339,24 +339,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// - /// The negated vector. - /// - /// - /// Added as an alternative to the unary negation operator. - /// - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = CreateVector(Count); - for (var index = 0; index < Count; index++) { - result.At(index, -At(index)); + target.At(index, -At(index)); } - - return result; } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 18a89561..5ec6ca2a 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -379,19 +379,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new DenseVector(_length); - CommonParallel.For( - 0, - _values.Length, - index => result[index] = -_values[index]); - - return result; + var denseResult = target as DenseVector; + if (denseResult == null) + { + base.DoNegate(target); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(-1.0d, _values, denseResult.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 22d03532..6233399d 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -416,24 +416,33 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new SparseVectorStorage(Count); - var valueCount = result.ValueCount = _storage.ValueCount; - var indices = result.Indices = new int[valueCount]; - var values = result.Values = new double[valueCount]; - - if (valueCount != 0) + var sparseResult = target as SparseVector; + if (sparseResult == null) { - CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]); - Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt); + target.Clear(); + for (var index = 0; index < _storage.ValueCount; index++) + { + target.At(_storage.Indices[index], -_storage.Values[index]); + } } + else + { + if (!ReferenceEquals(this, target)) + { + sparseResult._storage.ValueCount = _storage.ValueCount; + sparseResult._storage.Indices = new int[_storage.ValueCount]; + Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); + sparseResult._storage.Values = new double[_storage.ValueCount]; + Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); + } - return new SparseVector(result); + Control.LinearAlgebraProvider.ScaleArray(-1.0d, sparseResult._storage.Values, sparseResult._storage.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 211ae5b6..5c7a16e3 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -343,24 +343,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// - /// The negated vector. - /// - /// - /// Added as an alternative to the unary negation operator. - /// - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = CreateVector(Count); - for (var index = 0; index < Count; index++) { - result.At(index, -At(index)); + target.At(index, -At(index)); } - - return result; } /// diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 87c63371..45df3f47 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -335,7 +335,37 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// The negated vector. /// Added as an alternative to the unary negation operator. - public abstract Vector Negate(); + public Vector Negate() + { + var retrunVector = CreateVector(Count); + DoNegate(retrunVector); + return retrunVector; + } + + /// + /// Negates vector and save result to + /// + /// Target vector + public void Negate(Vector target) + { + if (target == null) + { + throw new ArgumentNullException("target"); + } + + if (Count != target.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); + } + + DoNegate(target); + } + + /// + /// Negates vector and save result to + /// + /// Target vector + protected abstract void DoNegate(Vector target); /// /// Subtracts another vector from this vector. @@ -399,7 +429,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public Vector Conjugate() { var retrunVector = CreateVector(Count); - Conjugate(retrunVector); + DoConjugate(retrunVector); return retrunVector; } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 8789c17a..b41493b1 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -369,19 +369,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new DenseVector(_length); - CommonParallel.For( - 0, - _values.Length, - index => result[index] = -_values[index]); - - return result; + var denseResult = target as DenseVector; + if (denseResult == null) + { + base.DoNegate(target); + } + else + { + Control.LinearAlgebraProvider.ScaleArray(-1.0f, _values, denseResult.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 7c0e6def..589e5bcf 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -417,24 +417,33 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// The negated vector. - /// Added as an alternative to the unary negation operator. - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = new SparseVectorStorage(Count); - var valueCount = result.ValueCount = _storage.ValueCount; - var indices = result.Indices = new int[valueCount]; - var values = result.Values = new float[valueCount]; - - if (valueCount != 0) + var sparseResult = target as SparseVector; + if (sparseResult == null) { - CommonParallel.For(0, valueCount, index => values[index] = -_storage.Values[index]); - Buffer.BlockCopy(_storage.Indices, 0, indices, 0, valueCount * Constants.SizeOfInt); + target.Clear(); + for (var index = 0; index < _storage.ValueCount; index++) + { + target.At(_storage.Indices[index], -_storage.Values[index]); + } } + else + { + if (!ReferenceEquals(this, target)) + { + sparseResult._storage.ValueCount = _storage.ValueCount; + sparseResult._storage.Indices = new int[_storage.ValueCount]; + Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt); + sparseResult._storage.Values = new float[_storage.ValueCount]; + Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount); + } - return new SparseVector(result); + Control.LinearAlgebraProvider.ScaleArray(-1.0f, sparseResult._storage.Values, sparseResult._storage.Values); + } } /// diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index 1f1e4679..330c43fb 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -343,24 +343,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Returns a negated vector. + /// Negates vector and saves result to /// - /// - /// The negated vector. - /// - /// - /// Added as an alternative to the unary negation operator. - /// - public override Vector Negate() + /// Target vector + protected override void DoNegate(Vector target) { - var result = CreateVector(Count); - for (var index = 0; index < Count; index++) { - result.At(index, -At(index)); + target.At(index, -At(index)); } - - return result; } ///