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); + } } ///