Normalise a Vector using the Standard Library

Task

Mathematics / Vector & Matrix Operations / Normalise a Vector

No packages used

This recipe uses only the OCaml Standard Library.

Code

sum returns the sum of all float type elements in the list lst.

let sum lst = List.fold_left ( +. ) 0. lst

Returns the magnitude for a sum of squares sqs.

let magnitude sqs = Float.sqrt (sum sqs)

normalize accepts a list v of float values and returns the corresponding unit vector as a list.

let normalize v =
  let sqs = List.map (fun x -> x *. x) v in
  let m = magnitude sqs in
  List.map (fun x -> x /. m) v

Example use.

Using the format string "%.2f", the float values are rounded to the 2nd decimal place for readability.

let () =
  let vector = [4.;6.;-1.]
  let vhat = normalize vector;;
  List.iter (Printf.printf "%.2f ") vhat



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

Open an issue or contribute to this recipe!

Other Recipes for this Task