From: Issac Trotts <ijtrotts@ucdavis.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Fortran-like array indexing / PDE numerical code with boundary cells in OCAML
Date: Sat, 27 Dec 2003 01:42:18 -0800 [thread overview]
Message-ID: <200312270142.18979.ijtrotts@ucdavis.edu> (raw)
In-Reply-To: <20031226054132.53C8780176@server2.messagingengine.com>
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:
#load "bigarray.cma";;
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
prev parent reply other threads:[~2003-12-27 9:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-26 5:41 Grégory Guyomarc'h
2003-12-27 9:42 ` Issac Trotts [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200312270142.18979.ijtrotts@ucdavis.edu \
--to=ijtrotts@ucdavis.edu \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox