-
Notifications
You must be signed in to change notification settings - Fork 1
/
complexNum.ml
72 lines (42 loc) · 1.34 KB
/
complexNum.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(* Description: This file creates and implements a way to manipluate complex
numbers in OCaml.*)
module type CNUM =
sig
type t
val define : float -> float -> t
val zero : t
val i : t
val one : t
val add : t -> t -> t
val sub : t -> t -> t
val mul : t -> t -> t
val conj : t -> t
val magn : t -> float
val inv : t -> t
val real : t -> float
val imag : t -> float
end ;;
module CNum : CNUM =
struct
(* re and im are the real and imaginary values *)
type t = { re : float ; im : float }
let define n1 n2 = {re = n1 ; im = n2 }
let zero = {re = 0. ; im = 0. }
let i = {re = 0. ; im = 1. }
let one = {re = 1. ; im = 0. }
let pointwise_op f z1 z2 =
let re1, im1 = z1.re, z1.im in
let re2, im2 = z2.re, z2.im in
{ re = f re1 re2 ; im = f im1 im2 }
let add z1 z2 = pointwise_op (+.) z1 z2
let sub z1 z2 = pointwise_op (-.) z1 z2
let mul z1 z2 =
let re1, im1 = z1.re, z1.im in
let re2, im2 = z2.re, z2.im in
{ re = (re1 *. re2) -. (im1 *. im2) ; im = (re1 *. im2) +. (re2 *. im1) }
let conj z = { re = z.re ; im = -. z.im }
let magn z = (mul z (conj z)).re
let inv z = { re = z.re /. (magn z) ; im = (-. z.im) /. (magn z)}
let real z = z.re
let imag z = z.im
end ;;