let zero = -. 1. /. 0.
let one = 0.
    
let add x y = 
  if x = zero then y
  else if y = zero then x
  else
    let max_val = max x y in
    let maxed_x = x -. max_val
    and maxed_y = y -. max_val in
    log (exp maxed_x +. exp maxed_y) +. max_val 

let add_vec v =
  let max = Array.fold_left (fun x y -> if x > y then x else y) zero v in
  let subbed_vec = Array.map (fun x -> 
    if x = zero 
    then 0. 
    else exp (x -. max)) v in
  let sum = log (Array.fold_left (fun x y -> x +. y) 0. subbed_vec) in
  sum +. max

let sub x y = 
  if x = zero then zero
  else if y = zero then x
  else log (exp x -. exp y)

      
