diff --git a/src/FSharp/Matrix.fs b/src/FSharp/Matrix.fs index a5dab726..0bb74012 100644 --- a/src/FSharp/Matrix.fs +++ b/src/FSharp/Matrix.fs @@ -126,6 +126,14 @@ module Matrix = C.[i,j] <- f i j (C.Item(i,j)) C + /// Map every matrix column into an element in a sequence using the given position dependent function. + let inline mapCols (f: int -> Vector -> 'a) (A: #Matrix) = + A.ColumnEnumerator() |> Seq.map (fun (j, row) -> f j row) + + /// Map every matrix row into an element in a sequence using the given position dependent function. + let inline mapRows (f: int -> Vector -> 'a) (A: #Matrix) = + A.RowEnumerator() |> Seq.map (fun (i, row) -> f i row) + /// In-place assignment. let inline inplaceAssign (f: int -> int -> float) (A: #Matrix) = for i=0 to A.RowCount-1 do @@ -151,7 +159,15 @@ module Matrix = for j=0 to A.ColumnCount-1 do f <- f + A.Item(i,j) f + + /// Returns the sum of the results generated by applying the function to each column of the matrix. + let inline sumColsBy (f: int -> Vector -> float) (A: #Matrix) = + mapCols f A |> Seq.fold (+) 0.0 + /// Returns the sum of the results generated by applying the function to each row of the matrix. + let inline sumRowsBy (f: int -> Vector -> float) (A: #Matrix) = + mapRows f A |> Seq.fold (+) 0.0 + /// Iterates over all elements of a matrix. let inline iter (f: float -> unit) (A: #Matrix) = for i=0 to A.RowCount-1 do