This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] Array1.fold_left
• Christophe TROESTLER
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2003-08-12 (00:02) From: Christophe TROESTLER Subject: [Caml-list] Array1.fold_left
```Hi all,

For the following discussion, one assumes to be in the scope of an
[open Bigarray].

Suppose for example I want to compute the maximum of [a] of type
(float, float64_elt, 'a) Array1.t.  I can imagine the following code:

let array1_max a =
let m = ref (-. infinity) in
if Array1.layout a = (Obj.magic c_layout : 'a Bigarray.layout) then
for i = 0 to Array1.dim a - 1 do
if !m < a.{i} then m := a.{i}
done
else (* fortran layout *)
for i = 1 to Array1.dim a do
if !m < a.{i} then m := a.{i}
done;
!m

Actually, the test on the layout is superfluous since all elements of
the array are accessed.  But there is no way to get rid of it unless
one writes a C function.  Wouldn't be nicer (and more efficient?) to
have primitives like Array1.fold_left ?  Is there a reason such
functions are not part of the standard library?

Another interesting functions may be:
c_to_fortran : ('a, 'b, c_layout) Array1.t -> ('a, 'b, fortran_layout) Array1.t
fortran_to_c : ('a, 'b, fortran_layout) Array1.t -> ('a, 'b, c_layout) Array1.t
(permuting the dimension).

Maybe other people working with numerical code could speak up -- I
guess I am not the only one with this kind of needs, am I.

Cheers,
ChriS

-------------------
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

```