From 0ead227b6782e08fd393bac1f69a3ca5157d81ef Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 25 Nov 2016 15:14:31 +0100 Subject: [PATCH] LA: Vector.Map consistent with Matrix.Map, automatic fallback to inplace --- src/Numerics/LinearAlgebra/Vector.cs | 30 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Vector.cs b/src/Numerics/LinearAlgebra/Vector.cs index 18266f0c..7bd2a51d 100644 --- a/src/Numerics/LinearAlgebra/Vector.cs +++ b/src/Numerics/LinearAlgebra/Vector.cs @@ -371,12 +371,17 @@ namespace MathNet.Numerics.LinearAlgebra /// If forceMapZero is not set to true, zero values may or may not be skipped depending /// on the actual data storage implementation (relevant mostly for sparse vectors). /// - public void Map(Func f, Vector result, Zeros zeros = Zeros.AllowSkip) - where TU : struct, IEquatable, IFormattable + public void Map(Func f, Vector result, Zeros zeros = Zeros.AllowSkip) { - // TODO: in v4 update this method to replace TU with T (consistent with Matrix, see MapConvert) - // then automatically do in-place if possible. - Storage.MapTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); + if (ReferenceEquals(this, result)) + { + // TODO: actual in-place + Storage.MapToUnchecked(Storage, f, zeros, ExistingData.AssumeZeros); + } + else + { + Storage.MapTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); + } } /// @@ -385,12 +390,17 @@ namespace MathNet.Numerics.LinearAlgebra /// If forceMapZero is not set to true, zero values may or may not be skipped depending /// on the actual data storage implementation (relevant mostly for sparse vectors). /// - public void MapIndexed(Func f, Vector result, Zeros zeros = Zeros.AllowSkip) - where TU : struct, IEquatable, IFormattable + public void MapIndexed(Func f, Vector result, Zeros zeros = Zeros.AllowSkip) { - // TODO: in v4 update this method to replace TU with T (consistent with Matrix, see MapIndexedConvert) - // then automatically do in-place if possible. - Storage.MapIndexedTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); + if (ReferenceEquals(this, result)) + { + // TODO: actual in-place + Storage.MapIndexedToUnchecked(Storage, f, zeros, ExistingData.AssumeZeros); + } + else + { + Storage.MapIndexedTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); + } } ///