Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Big arrays and the top level
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Liam Stewart <liam@u...>
Subject: Re: [Caml-list] Big arrays and the top level

I have some code that does that [1] - you basically need to write a
printer and then tell the top level to use it.

#install_printer printer-name;;

liam

On Thu, Oct 21, 2004 at 06:44:11PM +0100, Jon Harrop wrote:
> 
> Can the top level easily be made to print the contents of big arrays? I think 
> this could be quite useful...
> 
> Cheers,
> Jon.
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

[1]

Based on some code from Markus Mottl's excellent Lacaml library. It
works for fortran float vectors and matrices. It needs work, but 
suits my current needs.

open Bigarray
open Format

let print_vec f vec =
  let r = Array1.dim vec in
  fprintf f "@[";
  for i = 1 to r do
    fprintf f "@\n";
    fprintf f "@[%#11.6g@]" vec.{i};
  done;
  fprintf f "@]@\n"

let print_mat f mat =
  let r, c = Array2.dim1 mat, Array2.dim2 mat in
  let width = (get_margin ()) / (11+1) in

  let rec print_cols start num rows cols mat =
    let next_start = start + num in
    let finish = min (next_start - 1) cols in

    if cols > num then
      if start = finish then
        fprintf f "@\n@[Column %d@]@\n" start
      else
        fprintf f "@\n@[Columns %d through %d@]@\n" start finish;
    fprintf f "@[";
    for i = 1 to rows do
      fprintf f "@\n@[@;";
      for j = start to finish do
        fprintf f "@[%#11.6g@]" mat.{i,j};
        fprintf f "@;"
      done;
      fprintf f "@]"
    done;
    fprintf f "@]@\n";

    if finish = cols then
      ()
    else
      print_cols next_start num rows cols mat
  in

  print_cols 1 width r c mat

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners