From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA09520; Thu, 29 Mar 2001 16:26:06 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA09522 for ; Thu, 29 Mar 2001 16:26:05 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f2TEQ2926418; Thu, 29 Mar 2001 16:26:03 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA09499; Thu, 29 Mar 2001 16:26:02 +0200 (MET DST) Date: Thu, 29 Mar 2001 16:26:02 +0200 From: Xavier Leroy To: wester@ilt.fhg.de Cc: caml-list@inria.fr Subject: Re: [Caml-list] Complex numbers in OCaml Message-ID: <20010329162602.A9170@pauillac.inria.fr> References: <200103271622.SAA26996@ilt.fhg.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <200103271622.SAA26996@ilt.fhg.de>; from wester@ilt.fhg.de on Tue, Mar 27, 2001 at 06:22:06PM +0200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > I need complex numbers. I tried it this way: > type number = Real of float | Complex of (float*float);; > [...] > This works but is quite cumbersome to use. > Is there any other way to do it Unless you have strong reasons for doing so, I'd advise against the mixed-mode arithmetic (i.e. having a special case for Real): it makes operations slower and more complex. In terms of performance, the best representation is a record type: type complex = { re: float; im: float } since the compiler will then unbox the two floats. This would not be the case for a pair of floats: type complex = float * float The concrete syntax is a bit heavy, e.g. {re = 1.0; im = 0.0}, but at least highly non-ambiguous :-) If you're desperate for a more compact notation, Camlp4 could help. One last performance hint: write b *. b rather than b ** 2.0, it's much, much faster. > and how can arrays of complex numbers be implemented efficiently in OCaml? Just regular arrays of complex numbers, e.g. the type "complex array", should be reasonably efficient, although you pay one extra indirection compared with the equivalent C or Fortran array. As Markus mentioned, I'm considering extending the Bigarray module to handle complex numbers as well. This would help interfacing with Fortran, and provide essentially the same data representation than in C and Fortran. - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr