From: wester@ilt.fhg.de
To: caml-list@inria.fr
Subject: [Caml-list] Complex numbers in OCaml
Date: Tue, 27 Mar 2001 18:22:06 +0200 [thread overview]
Message-ID: <200103271622.SAA26996@ilt.fhg.de> (raw)
Hi,
I need complex numbers. I tried it this way:
type number = Real of float | Complex of (float*float);;
let add_num a b =
match (a,b) with
(Real a, Real b) -> Real (a +. b)
| (Real a, Complex (bx,by)) -> Complex (a +. bx, by)
| (Complex (ax,ay), Real b) -> Complex (ax +. b, ay)
| (Complex (ax,ay), Complex (bx,by)) -> Complex (ax +. bx, ay +. by);;
let sub_num a b =
match (a,b) with
(Real a, Real b) -> Real (a -. b)
| (Real a, Complex (bx,by)) -> Complex (a -. bx, by)
| (Complex (ax,ay), Real b) -> Complex (ax -. b, ay)
| (Complex (ax,ay), Complex (bx,by)) -> Complex (ax -. bx, ay -. by);;
let mul_num a b =
match (a,b) with
(Real a, Real b) -> Real (a *. b)
| (Real a, Complex (bx,by)) -> Complex (a *. bx, a *. by)
| (Complex (ax,ay), Real b) -> Complex (ax *. b, ay)
| (Complex (ax,ay), Complex (bx,by)) -> Complex (ax *. bx -. ay *. by, ax *. by +. ay *. bx);;
let div_num a b =
match (a,b) with
(Real a, Real b) -> Real (a /. b)
| (Real a, Complex (bx,by)) -> Complex (a *. bx /. (bx**2.0 +. by**2.0),
-. a *. by /. (bx**2.0 +. by**2.0))
| (Complex (ax,ay), Real b) -> Complex (ax /. b, ay /. b)
| (Complex (ax,ay), Complex (bx,by)) -> let n = bx**2.0 +. by**2.0 in
Complex ((ax *. bx +. ay *. by)/.n,
(ay *. bx -. ax *. by)/.n);;
let conj a =
match a with
Real a -> Real a;
| Complex (ax,ay) -> Complex (ax, -. ay);;
let real a =
match a with
Real a -> a
|Complex (ax, ay) -> ax;;
let imag a =
match a with
Real a -> 0.0
|Complex (ax, ay) -> ay;;
div_num (Complex (2.0,3.0)) (Complex (3.0,2.0));;
This works but is quite cumbersome to use. Is there any other way to do it and how can
arrays of complex numbers be implemented efficiently in OCaml?
Thanks in advance.
Rolf Wester
-------------------------------------
Rolf Wester
wester@ilt.fhg.de
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
next reply other threads:[~2001-03-27 16:22 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-03-27 16:22 wester [this message]
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
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=200103271622.SAA26996@ilt.fhg.de \
--to=wester@ilt.fhg.de \
--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