diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index 86e6ca4d..ba11a741 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -28,11 +28,12 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using MathNet.Numerics.LinearAlgebra.Storage; -using MathNet.Numerics.Properties; using System; using System.Collections.Generic; +using System.Linq; using System.Runtime; +using MathNet.Numerics.LinearAlgebra.Storage; +using MathNet.Numerics.Properties; using MathNet.Numerics.Threading; namespace MathNet.Numerics.LinearAlgebra @@ -1613,7 +1614,7 @@ namespace MathNet.Numerics.LinearAlgebra /// /// For each row, applies a function f to each element of the row, threading an accumulator argument through the computation. - /// Returns a vector with the resulting accumulator states for each row. + /// Returns an array with the resulting accumulator states for each row. /// public TU[] FoldByRow(Func f, TU state, Zeros zeros = Zeros.AllowSkip) { @@ -1634,7 +1635,7 @@ namespace MathNet.Numerics.LinearAlgebra /// /// For each column, applies a function f to each element of the column, threading an accumulator argument through the computation. - /// Returns a vector with the resulting accumulator states for each column. + /// Returns an array with the resulting accumulator states for each column. /// public TU[] FoldByColumn(Func f, TU state, Zeros zeros = Zeros.AllowSkip) { @@ -1652,5 +1653,49 @@ namespace MathNet.Numerics.LinearAlgebra Storage.FoldByColumnUnchecked(result, f, (x, c) => x, result, zeros); return result; } + + /// + /// Applies a function f to each row vector, threading an accumulator vector argument through the computation. + /// Returns the resulting accumulator vector. + /// + public Vector FoldRows(Func, Vector, Vector> f, Vector state) + where TU : struct, IEquatable, IFormattable + { + foreach (var vector in EnumerateRows()) + { + state = f(state, vector); + } + return state; + } + + /// + /// Applies a function f to each column vector, threading an accumulator vector argument through the computation. + /// Returns the resulting accumulator vector. + /// + public Vector FoldColumns(Func, Vector, Vector> f, Vector state) + where TU : struct, IEquatable, IFormattable + { + foreach (var vector in EnumerateColumns()) + { + state = f(state, vector); + } + return state; + } + + /// + /// Reduces all row vectors by applying a function between two of them, until only a single vector is left. + /// + public Vector ReduceRows(Func, Vector, Vector> f) + { + return EnumerateRows().Aggregate(f); + } + + /// + /// Reduces all column vectors by applying a function between two of them, until only a single vector is left. + /// + public Vector ReduceColumns(Func, Vector, Vector> f) + { + return EnumerateColumns().Aggregate(f); + } } }