text
stringlengths 0
601k
|
---|
type t = private X of string | Y |
type t = private { f1 : t1 ; f2 : t2 } |
type t = private t ' |
let _ = let open F ( X ) in ( ) |
let _ = let x = 1 and y = 2 in { x ; y } |
let f = function | { x ; y ; _ } -> ( ) |
let f = fun ( type t ) ( x : t ) -> ( ) |
let f ( type t ) ( x : t ) = ( ) |
type m = ( module M . Sig with type t = ' b ) * unit |
let x = let m = ( module M : M . Sig with type t = ' b ) in let module M = ( val m : M . sig with type t = ' b ) in M |
module type S = sig include module type of M end |
module type S = sig include M0 with type t := t val x : unit end inherit ! cl val ! v = v method ! m = m end |
type _ t = A : int t | B : ' a t * ' b t -> ( ' a ' * b ) t |
module M = struct end |
module M = struct ; ; end |
module M = functor ( M1 : T1 ) -> functor ( M2 : T2 ) -> struct end |
module M = functor ( M1 : T1 ) -> functor ( M2 : T2 ) -> struct end |
module M = functor ( M1 : T1 ) -> functor ( M2 : T2 ) -> struct end |
module M = functor ( M1 : T1 ) -> functor ( M2 : T2 ) -> struct end |
module M = F ( X ) ( Y ) |
module M = ( struct sig end ) |
module M : Sig = struct end |
module M ( X1 : T1 ) ( X2 : T2 ) = |
module type T = M . T |
module type T = sig end |
module type T = sig ; ; end |
module type T = functor ( M : T ) -> functor ( M1 : T1 ) -> sig end |
sig end and module M = M ' . MF ( X ) and type t ' = t ' ' |
module type T = ( sig end ) |
module type T = sig val v : t external x : ' a = " stub " type t = int and t2 = t exception Error of int class virtual [ ' a ] cl : object end and cl2 : object end class type clt = object end and [ ' a ] clt2 = object end module M : Sig module M ( X ) ( Y ) : Sig module type Sig module type Sig1 = sig end open M include M end |
type t type ' a t type ' + _a t type ' - a t type ( ' a , ' + b , ( ' - c , ' - d ) ) t |
type t = A | B of ' a | C of ' a * ' b | D of ( ' a ) Array . t * ' b list | E of _ |
type t = { f1 : t1 ; f2 : ' a ; mutable f3 : t2 ; f4 : ' a ' b . t2 ; } |
type ' a t constraint ' a = t constraint ' b = ' a type ( ' a , ' + b , ( ' - c , ' - d ) ) t = { f1 : t1 ; f2 : ' a ; mutable f3 : t2 ; f4 : t1 * t2 ; } constraint ' a = t constraint ' b = ' a exception E exception E of ' a t * string exception E ' = E |
' ident _ ( t ) int -> int -> t -> t -> t lab1 : int -> lab2 : ( t ) -> t ? lab1 : ( ? _ : int -> t ) -> t ( t1 * t2 ) * ( t ) int ( ' a -> ' b ) Array . t int as ' bla [ ` _ | ` _ ' | ` _00 | ` Aa of int ] | ` _ | ` _ ' | ` _00 | ` Aa of int ] ` _ | ` _ ' | ` _00 | ` Aa of int ] [ | ` _ | ` _ ' | ` _00 | ` Aa of int ] | ` Bb of int & string & t | int > ` a ` _bbb ` c ` d ] < > < . . > < meth : int option ; meth2 : ' a . ' a option ; meth3 : ' a ' b . ( ' a , ' b ) Hashtbl . t > < meth : int option ; meth2 : ' a . ' a option ; meth3 : ' a ' b . ( ' a , ' b ) Hashtbl . t ; . . > # M . meth ' a # M . meth ( ' a , ' b ' * c ) # M . meth |
let catch ( ? catch_only = fun _ -> true ) f = match f ( ) with | ( ) -> ( ) | exception ( ( Stack_overflow | Out_of_memory ) as e ) -> raise e | exception e -> if catch_only e then ( ) else raise e |
let catch_f ( ? catch_only = fun _ -> true ) f h = match f ( ) with | ( ) -> ( ) | exception ( ( Stack_overflow | Out_of_memory ) as e ) -> raise e | exception e -> if catch_only e then h e else raise e |
let catch_s ( ? catch_only = fun _ -> true ) f = Lwt . catch f ( function | ( Stack_overflow | Out_of_memory ) as e -> raise e | e -> if catch_only e then Lwt . return_unit else raise e ) |
let aei = Alcotest . check Alcotest . int |
let aes = Alcotest . check Alcotest . string |
let aeso = Alcotest . check Alcotest . ( option string ) |
let aesl = Alcotest . check Alcotest . ( list string ) |
let aessl = Alcotest . check Alcotest . ( list ( pair string string ) ) |
let aeb = Alcotest . check Alcotest . bool |
let t_header = Alcotest . testable ( fun fmt h -> let sexp = Cohttp . Header . sexp_of_t h in Sexplib0 . Sexp . pp_hum fmt sexp ) ( fun x y -> Cohttp . Header . compare x y = 0 ) |
let aeh = Alcotest . check t_header |
let hstr = [ ( " accept " , " application / xml " ) ; ( " transfer - encoding " , " chunked " ) ; ( " accept " , " text / html " ) ; ( " content - length " , " 100 " ) ; ] |
let prebuilt = H . of_list hstr |
let to_list_rev h = List . rev ( H . to_list h ) |
let to_list_tests ( ) = aessl " to_list ( init ( ) ) " [ ] H . ( to_list ( init ( ) ) ) ; aessl " to_list ( add ( init ( ) ) k v " [ ( " a " , " a1 " ) ] H . ( to_list ( add ( init ( ) ) " a " " a1 " ) ) ; aessl " to_list ( of_list h ) = h " hstr H . ( to_list prebuilt ) |
let is_empty_tests ( ) = aeb " is_empty ( init ( ) ) " true H . ( is_empty ( init ( ) ) ) ; aeb " is_empty ( add ( init ( ) ) k v " false H . ( is_empty ( add ( init ( ) ) " a " " a1 " ) ) ; aeb " is_empty ( remove ( add ( init ( ) ) k v ) k ) " true H . ( is_empty ( remove ( add ( init ( ) ) " a " " a1 " ) " a " ) ) |
let init_with_tests ( ) = aessl " init_with k v " [ ( " traNsfer - eNcoding " , " chunked " ) ] H . ( to_list ( init_with " traNsfer - eNcoding " " chunked " ) ) |
let mem_tests ( ) = aeb " mem ( init ( ) ) k = false " false H . ( mem ( init ( ) ) " a " ) ; aeb " mem h k " true H . ( mem prebuilt " accept " ) ; aeb " mem h k " true H . ( mem prebuilt " content - length " ) ; aeb " mem h k " false H . ( mem prebuilt " a " ) |
let add_tests ( ) = aessl " add h k v " ( hstr @ [ ( " a " , " a1 " ) ] ) H . ( to_list ( add prebuilt " a " " a1 " ) ) ; aessl " add ( add h k v ) k v " ( hstr @ [ ( " a " , " a1 " ) ; ( " a " , " a1 " ) ] ) H . ( to_list ( add ( add prebuilt " a " " a1 " ) " a " " a1 " ) ) ; aessl " add ( add h k ' v ' ) k v " ( hstr @ [ ( " a " , " a1 " ) ; ( " b " , " b1 " ) ] ) H . ( to_list ( add ( add prebuilt " a " " a1 " ) " b " " b1 " ) ) |
let get_tests ( ) = aeso " get ( add ( init ( ) k v ) k " ( Some " a1 " ) H . ( get ( add ( init ( ) ) " a " " a1 " ) " a " ) ; aeso " get ( add h k v ) k when mem h k = false " ( Some " a1 " ) H . ( get ( add prebuilt " a " " a1 " ) " a " ) ; aeso " get ( add h k v ) k when mem h k = true " ( Some " text / html " ) H . ( get ( add prebuilt " a " " a1 " ) " accept " ) ; aeso " get ( add ( add h k v ' ) k v ) k = v " ( Some " a2 " ) H . ( get ( add ( add prebuilt " a " " a1 " ) " a " " a2 " ) " a " ) |
let add_list_tests ( ) = let l = [ ( " a " , " a1 " ) ; ( " b " , " b1 " ) ] in aessl " add_list ( init ( ) ) [ ] " [ ] H . ( to_list ( add_list ( init ( ) ) [ ] ) ) ; aessl " add_list ( init ( ) ) l " l H . ( to_list ( add_list ( init ( ) ) l ) ) ; aessl " add_list h [ ] " hstr H . ( to_list ( add_list prebuilt [ ] ) ) ; aessl " add_list h [ k , v ] " ( hstr @ [ ( " a " , " a1 " ) ] ) H . ( to_list ( add_list prebuilt [ ( " a " , " a1 " ) ] ) ) ; aessl " add_list h l " ( hstr @ l ) H . ( to_list ( add_list prebuilt l ) ) |
let add_multi_tests ( ) = let k , vals = ( " a " , [ " a1 " ; " a2 " ; " a3 " ] ) in let l = List . map ( fun v -> ( " a " , v ) ) vals in aessl " add_multi ( init ( ) ) k [ ] " [ ] H . ( to_list ( add_multi ( init ( ) ) k [ ] ) ) ; aessl " add_multi ( init ( ) ) k vals " l H . ( to_list ( add_multi ( init ( ) ) k vals ) ) ; aessl " add_multi h k [ ] " hstr H . ( to_list ( add_multi prebuilt k [ ] ) ) ; aessl " add_multi h k vals " ( hstr @ l ) H . ( to_list ( add_multi prebuilt k vals ) ) |
let add_unless_exists_tests ( ) = let k , v = ( " a " , " a1 " ) in let k ' , v ' = ( " transfer - encoding " , " chunked " ) in let k ' ' , v ' ' = ( " accept " , " text " ) /* in aessl " add_unless_exists ( init ( ) ) k v " [ ( k , v ) ] H . ( to_list ( add_unless_exists ( init ( ) ) k v ) ) ; aessl " add_unless_exists h k v when mem h k = false " ( hstr @ [ ( k , v ) ] ) H . ( to_list ( add_unless_exists prebuilt k v ) ) ; aessl " add_unless_exists h k v when mem h k = true ) " hstr H . ( to_list ( add_unless_exists prebuilt k ' v ' ) ) ; aessl " add_unless_exists h k v when mem h k = true ) " hstr H . ( to_list ( add_unless_exists prebuilt k ' ' v ' ' ) ) |
let remove_tests ( ) = aessl " remove ( init ( ) ) k " [ ] H . ( to_list ( remove ( init ( ) ) " accept " ) ) ; aessl " remove ( add ( add ( init ( ) ) k v ) k v ) k " [ ] H . ( to_list ( remove ( add ( add ( init ( ) ) " k " " v " ) " k " " v " ) " k " ) ) ; aessl " remove h k when mem h k = false " hstr H . ( to_list ( remove prebuilt " a " ) ) ; aessl " remove h k when mem h k = true " [ ( " accept " , " application / xml " ) ; ( " accept " , " text / html " ) ; ( " content - length " , " 100 " ) ; ] H . ( to_list ( remove prebuilt " transfer - encoding " ) ) ; aessl " remove h k when mem h k = true " [ ( " transfer - encoding " , " chunked " ) ; ( " content - length " , " 100 " ) ] H . ( to_list ( remove prebuilt " accept " ) ) |
let replace_tests ( ) = let k , v , v ' = ( " a " , " a1 " , " a2 " ) in aessl " replace ( init ( ) ) k v " [ ( k , v ) ] H . ( to_list ( replace ( init ( ) ) k v ) ) ; aessl " replace ( add ( init ( ) ) k v ) k v " [ ( k , v ) ] H . ( to_list ( replace ( add ( init ( ) ) k v ) k v ) ) ; aessl " replace ( add ( init ( ) ) k v ) k v ' " [ ( k , v ' ) ] H . ( to_list ( replace ( add ( init ( ) ) k v ) k v ' ) ) ; aessl " replace h k v when mem h k = false " ( hstr @ [ ( k , v ) ] ) H . ( to_list ( replace prebuilt k v ) ) ; aessl " replace h k v when mem h k = true " [ ( " accept " , " application / xml " ) ; ( " transfer - encoding " , " gzip " ) ; ( " accept " , " text / html " ) ; ( " content - length " , " 100 " ) ; ] H . ( to_list ( replace prebuilt " transfer - encoding " " gzip " ) ) ; aessl " replace h k v when mem h = true " [ ( " transfer - encoding " , " chunked " ) ; ( " accept " , " text " ) ; /* ( " content - length " , " 100 " ) ; ] H . ( to_list ( replace prebuilt " accept " " text " ) ) /* |
let h = H . init ( ) |> fun h -> H . add h " first " " 1 " |> fun h -> H . add h " second " " 2 " |> fun h -> H . add h " accept " " foo " |> fun h -> H . add h " accept " " bar " |
let update_tests ( ) = let h1 = H . update h " second " ( function Some _ -> Some " 2a " | None -> None ) in let h2 = H . replace h " second " " 2a " in aeh " update existing header " h1 h2 ; let h1 = H . update h " second " ( function Some _ -> None | None -> Some " 3 " ) in let h2 = H . remove h " second " in aeh " update remove header " h1 h2 ; let h1 = H . update h " accept " ( function Some _ -> Some " baz " | None -> None ) in aesl " update existing header with multiple values " H . ( get_multi h1 " accept " ) [ " foo " ; " baz " ] ; let h ' = H . update h " third " ( function Some _ -> None | None -> Some " 3 " ) in aesl " update add new header " ( H . get_multi h ' " third " ) [ " 3 " ] ; let h1 = H . update h " third " ( function _ -> None ) in aeh " update_remove_absent_header " h h1 ; let h1 = H . update h " third " ( function Some _ -> Some " 3 " | None -> None ) in aeh " update_new_header : unchanged " h h1 ; let h1 = H . update h " accept " ( function Some _ -> None | None -> None ) in aeso " update_existing_header_remove_multivalue : remove last " ( Some " foo " ) ( H . get h1 " accept " ) |
let update_all_tests ( ) = let h1 = H . update_all h " second " ( function [ ] -> [ ] | _ -> [ " 2a " ] ) in let h2 = H . ( add ( remove h " second " ) " second " " 2a " ) in aeh " update_all existing header " h1 h2 ; let h1 = H . update_all h " second " ( function [ ] -> [ " 3 " ] | _ -> [ ] ) in let h2 = H . remove h " second " in aeh " update_all remove header " h1 h2 ; let h1 = H . update_all h " accept " ( function [ ] -> [ ] | _ -> [ " baz " ] ) in aesl " update_all existing header with multiple values " H . ( get_multi h1 " accept " ) [ " baz " ] ; let h1 = H . update_all h " accept " ( function [ ] -> [ ] | xs -> xs @ [ " baz " ] ) in let h2 = H . add h " accept " " baz " in aeso " update_all_existing_header_multivalued " ( H . get_multi_concat h1 " accept " ) ( H . get_multi_concat h2 " accept " ) ; let h1 = H . update_all h " accept " ( function _ -> [ ] ) in aeh " update_all_existing_header_multivalue : remove all " ( H . remove h " accept " ) h1 ; let h1 = H . update_all h " third " ( function [ ] -> [ " 3 " ; " 33 " ] | _ -> [ ] ) in let h2 = H . add_multi h " third " [ " 3 " ; " 33 " ] in aeh " update add new header " h1 h2 ; let h1 = H . update_all h " third " ( function _ -> [ ] ) in aeh " update_remove_absent_header " h h1 ; let h1 = H . update_all h " third " ( function [ ] -> [ ] | _ -> [ " 3 " ] ) in aeh " update_new_header : unchanged " h h1 |
let get_multi_tests ( ) = aesl " get_multi ( init ( ) ) k " [ ] H . ( get_multi ( init ( ) ) " a " ) ; aesl " get_multi h k when mem h k = false " [ ] H . ( get_multi prebuilt " a " ) ; aesl " get_multi h k when mem h k = true " [ " chunked " ] H . ( get_multi prebuilt " transfer - encoding " ) ; aesl " get_multi h k when mem h k = true " [ " application / xml " ; " text / html " ] H . ( get_multi prebuilt " accept " ) |
let hstr = [ ( " accept " , " application / xml " ) ; ( " transfer - encoding " , " chunked " ) ; ( " accept " , " text / html " ) ; ( " content - length " , " 100 " ) ; ] |
let get_multi_concat_tests ( ) = let h1 = H . ( add ( add prebuilt " a " " a1 " ) " a " " a2 " ) in aeso " get_multi_concat ( init ( ) ) k " None H . ( get_multi_concat ( init ( ) ) " a " ) ; aeso " get_multi_concat h k when mem h k = false " None H . ( get_multi_concat prebuilt " a " ) ; aeso " get_multi_concat h k when mem h k = true " ( Some " application / xml , text / html " ) H . ( get_multi_concat prebuilt " accept " ) ; aeso " get_multi_concat ~ list_value_only : false h k when mem h k = true " ( Some " a1 , a2 " ) H . ( get_multi_concat h1 " a " ) ; aeso " get_multi_concat ~ list_value_only : true h k when mem h k = true " ( Some " a2 " ) H . ( get_multi_concat ~ list_value_only : true h1 " a " ) |
let map_tests ( ) = let a = " , a " in aessl " map ( fun _ v -> v ) ( init ( ) ) " [ ] H . ( to_list ( map ( fun _k v -> v ) ( init ( ) ) ) ) ; aessl " map ( fun _ v -> v ) ( init ( ) ) " ( H . to_list prebuilt ) H . ( to_list ( map ( fun _k v -> v ) prebuilt ) ) ; aessl " map ( fun _ v -> v ^ a ) ( init ( ) ) " [ ( " accept " , " application / xml , a " ) ; ( " transfer - encoding " , " chunked , a " ) ; ( " accept " , " text / html , a " ) ; ( " content - length " , " 100 , a " ) ; ] H . ( to_list ( map ( fun _k v -> v ^ a ) prebuilt ) ) |
let fold_tests ( ) = let rev k v acc = H . ( add acc k v ) in let h1 = H . ( fold rev prebuilt ( init ( ) ) ) in aessl " [ fold ( fun k v acc -> H . ( add acc k v ) ) h ( init ( ) ) ] reverses the header " ( List . rev H . ( to_list h1 ) ) H . ( to_list prebuilt ) ; let h1 = H . ( fold rev ( fold rev prebuilt ( init ( ) ) ) ( init ( ) ) ) in aeh " [ fold rev ( fold rev h ( init ( ) ) ) ( init ( ) ) = h ] " h1 prebuilt ; let count _ _ acc = acc + 1 in aei " [ fold ( fun _ _ acc -> acc + 1 ) h 0 ] returns the length of h " ( List . length H . ( to_list prebuilt ) ) H . ( fold count prebuilt 0 ) |
let iter_tests ( ) = let h = ref H . ( init ( ) ) in let rev k v = h := H . ( add ! h k v ) in H . ( iter rev prebuilt ) ; aessl " [ iter ( fun k v -> href := H . ( add ! href k v ) ) h ] reverses the header " ( List . rev H . ( to_list ! h ) ) H . ( to_list prebuilt ) ; let c = ref 0 in let count _ _ = c := ! c + 1 in aei " [ iter ( fun _ _ -> count := ! count + 1 ) h ] works fine " ( List . length H . ( to_list prebuilt ) ) ( H . ( iter count prebuilt ) ; ! c ) |
let to_lines_tests ( ) = aesl " to_lines h " [ " accept : application / xml \ r \ n " ; " transfer - encoding : chunked \ r \ n " ; " accept : text / html \ r \ n " ; " content - length : 100 \ r \ n " ; ] H . ( to_lines prebuilt ) |
let to_frames_tests ( ) = aesl " to_frames h " [ " accept : application / xml " ; " transfer - encoding : chunked " ; " accept : text / html " ; " content - length : 100 " ; ] H . ( to_frames prebuilt ) |
let to_string_tests ( ) = aes " to_string h " " accept : application / xml \ r \ n \ transfer - encoding : chunked \ r \ n \ accept : text / html \ r \ n \ content - length : 100 \ r \ n \ \ r \ n " H . ( to_string prebuilt ) |
let many_headers ( ) = let size = 1000000 in let rec add_header num h = match num with | 0 -> h | n -> let k = Printf . sprintf " h % d " n in let v = Printf . sprintf " v % d " n in let h = H . add h k v in add_header ( num - 1 ) h in let h = add_header size ( H . init ( ) ) in Alcotest . ( check int ) " many_headers " ( List . length ( H . to_list h ) ) size |
let transfer_encoding_tests ( ) = let h = H . of_list [ ( " transfer - encoding " , " gzip " ) ; ( " transfer - encoding " , " chunked " ) ] in let sh = H . to_string h in aes " transfer_encoding_string_is_ordered " sh " transfer - encoding : gzip \ r \ ntransfer - encoding : chunked \ r \ n \ r \ n " ; let sh = H . get_multi_concat h " transfer - encoding " in aeso " transfer_encoding_get_is_ordered " ( Some " gzip , chunked " ) sh |
module HIO = Cohttp__Header_io . Make ( String_io . M ) |
let large_header ( ) = let sz = 1024 * 1024 * 100 in let h = H . init ( ) in let v1 = String . make sz ' a ' in let h = H . add h " x - large " v1 in let h = H . add h v1 " foo " in aeso " x - large " ( H . get h " x - large " ) ( Some v1 ) ; let obuf = Buffer . create ( sz + 1024 ) in HIO . write h obuf ; let ibuf = Buffer . contents obuf in let sbuf = String_io . open_in ibuf in Alcotest . check t_header " large_header " ( HIO . parse sbuf ) h |
let tests = ( " Unitary Header tests " , [ ( " Header . to_list " , ` Quick , to_list_tests ) ; ( " Header . is_empty " , ` Quick , is_empty_tests ) ; ( " Header . init_with " , ` Quick , init_with_tests ) ; ( " Header . mem " , ` Quick , mem_tests ) ; ( " Header . add " , ` Quick , add_tests ) ; ( " Header . get " , ` Quick , get_tests ) ; ( " Header . add_list " , ` Quick , add_list_tests ) ; ( " Header . add_multi " , ` Quick , add_multi_tests ) ; ( " Header . add_unless_exists " , ` Quick , add_unless_exists_tests ) ; ( " Header . remove " , ` Quick , remove_tests ) ; ( " Header . replace " , ` Quick , replace_tests ) ; ( " Header . get_multi " , ` Quick , get_multi_tests ) ; ( " Header . get_multi_concat " , ` Quick , get_multi_concat_tests ) ; ( " Header . to_lines " , ` Quick , to_lines_tests ) ; ( " Header . to_frames " , ` Quick , to_frames_tests ) ; ( " Header . to_string " , ` Quick , to_string_tests ) ; ( " Header . map " , ` Quick , map_tests ) ; ( " Header . fold " , ` Quick , fold_tests ) ; ( " Header . iter " , ` Quick , iter_tests ) ; ( " Header . update " , ` Quick , update_tests ) ; ( " Header . update_all " , ` Quick , update_all_tests ) ; ( " many headers " , ` Slow , many_headers ) ; ( " transfer encoding is in correct order " , ` Quick , transfer_encoding_tests ) ; ] @ if Sys . word_size = 64 then [ ( " large header " , ` Slow , large_header ) ] else [ ] ) |
let showdiff exp_str fnd_str = print_string ( " -- Expected --\ n " ^ exp_str ^ " \ n \ n -- Result --\ n " ^ fnd_str " ^\ n \ n " ) ; if not ( Debug . active " NO - VISUAL - DIFF " ) then match ( Debug . os ( ) ) with | " Darwin " -> ( let ( exp , exp_fd ) = Filename . open_temp_file " expected " " . diff " in let ( fnd , fnd_fd ) = Filename . open_temp_file " found " " . diff " in output_string exp_fd exp_str ; close_out exp_fd ; output_string fnd_fd fnd_str ; close_out fnd_fd ; let _ = Unix . system ( " opendiff " ^ exp " ^ " ^ fnd ) in Unix . unlink exp ; Unix . unlink fnd ) | _ -> ( ) ; ; |
let log_test ( title : string ) ( to_s ' : a -> string ) ( result ' : a ) ( expected ' : a ) : unit = if result = expected then print_endline ( title " ^: Passed " ) else ( print_endline ( title " ^: Failed " ) ; showdiff ( to_s expected ) ( to_s result ) ; exit 1 ) ; ; |
let log_boolean ( title : string ) ( result : bool ) ( expected : bool ) : unit = log_test title ( function true -> " TRUE " | false -> " FALSE " ) result expected ; ; |
let log_list_test ( title : string ) ( to_s ' : a -> string ) ( result ' : a list ) ( expected ' : a list ) : unit = if result = expected then print_endline ( title " ^: Passed " ) else ( let rec diff_lists rlist elist = let recurse rstr estr new_rlist new_elist = let ( new_rstr , new_estr ) = diff_lists new_rlist new_elist in ( rstr " ^\ n " ^ new_rstr , estr " ^\ n " ^ new_estr ) in if List . length rlist > 0 then if List . length elist > 0 then recurse ( to_s ( List . hd rlist ) ) ( to_s ( List . hd elist ) ) ( List . tl rlist ) ( List . tl elist ) else recurse ( to_s ( List . hd rlist ) ) " -- empty line " -- ( List . tl rlist ) [ ] else if List . length elist > 0 then recurse " -- empty line " -- ( to_s ( List . hd elist ) ) [ ] ( List . tl elist ) else ( " " , " " ) in let ( result_diffs , expected_diffs ) = diff_lists result expected in print_endline ( title " ^: Failed " ) ; showdiff ( expected_diffs ) ( result_diffs ) ; exit 1 ) ; ; |
let log_collection_test ( title : string ) ( result : Values . K3Value . t ) ( expected ( : Values . K3Value . t list * Values . K3Value . t ) list ) : unit = let collection_entries = match result with | Values . K3Value . SingleMap ( m ) -> Values . K3ValuationMap . to_list m | Values . K3Value . DoubleMap ( dm ) -> List . flatten ( List . map ( fun ( k1 , m ) -> List . map ( fun ( k2 , v ) -> ( k1 @ k2 , v ) ) ( Values . K3ValuationMap . to_list m ) ) ( Values . K3ValuationMap . to_list dm ) ) | Values . K3Value . TupleList ( tlist ) -> List . map ( fun tuple -> match tuple with | Values . K3Value . Tuple ( telems ) -> if telems = [ ] then failwith " Invalid tuple " else let revelems = List . rev telems in ( List . rev ( List . tl revelems ) , List . hd revelems ) | _ -> failwith " Invalid tuple list " ) tlist | _ -> print_endline ( title " ^: Failed " ) ; showdiff " -- A collection " -- ( Values . K3Value . string_of_value result ) ; exit 1 in let dom = ListAsSet . union ( fst ( List . split expected ) ) ( fst ( List . split collection_entries ) ) in let ( expected_strings , found_strings ) = List . fold_left ( fun ( expected_strings , found_strings ) k -> let k_string = ListExtras . string_of_list Values . K3Value . string_of_value k in if not ( List . mem_assoc k expected ) then ( ( k_string " ^ -> n / a " ) :: expected_strings , ( k_string " ^ -> " ( ^ Values . K3Value . string_of_value ( List . assoc k collection_entries ) ) ) :: found_strings ) else if not ( List . mem_assoc k collection_entries ) then ( ( k_string " ^ -> " ( ^ Values . K3Value . string_of_value ( List . assoc k expected ) ) ) :: expected_strings , ( k_string " ^ -> n / a " ) :: found_strings ) else let expected_val = List . assoc k expected in let found_val = List . assoc k collection_entries in if found_val = expected_val then ( expected_strings , found_strings ) else ( ( k_string " ^ -> " ( ^ Values . K3Value . string_of_value expected_val ) ) :: expected_strings , ( k_string " ^ -> " ( ^ Values . K3Value . string_of_value found_val ) ) :: found_strings ) ) ( [ ] , [ ] ) dom in if ( expected_strings <> [ ] ) then ( let suffix = if List . length expected_strings < List . length dom then " \ n . . . and the remaining values match " else " " in print_endline ( title " ^: Failed " ) ; showdiff ( ( String . concat " \ n " expected_strings ) ^ suffix ) ( ( String . concat " \ n " found_strings ) ^ suffix ) ; exit 1 ) else ( print_endline ( title " ^: Passed " ) ) ; ; |
let parse_sql_expr ( expr : string ) : Sql . expr_t = try Sqlparser . expression Sqllexer . tokenize ( Lexing . from_string expr ) with Parsing . Parse_error -> ( print_endline ( " Error parsing ' " :^ expr " ' " ) ; ^ let _ = Parsing . set_trace true in Sqlparser . expression Sqllexer . tokenize ( Lexing . from_string expr ) ) |
let parse_calc ( ? opt = false ) ( expr : string ) : Calculus . expr_t = try let ret = Calculusparser . calculusExpr Calculuslexer . tokenize ( Lexing . from_string expr ) in if opt then CalculusTransforms . optimize_expr ( Calculus . schema_of_expr ret ) ret else ret with Parsing . Parse_error -> ( print_endline ( " Error parsing ' " :^ expr " ' " ) ; ^ let _ = Parsing . set_trace true in Calculusparser . calculusExpr Calculuslexer . tokenize ( Lexing . from_string expr ) ) |
let parse_stmt ( m3_stmt : string ) = try Calculusparser . mapTriggerStmt Calculuslexer . tokenize ( Lexing . from_string m3_stmt ) with Parsing . Parse_error -> ( print_endline ( " Error parsing ' " :^ m3_stmt " ' " ) ; ^ let _ = Parsing . set_trace true in Calculusparser . mapTriggerStmt Calculuslexer . tokenize ( Lexing . from_string m3_stmt ) ) |
let k3_map ( decl : string ) : K3 . map_t = try K3parser . mapDeclaration K3lexer . tokenize ( Lexing . from_string ( decl " ; " ) ) ^ with Parsing . Parse_error -> ( print_endline ( " Error parsing ' " :^ decl " ; ' " ) ; ^ let _ = Parsing . set_trace true in K3parser . mapDeclaration K3lexer . tokenize ( Lexing . from_string ( decl " ; " ) ) ^ ) |
let parse_k3 ( expr : string ) : K3 . expr_t = try K3parser . statement K3lexer . tokenize ( Lexing . from_string expr ) with Parsing . Parse_error -> ( print_endline ( " Error parsing ' " :^ expr " ' " ) ; ^ let _ = Parsing . set_trace true in K3parser . statement K3lexer . tokenize ( Lexing . from_string expr ) ) |
let var v = ( v , Type . TFloat ) ; ; |
let rel rn rv = ( Calculus . Rel ( rn , List . map var rv ) ) ; ; |
let schema_rel ( reln : string ) ( relv : string list ) = ( reln , List . map var relv , Schema . StreamRel ) ; ; |
let event ins reln relv = ( ( if ins then Schema . InsertEvent ( schema_rel reln relv ) else Schema . DeleteEvent ( schema_rel reln relv ) ) ) ; ; |
let mk_db rels = ( let ret_db = Schema . empty_db ( ) in List . iter ( fun ( reln , relv ) -> Schema . add_rel ret_db ( schema_rel reln relv ) ) rels ; ret_db ) ; ; |
let mk_float_collection elems = List . map ( fun ( k , v ) -> ( List . map ( fun x -> Values . K3Value . BaseValue ( CFloat ( x ) ) ) k , Values . K3Value . BaseValue ( CFloat ( v ) ) ) ) elems |
let compile ( db : Schema . t ) ( name : string ) ( expr : string ) = Compiler . compile db [ ( name , parse_calc expr ) ] ; ; |
module Make ( M : Ndarray_Algodiff with type elt = float ) = struct module AlgoM = Owl_algodiff_generic . Make ( M ) open AlgoM let eps = 1e - 14 let approx_equal a b = Stdlib . abs_float ( a . - b ) < eps let approx_equal_arr ( ? eps = eps ) a b = let r = ref true in M . ( sub a b |> abs ) |> M . map ( fun c -> if c >= eps then r := false ; c ) |> ignore ; ! r module To_test = struct let dumb ( ) = true let sin1 x = x |> diff Maths . sin |> unpack_flt let sin2 x = x |> diff ( diff Maths . sin ) |> unpack_flt let sin3 x = x |> diff ( diff ( diff Maths . sin ) ) |> unpack_flt let pfuna x = Maths . ( ( F 2 . * ( x * x * x ) ) + ( F 3 . * ( x * x ) ) - ( F 5 . * x ) + F 1 . ) let poly1 x = x |> diff pfuna |> unpack_flt let poly2 x = x |> diff ( diff pfuna ) |> unpack_flt let poly3 x = x |> diff ( diff ( diff pfuna ) ) |> unpack_flt let pfunb x = Maths . ( ( F 2 . * x * x ) + ( F 3 . * sin x ) - ( F 1 . / x ) ) let poly4 x = x |> diff pfunb |> unpack_flt let poly5 x = x |> diff ( diff pfunb ) |> unpack_flt let poly6 x = x |> diff ( diff ( diff pfunb ) ) |> unpack_flt let pfunc x = Maths . ( ( F 2 . * sqr x ) + sqrt x - tanh x ) let poly7 x = x |> diff pfunc |> unpack_flt let poly8 x = x |> diff ( diff pfunc ) |> unpack_flt let poly9 x = x |> diff ( diff ( diff pfunc ) ) |> unpack_flt |> approx_equal 0 . 00636573616030225702 let derivative_delta f df x = ( diff f ( F x ) |> unpack_flt ) . - df x let check_derivative ( ? eps = 0 . ) f df x = let delta = derivative_delta f df x in abs_float delta < eps let check_derivative_array ( ? eps = 0 . ) f df x_arr = M . print x_arr ; let df_arr = M . map ( fun x -> df x ) x_arr in M . print df_arr ; let diff_arr = M . map ( fun x -> diff f ( F x ) |> unpack_flt ) x_arr in M . print diff_arr ; approx_equal_arr ~ eps df_arr diff_arr end let check_derivative ( ? eps = 1e - 10 ) text f df xs = let error_msg x = Printf . sprintf " % s : x =% f : error =% f " text x ( To_test . derivative_delta f df x ) in let test_x x = Alcotest . ( check bool ) ( error_msg x ) true ( To_test . check_derivative ~ eps f df x ) in Array . iter test_x xs ; let x_arr = M . init [ | Array . length xs ] | ( fun i -> xs . ( i ) ) in let array_passed = To_test . ( check_derivative_array ~ eps f df x_arr ) in Alcotest . ( check bool ) ( Printf . sprintf " Array test for % s " text ) true array_passed let xs = [ | - 4 . ; - 3 . ; - 2 . ; - 1 . 2 ; - 1 . 1 ; - 1 . ; - 0 . 9 ; - 0 . 8 ; - 0 . 7 ; - 0 . 6 ; - 0 . 5 ; - 0 . 4 ; - 0 . 3 ; - 0 . 2 ; - 0 . 1 ; 0 . ; 0 . 1 ; 0 . 2 ; 0 . 3 ; 0 . 4 ; 0 . 5 ; 0 . 6 ; 0 . 7 ; 0 . 8 ; 0 . 9 ; 1 . ; 1 . 1 ; 1 . 2 ; 2 . ; 3 . ; 4 . ] | let xs_filter f = let filter acc x = if f x then x :: acc else acc in let filtered_xs_list = Array . fold_left filter [ ] xs in Array . of_list filtered_xs_list let x_ne_y x y = abs_float ( x . - y ) > 0 . 00000001 let xs_gt_one = xs_filter ( fun x -> x > 1 . 0 ) let xs_abs_lt_one = xs_filter ( fun x -> x > - 1 . && x < 1 . 0 ) let xs_nonones = xs_filter ( fun x -> x_ne_y x 1 . 0 && x_ne_y x ( - 1 . ) ) let xs_nonzero = xs_filter ( fun x -> x_ne_y x 0 . 0 ) let xs_nonzero_nonone = xs_filter ( fun x -> x_ne_y x 0 . 0 && x_ne_y x 1 . 0 ) let xs_positive = xs_filter ( fun x -> x >= 0 . 0 ) let xs_positive_nonzero = xs_filter ( fun x -> x > 0 . 0 ) let constant ( ) = check_derivative " f ( x ) = 1 " ( fun _ -> F 1 . ) ( fun _ -> 0 . ) xs let linear ( ) = check_derivative " f ( x ) = x " ( fun x -> x ) ( fun _ -> 1 . ) xs let square ( ) = check_derivative " f ( x ) = x ^ 2 " ( fun x -> Maths . ( x * x ) ) ( fun x -> 2 . . * x ) xs let cube ( ) = check_derivative " f ( x ) = x ^ 3 " ( fun x -> Maths . ( x * x * x ) ) ( fun x -> 3 . . * x . * x ) xs let sum_x_x ( ) = check_derivative " f ( x ) = x + x " ( fun x -> Maths . ( x + x ) ) ( fun _ -> 2 . ) xs let diff_2x_x ( ) = check_derivative " f ( x ) = 2x - x " ( fun x -> Maths . ( ( F 2 . * x ) - x ) ) ( fun _ -> 1 . ) xs let div_x_x ( ) = check_derivative " f ( x ) = x / x " ( fun x -> Maths . ( x / x ) ) ( fun _ -> 0 . ) xs_nonzero let div_x2_x ( ) = check_derivative " f ( x ) = x ^ 2 / x " ( fun x -> Maths . ( x * x / x ) ) ( fun _ -> 1 . ) xs_nonzero let pow_x_2_5 ( ) = check_derivative " f ( x ) = x ( ^ 2 . 5 ) " ( fun x -> Maths . ( x ** F 2 . 5 ) ) ( fun x -> 2 . 5 . * ( x ** 1 . 5 ) ) xs_positive let pow_2_5_x ( ) = check_derivative " f ( x ) = 2 . 5 ^ x " ( fun x -> Maths . ( F 2 . 5 ** x ) ) ( fun x -> log 2 . 5 . * ( 2 . 5 ** x ) ) xs let min_x_x2 ( ) = check_derivative " f ( x ) = min ( x , x ^ 2 ) " ( fun x -> Maths . ( min2 x ( x * x ) ) ) ( fun x -> if x < 0 . 0 || x > 1 . 0 then 1 . else 2 . . * x ) xs_nonzero_nonone let max_x_x2 ( ) = check_derivative " f ( x ) = max ( x , x ^ 2 ) " ( fun x -> Maths . ( max2 x ( x * x ) ) ) ( fun x -> if x < 0 . 0 || x > 1 . 0 then 2 . . * x else 1 . ) xs_nonzero_nonone let neg_x ( ) = check_derivative " f ( x ) = - x " ( fun x -> Maths . ( neg x ) ) ( fun _ -> - 1 . ) xs let abs_x2 ( ) = check_derivative " f ( x ) = abs ( x ^ 2 ) " ( fun x -> Maths . ( abs ( x * x ) ) ) ( fun x -> 2 . . * x ) xs let abs_x2_m_1 ( ) = check_derivative " f ( x ) = abs ( x ^ 2 - 1 ) " ( fun x -> Maths . ( abs ( ( x * x ) - F 1 . ) ) ) ( fun x -> if x < - 1 . || x > 1 . then 2 . . * x else - 2 . . * x ) xs_nonones let sign_x2_m_1 ( ) = check_derivative " f ( x ) = signum ( x ^ 2 - 1 ) " ( fun x -> Maths . ( signum ( ( x * x ) - F 1 . ) ) ) ( fun _ -> 0 . ) xs let floor_x2_m_1 ( ) = check_derivative " f ( x ) = floor ( x ^ 2 - 1 ) " ( fun x -> Maths . ( floor ( ( x * x ) - F 1 . ) ) ) ( fun _ -> 0 . ) xs let ceil_x2_m_1 ( ) = check_derivative " f ( x ) = ceil ( x ^ 2 - 1 ) " ( fun x -> Maths . ( ceil ( ( x * x ) - F 1 . ) ) ) ( fun _ -> 0 . ) xs let round_x2_m_1 ( ) = check_derivative " f ( x ) = round ( x ^ 2 - 1 ) " ( fun x -> Maths . ( round ( ( x * x ) - F 1 . ) ) ) ( fun _ -> 0 . ) xs let sqr_x ( ) = check_derivative " f ( x ) = sqr ( x ) " ( fun x -> Maths . ( sqr x ) ) ( fun x -> 2 . . * x ) xs let sqr_x_x ( ) = check_derivative " f ( x ) = sqr ( x * x ) " ( fun x -> Maths . ( sqr ( x * x ) ) ) ( fun x -> 4 . . * x . * x . * x ) xs let sqrt_x ( ) = check_derivative " f ( x ) = sqrt ( x ) " ( fun x -> Maths . ( sqrt x ) ) ( fun x -> 0 . 5 . / ( x ** 0 . 5 ) ) xs_positive_nonzero let log_x ( ) = check_derivative " f ( x ) = log ( x ) " ( fun x -> Maths . ( log x ) ) ( fun x -> 1 . . / x ) xs_positive_nonzero let log_x_x ( ) = check_derivative " f ( x ) = log ( x * x ) " ( fun x -> Maths . ( log ( x * x ) ) ) ( fun x -> 2 . . / x ) xs_positive_nonzero let log2_x ( ) = check_derivative " f ( x ) = log2 ( x ) " ( fun x -> Maths . ( log2 x ) ) ( fun x -> log 2 . . / x ) xs_positive_nonzero let log10_x ( ) = check_derivative " f ( x ) = log10 ( x ) " ( fun x -> Maths . ( log10 x ) ) ( fun x -> log 10 . . / x ) xs_positive_nonzero let exp_x ( ) = check_derivative " f ( x ) = exp ( x ) " ( fun x -> Maths . ( exp x ) ) ( fun x -> exp x ) xs let sin_x ( ) = check_derivative " f ( x ) = sin ( x ) " ( fun x -> Maths . ( sin x ) ) ( fun x -> cos x ) xs let sin_x_x ( ) = check_derivative " f ( x ) = sin ( x * x ) " ( fun x -> Maths . ( sin ( x * x ) ) ) ( fun x -> 2 . . * x . * cos ( x . * x ) ) xs let cos_x ( ) = check_derivative " f ( x ) = cos ( x ) " ( fun x -> Maths . ( cos x ) ) ( fun x -> sin ( . - x ) ) xs let tan_x ( ) = check_derivative " f ( x ) = tan ( x ) " ( fun x -> Maths . ( tan x ) ) ( fun x -> 1 . . / ( cos x ** 2 . ) ) xs let asin_x ( ) = check_derivative " f ( x ) = asin ( x ) " ( fun x -> Maths . ( asin x ) ) ( fun x -> 1 . . / sqrt ( 1 . . - ( x ** 2 . ) ) ) xs_abs_lt_one let acos_x ( ) = check_derivative " f ( x ) = acos ( x ) " ( fun x -> Maths . ( acos x ) ) ( fun x -> - 1 . . / sqrt ( 1 . . - ( x ** 2 . ) ) ) xs_abs_lt_one let atan_x ( ) = check_derivative " f ( x ) = atan ( x ) " ( fun x -> Maths . ( atan x ) ) ( fun x -> 1 . . / ( 1 . . + ( x ** 2 . ) ) ) xs let atan2_x2_x ( ) = check_derivative " f ( x ) = atan2 ( x * x , x ) " ( fun x -> Maths . ( atan2 ( x * x ) x ) ) ( fun x -> 1 . . / ( 1 . . + ( x ** 2 . ) ) ) xs_nonzero let atan2_x_x2 ( ) = check_derivative " f ( x ) = atan2 ( x , x * x ) " ( fun x -> Maths . ( atan2 x ( x * x ) ) ) ( fun x -> - 1 . . / ( 1 . . + ( x ** 2 . ) ) ) xs_nonzero let sinh_x ( ) = check_derivative " f ( x ) = sinh ( x ) " ( fun x -> Maths . ( sinh x ) ) ( fun x -> cosh x ) xs let sinh_x_x ( ) = check_derivative " f ( x ) = sinh ( x * x ) " ( fun x -> Maths . ( sinh ( x * x ) ) ) ( fun x -> 2 . . * x . * cosh ( x . * x ) ) xs let cosh_x ( ) = check_derivative " f ( x ) = cosh ( x ) " ( fun x -> Maths . ( cosh x ) ) ( fun x -> sinh x ) xs let tanh_x ( ) = check_derivative " f ( x ) = tanh ( x ) " ( fun x -> Maths . ( tanh x ) ) ( fun x -> 1 . . - ( tanh x ** 2 . ) ) xs let asinh_x ( ) = check_derivative " f ( x ) = asinh ( x ) " ( fun x -> Maths . ( asinh x ) ) ( fun x -> 1 . . / sqrt ( 1 . . + ( x ** 2 . ) ) ) xs let acosh_x ( ) = check_derivative " f ( x ) = acosh ( x ) " ( fun x -> Maths . ( acosh x ) ) ( fun x -> 1 . . / sqrt ( - 1 . . + ( x ** 2 . ) ) ) xs_gt_one let atanh_x ( ) = check_derivative " f ( x ) = atanh ( x ) " ( fun x -> Maths . ( atanh x ) ) ( fun x -> 1 . . / ( 1 . . - ( x ** 2 . ) ) ) xs_abs_lt_one let dumb ( ) = Alcotest . ( check bool ) " dumb " true ( To_test . dumb ( ) ) let sin1 ( ) = Alcotest . ( check ( float eps ) ) " sin1 " ( cos 1 . ) ( To_test . sin1 ( F 1 . ) ) let sin2 ( ) = Alcotest . ( check ( float eps ) ) " sin2 " ( . - sin 1 . ) ( To_test . sin2 ( F 1 . ) ) let sin3 ( ) = Alcotest . ( check ( float eps ) ) " sin3 " ( . - cos 1 . ) ( To_test . sin3 ( F 1 . ) ) let poly1 ( ) = Alcotest . ( check ( float eps ) ) " poly1 " 31 . ( To_test . poly1 ( F 2 . ) ) let poly2 ( ) = Alcotest . ( check ( float eps ) ) " poly2 " 30 . ( To_test . poly2 ( F 2 . ) ) let poly3 ( ) = Alcotest . ( check ( float eps ) ) " poly3 " 12 . ( To_test . poly3 ( F 2 . ) ) let poly4 ( ) = Alcotest . ( check ( float eps ) ) " poly4 " ( 12 . . + ( 3 . . * cos 3 . ) . + ( 1 . . / 9 . ) ) ( To_test . poly4 ( F 3 . ) ) let poly5 ( ) = Alcotest . ( check ( float eps ) ) " poly5 " ( 4 . . - ( 3 . . * sin 3 . ) . - ( 2 . . / 27 . ) ) ( To_test . poly5 ( F 3 . ) ) let poly6 ( ) = Alcotest . ( check ( float eps ) ) " poly6 " ( ( - 3 . . * cos 3 . ) . + ( 6 . . / 81 . ) ) ( To_test . poly6 ( F 3 . ) ) let poly7 ( ) = Alcotest . ( check ( float eps ) ) " poly7 " ( 16 . . + 0 . 25 . - ( Owl . Maths . sech 4 . ** 2 . ) ) ( To_test . poly7 ( F 4 . ) ) let poly8 ( ) = Alcotest . ( check ( float eps ) ) " poly8 " ( ( - 0 . 25 . / ( 4 . ** 1 . 5 ) ) . + ( 2 . . * Owl . Maths . tanh 4 . . * ( Owl . Maths . sech 4 . ** 2 . ) ) . + 4 . ) ( To_test . poly8 ( F 4 . ) ) let poly9 ( ) = Alcotest . ( check bool ) " poly9 " true ( To_test . poly9 ( F 4 . ) ) let test_set = [ " constant " , ` Slow , constant ; " linear " , ` Slow , linear ; " square " , ` Slow , square ; " cube " , ` Slow , cube ; " sum_x_x " , ` Slow , sum_x_x ; " diff_2x_x " , ` Slow , diff_2x_x ; " div_x_x " , ` Slow , div_x_x ; " div_x2_x " , ` Slow , div_x2_x ; " pow_2_5_x " , ` Slow , pow_2_5_x ; " pow_x_2_5 " , ` Slow , pow_x_2_5 ; " min_x_x2 " , ` Slow , min_x_x2 ; " max_x_x2 " , ` Slow , max_x_x2 ; " neg_x " , ` Slow , neg_x ; " abs_x2 " , ` Slow , abs_x2 ; " abs_x2_m_1 " , ` Slow , abs_x2_m_1 ; " sign_x2_m_1 " , ` Slow , sign_x2_m_1 ; " floor_x2_m_1 " , ` Slow , floor_x2_m_1 ; " ceil_x2_m_1 " , ` Slow , ceil_x2_m_1 ; " round_x2_m_1 " , ` Slow , round_x2_m_1 ; " sqr_x " , ` Slow , sqr_x ; " sqr_x_x " , ` Slow , sqr_x_x ; " sqrt_x " , ` Slow , sqrt_x ; " log_x " , ` Slow , log_x ; " log_x_x " , ` Slow , log_x_x ; " log2_x " , ` Slow , log2_x ; " log10_x " , ` Slow , log10_x ; " exp_x " , ` Slow , exp_x ; " sin_x " , ` Slow , sin_x ; " sin_x_x " , ` Slow , sin_x_x ; " cos_x " , ` Slow , cos_x ; " tan_x " , ` Slow , tan_x ; " asin_x " , ` Slow , asin_x ; " acos_x " , ` Slow , acos_x ; " atan_x " , ` Slow , atan_x ; " atan2_x2_x " , ` Slow , atan2_x2_x ; " atan2_x_x2 " , ` Slow , atan2_x_x2 ; " sinh_x " , ` Slow , sinh_x ; " sinh_x_x " , ` Slow , sinh_x_x ; " cosh_x " , ` Slow , cosh_x ; " tanh_x " , ` Slow , tanh_x ; " asinh_x " , ` Slow , asinh_x ; " acosh_x " , ` Slow , acosh_x ; " atanh_x " , ` Slow , atanh_x ; " dumb " , ` Slow , dumb ; " sin1 " , ` Slow , sin1 ; " sin2 " , ` Slow , sin2 ; " sin3 " , ` Slow , sin3 ; " poly1 " , ` Slow , poly1 ; " poly2 " , ` Slow , poly2 ; " poly3 " , ` Slow , poly3 ; " poly4 " , ` Slow , poly4 ; " poly5 " , ` Slow , poly5 ; " poly6 " , ` Slow , poly6 ; " poly7 " , ` Slow , poly7 ; " poly8 " , ` Slow , poly8 ; " poly9 " , ` Slow , poly9 ] end |
let approx_equal ( ? eps = 1e - 12 ) a b = Stdlib . abs_float ( a . - b ) < eps |
module Make ( M : Ndarray_Algodiff with type elt = float ) = struct module AlgoM = Owl_algodiff_generic . Make ( M ) open AlgoM module To_test = struct let check_grad ( ? eps = 1e - 6 ) f dfs xys = let dim = Array . length xys in let xys_arr = M . init [ | 1 ; dim ] | ( fun i -> xys . ( i ) ) in let grad_f = grad f ( Arr xys_arr ) |> unpack_arr in let dfds = Array . map ( fun df -> df xys ) dfs in let grad_f = Array . init dim ( fun i -> M . get grad_f [ | 0 ; i ] ) | in let rec pass_fail acc i = let check_coord acc x y = acc && approx_equal ~ eps x y in if i == dim then acc else ( let new_acc = check_coord acc dfds . ( i ) grad_f . ( i ) in pass_fail new_acc ( i + 1 ) ) in pass_fail true 0 , grad_f , dfds end let coords = [ 0 . ; 0 . 1 ; 0 . 2 ; 0 . 3 ; 0 . 8 ; 0 . 9 ; 1 . 0 ; 1 . 1 ; 2 . 0 ; 3 . 0 ; 4 . 0 ] let vs_list = let vs_of_x acc x = List . fold_left ( fun acc y -> [ | x ; - 1 . . * y ] | :: [ | x ; y ] | :: acc ) acc coords in List . fold_left ( fun acc x -> vs_of_x acc x ) [ ] coords let vs = Array . of_list vs_list let x_ne_y x y = abs_float ( x . - y ) > 0 . 00000001 let vs_filter f vs = let filter acc v = if f v then v :: acc else acc in let filtered_vs_list vs = Array . fold_left filter [ ] vs in Array . of_list ( filtered_vs_list vs ) let vs_x_nonzero = vs_filter ( fun v -> x_ne_y v . ( 0 ) 0 . ) vs let vs_x_pos = vs_filter ( fun v -> v . ( 0 ) > 0 . 0000001 ) vs let vs_x_abs_lt_one = vs_filter ( fun v -> v . ( 0 ) > - 1 . && v . ( 0 ) < 1 . 0 ) vs let vs_y_abs_lt_one = vs_filter ( fun v -> v . ( 1 ) > - 1 . && v . ( 1 ) < 1 . 0 ) vs let vs_y_gt_one = vs_filter ( fun v -> v . ( 1 ) > 1 . ) vs let vs_prod_positive = vs_filter ( fun v -> v . ( 1 ) . * v . ( 0 ) > 0 . 00001 ) vs let vs_diff = vs_filter ( fun v -> x_ne_y v . ( 1 ) v . ( 0 ) ) vs let vs_sq_diff = vs_filter ( fun v -> x_ne_y ( v . ( 1 ) ** 2 . ) ( v . ( 0 ) ** 2 . ) ) vs let check_grad test f dfs vs = let test_v v = let r , test_result , good_result = To_test . check_grad f dfs v in let strvec v = let strx acc x = if String . length acc == 0 then Printf . sprintf " % f " x else Printf . sprintf " % s , % f " acc x in Array . fold_left strx " " v in let error_msg v = Printf . sprintf " Testing % s : % s : % s : % s " test ( strvec v ) ( strvec test_result ) ( strvec good_result ) in Alcotest . ( check bool ) ( error_msg v ) true r in Array . iter test_v vs let make_grad_test_set set_name test_assoc = let mk_test named_test = let name , test = named_test in let test_msg , test_f , test_dfs , test_vs = test in let test_fn ( ) = check_grad test_msg test_f test_dfs test_vs in Printf . sprintf " % s . % s " set_name name , ` Slow , test_fn in List . map mk_test test_assoc let grad_tests_poly = [ " one " , ( " 1 " , ( fun _ -> F 1 . ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> 0 . ) ] , | vs ) ; " x " , ( " x " , ( fun v -> Mat . get v 0 0 ) , [ | ( fun _ -> 1 . ) ; ( fun _ -> 0 . ) ] , | vs ) ; ( " x_p_y " , ( " x + y " , ( fun v -> Maths . ( Mat . get v 0 0 + Mat . get v 0 1 ) ) , [ | ( fun _ -> 1 . ) ; ( fun _ -> 1 . ) ] | , vs ) ) ; ( " x_m_y " , ( " x - y " , ( fun v -> Maths . ( Mat . get v 0 0 - Mat . get v 0 1 ) ) , [ | ( fun _ -> 1 . ) ; ( fun _ -> - 1 . ) ] | , vs ) ) ; ( " x_x " , ( " x * x " , ( fun v -> Maths . ( Mat . get v 0 0 * Mat . get v 0 0 ) ) , [ | ( fun v -> 2 . . * v . ( 0 ) ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " x_div_x " , ( " x / x " , ( fun v -> Maths . ( Mat . get v 0 0 / Mat . get v 0 0 ) ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> 0 . ) ] | , vs_x_nonzero ) ) ; ( " y_div_x " , ( " y / x " , ( fun v -> Maths . ( Mat . get v 0 1 / Mat . get v 0 0 ) ) , [ | ( fun v -> - 1 . . / ( v . ( 0 ) ** 2 . ) . * v . ( 1 ) ) ; ( fun v -> 1 . . / v . ( 0 ) ) ] | , vs_x_nonzero ) ) ; ( " xsq_p_y " , ( " x ^ 2 + y " , ( fun v -> Maths . ( ( Mat . get v 0 0 ** F 2 . ) + Mat . get v 0 1 ) ) , [ | ( fun v -> 2 . . * v . ( 0 ) ) ; ( fun _ -> 1 . ) ] | , vs ) ) ; ( " xy " , ( " xy " , ( fun v -> Maths . ( Mat . get v 0 0 * Mat . get v 0 1 ) ) , [ | ( fun v -> v . ( 1 ) ) ; ( fun v -> v . ( 0 ) ) ] | , vs ) ) ; ( " xsq_p_ysq " , ( " x ^ 2 + y ^ 2 " , ( fun v -> Maths . ( ( Mat . get v 0 0 ** F 2 . ) + ( Mat . get v 0 1 ** F 2 . ) ) ) , [ | ( fun v -> 2 . . * v . ( 0 ) ) ; ( fun v -> 2 . . * v . ( 1 ) ) ] | , vs ) ) ; ( " x_p_y_allsq " , ( " ( x + y ) ^ 2 = x ^ 2 + y ^ 2 + 2xy " , ( fun v -> Maths . ( ( Mat . get v 0 0 + Mat . get v 0 1 ) ** F 2 . ) ) , [ | ( fun v -> 2 . . * ( v . ( 0 ) . + v . ( 1 ) ) ) ; ( fun v -> 2 . . * ( v . ( 0 ) . + v . ( 1 ) ) ) ] | , vs ) ) ; ( " xy_sq " , ( " xy ^ 2 " , ( fun v -> Maths . ( ( Mat . get v 0 0 * Mat . get v 0 1 ) ** F 2 . ) ) , [ | ( fun v -> 2 . . * ( v . ( 0 ) . * ( v . ( 1 ) ** 2 . ) ) ) ; ( fun v -> 2 . . * ( ( v . ( 0 ) ** 2 . ) . * v . ( 1 ) ) ) ] | , vs ) ) ; ( " xy_cube " , ( " xy ^ 3 " , ( fun v -> Maths . ( Mat . get v 0 0 * Mat . get v 0 1 * Mat . get v 0 0 * Mat . get v 0 1 * Mat . get v 0 0 * Mat . get v 0 1 ) ) , [ | ( fun v -> 3 . . * ( ( v . ( 0 ) ** 2 . ) . * ( v . ( 1 ) ** 3 . ) ) ) ; ( fun v -> 3 . . * ( ( v . ( 0 ) ** 3 . ) . * ( v . ( 1 ) ** 2 . ) ) ) ] | , vs ) ) ; ( " x_pow_y " , ( " x ^ y " , ( fun v -> Maths . ( Mat . get v 0 0 ** Mat . get v 0 1 ) ) , [ | ( fun v -> v . ( 1 ) . * ( v . ( 0 ) ** ( v . ( 1 ) . - 1 . ) ) ) ; ( fun v -> log v . ( 0 ) . * ( v . ( 0 ) ** v . ( 1 ) ) ) ] | , vs_x_pos ) ) ] let grad_tests_other = [ ( " min_x_y " , ( " min ( x , y ) " , ( fun v -> Maths . ( min2 ( Mat . get v 0 0 ) ( Mat . get v 0 1 ) ) ) , [ | ( fun v -> if v . ( 0 ) < v . ( 1 ) then 1 . else 0 . ) ; ( fun v -> if v . ( 1 ) < v . ( 0 ) then 1 . else 0 . ) ] | , vs_diff ) ) ; ( " max_x_y " , ( " max ( x , y ) " , ( fun v -> Maths . ( max2 ( Mat . get v 0 0 ) ( Mat . get v 0 1 ) ) ) , [ | ( fun v -> if v . ( 0 ) > v . ( 1 ) then 1 . else 0 . ) ; ( fun v -> if v . ( 1 ) > v . ( 0 ) then 1 . else 0 . ) ] | , vs_diff ) ) ; ( " neg_y " , ( " - y ) " , ( fun v -> Maths . ( neg ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> - 1 . ) ] | , vs ) ) ; ( " abs_y2 " , ( " abs ( y ^ 2 ) " , ( fun v -> Maths . ( abs ( Mat . get v 0 1 ** F 2 . ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> 2 . . * v . ( 1 ) ) ] | , vs ) ) ; ( " abs_x2_m_y2 " , ( " abs ( x ^ 2 - y ^ 2 ) " , ( fun v -> Maths . ( abs ( ( Mat . get v 0 0 ** F 2 . ) - ( Mat . get v 0 1 ** F 2 . ) ) ) ) , [ | ( fun v -> ( if v . ( 0 ) ** 2 . < v . ( 1 ) ** 2 . then - 1 . else 1 . ) . * 2 . . * v . ( 0 ) ) ; ( fun v -> ( if v . ( 0 ) ** 2 . < v . ( 1 ) ** 2 . then - 1 . else 1 . ) . * - 2 . . * v . ( 1 ) ) ] | , vs_sq_diff ) ) ; ( " sign_x2_m_y2 " , ( " sign ( x ^ 2 - y ^ 2 ) " , ( fun v -> Maths . ( signum ( ( Mat . get v 0 0 ** F 2 . ) - ( Mat . get v 0 1 ** F 2 . ) ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " floor_x2_m_y2 " , ( " floor ( x ^ 2 - y ^ 2 ) " , ( fun v -> Maths . ( floor ( ( Mat . get v 0 0 ** F 2 . ) - ( Mat . get v 0 1 ** F 2 . ) ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " ceil_x2_m_y2 " , ( " ceil ( x ^ 2 - y ^ 2 ) " , ( fun v -> Maths . ( ceil ( ( Mat . get v 0 0 ** F 2 . ) - ( Mat . get v 0 1 ** F 2 . ) ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " round_x2_m_y2 " , ( " round ( x ^ 2 - y ^ 2 ) " , ( fun v -> Maths . ( round ( ( Mat . get v 0 0 ** F 2 . ) - ( Mat . get v 0 1 ** F 2 . ) ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun _ -> 0 . ) ] | , vs ) ) ] let grad_tests_logexp = [ ( " sqr_x_m_sqr_y " , ( " sqr ( x ) - sqr ( y ) " , ( fun v -> Maths . ( sqr ( Mat . get v 0 0 ) - sqr ( Mat . get v 0 1 ) ) ) , [ | ( fun v -> 2 . . * v . ( 0 ) ) ; ( fun v -> - 2 . . * v . ( 1 ) ) ] | , vs ) ) ; ( " sqrt_xy " , ( " sqrt ( xy ) " , ( fun v -> Maths . ( sqrt ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> 0 . 5 . * v . ( 1 ) . / sqrt ( v . ( 0 ) . * v . ( 1 ) ) ) ; ( fun v -> 0 . 5 . * v . ( 0 ) . / sqrt ( v . ( 0 ) . * v . ( 1 ) ) ) ] | , vs_prod_positive ) ) ; ( " log_xy " , ( " log ( xy ) " , ( fun v -> Maths . ( log ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> 1 . . / v . ( 0 ) ) ; ( fun v -> 1 . . / v . ( 1 ) ) ] | , vs_prod_positive ) ) ; ( " log2_xy " , ( " log2 ( xy ) " , ( fun v -> Maths . ( log2 ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> log 2 . . / v . ( 0 ) ) ; ( fun v -> log 2 . . / v . ( 1 ) ) ] | , vs_prod_positive ) ) ; ( " log10_xy " , ( " log10 ( xy ) " , ( fun v -> Maths . ( log10 ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> log 10 . . / v . ( 0 ) ) ; ( fun v -> log 10 . . / v . ( 1 ) ) ] | , vs_prod_positive ) ) ; ( " exp_xy " , ( " exp ( xy ) " , ( fun v -> Maths . ( exp ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> v . ( 1 ) . * exp ( v . ( 1 ) . * v . ( 0 ) ) ) ; ( fun v -> v . ( 0 ) . * exp ( v . ( 1 ) . * v . ( 0 ) ) ) ] | , vs ) ) ] let grad_tests_trig = [ ( " sin_x " , ( " sin ( x ) " , ( fun v -> Maths . ( sin ( Mat . get v 0 0 ) ) ) , [ | ( fun v -> cos v . ( 0 ) ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " cos_y " , ( " cos ( y ) " , ( fun v -> Maths . ( cos ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> - 1 . . * sin v . ( 1 ) ) ] | , vs ) ) ; ( " sin_xy " , ( " sin ( xy ) " , ( fun v -> Maths . ( sin ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> v . ( 1 ) . * cos ( v . ( 0 ) . * v . ( 1 ) ) ) ; ( fun v -> v . ( 0 ) . * cos ( v . ( 0 ) . * v . ( 1 ) ) ) ] | , vs ) ) ; ( " cos_xy " , ( " cos ( xy ) " , ( fun v -> Maths . ( cos ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> - 1 . . * v . ( 1 ) . * sin ( v . ( 0 ) . * v . ( 1 ) ) ) ; ( fun v -> - 1 . . * v . ( 0 ) . * sin ( v . ( 0 ) . * v . ( 1 ) ) ) ] | , vs ) ) ; ( " cos_y_div_x " , ( " cos ( y / x ) " , ( fun v -> Maths . ( cos ( Mat . get v 0 1 / Mat . get v 0 0 ) ) ) , [ | ( fun v -> 1 . . * v . ( 1 ) . / ( v . ( 0 ) ** 2 . ) . * sin ( v . ( 1 ) . / v . ( 0 ) ) ) ; ( fun v -> - 1 . . / v . ( 0 ) . * sin ( v . ( 1 ) . / v . ( 0 ) ) ) ] | , vs_x_nonzero ) ) ; ( " tan_x_p_y " , ( " tan ( x + y ) " , ( fun v -> Maths . ( tan ( Mat . get v 0 0 + Mat . get v 0 1 ) ) ) , [ | ( fun v -> 1 . . / ( cos ( v . ( 0 ) . + v . ( 1 ) ) ** 2 . ) ) ; ( fun v -> 1 . . / ( cos ( v . ( 0 ) . + v . ( 1 ) ) ** 2 . ) ) ] | , vs ) ) ; ( " asin_x " , ( " asin ( x ) " , ( fun v -> Maths . ( asin ( Mat . get v 0 0 ) ) ) , [ | ( fun v -> 1 . . / sqrt ( 1 . . - ( v . ( 0 ) ** 2 . ) ) ) ; ( fun _ -> 0 . ) ] | , vs_x_abs_lt_one ) ) ; ( " acos_y " , ( " acos ( y ) " , ( fun v -> Maths . ( acos ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> - 1 . . / sqrt ( 1 . . - ( v . ( 1 ) ** 2 . ) ) ) ] | , vs_y_abs_lt_one ) ) ; ( " atan_y " , ( " atan ( y ) " , ( fun v -> Maths . ( atan ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> 1 . . / ( 1 . . + ( v . ( 1 ) ** 2 . ) ) ) ] | , vs ) ) ; ( " atan2_y_x " , ( " atan2 ( y , x ) " , ( fun v -> Maths . ( atan2 ( Mat . get v 0 1 ) ( Mat . get v 0 0 ) ) ) , [ | ( fun v -> - 1 . . / ( 1 . . + ( ( v . ( 1 ) . / v . ( 0 ) ) ** 2 . ) ) . * v . ( 1 ) . / ( v . ( 0 ) ** 2 . ) ) ; ( fun v -> 1 . . / ( 1 . . + ( ( v . ( 1 ) . / v . ( 0 ) ) ** 2 . ) ) . / v . ( 0 ) ) ] | , vs_x_nonzero ) ) ] let grad_tests_hype = [ ( " sinh_x " , ( " sinh ( x ) " , ( fun v -> Maths . ( sinh ( Mat . get v 0 0 ) ) ) , [ | ( fun v -> cosh v . ( 0 ) ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " cosh_y " , ( " cosh ( y ) " , ( fun v -> Maths . ( cosh ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> 1 . . * sinh v . ( 1 ) ) ] | , vs ) ) ; ( " sinh_xy " , ( " sinh ( xy ) " , ( fun v -> Maths . ( sinh ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> v . ( 1 ) . * cosh ( v . ( 0 ) . * v . ( 1 ) ) ) ; ( fun v -> v . ( 0 ) . * cosh ( v . ( 0 ) . * v . ( 1 ) ) ) ] | , vs ) ) ; ( " cosh_xy " , ( " cosh ( xy ) " , ( fun v -> Maths . ( cosh ( Mat . get v 0 0 * Mat . get v 0 1 ) ) ) , [ | ( fun v -> 1 . . * v . ( 1 ) . * sinh ( v . ( 0 ) . * v . ( 1 ) ) ) ; ( fun v -> 1 . . * v . ( 0 ) . * sinh ( v . ( 0 ) . * v . ( 1 ) ) ) ] | , vs ) ) ; ( " cosh_y_div_x " , ( " cosh ( y / x ) " , ( fun v -> Maths . ( cosh ( Mat . get v 0 1 / Mat . get v 0 0 ) ) ) , [ | ( fun v -> - 1 . . * v . ( 1 ) . / ( v . ( 0 ) ** 2 . ) . * sinh ( v . ( 1 ) . / v . ( 0 ) ) ) ; ( fun v -> 1 . . / v . ( 0 ) . * sinh ( v . ( 1 ) . / v . ( 0 ) ) ) ] | , vs_x_nonzero ) ) ; ( " tanh_x_p_y " , ( " tanh ( x + y ) " , ( fun v -> Maths . ( tanh ( Mat . get v 0 0 + Mat . get v 0 1 ) ) ) , [ | ( fun v -> 1 . . / ( cosh ( v . ( 0 ) . + v . ( 1 ) ) ** 2 . ) ) ; ( fun v -> 1 . . / ( cosh ( v . ( 0 ) . + v . ( 1 ) ) ** 2 . ) ) ] | , vs ) ) ; ( " asinh_x " , ( " asinh ( x ) " , ( fun v -> Maths . ( asinh ( Mat . get v 0 0 ) ) ) , [ | ( fun v -> 1 . . / sqrt ( 1 . . + ( v . ( 0 ) ** 2 . ) ) ) ; ( fun _ -> 0 . ) ] | , vs ) ) ; ( " acosh_y " , ( " acos ( y ) " , ( fun v -> Maths . ( acosh ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> 1 . . / sqrt ( - 1 . . + ( v . ( 1 ) ** 2 . ) ) ) ] | , vs_y_gt_one ) ) ; ( " atanh_y " , ( " atan ( y ) " , ( fun v -> Maths . ( atanh ( Mat . get v 0 1 ) ) ) , [ | ( fun _ -> 0 . ) ; ( fun v -> 1 . . / ( 1 . . - ( v . ( 1 ) ** 2 . ) ) ) ] | , vs_y_abs_lt_one ) ) ] let test_set = List . concat [ make_grad_test_set " poly " grad_tests_poly ; make_grad_test_set " logexp " grad_tests_logexp ; make_grad_test_set " other " grad_tests_other ; make_grad_test_set " trig " grad_tests_trig ; make_grad_test_set " hyperbolic " grad_tests_hype ] end |
module Make ( M : Ndarray_Algodiff with type elt = float ) = struct module M = Owl . Mat module AlgoM = Owl . Algodiff . D open AlgoM let ( ) = Owl_stats_prng . init 0 let n = 3 let n_samples = 20 let threshold = 1E - 6 let eps = 1E - 5 module FD = Owl_algodiff_check . Make ( AlgoM ) module Make_tests ( P : sig val label : string val test_func : ( t -> t ) -> bool * int end ) = struct open P let neg ( ) = test_func Maths . neg let abs ( ) = test_func Maths . abs let signum ( ) = test_func Maths . signum let floor ( ) = test_func Maths . floor let ceil ( ) = test_func Maths . ceil let round ( ) = test_func Maths . round let sqr ( ) = test_func Maths . sqr let sqrt ( ) = test_func ( fun x -> Maths . ( sqrt ( x * x ) ) ) let log ( ) = test_func ( fun x -> Maths . ( log ( F 1 . + ( x * x ) ) ) ) let pow ( ) = test_func ( fun x -> Maths . ( log ( F 1 . + pow ( sqr x ) ( F 2 . 9 + x ) ) ) ) let sin ( ) = test_func Maths . sin let cos ( ) = test_func Maths . cos let tan ( ) = let f x = Maths . ( tan x * cos x * cos x ) in test_func f let sinh ( ) = test_func Maths . sinh let cosh ( ) = test_func Maths . cosh let exp ( ) = test_func Maths . exp let tanh ( ) = test_func Maths . tanh let sigmoid ( ) = test_func Maths . sigmoid let relu ( ) = test_func Maths . relu let dawsn ( ) = test_func Maths . dawsn let transpose ( ) = test_func Maths . transpose let diag ( ) = test_func Maths . diag let diagm ( ) = let f x = Maths . ( diagm ( get_row x 0 ) ) in test_func f let trace ( ) = test_func Maths . trace let log_sum_exp ' ( ) = test_func Maths . log_sum_exp ' let log_sum_exp ( ) = test_func Maths . ( log_sum_exp ~ axis : 1 ) let l1norm ' ( ) = test_func Maths . l1norm ' let l2norm ' ( ) = test_func Maths . l2norm ' let l2norm_sqr ' ( ) = test_func Maths . l2norm_sqr ' let get_slice ( ) = let f x = let y1 = Maths . get_slice [ [ 0 ] ] x in let y2 = Maths . get_slice [ [ 2 ] ] x in Maths . ( sum ' ( sin ( y1 * y2 ) ) ) in test_func f let get_fancy ( ) = let f x = let y1 = Maths . get_fancy [ R [ 0 ; 1 ] ; L [ 0 ; 2 ] ] x in let y2 = Maths . get_fancy [ R [ 1 ; 2 ] ; L [ 0 ; 1 ] ] x in Maths . ( sum ' ( tan ( y1 * y2 ) ) ) in test_func f let tril ( ) = test_func Maths . tril let triu ( ) = test_func Maths . triu let inv ( ) = let e = Arr ( Owl . Mat . eye n ) in let f x = let x = Maths . ( transpose x *@ x ) in Maths . ( inv ( x + e ) ) in test_func f let logdet ( ) = let e = Arr ( Owl . Mat . eye n ) in let f x = let x = Maths . ( transpose x *@ x ) in Linalg . logdet Maths . ( x + e ) in test_func f let qr ( ) = let f x = let q , r = Linalg . qr x in Maths . ( q + r ) in test_func f let lq ( ) = let f x = let l , q = Linalg . lq x in Maths . ( l + q ) in test_func f let svd ( ) = let f = let y = Mat . gaussian 20 n in fun x -> let x = Maths . ( y *@ x ) in let u , s , vt = Linalg . svd x in Maths . ( u + ( sum ' s * l2norm_sqr ' vt ) ) in test_func f let chol ( ) = let f x = let identity = Arr ( Owl . Mat . eye n ) in let s = Maths . ( transpose x *@ x ) in let s = Maths . ( s + ( F 0 . 001 * identity ) ) in Maths . ( inv ( Linalg . chol ~ upper : false s ) ) in test_func f let split ( ) = let f x = let a = Maths . split ~ axis : 0 [ | 1 ; 1 ; 1 ] | x in Maths . ( a . ( 0 ) + ( a . ( 1 ) * a . ( 2 ) ) ) in test_func f let concat ( ) = let y = Mat . gaussian n ( 2 * n ) in let z = Mat . gaussian n n in let f x = let z = Maths . concat ~ axis : 0 Maths . ( x + z ) x in Maths . ( y *@ z ) in test_func f let concatenate ( ) = let f = let y1 = Mat . gaussian 10 n in let y2 = Mat . gaussian 15 n in let h x = Maths . ( sum ' ( y1 *@ x ) ) in let h ' = grad h in fun x -> let y = Maths . concatenate ~ axis : 0 [ | y1 ; x ; y2 ; h ' x ] | in Maths . ( y *@ x ) in test_func f let stack ( ) = let f = let y1 = Mat . gaussian n n in let y2 = Mat . gaussian n n in let h x = Maths . ( sum ' ( y1 *@ x ) ) in let h ' = grad h in fun x -> Maths . stack ~ axis ( :- 1 ) [ | y1 ; x ; y2 ; h ' x ] | in test_func f let of_arrays ( ) = let f x = let y = Array . init n ( fun i -> Array . init n ( fun j -> if i = 0 then Maths . get_item x j i else Maths . get_item x i j ) ) |> Maths . of_arrays in Maths . ( x * sin ( y + x ) ) in test_func f let to_arrays ( ) = let f x = let a = Maths . to_arrays x in Maths . ( a . ( 0 ) . ( 1 ) * cos a . ( 1 ) . ( 0 ) ) in test_func f let init_2d ( ) = let f x = let y = Mat . init_2d n n ( fun i j -> if i = 0 then F 3 . else Maths . get_item x i j ) in Maths . ( y *@ x ) in test_func f let sylvester ( ) = let r1 = Mat . gaussian n n in let r2 = Mat . gaussian n n in let f x = let a = Maths . ( x + r1 ) in let b = Maths . ( x + r2 ) in let c = Maths . ( ( a *@ x ) + ( x *@ b ) ) in Linalg . sylvester a b c in test_func f let lyapunov1 ( ) = let r = Mat . gaussian n n in let identity = Arr Owl . Mat . ( eye n ) in let f x = let q = Maths . ( F 0 . 5 * ( x + transpose x + identity ) ) in let s = Maths . ( r - transpose r ) in let p = Maths . ( ( ( r + x ) *@ transpose ( r + x ) ) + identity ) in let a = Maths . ( ( s - ( F 0 . 5 * q ) ) *@ inv p ) in Linalg . lyapunov a Maths . ( neg q ) in test_func f let lyapunov2 ( ) = let r = Mat . gaussian n n in let identity = Arr Owl . Mat . ( eye n ) in let f x = let q = Maths . ( x + identity ) in let s = Maths . ( r - transpose r ) in let p = Maths . ( ( ( r + x ) *@ transpose ( r + x ) ) + identity ) in let a = Maths . ( ( s - ( F 0 . 5 * q ) ) *@ inv p ) in Linalg . lyapunov a Maths . ( neg q ) in test_func f let discrete_lyapunov1 ( ) = let r = Mat . gaussian n n in let identity = Arr Owl . Mat . ( eye n ) in let f x = let q = Maths . ( F 0 . 5 * ( ( x *@ transpose x ) + identity ) ) in let s = Maths . ( x - transpose x ) in let p = Maths . ( ( ( r + x ) *@ transpose ( r + x ) ) + identity ) in let a = Maths . ( ( ( s - ( F 0 . 5 * q ) ) *@ inv p ) - identity ) in Linalg . discrete_lyapunov a q in test_func f let discrete_lyapunov2 ( ) = let r = Mat . gaussian n n in let identity = Arr Owl . Mat . ( eye n ) in let f x = let q = x in let s = Maths . ( x - transpose x ) in let p = Maths . ( ( ( r + x ) *@ transpose ( r + x ) ) + identity ) in let a = Maths . ( ( ( s - ( F 0 . 5 * q ) ) *@ inv p ) - identity ) in Linalg . discrete_lyapunov a q in test_func f let linsolve ( ) = let x = Mat . gaussian n n in let f a = let b = Maths . ( a *@ x ) in let x = Linalg . ( linsolve a b ) in let at = Linalg . ( linsolve ~ trans : true x ( Maths . transpose b ) ) in Maths . ( a + at + x ) in test_func f let linsolve_triangular ( ) = let x = Mat . gaussian n n in let f a = let a_l = Maths . tril a in let p_l = Maths . ( a_l *@ transpose a_l ) in let a_u = Maths . triu a in let p_u = Maths . ( transpose a_u *@ a_u ) in let b_l = Maths . ( p_l *@ x ) in let b_u = Maths . ( p_u *@ x ) in let x_l = Linalg . linsolve ~ typ ` : l a_l b_l |> Linalg . ( linsolve ~ typ ` : l ~ trans : true a_l ) in let x_u = Linalg . ( linsolve ~ typ ` : u ~ trans : true a_u b_u ) |> Linalg . ( linsolve ~ typ ` : u a_u ) in let atl = Linalg . ( linsolve ~ trans : true x_l ( Maths . transpose b_l ) ) in let atu = Linalg . ( linsolve ~ trans : true x_u ( Maths . transpose b_u ) ) in Maths . ( atl *@ atu ) in test_func f let care ( ) = let b = Mat . gaussian n n in let q = Mat . gaussian n n in let id = Mat . eye n in let f x = let a = Maths . ( x - id ) in let b = Maths . ( tril x + b ) in let r = let e = Mat . eye n in let r = Maths . ( e + ( a *@ transpose a ) ) in Maths . ( r *@ transpose r ) in let q = let q = Maths . ( q + x ) in Maths . ( ( q *@ transpose q ) + Mat . ( eye n ) ) in let c1 = Linalg . care a b q r in let c2 = Linalg . care ~ diag_r : true a b q Maths . ( diagm ( diag r ) ) in Maths . ( c1 + c2 ) in test_func f let dare ( ) = let b = Mat . gaussian n n in let q = Mat . gaussian n n in let f x = let a = x in let b = Maths . ( tril x + b ) in let r = let e = Mat . eye n in let r = Maths . ( e + ( a *@ transpose a ) ) in Maths . ( r *@ transpose r ) in let q = let q = Maths . ( q + a ) in Maths . ( ( q *@ transpose q ) + Mat . ( eye n ) ) in let c1 = Linalg . dare a b q r in let c2 = Linalg . dare ~ diag_r : true a b q Maths . ( diagm ( diag r ) ) in Maths . ( c1 + c2 ) in test_func f let nested_grad1 = let x = Mat . gaussian 1 ( n * n ) in let r ~ theta x = Maths . ( sum ' ( sqr x *@ transpose ( theta * theta ) ) ) in let quad ~ theta x = let rlx ~ theta = grad ( r ~ theta ) in jacobian ( rlx ~ theta ) x in let test_theta x theta = quad ~ theta x |> Maths . l2norm_sqr ' in let f theta = let theta = Arr . reshape theta [ | 1 ; n * n ] | in test_theta x theta in fun ( ) -> test_func f let nested_grad2 = let ff = let z = Mat . gaussian n n in let hfwd = Mat . gaussian n n in let hrev = Mat . gaussian ( 3 * n ) n in fun x -> let zfwd = make_forward z hfwd ( tag ( ) ) in let zrev = make_reverse z ( tag ( ) ) in let f1 = Maths . ( concatenate ~ axis : 0 [ | x ; zfwd + sqr x ; zfwd ] ) | |> Maths . sin in let f2 = Maths . ( concatenate ~ axis : 0 [ | x ; zrev + sqr x ; zrev ] ) | |> Maths . sin in let df = tangent f1 in reverse_prop hrev f2 ; Maths . ( sum ' df + sum ' ( adjval zrev ) ) in fun ( ) -> test_func ff let test = [ " neg " , neg ; " abs " , abs ; " signum " , signum ; " floor " , floor ; " ceil " , ceil ; " round " , round ; " sqr " , sqr ; " sqrt " , sqrt ; " log " , log ; " pow " , pow ; " sin " , sin ; " cos " , cos ; " tan " , tan ; " sinh " , sinh ; " cosh " , cosh ; " tanh " , tanh ; " sigmoid " , sigmoid ; " relu " , relu ; " dawsn " , dawsn ; " exp " , exp ; " transpose " , transpose ; " diag " , diag ; " diagm " , diagm ; " trace " , trace ; " l1norm ' " , l1norm ' ; " l2norm ' " , l2norm ' ; " l2norm_sqr ' " , l2norm_sqr ' ; " get_slice " , get_slice ; " get_fancy " , get_fancy ; " tril " , tril ; " triu " , triu ; " inv " , inv ; " logdet " , logdet ; " chol " , chol ; " qr " , qr ; " lq " , lq ; " split " , split ; " concat " , concat ; " concatenate " , concatenate ; " stack " , stack ; " svd " , svd ; " of_arrays " , of_arrays ; " to_arrays " , to_arrays ; " init_2d " , init_2d ; " sylvester " , sylvester ; " lyapunov1 " , lyapunov1 ; " lyapunov2 " , lyapunov2 ; " discrete_lyapunov1 " , discrete_lyapunov1 ; " discrete_lyapunov2 " , discrete_lyapunov2 ; " linsolve " , linsolve ; " linsolve_triangular " , linsolve_triangular ; " care " , care ; " dare " , dare ; " log_sum_exp ' " , log_sum_exp ' ; " log_sum_exp " , log_sum_exp ; " nested_grad1 " , nested_grad1 ; " nested_grad2 " , nested_grad2 ] |> List . fold_left ( fun ( b , error_msg ) ( s , f ) -> let b ' , c = match f ( ) with | x -> x | exception Owl_exception . NOT_IMPLEMENTED _ -> true , n in if b ' then b && b ' , error_msg else ( let msg = Printf . sprintf " % s \ n % s ( % i /% i ) " error_msg s ( n_samples - c ) n_samples in false , msg ) ) ( true , Printf . sprintf " \ nfailed % s operations " label ) |> fun ( b , error_msg ) -> label , ` Slow , fun ( ) -> if b then ( ) else failwith error_msg end let samples , directions = FD . generate_test_samples ( n , n ) n_samples module Reverse = Make_tests ( struct let label = " reverse mode " let test_func f = FD . Reverse . check ~ threshold ~ order ` : fourth ~ eps ~ directions ~ f samples end ) module Forward = Make_tests ( struct let label = " forward mode " let test_func f = FD . Forward . check ~ threshold ~ directions ~ f samples end ) end |
object end M . cl [ ' a , ' b ] M . cl object ( ' ty ) inherit cl val mutable virtual var : bool method private bar1 x ~ y : bool method private virtual bar2 : ' a ' b . ( ' a , ' b ) Hashtbl . t constraint ' a = ' b end [ ' a , ' b ] M . cl fun a b -> object end val x = true end object ( _ : ' a ) inherit Something . someclass as v val mutable var : bool = true val mutable virtual var2 : string method private bar1 x ~ y : bool = false method private virtual bar2 : ' a ' b . ( ' a , ' b ) Hashtbl . t constraint ' a = ' b initializer z end |
let e = var <- true |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.