-
Notifications
You must be signed in to change notification settings - Fork 106
/
batComplex.ml
200 lines (168 loc) · 6.23 KB
/
batComplex.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
(*
* BatComplex - Extended Complex Numbers
* Copyright (C) 2007 Bluestorm <bluestorm dot dylc on-the-server gmail dot com>
* 2008 David Teller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
module BaseComplex = struct
include Complex
let modulo _ _ =
failwith "BatComplex.modulo is meaningless" (*BISECT-VISIT*)
let to_string x =
( string_of_float x.re ) ^ " + i " ^ ( string_of_float x.im )
let pred x = {x with re = x.re -. 1.}
let succ x = {x with re = x.re +. 1.}
let to_int x = int_of_float x.re
let of_int x = {re = float_of_int x; im = 0.}
let to_float x = x.re
let of_float x = {re = x; im = 0.}
let abs x = { re = norm x; im = 0. }
let compare t1 t2 =
match compare t1.re t2.re with
| 0 -> compare t1.im t2.im
| c -> c
let ord = BatOrd.ord compare
let equal t1 t2 =
t1.re = t2.re && t1.im = t2.im
let of_string x =
let fail s =
failwith (Printf.sprintf "BatComplex.of_string %S: %s" x s) in
let open Genlex in
let enum =
BatGenlex.to_enum_filter
( BatGenlex.of_list ["."; "i"; "+"; "-"; "*"] )
( BatString.enum x ) in
let rec parse_re () =
match BatEnum.peek enum with
| None -> fail "the string is empty"
| Some (Int i) -> BatEnum.junk enum; parse_separation (float_of_int i)
| Some (Float f) -> BatEnum.junk enum; parse_separation f
| Some (Kwd "-") -> BatEnum.junk enum; parse_i_im ~multiplier:(-1.) 0.
| Some (Kwd "+") -> BatEnum.junk enum; parse_i_im ~multiplier:1. 0.
| Some _token -> parse_i_im ~multiplier:1. 0.
and parse_separation re =
match BatEnum.get enum with
| None -> {re; im = 0.}
| Some (Kwd "-") -> parse_i_im ~multiplier:(-1.) re
| Some (Kwd "+") -> parse_i_im ~multiplier:1. re
| Some _ -> fail "unexpected token after real part"
and parse_i_im ~multiplier re =
match BatEnum.get enum with
| Some (Kwd "i") -> (
match BatEnum.peek enum with
| None -> {re; im = multiplier}
| Some (Kwd ".")
| Some (Kwd "*") ->
BatEnum.junk enum;
parse_im ~multiplier re
| Some _token ->
parse_im ~multiplier re
)
| _ -> fail "expected \"i\" before the imaginary part"
and parse_im ~multiplier re =
match BatEnum.peek enum with
| Some (Int i) ->
BatEnum.junk enum; parse_end {re; im = multiplier *. float_of_int i}
| Some (Float f) ->
BatEnum.junk enum; parse_end {re; im = multiplier *. f}
| _ -> fail "expected a number for the imaginary part"
and parse_end c =
match BatEnum.peek enum with
| None -> c
| Some _ -> fail "unexpected trailing tokens" in
parse_re ()
end
(* need to fix problem with Functor return type being `type t =
Complex.t` and needing `type t = Complex.t = {re: float; im:float}` *)
module CN = BatNumber.MakeNumeric(BaseComplex)
include BaseComplex
let operations = CN.operations
module Infix = BatNumber.MakeInfix(BaseComplex)
include Infix
module Compare = BatNumber.MakeCompare(BaseComplex)
let inv = Complex.inv
let i = Complex.i
let conj = Complex.conj
let sqrt = Complex.sqrt
let norm2 = Complex.norm2
let norm = Complex.norm
let arg = Complex.arg
let polar = Complex.polar
let exp = Complex.exp
let log = Complex.log
let pow = Complex.pow
let print out t = BatInnerIO.nwrite out (to_string t)
(*$T succ
succ {re = 2.; im = 4.} = {re = 3.; im = 4.}
*)
(*$T pred
pred {re = 2.; im = 4.} = {re = 1.; im = 4.}
*)
(*$T abs
abs {re = 3.; im = 4.} = {re = 5.; im = 0.}
*)
(*$T to_int
to_int {re = 2.; im = 3.} = 2
*)
(*$T of_int
of_int 2 = {re = 2.; im = 0.}
*)
(*$T to_float
to_float {re = 2.; im = 3.} = 2.
*)
(*$T of_float
of_float 2. = {re = 2.; im = 0.}
*)
(*$T compare
compare {re = 2.; im = 3.} {re = 2.; im = 3.} = 0
compare {re = 2.; im = 3.} {re = 3.; im = 2.} = -1
compare {re = 3.; im = 3.} {re = 2.; im = 3.} = 1
compare {re = 3.; im = 4.} {re = 3.; im = 3.} = 1
compare {re = 3.; im = -4.} {re = 3.; im = 3.} = -1
*)
(*$T equal
equal {re = 2.; im = 3.} {re = 2.; im = 3.}
not (equal {re = 2.; im = 3.} {re = 3.; im = 2.})
not (equal {re = 3.; im = 3.} {re = 2.; im = 3.})
not (equal {re = 3.; im = 4.} {re = 3.; im = 3.})
*)
(*$T of_string
of_string "1." = of_float 1.
of_string "-1." = {re = -1.; im = 0.}
of_string "1 + i 2." = {re = 1.; im = 2.}
of_string "1 - i 2." = {re = 1.; im = -2.}
of_string "1 - i 2e3" = {re = 1.; im = -2e3}
of_string "-1. - i -2e3" = {re = -1.; im = 2e3}
of_string "-1+i 2e3" = {re = -1.; im = 2e3}
of_string "-1. - i. -2e3" = {re = -1.; im = 2e3}
of_string "-1. - i * -2e3" = {re = -1.; im = 2e3}
of_string " - i * -2e3" = {re = 0.; im = 2e3}
of_string "+ i * -2e3" = {re = 0.; im = -2e3}
of_string "i * -2e3" = {re = 0.; im = -2e3}
of_string "i" = {re = 0.; im = 1.}
of_string "-i" = {re = 0.; im = -1.}
of_string "1 + i" = {re = 1.; im = 1.}
try ignore (of_string " "); false with Failure _ -> true
try ignore (of_string "("); false with BatGenlex.LexerError _ -> true
try ignore (of_string "1 +"); false with Failure _ -> true
try ignore (of_string "i +"); false with Failure _ -> true
try ignore (of_string "1 + i * 3 4"); false with Failure _ -> true
try ignore (of_string "1 2"); false with Failure _ -> true
*)
(*$T print
BatIO.to_string print {re=3.4; im= -5.6} = "3.4 + i -5.6"
*)