ef8a0fd2c26b7b8c574a94378155cffda2669b41
[Faustine.git] / interpretor / basic.ml
1 (**
2 Module: Basic
3 Description: basic data type in the vectorial faust interpreter.
4 @author WANG Haisheng
5 Created: 31/05/2013 Modified: 17/07/2013
6 *)
7
8 open Types;;
9 open Aux;;
10
11 exception Convert_Error of string;;
12 exception Basic_operation of string;;
13
14
15 (* MACRO *)
16
17 let faust_max = 2147483647;;
18 let faust_min = -2147483648;;
19 let faust_bits = 32;;
20
21 (* Functional operations *)
22
23 let fun_unary oper f = fun x -> oper (f x);;
24 let fun_binary oper f g = fun x -> oper (f x) (g x);;
25 let fun_ternary oper f g h = fun x -> oper (f x) (g x) (h x);;
26
27 (* basic operations *)
28
29 let memorize : int -> (index -> basic) -> (index -> basic) =
30 fun size ->
31 fun vec ->
32 let memory = Array.create size Error in
33 let filled = Array.create size false in
34 let vec_mem : index -> basic =
35 fun i ->
36 if i >= 0 && i < size then (
37 if filled.(i) then
38 memory.(i)
39 else
40 let result = vec i in
41 let () = memory.(i) <- result in
42 let () = filled.(i) <- true in
43 result)
44 else raise (Invalid_argument "vector overflow.") in
45 vec_mem;;
46
47 class vector : int -> (index -> basic) -> vector_type =
48 fun (size_init : int) ->
49 fun (vec_init : index -> basic) ->
50 object
51 val s = size_init
52 val vec = memorize size_init vec_init
53 method size = s
54 method nth = vec
55 end;;
56
57 let rec basic_to_int : basic -> int =
58 fun v ->
59 match v with
60 |N i -> i
61 |R f -> int_of_float f
62 |Vec vec ->
63 raise (Convert_Error "basic_to_int : vector.")
64 |Zero -> 0
65 |Error -> raise (Convert_Error "basic_to_int : Error");;
66
67
68 let basic_to_float : basic -> float =
69 fun v ->
70 match v with
71 |N i -> float_of_int i
72 |R f -> f
73 |Vec vec ->
74 raise (Convert_Error "basic_to_float : vector.")
75 |Zero -> 0.
76 |Error -> 0.;;
77
78
79 let basic_to_float_array : basic -> float array =
80 fun v ->
81 match v with
82 |Vec vec ->
83 let basics : basic array =
84 Array.init vec#size vec#nth in
85 Array.map basic_to_float basics
86 |_ -> [| (basic_to_float v)|];;
87
88
89 let rec basic_to_string : basic -> string =
90 fun (v : basic) ->
91 match v with
92 |N i1 -> string_of_int i1
93 |R f1 -> string_of_float f1
94 |Vec vec ->
95 let basics : basic array =
96 Array.init vec#size vec#nth in
97 let strings = Array.to_list
98 (Array.map basic_to_string basics) in
99 String.concat "," strings
100 |Zero -> "0"
101 |Error -> "0";;
102
103 let basic_of_float : float -> basic = fun f -> R f;;
104
105 let rec basic_of_float_array : float array -> basic =
106 fun (data : float array) ->
107 let n = Array.length data in
108 if n = 0 then
109 raise (Convert_Error "basic_of_float_array : empty.")
110 else if n = 1 then basic_of_float data.(0)
111 else
112 let vec = Array.get (Array.map basic_of_float data) in
113 Vec (new vector n vec);;
114
115 let basic_to_bool : basic -> bool =
116 fun b ->
117 match b with
118 | N i ->
119 if i = 1 then true
120 else if i = 0 then false
121 else raise (Convert_Error "basic_to_bool : only for 0 or 1.")
122 | Zero -> false
123 | _ -> raise (Convert_Error "basic_to_bool : only for 0 or 1.");;
124
125 (* VALUE OPERATIONS *)
126
127 let rec basic_normalize : basic -> basic =
128 fun b ->
129 let n = 2. ** float_of_int (faust_bits) in
130 match b with
131 |N i ->
132 if i > faust_max then
133 N (i - int_of_float
134 (n *. floor (((float_of_int i) +. n/.2.)/.n)))
135 else if i < faust_min then
136 N (i + int_of_float
137 (n *. floor ((n/.2. -. (float_of_int i) -. 1.)/.n)))
138 else N i
139 |R f ->
140 if f > float_of_int (faust_max) then
141 R (f -. (n *. floor ((f +. n/.2.)/.n)))
142 else if f < float_of_int (faust_min) then
143 R (f +. (n *. floor ((n/.2. -. f -. 1.)/.n)))
144 else R f
145 |Vec vec ->
146 Vec (new vector vec#size
147 (fun_unary basic_normalize vec#nth))
148 |Zero -> Zero
149 |Error -> Error;;
150
151
152 let rec basic_add : basic -> basic -> basic =
153 fun b1 -> fun b2 ->
154 match (b1, b2) with
155 | (Zero, _) -> b2
156 | (_, Zero) -> b1
157
158 | (Vec vec1, Vec vec2) ->
159 if vec1#size = vec2#size then
160 Vec (new vector vec1#size
161 (fun_binary basic_add vec1#nth vec2#nth))
162 else raise (Basic_operation "vector size not matched.")
163 | (Vec vec1, _) -> raise (Basic_operation "vec1 +~ sca2")
164
165 | (N i1, N i2) -> basic_normalize (N (i1 + i2))
166 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) +. f2))
167 | (N i1, Vec vec2) -> raise (Basic_operation "i1 +~ vec2")
168 | (N i1, Error) -> Error
169
170 | (R f1, N i2) -> basic_normalize (R (f1 +. (float_of_int i2)))
171 | (R f1, R f2) -> basic_normalize (R (f1 +. f2))
172 | (R f1, Vec vec2) -> raise (Basic_operation "f1 +~ vec2")
173 | (R f1, Error) -> Error
174
175 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
176 | (Error, _) -> Error;;
177
178
179 let (+~) b1 b2 = basic_add b1 b2;;
180
181
182 let rec basic_neg : basic -> basic =
183 fun b ->
184 match b with
185 |N i -> N (-i)
186 |R f -> R (-.f)
187 |Vec vec -> Vec (new vector vec#size (fun_unary basic_neg vec#nth))
188 |Zero -> Zero
189 |Error -> Error;;
190
191
192 let basic_sub : basic -> basic -> basic =
193 fun b1 ->
194 fun b2 ->
195 basic_add b1 (basic_neg b2);;
196
197
198 let (-~) b1 b2 = basic_sub b1 b2;;
199
200
201 let rec basic_mul : basic -> basic -> basic =
202 fun b1 ->
203 fun b2 ->
204 match (b1, b2) with
205 | (Vec vec1, Vec vec2) ->
206 if vec1#size = vec2#size then
207 Vec (new vector vec1#size
208 (fun_binary basic_mul vec1#nth vec2#nth))
209 else raise (Basic_operation "vector size not matched.")
210 | (Vec vec1, Zero) ->
211 Vec (new vector vec1#size
212 (fun_unary (basic_mul Zero) vec1#nth))
213 | (Vec vec1, _) -> raise (Basic_operation "vec1 *~ sca2")
214
215 | (N i1, N i2) -> basic_normalize (N (i1 * i2))
216 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) *. f2))
217 | (N i1, Vec vec2) -> raise (Basic_operation "i1 *~ vec2")
218 | (N i1, Zero) -> N 0
219 | (N i1, Error) -> Error
220
221 | (R f1, N i2) -> basic_normalize (R (f1 *. (float_of_int i2)))
222 | (R f1, R f2) -> basic_normalize (R (f1 *. f2))
223 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
224 | (R f1, Zero) -> R 0.
225 | (R f1, Error) -> Error
226
227 | (Zero, N i2) -> N 0
228 | (Zero, R f2) -> R 0.
229 | (Zero, Vec vec2) ->
230 Vec (new vector vec2#size
231 (fun i -> basic_mul Zero (vec2#nth i)))
232 | (Zero, Zero) -> Zero
233 | (Zero, Error) -> Error
234
235 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
236 | (Error, _) -> Error;;
237
238
239 let ( *~ ) b1 b2 = basic_mul b1 b2;;
240
241
242 let rec basic_recip : basic -> basic =
243 fun v ->
244 match v with
245 |N i -> basic_recip (R (float_of_int i))
246 |R f -> if f = 0. then Error else R (1./.f)
247 |Vec vec -> Vec (new vector vec#size
248 (fun_unary basic_recip vec#nth))
249 |Zero -> Error
250 |Error -> R 0.;;
251
252
253 let basic_div : basic -> basic -> basic =
254 fun b1 ->
255 fun b2 ->
256 basic_mul b1 (basic_recip b2);;
257
258
259 let (/~) b1 b2 = basic_div b1 b2;;
260
261
262 let rec basic_zero : basic -> basic =
263 fun v ->
264 match v with
265 |N i -> N 0
266 |R f -> R 0.
267 |Vec vec -> Vec (new vector vec#size
268 (fun_unary basic_zero vec#nth))
269 |Zero -> Zero
270 |Error -> R 0.;;
271
272
273 let rec basic_power : basic -> basic -> basic =
274 fun b1 ->
275 fun b2 ->
276 match (b1, b2) with
277 | (Vec vec1, Vec vec2) ->
278 if vec1#size = vec2#size then
279 Vec (new vector vec1#size
280 (fun_binary basic_power vec1#nth vec2#nth))
281 else raise (Basic_operation "vector size not matched.")
282 | (Vec vec1, Zero) ->
283 let vec_zeros = Vec (new vector vec1#size (fun i -> Zero)) in
284 basic_power b1 vec_zeros
285 | (Vec vec1, _) -> raise (Basic_operation "vec1 *~ sca2")
286
287 | (N i1, _) -> basic_power (R (float_of_int i1)) b2
288
289 | (R f1, N i2) -> basic_power b1 (R (float_of_int i2))
290 | (R f1, R f2) -> basic_normalize (R (f1 ** f2))
291 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
292 | (R f1, Zero) -> R 1.
293 | (R f1, Error) -> Error
294
295 | (Zero, N i2) -> basic_power b1 (R (float_of_int i2))
296 | (Zero, R f2) -> R 0.
297 | (Zero, Vec vec2) ->
298 let vec_zeros = Vec (new vector vec2#size (fun i -> Zero)) in
299 basic_power vec_zeros b2
300 | (Zero, Zero) -> basic_power (R 0.) (R 0.)
301 | (Zero, Error) -> Error
302
303 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
304 | (Error, _) -> Error;;
305
306
307 let rec basic_logic :
308 (bool -> bool -> bool) -> basic -> basic -> basic =
309 fun oper -> fun b1 -> fun b2 ->
310 match (b1, b2) with
311 | (Vec vec1, Vec vec2) ->
312 if vec1#size = vec2#size then
313 Vec (new vector vec1#size
314 (fun_binary (basic_logic oper) vec1#nth vec2#nth))
315 else raise (Basic_operation "vector size not matched.")
316 | (Vec vec1, Zero) ->
317 let vec_zeros = Vec (new vector vec1#size (fun i -> Zero)) in
318 basic_and b1 vec_zeros
319 | (Vec vec1, _) -> raise (Basic_operation "vec1 logic sca2")
320
321 | (N i1, N i2) -> oper (basic_to_bool b1) (basic_to_bool b2)
322 | (N i1, R f2) ->
323 raise (Basic_operation "Float shouldn't be in logical oper.")
324 | (N i1, Vec vec2) -> raise (Basic_operation "f1 logic vec2")
325 | (N i1, Zero) -> basic_logic oper b1 (N 0)
326 | (N i1, Error) -> Error
327
328 | (R f1, _) ->
329 raise (Basic_operation "Float shouldn't be in logical oper.")
330
331 | (Zero, N i2) -> basic_logic oper (N 0) b2
332 | (Zero, R f2) ->
333 raise (Basic_operation "Float shouldn't be in logical oper.")
334 | (Zero, Vec vec2) ->
335 let vec_zeros = Vec (new vector vec2#size (fun i -> Zero)) in
336 basic_logic oper vec_zeros b2
337 | (Zero, Zero) -> basic_logic oper (N 0) (N 0)
338 | (Zero, Error) -> Error
339
340 | (Error, Vec vec2) -> raise (Basic_operation "Error logic vec2")
341 | (Error, _) -> Error;;
342
343 let basic_and = basic_logic (&&);;
344 let basic_or = basic_logic (||);;
345 let basic_xor = basic_logic xor;;
346
347 let rec basic_adjust : (float -> float) -> basic -> basic =
348 fun oper -> fun b ->
349 match b with
350 |N i -> R (float_of_int i)
351 |R f -> R (floor f)
352 |Vec vec -> Vec (new vector vec#size
353 (fun_unary basic_floor vec#nth))
354 |Zero -> R 0.
355 |Error -> Error;;
356
357 let basic_floor = basic_adjust floor;;
358 let basic_ceil = basic_adjust ceil;;
359 let basic_rint = basic_adjust rint;;
360
361 let rec basic_int : basic -> basic =
362 fun b ->
363 match b with
364 |N i -> b
365 |R f -> N (int_of_float f)
366 |Vec vec -> Vec (new vector vec#size
367 (fun_unary basic_int vec#nth))
368 |Zero -> N 0
369 |Error -> Error;;
370
371 let rec basic_float : basic -> basic =
372 fun b ->
373 match b with
374 | N i -> R (float_of_int i)
375 | R f -> b
376 | Vec vec -> Vec (new vector vec#size
377 (fun_unary basic_float vec#nth))
378 | Zero -> R 0.
379 | Error -> Error;;
380
381
382 let rec basic_unary : (float -> float) -> basic -> basic =
383 fun oper ->
384 fun b ->
385 match b with
386 |N i -> R (oper (float_of_int i))
387 |R f -> R (oper f)
388 |Vec vec -> Vec (new vector vec#size
389 (fun_unary (basic_unary oper) vec#nth))
390 |Zero -> R (oper 0.)
391 |Error -> Error;;
392
393
394 let basic_sin : basic -> basic = basic_unary sin;;
395 let basic_cos : basic -> basic = basic_unary cos;;
396 let basic_atan : basic -> basic = basic_unary atan;;
397
398
399 let rec basic_atan2 : basic -> basic -> basic =
400 fun v1 ->
401 fun v2 ->
402 match (v1, v2) with
403 | (N i1, N i2) -> basic_atan2
404 (R (float_of_int i1)) (R (float_of_int i2))
405 | (N i1, R f2) -> basic_atan2 (R (float_of_int i1)) v2
406 | (N i1, Zero) -> basic_atan2 (R (float_of_int i1)) (R 0.)
407 | (N i1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
408 | (N i1, Error) -> Error
409
410 | (R f1, N i2) -> basic_atan2 v1 (R (float_of_int i2))
411 | (R f1, R f2) -> R (atan2 f1 f2)
412 | (R f1, Zero) -> basic_atan2 v1 (R 0.)
413 | (R f1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
414 | (R f1, Error) -> Error
415
416 | (Vec vec1, Vec vec2) -> Vec (new vector vec1#size
417 (fun_binary basic_atan2 vec1#nth vec2#nth))
418 | (Vec vec1, Zero) -> Vec (new vector vec1#size
419 (fun i -> basic_atan2 (vec1#nth i) Zero))
420 | (Vec vec1, _) -> raise (Basic_operation "atan2 vec sca.")
421
422 | (Zero, N i2) -> basic_atan2 (R 0.) (R (float_of_int i2))
423 | (Zero, R f2) -> basic_atan2 (R 0.) v2
424 | (Zero, Vec vec2) -> Vec (new vector vec2#size
425 (fun_unary (basic_atan2 Zero) vec2#nth))
426 | (Zero, Zero) -> basic_atan2 (R 0.) (R 0.)
427 | (Zero, Error) -> Error
428
429 | (Error, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
430 | (Error, _) -> Error;;
431
432
433 let rec basic_sqrt v = match v with
434 |N i ->
435 if i >= 0 then R (sqrt (float_of_int i))
436 else raise (Basic_operation "sqrt parameter < 0.")
437 |R f ->
438 if f >= 0. then R (sqrt f)
439 else raise (Basic_operation "sqrt parameter < 0.")
440 |Vec vec -> Vec (new vector vec#size (fun_unary basic_sqrt vec#nth))
441 |Zero -> R (sqrt 0.)
442 |Error -> Error;;
443
444
445 let rec basic_mod : basic -> basic -> basic =
446 fun b1 ->
447 fun b2 ->
448 match (b1, b2) with
449 | (N i1, N i2) -> N (i1 mod i2)
450 | (_, R f2) ->
451 raise (Basic_operation "b1 mod b2: b2 cannot be float.")
452 | (R f1, _) ->
453 raise (Basic_operation "b1 mod b2: b1 cannot be float.")
454 | (N i1, Vec vec2) ->
455 raise (Basic_operation "Scalar_Vector: sca mod vec.")
456 | (_, Zero) ->
457 raise (Basic_operation "b1 mod b2: b2 cannot be zero.")
458 | (N i1, Error) -> Error
459 | (Vec vec1, Vec vec2) ->
460 if vec1#size = vec2#size then
461 Vec (new vector vec1#size
462 (fun_binary basic_mod vec1#nth vec2#nth))
463 else raise (Basic_operation "vector size not matched.")
464 | (Vec vec1, _) ->
465 raise (Basic_operation "Vector_Scalar: vec mod sca.")
466 | (Zero, Vec vec2) ->
467 basic_mod (Vec (new vector vec2#size (fun i -> Zero))) b2
468 | (Zero, _) -> basic_mod (N 0) b2
469 | (Error, Vec vec2) ->
470 raise (Basic_operation "Scalar_Vector: sca mod vec.")
471 | (Error, _) -> Error;;
472
473 let rec basic_fmod : basic -> basic -> basic =
474 fun b1 ->
475 fun b2 ->
476 match (b1, b2) with
477 | (R f1, R f2) -> R (mod_float f1 f2)
478 | (_, N i2) ->
479 raise (Basic_operation "b1 fmod b2: b2 cannot be int.")
480 | (N i1, _) ->
481 raise (Basic_operation "b1 fmod b2: b1 cannot be int.")
482 | (R f1, Vec vec2) ->
483 raise (Basic_operation "Scalar_Vector: sca fmod vec.")
484 | (_, Zero) ->
485 raise (Basic_operation "b1 fmod b2: b2 cannot be zero.")
486 | (R f1, Error) -> Error
487 | (Vec vec1, Vec vec2) ->
488 if vec1#size = vec2#size then
489 Vec (new vector vec1#size
490 (fun_binary basic_fmod vec1#nth vec2#nth))
491 else raise (Basic_operation "vector size not matched.")
492 | (Vec vec1, _) ->
493 raise (Basic_operation "Vector_Scalaire: vec fmod sca.")
494 | (Zero, Vec vec2) ->
495 basic_fmod (Vec (new vector vec2#size (fun i -> Zero))) b2
496 | (Zero, _) -> basic_mod (N 0) b2
497 | (Error, Vec vec2) ->
498 raise (Basic_operation "Scalaire_Vector: int mod vec.")
499 | (Error, _) -> Error;;
500
501
502 let rec basic_larger_than_zero : basic -> basic =
503 fun v ->
504 match v with
505 |N i -> if i > 0 then N 1 else N 0
506 |R f -> if f > 0. then N 1 else N 0
507 |Vec vec ->
508 Vec (new vector vec#size
509 (fun_unary basic_larger_than_zero vec#nth ))
510 |Zero -> N 0
511 |Error -> Error;;
512
513
514 let basic_larger : basic -> basic -> basic =
515 fun b1 ->
516 fun b2 ->
517 basic_larger_than_zero (b1 -~ b2);;
518
519
520 let basic_smaller : basic -> basic -> basic =
521 fun b1 ->
522 fun b2 ->
523 basic_larger_than_zero (b2 -~ b1);;
524
525
526 let basic_max : basic -> basic -> basic =
527 fun b1 ->
528 fun b2 ->
529 let compare = basic_larger_than_zero (b1 -~ b2) in
530 match compare with
531 | N i ->
532 if i = 1 then b1
533 else if i = 0 then b2
534 else raise (Basic_operation "compare result not bool.")
535 | Vec vec ->
536 let basics = Array.init vec#size vec#nth in
537 let sum = basic_to_int (Array.fold_left basic_add Zero basics) in
538 if sum = vec#size then b1
539 else if sum = 0 then b2
540 else Error
541 | Error -> Error
542 | _ -> raise (Basic_operation "compare result not bool.");;
543
544
545 let basic_min : basic -> basic -> basic =
546 fun b1 ->
547 fun b2 ->
548 let compare = basic_larger_than_zero (b1 -~ b2) in
549 match compare with
550 | N i ->
551 if i = 1 then b2
552 else if i = 0 then b1
553 else raise (Basic_operation "compare result not bool.")
554 | Vec vec ->
555 let basics = Array.init vec#size vec#nth in
556 let sum = basic_to_int (Array.fold_left basic_add Zero basics) in
557 if sum = vec#size then b2
558 else if sum = 0 then b1
559 else Error
560 | Error -> Error
561 | _ -> raise (Basic_operation "compare result not bool.");;