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

[Caml-list] Fortran-like array indexing / PDE numerical code with boundary cells in OCAML
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2003-12-27 (09:41) From: Issac Trotts Subject: Re: [Caml-list] Fortran-like array indexing / PDE numerical code with boundary cells in OCAML
```On Thursday 25 December 2003 21:41, Grégory Guyomarc'h wrote:
> Hello,
>
> Does anyone know how to index bigarrays with negative integers as in
> fortran (either through a modified version of Bigarray or some
> pre-Processing macro with caml-p4)? For those who wonder, it is for a pde
> numerical code, i wish i could index boundary cells naturally as u.{-1}
> u.{-2}, ... and u.{n+1}, u.{n+2}, ... Or if you have any experience in
> implementing such numerical code in OCaml and want to suggest better
> options, i am also interested :o).

One way is to make a record type like this

type ifunc =
{
lo: int; (* lowest index inside the boundaries *)
hi: int; (* highest index inside the boundaries *)
f: int -> float;
}

and then wrap your bigarray like this:

open Bigarray
let b = Array1.create float64 c_layout 32;;
let ifunc_of_ba ba =
let n = Array1.dim ba in
let rec f = fun i ->
if i < 0 then f(n+i)
else if i >= n then f(i-n)
else ba.{i}
in
{
lo = 0;
hi = n-1;
f = f;
}
# let b2 = ifunc_of_ba b;;
val b2 : ifunc = {lo = 0; hi = 31; f = <fun>}
(* now let's look at the uninitialized values in the bigarray **)
# b2.f(-1);;
- : float = 8.75916002045846257e+189
# b2.f(-2);;
- : float = 1.96098134143591667e+243
# b2.f(32+1);;
- : float = 5.66882172258997e-228
# b2.f(32+2);;
- : float = -1.24992649718407699e+65
# b2.f(1);;
- : float = 5.66882172258997e-228
# b2.f(2);;
- : float = -1.24992649718407699e+65

It wasn't really clear from your message how you wanted to handle the boundary
conditions, so I made them periodic.

I hope this helps.

--
Issac Trotts

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

```