Source file owl_slicing_basic.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 1 "src/owl/core/owl_slicing_basic.ml"
(*
 * OWL - OCaml Scientific and Engineering Computing
 * Copyright (c) 2016-2019 Liang Wang <liang.wang@cl.cam.ac.uk>
 *)

open Bigarray

open Owl_types

open Owl_core_types


(* Interface to the native c functions *)

external owl_float32_ndarray_get_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_float32_ndarray_get_slice"
external owl_float64_ndarray_get_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_float64_ndarray_get_slice"
external owl_complex32_ndarray_get_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_complex32_ndarray_get_slice"
external owl_complex64_ndarray_get_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_complex64_ndarray_get_slice"

let _ndarray_get_slice
  : type a b c. (a, b) kind -> (a, b) owl_arr -> (a, b) owl_arr -> (int64, c) owl_arr -> unit
  = function
  | Float32   -> owl_float32_ndarray_get_slice
  | Float64   -> owl_float64_ndarray_get_slice
  | Complex32 -> owl_complex32_ndarray_get_slice
  | Complex64 -> owl_complex64_ndarray_get_slice
  | _         -> failwith "_ndarray_get_slice: unsupported operation"


external owl_float32_ndarray_set_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_float32_ndarray_set_slice"
external owl_float64_ndarray_set_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_float64_ndarray_set_slice"
external owl_complex32_ndarray_set_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_complex32_ndarray_set_slice"
external owl_complex64_ndarray_set_slice : ('a, 'b) owl_arr -> ('a, 'b) owl_arr -> (int64, 'c) owl_arr -> unit = "stub_complex64_ndarray_set_slice"

let _ndarray_set_slice
  : type a b c. (a, b) kind -> (a, b) owl_arr -> (a, b) owl_arr -> (int64, c) owl_arr -> unit
  = function
  | Float32   -> owl_float32_ndarray_set_slice
  | Float64   -> owl_float64_ndarray_set_slice
  | Complex32 -> owl_complex32_ndarray_set_slice
  | Complex64 -> owl_complex64_ndarray_set_slice
  | _         -> failwith "_ndarray_set_slice: unsupported operation"


(* Core functions for fancy slicing *)


(* encode slice definition consisting only R_ to (start,stop,step) triplets *)
let encode_slice_definition s =
  let t = Genarray.create Int64 C_layout [|3 * Array.length s|] in
  Array.iteri (fun i a ->
    match a with
    | R_ x -> (
        let j = 3 * i in
        Genarray.set t [|j|]     (Int64.of_int x.(0));
        Genarray.set t [|j + 1|] (Int64.of_int x.(1));
        Genarray.set t [|j + 2|] (Int64.of_int x.(2));
      )
    | _    -> failwith "owl_slicing_basic:encode_slice_definition"
  ) s;
  t


let get kind axis x y =
  let triplets = encode_slice_definition axis in
  _ndarray_get_slice kind x y triplets


let set kind axis x y =
  let triplets = encode_slice_definition axis in
  _ndarray_set_slice kind x y triplets