<commit_before>(*
 * OWL - an OCaml numerical library for scientific computing
 * Copyright (c) 2016-2017 <NAME> <<EMAIL>>
 *)

module M = Owl_dense_real

type mat = Owl_dense_real.mat

type t =
  | Float of float
  | Matrix of mat
  | D of dual
  | A of adjoint
and dual = {
  p : t;       (* primal value *)
  t : t;       (* tangent value *)
}
and adjoint = {
  p : t;       (* primal value *)
  a : t;       (* adjoint value *)
}
and trace_op =
  | Add of t * t
  | Sub of t * t
  | Mul of t * t
  | Div of t * t
  | Sin of t
  | Cos of t


let make_dual p t = D { p; t }

let dual = function
  | Float a -> Float 0.
  | Matrix a -> Float 0.
  | D a -> a.t

let rec zero = function
  | Float _ -> Float 0.
  | Matrix _ -> Float 0.
  | D a -> make_dual (zero a.p) (zero a.t)

let rec one = function
  | Float _ -> Float 1.
  | Matrix _ -> failwith "Error: one does not take matrix."
  | D a -> make_dual (one a.p) (zero a.t)


module Maths = struct

  let rec noop _ = ()

  and op_d_d a ff fd df =
    match a with
    | D a -> let cp = fd a.p in make_dual cp (df cp a.p a.t)
    | A a -> failwith "error: not implemented."
    | ap -> ff ap

  and op_d_d_d a b ff fd df_da df_db df_dab = None<commit_msg>algodiff : experiment<commit_after>(*
 * OWL - an OCaml numerical library for scientific computing
 * Copyright (c) 2016-2017 <NAME> <<EMAIL>>
 *)

module M = Owl_dense_real

type mat = Owl_dense_real.mat

type t =
  | Float of float
  | Matrix of mat
  | D of dual
  | A of adjoint
and dual = {
  p : t;       (* primal value *)
  t : t;       (* tangent value *)
}
and adjoint = {
  p : t;       (* primal value *)
  a : t;       (* adjoint value *)
}
and trace_op =
  | Add of t * t
  | Sub of t * t
  | Mul of t * t
  | Div of t * t
  | Sin of t
  | Cos of t


let make_dual p t = D { p; t }

let dual = function
  | Float a -> Float 0.
  | Matrix a -> Float 0.
  | D a -> a.t

let rec zero = function
  | Float _ -> Float 0.
  | Matrix _ -> Float 0.
  | D a -> make_dual (zero a.p) (zero a.t)

let rec one = function
  | Float _ -> Float 1.
  | Matrix _ -> failwith "Error: one does not take matrix."
  | D a -> make_dual (one a.p) (zero a.t)


module Maths = struct

  let rec noop _ = ()

  and op_d_d a ff fd df =
    match a with
    | D a -> let cp = fd a.p in make_dual cp (df cp a.p a.t)
    | A a -> failwith "error: not implemented."
    | ap -> ff ap

  and op_d_d_d a b ff fd df_da df_db df_dab =
    match a, b with
    | Float ap, D b  -> let cp = fd a b.p in make_dual cp (df_db cp b.p b.t)
    | D a, Float bp  -> let cp = fd a.p b in make_dual cp (df_da cp a.p a.t)
    | Matrix ap, D b -> let cp = fd a b.p in make_dual cp (df_db cp b.p b.t)
    | D a, Matrix bp -> let cp = fd a.p b in make_dual cp (df_da cp a.p a.t)
    | D a, D b       -> let cp = fd a.p b.p in make_dual cp (df_dab cp a.p a.t b.p b.t)
    | a, b           -> ff a b
{'fraction_non_alphanumeric': 0.12447698744769875, 'fraction_numerical': 0.00906555090655509, 'mean_word_length': 1.90973630831643, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2617839', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1125, 'n_words': 460}