From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: wester@ilt.fhg.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Complex numbers in OCaml
Date: Thu, 29 Mar 2001 16:26:02 +0200 [thread overview]
Message-ID: <20010329162602.A9170@pauillac.inria.fr> (raw)
In-Reply-To: <200103271622.SAA26996@ilt.fhg.de>; from wester@ilt.fhg.de on Tue, Mar 27, 2001 at 06:22:06PM +0200
> 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
next prev parent reply other threads:[~2001-03-29 14:26 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-03-27 16:22 wester
2001-03-27 17:19 ` Markus Mottl
2001-03-27 17:48 ` Jan Skibinski
2001-03-27 19:25 ` Markus Mottl
2001-03-28 1:04 ` Michael Hohn
2001-03-29 14:26 ` Xavier Leroy [this message]
2001-03-27 17:49 Arturo Borquez
2001-09-21 22:25 [Caml-list] Complex numbers in ocaml Post Office!~/sentmail
2001-09-22 17:59 ` John Max Skaller
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=20010329162602.A9170@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@inria.fr \
--cc=wester@ilt.fhg.de \
/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