From 60effc957838fd68155001d9851cfae0021ffd12 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 15 Jun 2014 22:36:44 +0200 Subject: [PATCH] LA: Split Matrix.Map into Map and MapConvert to allow optimizing common inplace case --- src/Numerics/LinearAlgebra/Matrix.cs | 39 ++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index f2acc287..7ca21001 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -1530,7 +1530,42 @@ 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 matrices). /// - public void Map(Func f, Matrix result, Zeros zeros = Zeros.AllowSkip) + public void Map(Func f, Matrix result, Zeros zeros = Zeros.AllowSkip) + { + if (ReferenceEquals(this, result)) + { + Storage.MapInplace(f, zeros); + } + else + { + Storage.MapTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); + } + } + + /// + /// Applies a function to each value of this matrix and replaces the value in the result matrix. + /// The index of each value (zero-based) is passed as first argument to the function. + /// 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 matrices). + /// + public void MapIndexed(Func f, Matrix result, Zeros zeros = Zeros.AllowSkip) + { + if (ReferenceEquals(this, result)) + { + Storage.MapIndexedInplace(f, zeros); + } + else + { + Storage.MapIndexedTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); + } + } + + /// + /// Applies a function to each value of this matrix and replaces the value in the result matrix. + /// 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 matrices). + /// + public void MapConvert(Func f, Matrix result, Zeros zeros = Zeros.AllowSkip) where TU : struct, IEquatable, IFormattable { Storage.MapTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); @@ -1542,7 +1577,7 @@ 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 matrices). /// - public void MapIndexed(Func f, Matrix result, Zeros zeros = Zeros.AllowSkip) + public void MapIndexedConvert(Func f, Matrix result, Zeros zeros = Zeros.AllowSkip) where TU : struct, IEquatable, IFormattable { Storage.MapIndexedTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear);