caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Complex numbers in ocaml
@ 2001-09-21 22:25 Post Office!~/sentmail
  2001-09-22 17:59 ` John Max Skaller
  0 siblings, 1 reply; 9+ messages in thread
From: Post Office!~/sentmail @ 2001-09-21 22:25 UTC (permalink / raw)
  To: caml-list

I'm trying to write a system for complex numbers in ocaml for some
numerical work I'm doing, and I thought I'd ask if anyone else has such
code already existant.

Otherwise, given an abstract for numbers
	type number = Int of int | Float of float | Complex of float *
float | Error;;

is there an easy way to coerce numbers back out of type number in order to
operate on them?

Fred Ross

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: [Caml-list] Complex numbers in OCaml
@ 2001-03-27 17:49 Arturo Borquez
  0 siblings, 0 replies; 9+ messages in thread
From: Arturo Borquez @ 2001-03-27 17:49 UTC (permalink / raw)
  To: wester; +Cc: caml-list

On Tue, 27 March 2001, wester@ilt.fhg.de wrote:

> 
> 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) .................

Hi: Rolf:
Here is a little example of an alternative to operate with complex numbers. The key is to define a complex a a duple and define new opertators to do computations, ie +! for addition -! for substraction *! /!...

let (+!) (xr, xi) (yr, yi) = (xr +. yr), (xi +. yi);;
let a = (1.0, 2.0);;
let b = (0.0, -4.75);;
let z = a +! b;;
z;;
val z : float * float = 1, -2.75

The same applies for sub, mpy and div ops.
hope this helps.

Best regards.
Arturo Borquez


Find the best deals on the web at AltaVista Shopping!
http://www.shopping.altavista.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 9+ messages in thread
* [Caml-list] Complex numbers in OCaml
@ 2001-03-27 16:22 wester
  2001-03-27 17:19 ` Markus Mottl
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: wester @ 2001-03-27 16:22 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2001-09-22 17:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-21 22:25 [Caml-list] Complex numbers in ocaml Post Office!~/sentmail
2001-09-22 17:59 ` John Max Skaller
  -- strict thread matches above, loose matches on Subject: below --
2001-03-27 17:49 [Caml-list] Complex numbers in OCaml Arturo Borquez
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).