text
stringlengths
12
786k
let fun11 ( ) = Alcotest . ( check bool ) " fun11 " true ( To_test . fun11 ( ) )
let fun12 ( ) = Alcotest . ( check bool ) " fun12 " true ( To_test . fun12 ( ) )
let fun13 ( ) = Alcotest . ( check bool ) " fun13 " true ( To_test . fun13 ( ) )
let test_set = [ " fun00 " , ` Slow , fun00 ; " fun01 " , ` Slow , fun01 ; " fun02 " , ` Slow , fun02 ; " fun03 " , ` Slow , fun03 ; " fun04 " , ` Slow , fun04 ; " fun05 " , ` Slow , fun05 ; " fun06 " , ` Slow , fun06 ; " fun07 " , ` Slow , fun07 ; " fun08 " , ` Slow , fun08 ; " fun09 " , ` Slow , fun09 ; " fun10 " , ` Slow , fun10 ; " fun11 " , ` Slow , fun11 ; " fun12 " , ` Slow , fun12 ; " fun13 " , ` Slow , fun13 ]
let close a b = Maths . ( a - b < F 1e - 5 )
let init_ch = [ | F 0 . ; F 0 . ] |
let rate_adagrad = LR . Adagrad 3 .
let rate_rmsprop = LR . RMSprop ( 3 . , 0 . 9 )
let rate_adam = LR . Adam ( 0 . 001 , 0 . 9 , 0 . 999 )
let test_optimiser rate_fun c g x = let ch = init_ch in let _ = Array . mapi ( fun i ci -> ch . ( i ) <- F ci ) c in let c = ref ch in let x = ref ( F x ) in let g = F g in for i = 1 to 3 do c := LR . update_ch rate_fun g ! c ; let lr = LR . run rate_fun i g ! c in x := Maths . ( ! x - ( lr * g ) ) done ; ! x
module To_test_adagrad = struct let fun00 ( ) = let o = test_optimiser rate_adagrad [ | 0 . 1 ] | 0 . 1 1 . in close o ( F ( - 1 . 6026098728179932 ) ) let fun01 ( ) = let o = test_optimiser rate_adagrad [ | 0 . 1 ] | 0 . 1 2 . in close o ( F ( - 0 . 6026098728179932 ) ) let fun02 ( ) = let o = test_optimiser rate_adagrad [ | 0 . 1 ] | 0 . 01 3 . in close o ( F 2 . 715679168701172 ) let fun03 ( ) = let o = test_optimiser rate_adagrad [ | 0 . 1 ] | 0 . 01 4 . in close o ( F 3 . 715679168701172 ) end
module To_test_rmsprop = struct let fun00 ( ) = let o = test_optimiser rate_rmsprop [ | 1 . ] | 0 . 1 1 . in close o ( F 2 . 91705132e - 04 ) let fun01 ( ) = let o = test_optimiser rate_rmsprop [ | 1 . ] | 0 . 1 2 . in close o ( F 1 . 00029182 ) let fun02 ( ) = let o = test_optimiser rate_rmsprop [ | 1 . ] | 0 . 01 3 . in close o ( F 2 . 89990854 ) let fun03 ( ) = let o = test_optimiser rate_rmsprop [ | 1 . ] | 0 . 01 4 . in close o ( F 3 . 89990854 ) end
module To_test_adam = struct let fun00 ( ) = let o = test_optimiser rate_adam [ | 0 . ; 0 . ] | 0 . 1 1 . in close o ( F 0 . 998 ) let fun01 ( ) = let o = test_optimiser rate_adam [ | 0 . ; 0 . ] | 0 . 1 2 . in close o ( F 1 . 998 ) let fun02 ( ) = let o = test_optimiser rate_adam [ | 0 . ; 0 . ] | 0 . 01 3 . in close o ( F 2 . 999 ) let fun03 ( ) = let o = test_optimiser rate_adam [ | 0 . ; 0 . ] | 0 . 01 4 . in close o ( F 3 . 999 ) end
let fun00 ( ) = Alcotest . ( check bool ) " fun00 " true ( To_test_adagrad . fun00 ( ) )
let fun01 ( ) = Alcotest . ( check bool ) " fun01 " true ( To_test_adagrad . fun01 ( ) )
let fun02 ( ) = Alcotest . ( check bool ) " fun02 " true ( To_test_adagrad . fun02 ( ) )
let fun03 ( ) = Alcotest . ( check bool ) " fun03 " true ( To_test_adagrad . fun03 ( ) )
let fun04 ( ) = Alcotest . ( check bool ) " fun04 " true ( To_test_rmsprop . fun00 ( ) )
let fun05 ( ) = Alcotest . ( check bool ) " fun05 " true ( To_test_rmsprop . fun01 ( ) )
let fun06 ( ) = Alcotest . ( check bool ) " fun06 " true ( To_test_rmsprop . fun02 ( ) )
let fun07 ( ) = Alcotest . ( check bool ) " fun07 " true ( To_test_rmsprop . fun03 ( ) )
let fun08 ( ) = Alcotest . ( check bool ) " fun08 " true ( To_test_adam . fun00 ( ) )
let fun09 ( ) = Alcotest . ( check bool ) " fun09 " true ( To_test_adam . fun01 ( ) )
let fun10 ( ) = Alcotest . ( check bool ) " fun10 " true ( To_test_adam . fun02 ( ) )
let fun11 ( ) = Alcotest . ( check bool ) " fun11 " true ( To_test_adam . fun03 ( ) )
let test_set = [ " fun00 " , ` Slow , fun00 ; " fun01 " , ` Slow , fun01 ; " fun02 " , ` Slow , fun02 ; " fun03 " , ` Slow , fun03 ; " fun04 " , ` Slow , fun04 ; " fun05 " , ` Slow , fun05 ; " fun06 " , ` Slow , fun06 ; " fun07 " , ` Slow , fun07 ; " fun08 " , ` Slow , fun08 ; " fun09 " , ` Slow , fun09 ; " fun10 " , ` Slow , fun10 ; " fun11 " , ` Slow , fun11 ]
let approx_equal a b = let eps = 1e - 6 in Stdlib . ( abs_float ( a . - b ) < eps )
let x0 = Mat . sequential ~ a : 1 . 1 6
let x1 = Mat . sequential ~ a : 1 . 3 3
module To_test = struct let rank ( ) = let x = Mat . sequential 4 4 in M . rank x = 2 let det ( ) = let x = Mat . hadamard 4 in M . det x = 16 . let inv ( ) = let x = Mat . hadamard 4 in M . inv x |> Mat . sum ' = 1 . let vecnorm_01 ( ) = let a = M . vecnorm ~ p : 1 . x0 in approx_equal a 21 . let vecnorm_02 ( ) = let a = M . vecnorm ~ p : 2 . x0 in approx_equal a 9 . 539392014169456 let vecnorm_03 ( ) = let a = M . vecnorm ~ p : 3 . x0 in approx_equal a 7 . 6116626110202441 let vecnorm_04 ( ) = let a = M . vecnorm ~ p : 4 . x0 in approx_equal a 6 . 9062985796189906 let vecnorm_05 ( ) = let a = M . vecnorm ~ p : infinity x0 in approx_equal a 6 . let vecnorm_06 ( ) = let a = M . vecnorm ~ p ( :- 1 . ) x0 in approx_equal a 0 . 40816326530612251 let vecnorm_07 ( ) = let a = M . vecnorm ~ p ( :- 2 . ) x0 in approx_equal a 0 . 81885036774322384 let vecnorm_08 ( ) = let a = M . vecnorm ~ p ( :- 3 . ) x0 in approx_equal a 0 . 94358755060582611 let vecnorm_09 ( ) = let a = M . vecnorm ~ p ( :- 4 . ) x0 in approx_equal a 0 . 98068869669651115 let vecnorm_10 ( ) = let a = M . vecnorm ~ p : neg_infinity x0 in approx_equal a 1 . let norm_01 ( ) = let a = M . norm ~ p : 1 . x1 in approx_equal a 18 . let norm_02 ( ) = let a = M . norm ~ p : 2 . x1 in approx_equal a 16 . 84810335261421 let norm_03 ( ) = let a = M . norm ~ p : infinity x1 in approx_equal a 24 . let norm_04 ( ) = let a = M . norm ~ p ( :- 1 . ) x1 in approx_equal a 12 . let norm_05 ( ) = let a = M . norm ~ p ( :- 2 . ) x1 in approx_equal a 3 . 3347528650314325e - 16 let norm_06 ( ) = let a = M . norm ~ p : neg_infinity x1 in approx_equal a 6 . let is_triu_1 ( ) = let x = Mat . of_array [ | 1 . ; 2 . ; 3 . ; 0 . ; 5 . ; 6 . ; 0 . ; 0 . ; 9 . ] | 3 3 in M . is_triu x = true let is_triu_2 ( ) = let x = Mat . of_array [ | 1 . ; 2 . ; 3 . ; 4 . ; 5 . ; 6 . ; 0 . ; 0 . ; 9 . ] | 3 3 in M . is_triu x = false let is_tril_1 ( ) = let x = Mat . of_array [ | 1 . ; 0 . ; 0 . ; 4 . ; 5 . ; 0 . ; 7 . ; 8 . ; 9 . ] | 3 3 in M . is_tril x = true let is_tril_2 ( ) = let x = Mat . of_array [ | 1 . ; 0 . ; 3 . ; 4 . ; 5 . ; 0 . ; 7 . ; 8 . ; 9 . ] | 3 3 in M . is_tril x = false let is_symmetric_1 ( ) = let x = Mat . of_array [ | 1 . ; 2 . ; 3 . ; 2 . ; 5 . ; 6 . ; 3 . ; 6 . ; 9 . ] | 3 3 in M . is_symmetric x = true let is_symmetric_2 ( ) = let x = Mat . of_array [ | 1 . ; 2 . ; 3 . ; 4 . ; 5 . ; 6 . ; 3 . ; 6 . ; 9 . ] | 3 3 in M . is_symmetric x = false let is_diag_1 ( ) = let x = Mat . of_array [ | 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ] | 3 3 in M . is_diag x = true let is_diag_2 ( ) = let x = Mat . of_array [ | 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 0 . ; 1 . ; 1 . ] | 3 3 in M . is_diag x = false let mpow ( ) = let x = Mat . uniform 4 4 in let y = M . mpow x 3 . in let z = Mat . ( dot x ( dot x x ) ) in approx_equal Mat . ( y - z |> sum ' ) 0 . let expm_1 ( ) = let x = Mat . sequential ~ a : 1 . 3 3 in let y = Mat . of_array [ | 1118906 . 6994132 ; 1374815 . 06293582 ; 1630724 . 42645844 ; 2533881 . 04189899 ; 3113415 . 03138058 ; 3692947 . 02086217 ; 3948856 . 38438479 ; 4852012 . 99982535 ; 5755170 . 6152659 ] | 3 3 in let z = M . expm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let expm_2 ( ) = let x = Mat . ( sequential ~ a : 1 . 3 3 /$ 10 . ) in let y = Mat . of_array [ | 1 . 37316027 ; 0 . 53148466 ; 0 . 68980905 ; 1 . 00926035 ; 2 . 2481482 ; 1 . 48703605 ; 1 . 64536043 ; 1 . 96481174 ; 3 . 28426304 ] | 3 3 in let z = M . expm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let expm_3 ( ) = let x = Mat . ( sequential ~ a : 1 . 3 3 /$ 50 . ) in let y = Mat . of_array [ | 1 . 02667783 ; 0 . 04803414 ; 0 . 06939044 ; 0 . 09473789 ; 1 . 11808977 ; 0 . 14144165 ; 0 . 16279795 ; 0 . 1881454 ; 1 . 21349285 ] | 3 3 in let z = M . expm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let expm_4 ( ) = let x = Mat . ( sequential ~ a : 1 . 3 3 /$ 200 . ) in let y = Mat . of_array [ | 1 . 00538495 ; 0 . 01046225 ; 0 . 01553954 ; 0 . 02084758 ; 1 . 02604024 ; 0 . 03123291 ; 0 . 03631021 ; 0 . 04161824 ; 1 . 04692628 ] | 3 3 in let z = M . expm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let sinm ( ) = let x = Mat . sequential ~ a : 1 . 2 2 in let y = Mat . of_array [ | - 0 . 46558149 ; - 0 . 14842446 ; - 0 . 22263669 ; - 0 . 68821818 ] | 2 2 in let z = M . sinm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let cosm ( ) = let x = Mat . sequential ~ a : 1 . 2 2 in let y = Mat . of_array [ | 0 . 85542317 ; - 0 . 11087638 ; - 0 . 16631457 ; 0 . 68910859 ] | 2 2 in let z = M . cosm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let tanm ( ) = let x = Mat . sequential ~ a : 1 . 2 2 in let y = Mat . of_array [ | - 0 . 60507478 ; - 0 . 31274165 ; - 0 . 46911248 ; - 1 . 07418726 ] | 2 2 in let z = M . tanm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let sincosm ( ) = let x = Mat . sequential ~ a : 1 . 2 2 in let s = M . sinm x in let c = M . cosm x in let s ' , c ' = M . sincosm x in approx_equal Mat . ( s - s ' |> sum ' ) 0 . && approx_equal Mat . ( c - c ' |> sum ' ) 0 . let sinhm ( ) = let x = Mat . ( sequential ~ a : 1 . 2 2 /$ 10 . ) in let y = Mat . of_array [ | 0 . 10625636 ; 0 . 20913073 ; 0 . 31369609 ; 0 . 41995246 ] | 2 2 in let z = M . sinhm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let coshm ( ) = let x = Mat . ( sequential ~ a : 1 . 2 2 /$ 10 . ) in let y = Mat . of_array [ | 1 . 03583718 ; 0 . 05122002 ; 0 . 07683003 ; 1 . 11266721 ] | 2 2 in let z = M . coshm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let tanhm ( ) = let x = Mat . ( sequential ~ a : 1 . 2 2 /$ 10 . ) in let y = Mat . of_array [ | 0 . 08894293 ; 0 . 18386007 ; 0 . 2757901 ; 0 . 36473303 ] | 2 2 in let z = M . tanhm x in approx_equal Mat . ( y - z |> sum ' ) 0 . let sinhcoshm ( ) = let x = Mat . ( sequential ~ a : 1 . 2 2 /$ 10 . ) in let s = M . sinhm x in let c = M . coshm x in let s ' , c ' = M . sinhcoshm x in approx_equal Mat . ( s - s ' |> sum ' ) 0 . && approx_equal Mat . ( c - c ' |> sum ' ) 0 . end
let rank ( ) = Alcotest . ( check bool ) " rank " true ( To_test . rank ( ) )
let det ( ) = Alcotest . ( check bool ) " det " true ( To_test . det ( ) )
let inv ( ) = Alcotest . ( check bool ) " inv " true ( To_test . inv ( ) )
let vecnorm_01 ( ) = Alcotest . ( check bool ) " vecnorm_01 " true ( To_test . vecnorm_01 ( ) )
let vecnorm_02 ( ) = Alcotest . ( check bool ) " vecnorm_02 " true ( To_test . vecnorm_02 ( ) )
let vecnorm_03 ( ) = Alcotest . ( check bool ) " vecnorm_03 " true ( To_test . vecnorm_03 ( ) )
let vecnorm_04 ( ) = Alcotest . ( check bool ) " vecnorm_04 " true ( To_test . vecnorm_04 ( ) )
let vecnorm_05 ( ) = Alcotest . ( check bool ) " vecnorm_05 " true ( To_test . vecnorm_05 ( ) )
let vecnorm_06 ( ) = Alcotest . ( check bool ) " vecnorm_06 " true ( To_test . vecnorm_06 ( ) )
let vecnorm_07 ( ) = Alcotest . ( check bool ) " vecnorm_07 " true ( To_test . vecnorm_07 ( ) )
let vecnorm_08 ( ) = Alcotest . ( check bool ) " vecnorm_08 " true ( To_test . vecnorm_08 ( ) )
let vecnorm_09 ( ) = Alcotest . ( check bool ) " vecnorm_09 " true ( To_test . vecnorm_09 ( ) )
let vecnorm_10 ( ) = Alcotest . ( check bool ) " vecnorm_10 " true ( To_test . vecnorm_10 ( ) )
let norm_01 ( ) = Alcotest . ( check bool ) " norm_01 " true ( To_test . norm_01 ( ) )
let norm_02 ( ) = Alcotest . ( check bool ) " norm_02 " true ( To_test . norm_02 ( ) )
let norm_03 ( ) = Alcotest . ( check bool ) " norm_03 " true ( To_test . norm_03 ( ) )
let norm_04 ( ) = Alcotest . ( check bool ) " norm_04 " true ( To_test . norm_04 ( ) )
let norm_05 ( ) = Alcotest . ( check bool ) " norm_05 " true ( To_test . norm_05 ( ) )
let norm_06 ( ) = Alcotest . ( check bool ) " norm_06 " true ( To_test . norm_06 ( ) )
let is_triu_1 ( ) = Alcotest . ( check bool ) " is_triu_1 " true ( To_test . is_triu_1 ( ) )
let is_triu_2 ( ) = Alcotest . ( check bool ) " is_triu_2 " true ( To_test . is_triu_2 ( ) )
let is_tril_1 ( ) = Alcotest . ( check bool ) " is_tril_1 " true ( To_test . is_tril_1 ( ) )
let is_tril_2 ( ) = Alcotest . ( check bool ) " is_tril_2 " true ( To_test . is_tril_2 ( ) )
let is_symmetric_1 ( ) = Alcotest . ( check bool ) " is_symmetric_1 " true ( To_test . is_symmetric_1 ( ) )
let is_symmetric_2 ( ) = Alcotest . ( check bool ) " is_symmetric_2 " true ( To_test . is_symmetric_2 ( ) )
let is_diag_1 ( ) = Alcotest . ( check bool ) " is_diag_1 " true ( To_test . is_diag_1 ( ) )
let is_diag_2 ( ) = Alcotest . ( check bool ) " is_diag_2 " true ( To_test . is_diag_2 ( ) )
let mpow ( ) = Alcotest . ( check bool ) " mpow " true ( To_test . mpow ( ) )
let expm_1 ( ) = Alcotest . ( check bool ) " expm_1 " true ( To_test . expm_1 ( ) )
let expm_2 ( ) = Alcotest . ( check bool ) " expm_2 " true ( To_test . expm_2 ( ) )
let expm_3 ( ) = Alcotest . ( check bool ) " expm_3 " true ( To_test . expm_3 ( ) )
let expm_4 ( ) = Alcotest . ( check bool ) " expm_4 " true ( To_test . expm_4 ( ) )
let sinm ( ) = Alcotest . ( check bool ) " sinm " true ( To_test . sinm ( ) )
let cosm ( ) = Alcotest . ( check bool ) " cosm " true ( To_test . cosm ( ) )
let tanm ( ) = Alcotest . ( check bool ) " tanm " true ( To_test . tanm ( ) )
let sincosm ( ) = Alcotest . ( check bool ) " sincosm " true ( To_test . sincosm ( ) )
let sinhm ( ) = Alcotest . ( check bool ) " sinhm " true ( To_test . sinhm ( ) )
let coshm ( ) = Alcotest . ( check bool ) " coshm " true ( To_test . coshm ( ) )
let tanhm ( ) = Alcotest . ( check bool ) " tanhm " true ( To_test . tanhm ( ) )
let sinhcoshm ( ) = Alcotest . ( check bool ) " sinhcoshm " true ( To_test . sinhcoshm ( ) )
let test_set = [ " rank " , ` Slow , rank ; " det " , ` Slow , det ; " inv " , ` Slow , inv ; " vecnorm_01 " , ` Slow , vecnorm_01 ; " vecnorm_02 " , ` Slow , vecnorm_02 ; " vecnorm_03 " , ` Slow , vecnorm_03 ; " vecnorm_04 " , ` Slow , vecnorm_04 ; " vecnorm_05 " , ` Slow , vecnorm_05 ; " vecnorm_06 " , ` Slow , vecnorm_06 ; " vecnorm_07 " , ` Slow , vecnorm_07 ; " vecnorm_08 " , ` Slow , vecnorm_08 ; " vecnorm_09 " , ` Slow , vecnorm_09 ; " vecnorm_10 " , ` Slow , vecnorm_10 ; " norm_01 " , ` Slow , norm_01 ; " norm_02 " , ` Slow , norm_02 ; " norm_03 " , ` Slow , norm_03 ; " norm_04 " , ` Slow , norm_04 ; " norm_05 " , ` Slow , norm_05 ; " norm_06 " , ` Slow , norm_06 ; " is_triu_1 " , ` Slow , is_triu_1 ; " is_triu_2 " , ` Slow , is_triu_2 ; " is_tril_1 " , ` Slow , is_tril_1 ; " is_tril_2 " , ` Slow , is_tril_2 ; " is_symmetric_1 " , ` Slow , is_symmetric_1 ; " is_symmetric_2 " , ` Slow , is_symmetric_2 ; " is_diag_1 " , ` Slow , is_diag_1 ; " is_diag_2 " , ` Slow , is_diag_2 ; " mpow " , ` Slow , mpow ; " expm_1 " , ` Slow , expm_1 ; " expm_2 " , ` Slow , expm_2 ; " expm_3 " , ` Slow , expm_3 ; " expm_4 " , ` Slow , expm_4 ; " sinm " , ` Slow , sinm ; " cosm " , ` Slow , cosm ; " tanm " , ` Slow , tanm ; " sincosm " , ` Slow , sincosm ; " sinhm " , ` Slow , sinhm ; " coshm " , ` Slow , coshm ; " tanhm " , ` Slow , tanhm ; " sinhcoshm " , ` Slow , sinhcoshm ]
let approx_equal a b = let eps = 1e - 5 in N . approx_equal ~ eps a b
module To_test_gauss = struct let test01 ( ) = let a = N . of_array [ | 1 . ; 20 . ; - 30 . ; 4 . ] | [ | 2 ; 2 ] | in let b = N . of_array [ | 1 . ; 0 . ; 1 . ; 2 . ; - 30 . ; 0 . ; 1 . ; 0 . ; 1 . ; 4 . ] | [ | 2 ; 5 ] | in let a_inv , x = L . linsolve_gauss a b in let flag01 = approx_equal ( N . dot a a_inv ) ( M . eye 2 ) in let flag02 = approx_equal ( N . dot a x ) b in flag01 && flag02 let test02 ( ) = let a = N . of_array [ | 2 . ; 3 . ; 3 . ; 5 . ] | [ | 2 ; 2 ] | in let b = N . of_array [ | 1 . ; 0 . ; 1 . ; 0 . ; 1 . ; 0 . ] | [ | 2 ; 3 ] | in let a_inv , x = L . linsolve_gauss a b in let flag01 = approx_equal ( N . dot a a_inv ) ( M . eye 2 ) in let flag02 = approx_equal ( N . dot a x ) b in flag01 && flag02 let test03 ( ) = let n = 20 in let a = N . uniform [ | n ; n ] | in for i = 0 to n - 1 do let v = N . get a [ | i ; i ] | in N . set a [ | i ; i ] | ( ( v . + 0 . 1 ) . * 20 . ) done ; let flag = ref true in for _ = 0 to 9 do let b = N . uniform [ | n ; 3 ] | in let a_inv , x = L . linsolve_gauss a b in let flag01 = approx_equal ( N . dot a a_inv ) ( M . eye n ) in let flag02 = approx_equal ( N . dot a x ) b in flag := ! flag && flag01 && flag02 done ; ! flag let test04 ( ) = let a = N . of_array [ | 1 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ] | [ | 9 ; 9 ] | in let b = N . uniform [ | 9 ; 1 ] | in let flag = ref false in let _ = try L . linsolve_gauss a b |> ignore with | SINGULAR -> flag := true in ! flag end
module To_test_lu = struct let perm_vec_to_mat vec = let n = Array . length vec in let mat = ref ( M . eye n ) in for i = n - 1 downto 0 do let j = vec . ( i ) in let a = M . eye n in N . set a [ | i ; i ] | 0 . ; N . set a [ | j ; j ] | 0 . ; N . set a [ | i ; j ] | 1 . ; N . set a [ | j ; i ] | 1 . ; mat := N . dot a ! mat done ; ! mat let test01 ( ) = let x = N . of_array [ | 7 . ; 3 . ; - 1 . ; 2 . ; 3 . ; 8 . ; 1 . ; - 4 . ; - 1 . ; 1 . ; 4 . ; - 1 . ; 2 . ; - 4 . ; - 1 . ; 6 . ] | [ | 4 ; 4 ] | in let l , u , p = L . lu x in let l_expected = N . of_array [ | 1 . 0 ; 0 . ; 0 . ; 0 . ; 0 . 42857142857142855 ; 1 . 0 ; 0 . 0 ; 0 . 0 ; - 0 . 14285714285714285 ; 0 . 2127659574468085 ; 1 . 0 ; 0 . 0 ; 0 . 2857142857142857 ; - 0 . 7234042553191489 ; 0 . 0898203592814371 ; 1 . 0 ] | [ | 4 ; 4 ] | in let u_expected = N . of_array [ | 7 . 0 ; 3 . 0 ; - 1 . 0 ; 2 . 0 ; 0 . 0 ; 6 . 714285714285714 ; 1 . 4285714285714286 ; - 4 . 857142857142857 ; 0 . 0 ; 0 . 0 ; 3 . 5531914893617023 ; 0 . 31914893617021267 ; 0 . 0 ; 0 . 0 ; 0 . 0 ; 1 . 88622754491018 ] | [ | 4 ; 4 ] | in let flag01 = approx_equal l l_expected in let flag02 = approx_equal u u_expected in let flag03 = p = [ | 0 ; 1 ; 2 ; 3 ] | in flag01 && flag02 && flag03 let test02 ( ) = let x = N . of_array [ | 1 . ; 2 . ; 3 . ; 1 . ; 2 . ; 3 . ; 2 . ; 5 . ; 6 . ] | [ | 3 ; 3 ] | in let l , u , perm = L . lu x in let perm_mat = perm_vec_to_mat perm in let result = N . dot ( N . dot perm_mat l ) u in let flag = approx_equal result x in flag let test03 ( ) = let n = 20 in let flag = ref true in for _ = 0 to 9 do let x = N . uniform [ | n ; n ] | in let l , u , perm = L . lu x in let perm_mat = perm_vec_to_mat perm in let result = N . dot ( N . dot perm_mat l ) u in let f = approx_equal result x in flag := ! flag && f done ; ! flag let test04 ( ) = let a = N . of_array [ | 1 . ; 20 . ; - 30 . ; 4 . ] | [ | 2 ; 2 ] | in let b = N . of_array [ | 1 . ; 0 . ; 1 . ; 2 . ; - 30 . ; 0 . ; 1 . ; 0 . ; 1 . ; 4 . ] | [ | 2 ; 5 ] | in let x = L . linsolve_lu a b in approx_equal ( N . dot a x ) b let test05 ( ) = let a = N . of_array [ | 2 . ; 3 . ; 3 . ; 5 . ] | [ | 2 ; 2 ] | in let b = N . of_array [ | 1 . ; 0 . ; 1 . ; 0 . ; 1 . ; 0 . ] | [ | 2 ; 3 ] | in let x = L . linsolve_lu a b in approx_equal ( N . dot a x ) b let test06 ( ) = let n = 20 in let a = N . uniform [ | n ; n ] | in for i = 0 to n - 1 do let v = N . get a [ | i ; i ] | in N . set a [ | i ; i ] | ( ( v . + 0 . 1 ) . * 20 . ) done ; let flag = ref true in for _ = 0 to 9 do let b = N . uniform [ | n ; 3 ] | in let x = L . linsolve_lu a b in let f = approx_equal ( N . dot a x ) b in flag := ! flag && f done ; ! flag let test07 ( ) = let a = N . of_array [ | 1 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 0 . ; 0 . ; 0 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ; 1 . ] | [ | 9 ; 9 ] | in let b = N . uniform [ | 9 ; 1 ] | in let flag = ref false in let _ = try L . linsolve_lu a b |> ignore with | SINGULAR -> flag := true in ! flag let test08 ( ) = let a = N . of_array [ | 1 . ; 2 . ; 3 . ; 4 . ] | [ | 2 ; 2 ] | in let det = L . det a in let result = N . of_array [ | det ] | [ | 1 ] | in let expect = N . of_array [ | - 2 . ] | [ | 1 ] | in approx_equal result expect let test09 ( ) = let a = N . of_array [ | 6 . ; 1 . ; 1 . ; 4 . ; - 2 . ; 5 . ; 2 . ; 8 . ; 7 . ] | [ | 3 ; 3 ] | in let det = L . det a in let result = N . of_array [ | det ] | [ | 1 ] | in let expect = N . of_array [ | - 306 . ] | [ | 1 ] | in approx_equal result expect let test10 ( ) = let n = 20 in let a = N . uniform [ | n ; n ] | in for i = 0 to n - 1 do let v = N . get a [ | i ; i ] | in N . set a [ | i ; i ] | ( ( v . + 0 . 1 ) . * 20 . ) done ; let flag = ref true in for _ = 0 to 9 do let a_inv = L . inv a in let f = approx_equal ( N . dot a a_inv ) ( M . eye n ) in flag := ! flag && f done ; ! flag end
module To_test_bandiag = struct let _test_tridiag n = let a = N . uniform [ | n ] | in let trimat = N . zeros [ | n ; n ] | in let a_vec = Array . make n 0 . in for i = 1 to n - 1 do let v = N . get a [ | i ] | in a_vec . ( i ) <- v ; N . set trimat [ | i ; i - 1 ] | v done ; let b = N . uniform [ | n ] | in let b_vec = Array . make n 0 . in for i = 0 to n - 1 do let v = N . get b [ | i ] | in b_vec . ( i ) <- v ; N . set trimat [ | i ; i ] | v done ; let c = N . uniform [ | n ] | in let c_vec = Array . make n 0 . in for i = 0 to n - 2 do let v = N . get c [ | i ] | in c_vec . ( i ) <- v ; N . set trimat [ | i ; i + 1 ] | v done ; let r = N . uniform [ | n ] | in let r_vec = Array . make n 0 . in for i = 0 to n - 1 do r_vec . ( i ) <- N . get r [ | i ] | done ; let x = L . tridiag_solve_vec a_vec b_vec c_vec r_vec in let x = N . of_array x [ | n ; 1 ] | in let result = N . dot trimat x in approx_equal ( N . reshape result [ | n ] ) | r let test01 ( ) = let n = 20 in let repeat = 10 in let flag = ref true in for _ = 0 to repeat - 1 do flag := ! flag && _test_tridiag n done ; ! flag end
let test_gauss_01 ( ) = Alcotest . ( check bool ) " test_guass_01 " true ( To_test_gauss . test01 ( ) )
let test_gauss_02 ( ) = Alcotest . ( check bool ) " test_guass_02 " true ( To_test_gauss . test02 ( ) )
let test_gauss_03 ( ) = Alcotest . ( check bool ) " test_guass_03 " true ( To_test_gauss . test03 ( ) )
let test_gauss_04 ( ) = Alcotest . ( check bool ) " test_guass_04 " true ( To_test_gauss . test04 ( ) )
let test_lu_01 ( ) = Alcotest . ( check bool ) " test_lu_01 " true ( To_test_lu . test01 ( ) )
let test_lu_02 ( ) = Alcotest . ( check bool ) " test_lu_02 " true ( To_test_lu . test02 ( ) )
let test_lu_03 ( ) = Alcotest . ( check bool ) " test_lu_03 " true ( To_test_lu . test03 ( ) )
let test_lu_04 ( ) = Alcotest . ( check bool ) " test_lu_04 " true ( To_test_lu . test04 ( ) )
let test_lu_05 ( ) = Alcotest . ( check bool ) " test_lu_05 " true ( To_test_lu . test05 ( ) )
let test_lu_06 ( ) = Alcotest . ( check bool ) " test_lu_06 " true ( To_test_lu . test06 ( ) )
let test_lu_07 ( ) = Alcotest . ( check bool ) " test_lu_07 " true ( To_test_lu . test07 ( ) )
let test_lu_08 ( ) = Alcotest . ( check bool ) " test_lu_08 " true ( To_test_lu . test08 ( ) )
let test_lu_09 ( ) = Alcotest . ( check bool ) " test_lu_09 " true ( To_test_lu . test09 ( ) )
let test_lu_10 ( ) = Alcotest . ( check bool ) " test_lu_10 " true ( To_test_lu . test10 ( ) )
let test_bandiag_01 ( ) = Alcotest . ( check bool ) " To_test_bandiag_01 " true ( To_test_bandiag . test01 ( ) )
let test_set = [ " test_gauss_01 " , ` Slow , test_gauss_01 ; " test_gauss_02 " , ` Slow , test_gauss_02 ; " test_gauss_03 " , ` Slow , test_gauss_03 ; " test_gauss_04 " , ` Slow , test_gauss_04 ; " test_lu_01 " , ` Slow , test_lu_01 ; " test_lu_02 " , ` Slow , test_lu_02 ; " test_lu_03 " , ` Slow , test_lu_03 ; " test_lu_04 " , ` Slow , test_lu_04 ; " test_lu_05 " , ` Slow , test_lu_05 ; " test_lu_06 " , ` Slow , test_lu_06 ; " test_lu_08 " , ` Slow , test_lu_08 ; " test_lu_09 " , ` Slow , test_lu_09 ; " test_lu_10 " , ` Slow , test_lu_10 ; " test_bandiag_01 " , ` Slow , test_bandiag_01 ]
let approx_equal a b = Stdlib . ( abs_float ( a . - b ) < eps )
module To_test = struct let test_j0 ( ) = approx_equal ( M . j0 0 . 5 ) 0 . 93846980724081297 let test_j1 ( ) = approx_equal ( M . j1 0 . 5 ) 0 . 24226845767487387 let test_jv ( ) = approx_equal ( M . jv 0 . 1 0 . 3 ) 0 . 85180759557596664 let test_y0 ( ) = approx_equal ( M . y0 0 . 5 ) ( - 0 . 44451873350670662 ) let test_y1 ( ) = approx_equal ( M . y1 0 . 3 ) ( - 2 . 2931051383885293 ) let test_yv ( ) = approx_equal ( M . yv 0 . 3 0 . 2 ) ( - 1 . 470298525261079 ) let test_i0 ( ) = approx_equal ( M . i0 0 . 3 ) 1 . 0226268793515974 let test_i0e ( ) = approx_equal ( M . i0e 0 . 3 ) 0 . 7575806251825481 let test_i1 ( ) = approx_equal ( M . i1 0 . 3 ) 0 . 15169384000359282 let test_i1e ( ) = approx_equal ( M . i1e 0 . 3 ) 0 . 11237756063983881 let test_iv ( ) = approx_equal ( M . iv 0 . 3 0 . 1 ) 0 . 45447035229197424 let test_k0 ( ) = approx_equal ( M . k0 0 . 3 ) 1 . 3724600605442983 let test_k0e ( ) = approx_equal ( M . k0e 0 . 3 ) 1 . 8526273007720155 let test_k1 ( ) = approx_equal ( M . k1 0 . 3 ) 3 . 0559920334573252 let test_k1e ( ) = approx_equal ( M . k1e 0 . 3 ) 4 . 12515776224447 let test_airy ( ) = let a , b , c , d = M . airy 0 . 7 in approx_equal a 0 . 18916240039814997 && approx_equal b ( - 0 . 19985119158228049 ) && approx_equal c 0 . 97332865587816564 && approx_equal d 0 . 65440591917213997 let test_ellipj ( ) = let a , b , c , d = M . ellipj 0 . 2 0 . 3 in approx_equal a 0 . 19828063826280756 && approx_equal b 0 . 98014528948013302 && approx_equal c 0 . 99408522599776539 && approx_equal d 0 . 19960341784106181 let test_ellipk ( ) = approx_equal ( M . ellipk 0 . 3 ) 1 . 713889448178791 let test_ellipkm1 ( ) = approx_equal ( M . ellipkm1 0 . 3 ) 2 . 0753631352924691 let test_ellipkinc ( ) = approx_equal ( M . ellipkinc 0 . 2 0 . 3 ) 0 . 20039894647982787 let test_ellipe ( ) = approx_equal ( M . ellipe 0 . 3 ) 1 . 4453630644126654 let test_ellipeinc ( ) = approx_equal ( M . ellipeinc 0 . 2 0 . 3 ) 0 . 19960247841509551 let test_gamma ( ) = approx_equal ( M . gamma 0 . 7 ) 1 . 2980553326475581 let test_rgamma ( ) = approx_equal ( M . rgamma 0 . 7 ) 0 . 7703831838665659 let test_loggamma ( ) = approx_equal ( M . loggamma 0 . 7 ) 0 . 26086724653166637 let test_gammainc ( ) = approx_equal ( M . gammainc 0 . 2 0 . 7 ) 0 . 91521960195630503 let test_gammaincinv ( ) = approx_equal ( M . gammaincinv 0 . 2 0 . 7 ) 0 . 12103758588873516 let test_gammaincc ( ) = approx_equal ( M . gammaincc 0 . 2 0 . 7 ) 0 . 084780398043694499 let test_gammainccinv ( ) = approx_equal ( M . gammainccinv 0 . 2 0 . 7 ) 0 . 0015877907243441165 let test_psi ( ) = approx_equal ( M . psi 0 . 7 ) ( - 1 . 2200235536979349 ) let test_beta ( ) = approx_equal ( M . beta 0 . 2 0 . 7 ) 5 . 5764636958498768 let test_betainc ( ) = approx_equal ( M . betainc 0 . 1 0 . 2 0 . 7 ) 0 . 71632698299586095 let test_bdtr ( ) = approx_equal ( M . bdtr 5 25 0 . 2 ) 0 . 6166894117793692 let test_bdtrc ( ) = approx_equal ( M . bdtrc 5 25 0 . 2 ) 0 . 3833105882206313 let test_bdtri ( ) = approx_equal ( M . bdtri 5 25 0 . 2 ) 0 . 29781277333188838 let test_btdtr ( ) = approx_equal ( M . btdtr 5 . 25 . 0 . 2 ) 0 . 71605354811801325 let test_btdtri ( ) = approx_equal ( M . btdtri 5 . 25 . 0 . 2 ) 0 . 10833617793798132 let test_fact ( ) = let x = [ | 0 ; 1 ; 10 ; 50 ; 100 ; 150 ; 170 ] | in let y = Array . map M . fact x in let z = [ | 1 . ; 1 . ; 3628800 . ; 3 . 04140932017133780436126081661e + 64 ; 9 . 33262154439441526816992388563e + 157 ; 5 . 71338395644585459047893286526e + 262 ; 7 . 25741561530799896739672821113e + 306 ] | in let z = Array . map2 approx_equal y z in Array . for_all ( fun a -> a = true ) z let test_log_fact ( ) = let x = [ | 0 ; 1 ; 10 ; 50 ; 100 ; 150 ; 170 ] | in let y = Array . map M . log_fact x in let z = [ | 0 . ; 0 . ; 15 . 1044125730755159 ; 148 . 47776695177302 ; 363 . 73937555556347 ; 605 . 020105849423658 ; 706 . 573062245787355 ] | in let z = Array . map2 approx_equal y z in Array . for_all ( fun a -> a = true ) z let test_combination ( ) = let n = 100 in let x = [ | 0 ; 1 ; 10 ; 30 ; 50 ; 80 ] | in let y = Array . map ( M . combination_float n ) x in let z = [ | 1 . ; 100 . ; 17310309456440 . ; 2 . 93723398216109426e + 25 ; 1 . 0089134454556422e + 29 ; 5 . 35983370403809657e + 20 ] | in let z = Array . map2 ( fun a b -> a . - b ) y z in Array . for_all ( fun a -> a < 1e - 8 ) z let test_mulmod ( ) = let i = max_int in let x = [ | 0 , 1 , 2 ; 1 , 2 , 1 ; 2 , 1 , 2 ; 2 , 2 , 3 ; 4 , 5 , 3 ; 1847 , 5516 , 15268 ; 4549 , 9581 , 2679 ; i - 1 , i - 1 , 2 ; i - 1 , i - 1 , 10 ; i - 1 , i - 1 , i ] | in let y = [ | 0 ; 0 ; 0 ; 1 ; 2 ; 4296 ; 1997 ; 0 ; 4 ; 1 ] | in let b = Array . map ( fun ( a , b , m ) -> M . mulmod a b m ) x in let t = Array . map2 ( = ) b y in Array . for_all ( fun a -> a = true ) t let test_powmod ( ) = let i = max_int in let x = [ | 0 , 0 , 1 ; 0 , 0 , 2 ; 0 , 2 , 2 ; 1 , 2 , 1 ; 2 , 1 , 2 ; 5 , 3 , 3 ; 1847 , 5516 , 15268 ; 4549 , 9581 , 2679 ; i - 1 , i - 1 , 2 ; i - 1 , i - 1 , 10 ; i - 1 , i - 1 , i ] | in let y = [ | 0 ; 1 ; 0 ; 0 ; 0 ; 2 ; 4973 ; 1513 ; 0 ; 4 ; 1 ] | in let b = Array . map ( fun ( a , b , m ) -> M . powmod a b m ) x in let t = Array . map2 ( = ) b y in Array . for_all ( fun a -> a = true ) t let test_is_prime ( ) = let x = [ | - 1 ; 0 ; 1 ; 2 ; 3 ; 4 ; 8191 ; 12345 ; 524287 ; 780865231 ; 1073741789 ] | in let y = [ | false ; false ; false ; true ; true ; false ; true ; false ; true ; false ; true ] | in let r = Array . map M . is_prime x in let t = Array . map2 ( = ) r y in Array . for_all ( fun a -> a = true ) t end
let test_j0 ( ) = Alcotest . ( check bool ) " test j0 " true ( To_test . test_j0 ( ) )
let test_j1 ( ) = Alcotest . ( check bool ) " test j1 " true ( To_test . test_j1 ( ) )
let test_jv ( ) = Alcotest . ( check bool ) " test jv " true ( To_test . test_jv ( ) )
let test_y0 ( ) = Alcotest . ( check bool ) " test y0 " true ( To_test . test_y0 ( ) )
let test_y1 ( ) = Alcotest . ( check bool ) " test y1 " true ( To_test . test_y1 ( ) )
let test_yv ( ) = Alcotest . ( check bool ) " test yv " true ( To_test . test_yv ( ) )
let test_i0 ( ) = Alcotest . ( check bool ) " test i0 " true ( To_test . test_i0 ( ) )