Owl - Need help with basic usage

Hi,

finally I have building and installing owl 0.6.0 and the tests succeed.

However I am struggling with basic things using this library. Nearly all what I try fails.
(I have tried examples from http://ocaml.xyz/chapter/matrix.html?highlight=tutorial#create-matrices )

For example:

open Owl

let x = (Mat.uniform_int 5 5)

When i try to compile it I am getting
File "m.ml", line 4, characters 9-24: Error: Unbound value Mat.uniform_int

leaving int away gives:
?a:Owl.Mat.elt -> ?b:Owl.Mat.elt -> out:Owl.Mat.mat -> unit
This argument cannot be applied without label

My goal is to create a matrix and print it:

open Owl

let x = (Mat.uniform_int 5 5) in 
   Mat.iteri (fun i j a -> Printf.printf "(%i,%i) %.1f\n" i j a) x

What am I doing wrong?
Is there a tutorial for owl 0.6.0?

Some findings:

  1. owl comes with a command line program owl that starts the ocaml-interpreter

owl ./m.ml

  1. In a ml-program using owl following is not allowed:

    open Owl

    let x =9 in print_int x;

with Owl it causes a syntax error, without not

  1. Following test program runs, which means that the examples I found are inconsistent :

    open Owl

    let f =
    let x = (Mat.of_array [|1.;0.;3.;4.;5.;0.;7.;8.;9.|] 3 3) in
    Mat.iteri (fun i a -> Printf.printf “(%i) %.1f\n” i a) x

This runs with

owl ./m.ml

I see clearer now. Working thru the examlpes and tests helped.

open Owl

    let f =
      let x = (Mat.of_array [|1.;0.;3.;4.;5.;5.;0.;8.;7.;8.;9.;4.;3.;-2.;1.;9.|] 4 4) in
      let y = Mat.inv x in
      let dx = Owl.Linalg.D.det x in
      let dy = Owl.Linalg.D.det y in
      let xy = Mat.(y *@ x) in
        Mat.iteri_rows (fun i  r ->
            Mat.iteri (fun j a -> Printf.printf "(%i,%i) %.1f\n" i j  a) r) xy;
        Printf.printf "det(x) = %.4f det(y) = %.4f %4f\n" dx dy (dx *. dy)

Thanks for pointing this out. I think some tutorial parts got stale, we should really find a way to integrate them with mdx and run them on PRs. I’ll open a ticket for it.

For some reason the uniform_int function has disappeared for dense matrices (you can still do it for sparse matrices: Sparse.Matrix.D.uniform_int 5 5 works). I don’t know if this was on purpose or happened by mistake. I’ll open a ticket for it.

To see the shape of ndarrays and matrices, owl provides pretty printers but they need to be installed. One way to use them is to install owl-top, open utop and require owl-top. For example, try the following:

# require "owl-top" ;;
# open Owl;;
# Sparse.Matrix.D.uniform_int 5 5;;
# Mat.uniform 5 5;;
# let xy, x, y =
      let x = (Mat.of_array [|1.;0.;3.;4.;5.;5.;0.;8.;7.;8.;9.;4.;3.;-2.;1.;9.|] 4 4) in
      let y = Mat.inv x in
      let xy = Mat.(y *@ x) in
      xy, x, y
;;
# Linalg.D.det x ;;

You should see all the matrices pretty printed after each execution.

Please, if you go through the tutorial and find issues, think that there are missing concepts or tricks, or have any other suggestion, do open an issue or send a PR to https://github.com/owlbarn/owlbarn.github.io/

For example, the tutorial page for the matrices linked above, is generated from https://github.com/owlbarn/owlbarn.github.io/blob/master/_sources/chapter/matrix.rst.txt

2 Likes

Wonderful, this helps a lot!
It works line a charm if I start owl rather than plain ocaml from command line.