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 SAA10822; Tue, 27 Mar 2001 18:22:16 +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 SAA10818 for ; Tue, 27 Mar 2001 18:22:15 +0200 (MET DST) Received: from mailgw1.fhg.de (mailgw1.fhg.de [153.96.1.2]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f2RGMDn05018 for ; Tue, 27 Mar 2001 18:22:13 +0200 (MET DST) Received: from ilt.fhg.de (ilt.ilt.fhg.de [153.96.180.2]) by mailgw1.fhg.de (8.11.1/8.11.1) with ESMTP id f2RGM9h29809 for ; Tue, 27 Mar 2001 18:22:09 +0200 (MET DST) Received: from eent230 (iltpc099.ilt.fhg.de [192.102.148.99]) by ilt.fhg.de (8.9.3/8.9.3) with SMTP id SAA26996 for ; Tue, 27 Mar 2001 18:22:09 +0200 (MET DST) Message-Id: <200103271622.SAA26996@ilt.fhg.de> From: wester@ilt.fhg.de To: caml-list@inria.fr Date: Tue, 27 Mar 2001 18:22:06 +0200 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: [Caml-list] Complex numbers in OCaml X-mailer: Pegasus Mail for Win32 (v3.01d) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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