F# Scribbles

My experiments with F#

Python Numpy array.sum(axis = 1) in F#

In Python Numpy, the array.sum enables to specify axis over which the sum is taken for elements.

For example,

1
2
k = array ([[1, 2], [4, 5]])
k.sum(axis=1)

will give

1
array([3, 9])

In F# we can do that with FSharp.PowerPack.

Let us define a method to perform the above feature

1
2
3
let sumAxis1 (m:matrix) =
    let nrow = m.NumRows
    Matrix.foldByRow (fun a b -> a+ b) (Vector.create nrow 0.) m //fold by row

Let us apply the above function into a matrix

1
2
let m = matrix [[1.;2.]; [4.;5.]];;
sumAxis1 m;;

will give

1
val it : Vector<float> = vector [|3.0; 9.0|]