Skip to content

Instantly share code, notes, and snippets.

@andrewray
Created April 3, 2014 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewray/9952818 to your computer and use it in GitHub Desktop.
Save andrewray/9952818 to your computer and use it in GitHub Desktop.
Tests for new js_of_ocaml bigarray implementations
{"metadata":{"name":"","language":"ocaml"},"worksheets":[{"cells":[{"metadata":{},"input":"#use \"topfind\";;\n#require \"bigarray\";;\nopen Bigarray;;","cell_type":"code","prompt_number":1,"outputs":[{"output_type":"stream","text":"- : unit = ()\n","stream":"stdout"},{"output_type":"stream","text":"Findlib has been successfully loaded. Additional directives:\n #require \"package\";; to load a package\n #list;; to list the available packages\n #camlp4o;; to load camlp4 (standard syntax)\n #camlp4r;; to load camlp4 (revised syntax)\n #predicates \"p,q,...\";; to set these predicates\n Topfind.reset();; to force that packages will be reloaded\n #thread;; to enable threads\n","stream":"stdout"},{"output_type":"stream","text":"\n","stream":"stdout"},{"output_type":"stream","text":"- : unit = ()\n","stream":"stdout"},{"output_type":"stream","text":"/home/andyman/.opam/4.01.0-test/lib/ocaml/unix.cma: loaded\n","stream":"stderr"},{"output_type":"stream","text":"/home/andyman/.opam/4.01.0-test/lib/ocaml/bigarray.cma: loaded\n","stream":"stderr"}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Array1\n\nBasic Array1 get/set functions"},{"metadata":{},"input":"let len = 10\nlet b1 = Array1.create int c_layout len\nlet () = for i=0 to len-1 do b1.{i} <- (i+1)*3 done\nlet a1 = Array.init len (fun i -> b1.{i})","cell_type":"code","prompt_number":2,"outputs":[{"output_type":"pyout","prompt_number":2,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val len : int = 10\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":2,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b1 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":2,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a1 : int array = [|3; 6; 9; 12; 15; 18; 21; 24; 27; 30|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Array2\n\nBasic Array2 get/set functions using floats."},{"metadata":{},"input":"let len1, len2 = 2, 3\nlet b2 = Array2.create float32 c_layout len1 len2\nlet () = \n for i=0 to len1-1 do \n for j=0 to len2-1 do \n b2.{i,j} <- (float j *. 0.5) +. float i \n done\n done\nlet a2 = Array.init len2 (fun j -> Array.init len1 (fun i -> b2.{i,j}))","cell_type":"code","prompt_number":3,"outputs":[{"output_type":"pyout","prompt_number":3,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val len1 : int = 2\nval len2 : int = 3\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":3,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b2 : (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":3,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a2 : float array array = [|[|0.; 1.|]; [|0.5; 1.5|]; [|1.; 2.|]|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Array3\n\nBasic Array3 get/set functions using int32"},{"metadata":{},"input":"let len1, len2, len3 = 2, 3, 4\nlet b3 = Array3.create int32 c_layout len1 len2 len3\nlet () = \n for i=0 to len1-1 do \n for j=0 to len2-1 do \n for k=0 to len3-1 do \n b3.{i,j,k} <- Int32.(add (mul (of_int k) 100l) (add (mul (of_int j) 10l) (of_int i))) \n done\n done\n done\nlet a3 = Array.init len3 (fun k -> Array.init len2 (fun j -> Array.init len1 (fun i -> b3.{i,j,k})))","cell_type":"code","prompt_number":4,"outputs":[{"output_type":"pyout","prompt_number":4,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val len1 : int = 2\nval len2 : int = 3\nval len3 : int = 4\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":4,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b3 : (int32, Bigarray.int32_elt, Bigarray.c_layout) Bigarray.Array3.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":4,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a3 : int32 array array array =\n [|[|[|0l; 1l|]; [|10l; 11l|]; [|20l; 21l|]|];\n [|[|100l; 101l|]; [|110l; 111l|]; [|120l; 121l|]|];\n [|[|200l; 201l|]; [|210l; 211l|]; [|220l; 221l|]|];\n [|[|300l; 301l|]; [|310l; 311l|]; [|320l; 321l|]|]|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Genarray\n\nBasic Genarray get/set functions"},{"metadata":{},"cell_type":"markdown","source":"Start off with a very basic Genarray test"},{"metadata":{},"input":"let b = Genarray.create int c_layout [|2;2|]\nlet _ = Genarray.set b [|0;0|] 1, Genarray.set b [|0;1|] 2, Genarray.set b [|1;0|] 3, Genarray.set b [|1;1|] 4\nlet x = Genarray.get b [|0;0|], Genarray.get b [|0;1|], Genarray.get b [|1;0|], Genarray.get b [|1;1|]","cell_type":"code","prompt_number":5,"outputs":[{"output_type":"pyout","prompt_number":5,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Genarray.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":5,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : unit * unit * unit * unit = ((), (), (), ())\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":5,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val x : int * int * int * int = (1, 2, 3, 4)\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"Make a `Genarray` with more complex dimensions. The `incr` function creates all the indices over the `Genarray`. The index is hashed, stored in the array an xor'ed together. When the array is read the hash is recomputed."},{"metadata":{},"input":"let dims = [| 2; 3; 4; 2; 3; 4; 2; 3; 4 |]\n\nlet incr x = \n let rec f i = \n if x.(i)+1 = dims.(i) then (x.(i) <- 0; f (i+1)) \n else (x.(i) <- x.(i)+1;) \n in\n f 0 \n \nlet bn = Genarray.create int c_layout dims\nlet size = Array.fold_left ( * ) 1 dims \n\nlet hash = \n let h = ref 0 in\n let idx = Array.map (fun _ -> 0) dims in\n for i=0 to size-1 do \n let h' = Hashtbl.hash idx in\n Genarray.set bn idx h';\n h := !h lxor h';\n (try incr idx with _ -> ());\n done;\n !h\n\nlet hash_get = \n let h = ref 0 in\n let idx = Array.map (fun _ -> 0) dims in\n for i=0 to size-1 do \n h := !h lxor (Genarray.get bn idx);\n (try incr idx with _ -> ());\n done;\n !h","cell_type":"code","prompt_number":6,"outputs":[{"output_type":"pyout","prompt_number":6,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val dims : int array = [|2; 3; 4; 2; 3; 4; 2; 3; 4|]\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":6,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val incr : int array -&gt; unit = &lt;fun&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":6,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val bn : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Genarray.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":6,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val size : int = 13824\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":6,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val hash : int = 312082685\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":6,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val hash_get : int = 312082685\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Index exceptions\n\nCheck for index out of bounds exceptions"},{"metadata":{},"input":"let exn f i = try f i |> ignore; false with x when x = Invalid_argument(\"index out of bounds\") -> true | _ -> false","cell_type":"code","prompt_number":7,"outputs":[{"output_type":"pyout","prompt_number":7,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val exn : ('a -&gt; 'b) -&gt; 'a -&gt; bool = &lt;fun&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"let ok1 = \n let x = exn (Array1.get b1) in \n x (-100) && x (-1) && x 10 && x 100\nlet ok2 = \n let x = exn (fun (i,j) -> Array2.get b2 i j) in \n x (-1,0) && x (0,-1) && x (-10,-10) && x (0,3) && x (2,0) && x (2,3)\nlet ok3 = \n let x = exn (fun (i,j,k) -> Array3.get b3 i j k) in \n x (-1,0,0) && x (0,-1,0) && x (0,0,-1) && x (-10,-10,-10) && \n x (2,0,0) && x (0,3,0) && x (0,0,4) && x (10,20,30)","cell_type":"code","prompt_number":8,"outputs":[{"output_type":"pyout","prompt_number":8,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ok1 : bool = true\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":8,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ok2 : bool = true\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":8,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ok3 : bool = true\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# fortran_layout"},{"metadata":{},"input":"let len = 10\nlet b1 = Array1.create int fortran_layout len\nlet () = for i=1 to len do b1.{i} <- i*2 done\nlet a1 = Array.init len (fun i -> b1.{i+1})","cell_type":"code","prompt_number":9,"outputs":[{"output_type":"pyout","prompt_number":9,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val len : int = 10\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":9,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b1 : (int, Bigarray.int_elt, Bigarray.fortran_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":9,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a1 : int array = [|2; 4; 6; 8; 10; 12; 14; 16; 18; 20|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"let len1, len2 = 2, 3\nlet b2 = Genarray.create float32 fortran_layout [|len1; len2|]\nlet () = \n for i=1 to len1 do \n for j=1 to len2 do \n Genarray.set b2 [|i; j|] ((float j *. 0.5) +. float i) \n done\n done\nlet a2 = Array.init len2 (fun j -> Array.init len1 (fun i -> Genarray.get b2 [|i+1;j+1|]))","cell_type":"code","prompt_number":10,"outputs":[{"output_type":"pyout","prompt_number":10,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val len1 : int = 2\nval len2 : int = 3\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":10,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b2 :\n (float, Bigarray.float32_elt, Bigarray.fortran_layout) Bigarray.Genarray.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":10,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a2 : float array array = [|[|1.5; 2.5|]; [|2.; 3.|]; [|2.5; 3.5|]|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# fill"},{"metadata":{},"input":"let b = Array1.create int16_unsigned c_layout 4\nlet () = Array1.fill b 0x1_ffff\nlet a = Array.init 4 (Array1.get b)","cell_type":"code","prompt_number":11,"outputs":[{"output_type":"pyout","prompt_number":11,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b :\n (int, Bigarray.int16_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":11,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a : int array = [|65535; 65535; 65535; 65535|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"let b = Genarray.create int fortran_layout [|2;2;2|]\nlet () = Genarray.fill b (-2)\nlet a = Array.init 2 (fun k -> Array.init 2 (fun j -> Array.init 2 (fun i -> Genarray.get b [|i+1;j+1;k+1|])))","cell_type":"code","prompt_number":12,"outputs":[{"output_type":"pyout","prompt_number":12,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b : (int, Bigarray.int_elt, Bigarray.fortran_layout) Bigarray.Genarray.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":12,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a : int array array array =\n [|[|[|-2; -2|]; [|-2; -2|]|]; [|[|-2; -2|]; [|-2; -2|]|]|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# blit"},{"metadata":{},"input":"let b = Array1.create int16_unsigned c_layout 4\nlet () = Array1.fill b 0xbeef\nlet c = Array1.create int16_unsigned c_layout 4\nlet () = Array1.blit b c\nlet a = Array.init 4 (Array1.get c)","cell_type":"code","prompt_number":13,"outputs":[{"output_type":"pyout","prompt_number":13,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val b :\n (int, Bigarray.int16_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":13,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val c :\n (int, Bigarray.int16_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":13,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val a : int array = [|48879; 48879; 48879; 48879|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Generic arrays\n\nTest all array layouts/kinds using `Genarray` over various dimensions."},{"metadata":{},"input":"(* generic arrays *)\nmodule type B = sig\n type t \n type elt\n val kind : (t,elt) Bigarray.kind\n val rand : unit -> t\n val equal : t -> t -> bool\n val name : string\nend\nmodule type D = sig\n val dims : int array\nend\nmodule TestGenarray(B : B)(D : D) = struct\n\n let size = Array.fold_left ( * ) 1 D.dims\n let rand_init() = Random.init 1000\n\n let c_layout = (c_layout,0)\n let fortran_layout = (fortran_layout,1)\n \n (* multi-dimensional indices *)\n let mk_idx (_,ofs) = Array.map (fun _ -> ofs) D.dims\n let incr (_,ofs) x = \n let rec f i = \n if x.(i) = D.dims.(i)-(1-ofs) then (x.(i) <- ofs; f (i+1)) \n else (x.(i) <- x.(i)+1;) \n in\n try f 0 with _ -> ()\n \n let test layout =\n let ba = Genarray.create B.kind (fst layout) D.dims in\n let ba2 = Genarray.create B.kind (fst layout) D.dims in\n \n (* write random gunk *)\n let idx,() = mk_idx layout, rand_init() in\n for i=0 to size-1 do\n Genarray.set ba idx (B.rand());\n incr layout idx\n done;\n (* read back and check *)\n let idx,() = mk_idx layout, rand_init() in\n for i=0 to size-1 do\n let r = B.rand() in\n let a = Genarray.get ba idx in\n assert (B.equal r a);\n incr layout idx\n done;\n \n (* blit to new array *)\n let () = Genarray.blit ba ba2 in\n\n (* fill 1st array with random value *)\n let x = B.rand() in\n let () = Genarray.fill ba x in\n (* read back and check *)\n let idx = mk_idx layout in\n for i=0 to size-1 do\n let a = Genarray.get ba idx in\n assert (B.equal x a);\n incr layout idx\n done;\n\n (* read back 2nd array check *)\n let idx,() = mk_idx layout, rand_init() in\n for i=0 to size-1 do\n let r = B.rand() in\n let a = Genarray.get ba2 idx in\n assert (B.equal r a);\n incr layout idx\n done\n \n \n \n let run () = \n (*let () = Printf.printf \"%s\\n\" B.name in*)\n let () = test c_layout in\n let () = test fortran_layout in\n ()\n\nend\n","cell_type":"code","prompt_number":14,"outputs":[{"output_type":"pyout","prompt_number":14,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module type B =\n sig\n type t\n type elt\n val kind : (t, elt) Bigarray.kind\n val rand : unit -&gt; t\n val equal : t -&gt; t -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":14,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module type D = sig val dims : int array end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":14,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module TestGenarray :\n functor (B : B) -&gt;\n functor (D : D) -&gt;\n sig\n val size : int\n val rand_init : unit -&gt; unit\n val c_layout : Bigarray.c_layout Bigarray.layout * int\n val fortran_layout : Bigarray.fortran_layout Bigarray.layout * int\n val mk_idx : 'a * 'b -&gt; 'b array\n val incr : 'a * int -&gt; int array -&gt; unit\n val test : 'a Bigarray.layout * int -&gt; unit\n val run : unit -&gt; unit\n end\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"module Int = struct\n type t = int\n type elt = Bigarray.int_elt\n let kind = Bigarray.int\n let rand () = Random.int 1000\n let equal a b = Pervasives.compare a b = 0\n let name = \"int\"\nend\nmodule Int32 = struct\n type t = int32\n type elt = Bigarray.int32_elt\n let kind = Bigarray.int32\n let rand () = Random.int32 1000l\n let equal a b = Pervasives.compare a b = 0\n let name = \"int32\"\nend\nmodule Int64 = struct\n type t = int64\n type elt = Bigarray.int64_elt\n let kind = Bigarray.int64\n let rand () = Random.int64 1000L\n let equal a b = Pervasives.compare a b = 0\n let name = \"int64\"\nend\nmodule Char_ = struct\n type t = char\n type elt = Bigarray.int8_unsigned_elt\n let kind = Bigarray.char\n let rand () = Char.chr (Random.int 256)\n let equal a b = Pervasives.compare a b = 0\n let name = \"char\"\nend\nmodule Uint8 = struct\n type t = int\n type elt = Bigarray.int8_unsigned_elt\n let kind = Bigarray.int8_unsigned\n let rand () = Random.int 256\n let equal a b = Pervasives.compare a b = 0\n let name = \"uint8\"\nend\nmodule Sint8 = struct\n type t = int\n type elt = Bigarray.int8_signed_elt\n let kind = Bigarray.int8_signed\n let rand () = Random.int 128\n let equal a b = Pervasives.compare a b = 0\n let name = \"sint8\"\nend\nmodule Uint16 = struct\n type t = int\n type elt = Bigarray.int16_unsigned_elt\n let kind = Bigarray.int16_unsigned\n let rand () = Random.int 1000\n let equal a b = Pervasives.compare a b = 0\n let name = \"uin16\"\nend\nmodule Sint16 = struct\n type t = int\n type elt = Bigarray.int16_signed_elt\n let kind = Bigarray.int16_signed\n let rand () = Random.int 1000\n let equal a b = Pervasives.compare a b = 0\n let name = \"sint16\"\nend\nmodule Float32 = struct\n type t = float\n type elt = Bigarray.float32_elt\n let kind = Bigarray.float32\n let rand () = Random.float 1000.\n let equal a b = abs_float (a -. b) < 0.0001\n let name = \"float32\"\nend\nmodule Float64 = struct\n type t = float\n type elt = Bigarray.float64_elt\n let kind = Bigarray.float64\n let rand () = Random.float 1000.\n let equal a b = Pervasives.compare a b = 0\n let name = \"float64\"\nend\nmodule Complex32 = struct\n type t = Complex.t\n type elt = Bigarray.complex32_elt\n let kind = Bigarray.complex32\n let rand () = Complex.({re=Random.float 1000.;im=Random.float 1000.})\n let equal a b = \n let open Complex in\n Float32.equal a.re b.re &&\n Float32.equal a.im b.im\n let name = \"complex32\"\nend\nmodule Complex64 = struct\n type t = Complex.t\n type elt = Bigarray.complex64_elt\n let kind = Bigarray.complex64\n let rand () = Complex.({re=Random.float 1000.;im=Random.float 1000.})\n let equal a b = Pervasives.compare a b = 0\n let name = \"complex64\"\nend\n\nlet ba_types = [\n (module Int : B);\n (module Int32 : B);\n (module Int64 : B);\n (module Char_ : B);\n (module Uint8 : B);\n (module Sint8 : B);\n (module Uint16 : B);\n (module Sint16 : B);\n (module Float32 : B);\n (module Float64 : B);\n (module Complex32 : B);\n (module Complex64 : B);\n]","cell_type":"code","prompt_number":15,"outputs":[{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Int :\n sig\n type t = int\n type elt = Bigarray.int_elt\n val kind : (int, Bigarray.int_elt) Bigarray.kind\n val rand : unit -&gt; int\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Int32 :\n sig\n type t = int32\n type elt = Bigarray.int32_elt\n val kind : (int32, Bigarray.int32_elt) Bigarray.kind\n val rand : unit -&gt; Int32.t\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Int64 :\n sig\n type t = int64\n type elt = Bigarray.int64_elt\n val kind : (int64, Bigarray.int64_elt) Bigarray.kind\n val rand : unit -&gt; Int64.t\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Char_ :\n sig\n type t = char\n type elt = Bigarray.int8_unsigned_elt\n val kind : (char, Bigarray.int8_unsigned_elt) Bigarray.kind\n val rand : unit -&gt; char\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Uint8 :\n sig\n type t = int\n type elt = Bigarray.int8_unsigned_elt\n val kind : (int, Bigarray.int8_unsigned_elt) Bigarray.kind\n val rand : unit -&gt; int\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Sint8 :\n sig\n type t = int\n type elt = Bigarray.int8_signed_elt\n val kind : (int, Bigarray.int8_signed_elt) Bigarray.kind\n val rand : unit -&gt; int\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Uint16 :\n sig\n type t = int\n type elt = Bigarray.int16_unsigned_elt\n val kind : (int, Bigarray.int16_unsigned_elt) Bigarray.kind\n val rand : unit -&gt; int\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Sint16 :\n sig\n type t = int\n type elt = Bigarray.int16_signed_elt\n val kind : (int, Bigarray.int16_signed_elt) Bigarray.kind\n val rand : unit -&gt; int\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Float32 :\n sig\n type t = float\n type elt = Bigarray.float32_elt\n val kind : (float, Bigarray.float32_elt) Bigarray.kind\n val rand : unit -&gt; float\n val equal : float -&gt; float -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Float64 :\n sig\n type t = float\n type elt = Bigarray.float64_elt\n val kind : (float, Bigarray.float64_elt) Bigarray.kind\n val rand : unit -&gt; float\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Complex32 :\n sig\n type t = Complex.t\n type elt = Bigarray.complex32_elt\n val kind : (Complex.t, Bigarray.complex32_elt) Bigarray.kind\n val rand : unit -&gt; Complex.t\n val equal : Complex.t -&gt; Complex.t -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module Complex64 :\n sig\n type t = Complex.t\n type elt = Bigarray.complex64_elt\n val kind : (Complex.t, Bigarray.complex64_elt) Bigarray.kind\n val rand : unit -&gt; Complex.t\n val equal : 'a -&gt; 'a -&gt; bool\n val name : string\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":15,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba_types : (module B) list =\n [&lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;;\n &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"Check generation of indices is working correctly"},{"metadata":{},"input":"module X = TestGenarray(Int)(struct let dims = [| 2;3 |] end)\nlet idx = X.mk_idx X.c_layout\nlet c = Array.init (2*3) (fun _ -> let a = Array.copy idx in X.incr X.c_layout idx; a)\nlet idx = X.mk_idx X.fortran_layout\nlet fortran = Array.init (2*3) (fun _ -> let a = Array.copy idx in X.incr X.fortran_layout idx; a)","cell_type":"code","prompt_number":16,"outputs":[{"output_type":"pyout","prompt_number":16,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">module X :\n sig\n val size : int\n val rand_init : unit -&gt; unit\n val c_layout : Bigarray.c_layout Bigarray.layout * int\n val fortran_layout : Bigarray.fortran_layout Bigarray.layout * int\n val mk_idx : 'a * 'b -&gt; 'b array\n val incr : 'a * int -&gt; int array -&gt; unit\n val test : 'a Bigarray.layout * int -&gt; unit\n val run : unit -&gt; unit\n end\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":16,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val idx : int array = [|0; 0|]\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":16,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val c : int array array =\n [|[|0; 0|]; [|1; 0|]; [|0; 1|]; [|1; 1|]; [|0; 2|]; [|1; 2|]|]\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":16,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val idx : int array = [|1; 1|]\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":16,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val fortran : int array array =\n [|[|1; 1|]; [|2; 1|]; [|1; 2|]; [|2; 2|]; [|1; 3|]; [|2; 3|]|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"Run tests. An exception is raised if there is an error"},{"metadata":{},"input":"let dims = List.map (fun d -> (module (struct let dims = [| 100 |] end) : D)) \n [\n [|1|];\n [|10|];\n [|10000|];\n [|1;1|];\n [|10;20|];\n [|100;200|];\n [|1;1;1|];\n [|10;8;12|];\n [|12;20;14;6;2;1;4|];\n ]\n \nlet test ba dims = \n let module Test = TestGenarray((val ba : B))((val dims : D)) in\n Test.run()\n \nlet _ = List.iter (fun ba -> List.iter (fun d -> test ba d) dims) ba_types","cell_type":"code","prompt_number":17,"outputs":[{"output_type":"pyout","prompt_number":17,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val dims : (module D) list =\n [&lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;; &lt;module&gt;;\n &lt;module&gt;; &lt;module&gt;]\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":17,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val test : (module B) -&gt; (module D) -&gt; unit = &lt;fun&gt;\n</pre>","metadata":{}},{"output_type":"pyout","prompt_number":17,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : unit = ()\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Sub arrays"},{"metadata":{},"input":"let ba = Array2.of_array int c_layout [|\n [| 0; 1 |];\n [| 2; 3 |];\n [| 4; 5 |];\n [| 6; 7 |];\n|]","cell_type":"code","prompt_number":18,"outputs":[{"output_type":"pyout","prompt_number":18,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"extract middle 2 rows"},{"metadata":{},"input":"let ba2 = Array2.sub_left ba 1 2","cell_type":"code","prompt_number":19,"outputs":[{"output_type":"pyout","prompt_number":19,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba2.{0,0}, ba2.{0,1}, ba2.{1,0}, ba2.{1,1}","cell_type":"code","prompt_number":20,"outputs":[{"output_type":"pyout","prompt_number":20,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int * int * int * int = (2, 3, 4, 5)\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"Ensure both arrays share data"},{"metadata":{},"input":"ba2.{0,0} <- 100","cell_type":"code","prompt_number":21,"outputs":[{"output_type":"pyout","prompt_number":21,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : unit = ()\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba.{1,0}","cell_type":"code","prompt_number":22,"outputs":[{"output_type":"pyout","prompt_number":22,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int = 100\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"Fortran layout"},{"metadata":{},"input":"let ba = Array2.of_array int fortran_layout [|\n [| 0; 1 |];\n [| 2; 3 |];\n [| 4; 5 |];\n [| 6; 7 |];\n|]","cell_type":"code","prompt_number":23,"outputs":[{"output_type":"pyout","prompt_number":23,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba : (int, Bigarray.int_elt, Bigarray.fortran_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"extract right hand column"},{"metadata":{},"input":"let ba2 = Array2.sub_right ba 2 1","cell_type":"code","prompt_number":24,"outputs":[{"output_type":"pyout","prompt_number":24,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.fortran_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"Array.init 4 (fun i -> ba2.{i+1,1})","cell_type":"code","prompt_number":25,"outputs":[{"output_type":"pyout","prompt_number":25,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int array = [|1; 3; 5; 7|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba2.{3,1} <- 50","cell_type":"code","prompt_number":26,"outputs":[{"output_type":"pyout","prompt_number":26,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : unit = ()\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba.{3,2}","cell_type":"code","prompt_number":27,"outputs":[{"output_type":"pyout","prompt_number":27,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int = 50\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Slicing arrays"},{"metadata":{},"input":"let ba = Array3.of_array int c_layout [|\n [| [| 0; 1 |]; [| 2; 3 |]; |];\n [| [| 4; 5 |]; [| 6; 7 |]; |];\n|]","cell_type":"code","prompt_number":28,"outputs":[{"output_type":"pyout","prompt_number":28,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array3.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"get the `[2,3]` subarray"},{"metadata":{},"input":"let ba2 = Array3.slice_left_1 ba 0 1","cell_type":"code","prompt_number":29,"outputs":[{"output_type":"pyout","prompt_number":29,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba2.{0}, ba2.{1}","cell_type":"code","prompt_number":30,"outputs":[{"output_type":"pyout","prompt_number":30,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int * int = (2, 3)\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"get the `[4,5]` subarray"},{"metadata":{},"input":"let ba2 = Array3.slice_left_1 ba 1 0","cell_type":"code","prompt_number":31,"outputs":[{"output_type":"pyout","prompt_number":31,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba2.{0}, ba2.{1}","cell_type":"code","prompt_number":32,"outputs":[{"output_type":"pyout","prompt_number":32,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int * int = (4, 5)\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"get `[[4,5][6,7]]`"},{"metadata":{},"input":"let ba2 = Array3.slice_left_2 ba 1","cell_type":"code","prompt_number":33,"outputs":[{"output_type":"pyout","prompt_number":33,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba2.{0,0}, ba2.{0,1}, ba2.{1,0}, ba2.{1,1}","cell_type":"code","prompt_number":34,"outputs":[{"output_type":"pyout","prompt_number":34,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int * int * int * int = (4, 5, 6, 7)\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"# Reshaping"},{"metadata":{},"input":"let ba = Array2.of_array int c_layout [|\n [| 0; 1; 2; 3 |];\n [| 4; 5; 6; 7 |];\n|]","cell_type":"code","prompt_number":35,"outputs":[{"output_type":"pyout","prompt_number":35,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array2.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"2d to 1d"},{"metadata":{},"input":"let ba2 = reshape_1 (genarray_of_array2 ba) 8","cell_type":"code","prompt_number":36,"outputs":[{"output_type":"pyout","prompt_number":36,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array1.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"Array.init 8 (fun i -> ba2.{i})","cell_type":"code","prompt_number":37,"outputs":[{"output_type":"pyout","prompt_number":37,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int array = [|0; 1; 2; 3; 4; 5; 6; 7|]\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"cell_type":"markdown","source":"2d to 3d"},{"metadata":{},"input":"let ba2 = array3_of_genarray (reshape (genarray_of_array2 ba) [|2;2;2|])","cell_type":"code","prompt_number":38,"outputs":[{"output_type":"pyout","prompt_number":38,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">val ba2 : (int, Bigarray.int_elt, Bigarray.c_layout) Bigarray.Array3.t =\n &lt;abstr&gt;\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"ba2.{0,0,0}, ba2.{0,0,1}, ba2.{0,1,0}, ba2.{0,1,1}, \nba2.{1,0,0}, ba2.{1,0,1}, ba2.{1,1,0}, ba2.{1,1,1}","cell_type":"code","prompt_number":39,"outputs":[{"output_type":"pyout","prompt_number":39,"html":"<pre style=\"color:slategray;max-height:100px;overflow:hidden\" \nonclick=\"\nif (this.style.maxHeight === 'none') \n this.style.maxHeight = '100px';\nelse\n this.style.maxHeight = 'none'; \n\">- : int * int * int * int * int * int * int * int = (0, 1, 2, 3, 4, 5, 6, 7)\n</pre>","metadata":{}}],"language":"python","collapsed":false},{"metadata":{},"input":"","cell_type":"code","outputs":[],"language":"python","collapsed":false}],"metadata":{}}],"nbformat":3,"nbformat_minor":0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment