Commits

Chris_T  committed 19abe05

Register a pretty printer (remove the pp function)

  • Participants
  • Parent commits 0903293

Comments (0)

Files changed (5)

File examples/linalg_ex.ml

 open Gsl
 
 let mA = [|
-  0.18; 0.60; 0.57; 0.96; 
-  0.41; 0.24; 0.99; 0.58; 
-  0.14; 0.30; 0.97; 0.66; 
+  0.18; 0.60; 0.57; 0.96;
+  0.41; 0.24; 0.99; 0.58;
+  0.14; 0.30; 0.97; 0.66;
   0.51; 0.13; 0.19; 0.85; |]
 
-let vB = [| 1.0; 2.0; 3.0; 4.0; |]
+let vB = [| 1.0; 2.0; 3.0; 4.0 |]
 
 let test () =
   let x = Linalg.solve_LU ~protect:true
       (`A (mA, 4, 4)) (`A vB) in
   Printf.printf "x = \n" ;
-  Array.iter (fun v -> Printf.printf " %g\n" v) x 
+  Array.iter (fun v -> Printf.printf " %g\n" v) x
 
-let _ = 
-  Error.init () ;
-  Error.handle_exn test ()
+let () =
+  test ()

File examples/siman_tsp_ex.ml

     long : float ;
   }
 
-let cities = 
-  Array.map (fun (n,la,lo) -> { name = n; lat = la; long = lo }) 
+let cities =
+  Array.map (fun (n,la,lo) -> { name = n; lat = la; long = lo })
     [| ("Santa Fe",    35.68,   105.95) ;
        ("Phoenix",     33.54,   112.07) ;
        ("Albuquerque", 35.12,   106.62) ;
   let cla2 = cos (la2 *. pi_180) in
   let slo2 = sin (lo2 *. pi_180) in
   let clo2 = cos (lo2 *. pi_180) in
-  let dot_prod = 
-    (cla1 *. clo1) *. (cla2 *. clo2) 
+  let dot_prod =
+    (cla1 *. clo1) *. (cla2 *. clo2)
       +. (cla1 *. slo1) *. (cla2 *. slo2)
       +. sla1 *. sla2 in
   earth_radius *. acos dot_prod
     for j= succ i to pred nb do
       let dist = city_distance cities.(i) cities.(j) in
       mat.(i).(j) <- dist ;
-      mat.(j).(i) <- dist 
-    done 
+      mat.(j).(i) <- dist
+    done
   done ;
   mat
 
-let print_distance_matrix mat = 
+let print_distance_matrix mat =
   let nb = Array.length mat in
   for i=0 to pred nb do
     Printf.printf "# " ;
   flush stdout
 
 
-let energ_func dist_mat route = 
+let energ_func dist_mat route =
   let nb = Array.length route in
   let e = ref 0. in
   for i=0 to pred nb do
   done ;
   !e
 
-let step_func rng route _step_size = 
+let step_func rng route _step_size =
   let nb = Array.length route in
   let x1 = (Rng.uniform_int rng (pred nb)) + 1 in
   let x2 = ref x1 in
   let route = Array.copy route in
   let swap = route.(x1) in
   route.(x1)  <- route.(!x2) ;
-  route.(!x2) <- swap ; 
+  route.(!x2) <- swap ;
   route
 
-let print_func route = 
+let print_func route =
   let nb = Array.length route in
   print_string "  [" ;
   for i=0 to pred nb do
   print_string "]\n"
 
 
-let main () = 
+let main () =
   let rng = Rng.make (Rng.default ()) in
   let nb = Array.length cities in
 
   Printf.printf "# initial order of cities:\n" ;
   for i=0 to pred nb do
     Printf.printf "# \"%s\"\n" cities.(route_init.(i)).name
-  done ; 
+  done ;
   Printf.printf "# distance matrix is:\n" ;
   print_distance_matrix matrix ;
   Printf.printf "# initial coordinates of cities (longitude and latitude)\n" ;
     Siman.t_initial = 5000. ; Siman.mu_t = 1.002 ;
     Siman.t_min = 5e-1 } in
 
-  let final_route = 
+  let final_route =
     Siman.solve
-      rng route_init 
+      rng route_init
       ~energ_func:(energ_func matrix)
       ~step_func
       (* ~print_func *)
       siman_params in
-  
+
   Printf.printf "# final order of cities:\n" ;
   for i=0 to pred nb do
     Printf.printf "# \"%s\"\n" cities.(final_route.(i)).name
-  done ; 
+  done ;
   Printf.printf "# final coordinates of cities (longitude and latitude)\n" ;
   for i=0 to pred nb do
     let c = final_route.(i) in
   done ;
   flush stdout
 
-  
 
-let _ = 
-  Error.init () ;
+
+let () =
   Rng.env_setup () ;
   Ieee.env_setup () ;
-  Error.handle_exn main ()
+  main ()

File examples/wavelet_ex.ml

   begin
     try
       while true do
-	let l = input_line ic in
-	acc := do_line !acc l
+        let l = input_line ic in
+        acc := do_line !acc l
       done
-    with 
+    with
     | End_of_file -> close_in ic
     | exn -> close_in ic ; raise exn
   end ;
 
   let w = Wavelet.make Wavelet.DAUBECHIES 4 in
   Wavelet.transform_forward w data ;
-  let high = 
-    Gsl_sort.vector_flat_largest_index 20 
+  let high =
+    Gsl_sort.vector_flat_largest_index 20
       (Vector_flat.view_array (Array.map abs_float data)) in
   let high_coeff = Array.make n 0. in
   for i = 0 to 20 - 1 do
     high_coeff.(j) <- data.(j)
   done ;
   Wavelet.transform_inverse w high_coeff ;
-  
+
   Array.iter
     (fun f -> Printf.printf "%g\n" f)
     high_coeff
 
-let _ = 
-  Error.init () ;
-  Error.handle_exn main "ecg.dat"
+let () =
+  main "ecg.dat"

File lib/error.ml

 (* Copyright (©) 2002-2012 - Olivier Andrieu                *)
 (* Distributed under the terms of the GPL version 3         *)
 
+open Printf
+
 external gsl_version : unit -> string
     = "ml_gsl_version"
 
   | ETOLG    -> "ETOLG"
   | EOF      -> "EOF"
 
-let pprint_exn = function
-  | Gsl_exn (errno, msg) ->
-      Printf.sprintf "GSL error: %s, %s\n  %s"
-        (string_of_errno errno)
-        (strerror errno) msg
-  | e ->
-      Printexc.to_string e
+let printer = function
+  | Gsl_exn(errno, msg) ->
+     Some(sprintf "Gsl.Error.Gsl_exn(%s, %S)" (string_of_errno errno) msg)
+  | _ -> None
 
-let handle_exn f x =
-  try f x
-  with exn -> prerr_endline (pprint_exn exn) ; raise exn
+let () =
+  Printexc.register_printer printer

File lib/error.mli

 val string_of_errno : errno -> string
 (** [string_of_errno e] returns the name of [e]. *)
 
-val pprint_exn : exn -> string
-(** [pprint_exn e] pretty print the exception [e].  If [e] is not a
-    GSL exception, use [Printexc.to_string]. *)
-
-val handle_exn : ('a -> 'b) -> 'a -> 'b
+val printer : exn -> string option
+(** [printer] is an exceoption printer for {!Gsl_exn}.  It is
+    registered by default with [Printexc]. *)