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

open Bigarray

type mat_d = Owl_dense.Matrix.D.mat
type mat_z = Owl_dense.Matrix.Z.mat
type ('a, 'b) t = ('a, 'b) Owl_dense.Matrix.Generic.t

module MD = Owl_dense_matrix_d
module MZ = Owl_dense_matrix_z

module M = Owl_dense.Matrix.Generic


(** [ Helper functions ]  *)

let tridiag_to_vecs x =
  let m = min (MD.row_num x) (MD.col_num x) in
  let d = MD.empty 1 m in
  let e = MD.empty 1 (m - 1) in
  let f = MD.empty 1 (m - 1) in
  for i = 0 to m - 1 do
    d.{0,i} <- x.{i,i};
    if (i > 0) then e.{0,i-1} <- x.{i-1,i};
    if (i < m - 1) then f.{0,i} <- x.{i+1,i};
  done; d, e, f


let vecs_to_tridiag d e f =
  let m = MD.numel d in
  let x = MD.zeros m m in
  for i = 0 to m - 1 do
    x.{i,i} <- d.{0,i};
    if (i > 0) then x.{i-1,i} <- e.{0,i-1};
    if (i < m - 1) then x.{i+1,i} <- f.{0,i};
  done; x


(** [ LU decomposition ]  *)

let inv x =
  let open Gsl.Vectmat in
  let y = Gsl.Linalg.invert_LU (`M x) in
  match y with
    | `M y -> y
    | _ -> MD.empty 0 0

let det x =
  let open Gsl.Vectmat in
  Gsl.Linalg.det_LU (`M x)

let lu x =
  let open Gsl.Vectmat in
  let y = Gsl.Linalg.decomp_LU (`M x) in
  match y with
    | `M a, b, c -> a, b, c
    | _, b, c -> MD.empty 0 0, b, c

let lu_solve = None


(** [ QR decomposition ]  *)


let _get_q
  : type a b. (a, b) kind -> (a, b) t -> (a, b) t -> (a, b) t
  = fun k a tau ->
  match k with
  | Float32   -> Owl_lapacke.orgqr a tau
  | Float64   -> Owl_lapacke.orgqr a tau
  | Complex32 -> Owl_lapacke.ungqr a tau
  | Complex64 -> Owl_lapacke.ungqr a tau
  | _         -> failwith "owl_linalg:_get_q"


let qr ?(thin=true) x =
  let x = M.clone x in
  let m, n = M.shape x in
  let a, tau = Owl_lapacke.geqrf x in
  let r = M.resize ~head:true n n (M.triu a) in
  let a = match thin with
    | true  -> a
    | false ->
        if m <= n then a
        else (
          let a' = M.empty (M.kind x) m (m - n) in
          M.concat_horizontal a a'
        )
  in
  let q = _get_q (M.kind x) a tau in
  q, r


let qr_pivot x =
  let x = M.clone x in<commit_msg>fix qr<commit_after>(*
 * OWL - an OCaml numerical library for scientific computing
 * Copyright (c) 2016-2017 <NAME> <<EMAIL>>
 *)

open Bigarray

type mat_d = Owl_dense.Matrix.D.mat
type mat_z = Owl_dense.Matrix.Z.mat
type ('a, 'b) t = ('a, 'b) Owl_dense.Matrix.Generic.t

module MD = Owl_dense_matrix_d
module MZ = Owl_dense_matrix_z

module M = Owl_dense.Matrix.Generic


(** [ Helper functions ]  *)

let tridiag_to_vecs x =
  let m = min (MD.row_num x) (MD.col_num x) in
  let d = MD.empty 1 m in
  let e = MD.empty 1 (m - 1) in
  let f = MD.empty 1 (m - 1) in
  for i = 0 to m - 1 do
    d.{0,i} <- x.{i,i};
    if (i > 0) then e.{0,i-1} <- x.{i-1,i};
    if (i < m - 1) then f.{0,i} <- x.{i+1,i};
  done; d, e, f


let vecs_to_tridiag d e f =
  let m = MD.numel d in
  let x = MD.zeros m m in
  for i = 0 to m - 1 do
    x.{i,i} <- d.{0,i};
    if (i > 0) then x.{i-1,i} <- e.{0,i-1};
    if (i < m - 1) then x.{i+1,i} <- f.{0,i};
  done; x


(** [ LU decomposition ]  *)

let inv x =
  let open Gsl.Vectmat in
  let y = Gsl.Linalg.invert_LU (`M x) in
  match y with
    | `M y -> y
    | _ -> MD.empty 0 0

let det x =
  let open Gsl.Vectmat in
  Gsl.Linalg.det_LU (`M x)

let lu x =
  let open Gsl.Vectmat in
  let y = Gsl.Linalg.decomp_LU (`M x) in
  match y with
    | `M a, b, c -> a, b, c
    | _, b, c -> MD.empty 0 0, b, c

let lu_solve = None


(** [ QR decomposition ]  *)


let _get_q
  : type a b. (a, b) kind -> (a, b) t -> (a, b) t -> (a, b) t
  = fun k a tau ->
  match k with
  | Float32   -> Owl_lapacke.orgqr a tau
  | Float64   -> Owl_lapacke.orgqr a tau
  | Complex32 -> Owl_lapacke.ungqr a tau
  | Complex64 -> Owl_lapacke.ungqr a tau
  | _         -> failwith "owl_linalg:_get_q"


let qr ?(thin=true) x =
  let x = M.clone x in
  let m, n = M.shape x in
  let minmn = Pervasives.min m n in
  let a, tau = Owl_lapacke.geqrf x in
  let r = match thin with
    | true  -> M.resize ~head:true minmn n (M.triu a)
    | false -> M.resize ~head:true m n (M.triu a)
  in
  let a = match thin with
    | true  -> a
    | false ->
        if m <= n then a
        else (
          let a' = M.zeros (M.kind x) m (m - n) in
          M.concat_horizontal a a'
        )
  in
  let q = _get_q (M.kind x) a tau in
  q, r


let qr_pivot x =
  let x = M.clone x in
{'fraction_non_alphanumeric': 0.15135135135135136, 'fraction_numerical': 0.02027027027027027, 'mean_word_length': 2.016983695652174, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 45, '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': '1256432', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2009, 'n_words': 663}