Normalise a Vector using owl

Task

Mathematics / Vector & Matrix Operations / Normalise a Vector

Opam Packages Used

  • owl Tested with version: 1.1 — Used libraries: owl

Code

Create a 2x1 matrix (column vector) with values [1.; 1.]

let v = Owl.Mat.of_array [|1.;1.|] 2 1

Calculate the Euclidean norm of the vector

let norm = Owl.Linalg.D.norm v

The /$ operator performs element-wise division between a matrix and a scalar

let v' = Owl.Mat.( v /$ norm );;

let () =
  Owl.Mat.iteri_2d
    (fun i j a -> Printf.printf "(%i) %.1f\n" i a)
    v';;

Recipe not working? Comments not clear or out of date?

Open an issue or contribute to this recipe!

Other Recipes for this Task