3 Description: basic data type in the vectorial faust interpreter.
5 Created: 31/05/2013 Modified: 17/07/2013
11 exception Convert_Error of string;;
12 exception Basic_operation of string;;
17 let faust_max = 2147483647;;
18 let faust_min = -2147483648;;
21 (* Functional operations *)
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);;
27 (* basic operations *)
29 let memorize : int -> (index -> basic) -> (index -> basic) =
32 let memory = Array.create size Error in
33 let filled = Array.create size false in
34 let vec_mem : index -> basic =
36 if i >= 0 && i < size then (
41 let () = memory.(i) <- result in
42 let () = filled.(i) <- true in
44 else raise (Invalid_argument "vector overflow.") in
47 class vector : int -> (index -> basic) -> vector_type =
48 fun (size_init : int) ->
49 fun (vec_init : index -> basic) ->
52 val vec = memorize size_init vec_init
57 let rec basic_to_int : basic -> int =
61 |R f -> int_of_float f
63 raise (Convert_Error "basic_to_int : vector.")
65 |Error -> raise (Convert_Error "basic_to_int : Error");;
68 let basic_to_float : basic -> float =
71 |N i -> float_of_int i
74 raise (Convert_Error "basic_to_float : vector.")
79 let basic_to_float_array : basic -> float array =
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)|];;
89 let rec basic_to_string : basic -> string =
92 |N i1 -> string_of_int i1
93 |R f1 -> string_of_float f1
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
103 let basic_of_float : float -> basic = fun f -> R f;;
105 let rec basic_of_float_array : float array -> basic =
106 fun (data : float array) ->
107 let n = Array.length data in
109 raise (Convert_Error "basic_of_float_array : empty.")
110 else if n = 1 then basic_of_float data.(0)
112 let vec = Array.get (Array.map basic_of_float data) in
113 Vec (new vector n vec);;
115 let basic_to_bool : basic -> bool =
120 else if i = 0 then false
121 else raise (Convert_Error "basic_to_bool : only for 0 or 1.")
123 | _ -> raise (Convert_Error "basic_to_bool : only for 0 or 1.");;
125 let basic_of_bool : bool -> basic =
126 fun tof -> if tof then N 1 else N 0;;
129 (* VALUE OPERATIONS *)
131 let rec basic_normalize : basic -> basic =
133 let n = 2. ** float_of_int (faust_bits) in
136 if i > faust_max then
138 (n *. floor (((float_of_int i) +. n/.2.)/.n)))
139 else if i < faust_min then
141 (n *. floor ((n/.2. -. (float_of_int i) -. 1.)/.n)))
144 if f > float_of_int (faust_max) then
145 R (f -. (n *. floor ((f +. n/.2.)/.n)))
146 else if f < float_of_int (faust_min) then
147 R (f +. (n *. floor ((n/.2. -. f -. 1.)/.n)))
150 Vec (new vector vec#size
151 (fun_unary basic_normalize vec#nth))
156 let rec basic_add : basic -> basic -> basic =
161 | (Vec vec1, Vec vec2) ->
162 if vec1#size = vec2#size then
163 Vec (new vector vec1#size
164 (fun_binary basic_add vec1#nth vec2#nth))
165 else raise (Basic_operation "vector size not matched.")
166 | (Vec vec1, _) -> raise (Basic_operation "vec1 +~ sca2")
167 | (N i1, N i2) -> basic_normalize (N (i1 + i2))
168 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) +. f2))
169 | (N i1, Vec vec2) -> raise (Basic_operation "i1 +~ vec2")
170 | (N i1, Error) -> Error
171 | (R f1, N i2) -> basic_normalize (R (f1 +. (float_of_int i2)))
172 | (R f1, R f2) -> basic_normalize (R (f1 +. f2))
173 | (R f1, Vec vec2) -> raise (Basic_operation "f1 +~ vec2")
174 | (R f1, Error) -> Error
175 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
176 | (Error, _) -> Error;;
179 let (+~) b1 b2 = basic_add b1 b2;;
182 let rec basic_neg : basic -> basic =
187 |Vec vec -> Vec (new vector vec#size (fun_unary basic_neg vec#nth))
192 let basic_sub : basic -> basic -> basic =
195 basic_add b1 (basic_neg b2);;
198 let (-~) b1 b2 = basic_sub b1 b2;;
201 let rec basic_mul : basic -> basic -> basic =
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 | (N i1, N i2) -> basic_normalize (N (i1 * i2))
215 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) *. f2))
216 | (N i1, Vec vec2) -> raise (Basic_operation "i1 *~ vec2")
217 | (N i1, Zero) -> N 0
218 | (N i1, Error) -> Error
219 | (R f1, N i2) -> basic_normalize (R (f1 *. (float_of_int i2)))
220 | (R f1, R f2) -> basic_normalize (R (f1 *. f2))
221 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
222 | (R f1, Zero) -> R 0.
223 | (R f1, Error) -> Error
224 | (Zero, N i2) -> N 0
225 | (Zero, R f2) -> R 0.
226 | (Zero, Vec vec2) ->
227 Vec (new vector vec2#size
228 (fun i -> basic_mul Zero (vec2#nth i)))
229 | (Zero, Zero) -> Zero
230 | (Zero, Error) -> Error
231 | (Error, Vec vec2) -> raise (Basic_operation "Error *~ vec2")
232 | (Error, _) -> Error;;
235 let ( *~ ) b1 b2 = basic_mul b1 b2;;
238 let rec basic_recip : basic -> basic =
241 |N i -> basic_recip (R (float_of_int i))
242 |R f -> if f = 0. then Error else R (1./.f)
243 |Vec vec -> Vec (new vector vec#size
244 (fun_unary basic_recip vec#nth))
249 let basic_div : basic -> basic -> basic =
252 basic_mul b1 (basic_recip b2);;
255 let (/~) b1 b2 = basic_div b1 b2;;
258 let rec basic_zero : basic -> basic =
263 |Vec vec -> Vec (new vector vec#size
264 (fun_unary basic_zero vec#nth))
269 let rec basic_power : basic -> basic -> basic =
273 | (Vec vec1, Vec vec2) ->
274 if vec1#size = vec2#size then
275 Vec (new vector vec1#size
276 (fun_binary basic_power vec1#nth vec2#nth))
277 else raise (Basic_operation "vector size not matched.")
278 | (Vec vec1, Zero) ->
279 let vec_zeros = Vec (new vector vec1#size (fun i -> Zero)) in
280 basic_power b1 vec_zeros
281 | (Vec vec1, _) -> raise (Basic_operation "vec1 *~ sca2")
282 | (N i1, _) -> basic_power (R (float_of_int i1)) b2
283 | (R f1, N i2) -> basic_power b1 (R (float_of_int i2))
284 | (R f1, R f2) -> basic_normalize (R (f1 ** f2))
285 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
286 | (R f1, Zero) -> R 1.
287 | (R f1, Error) -> Error
288 | (Zero, N i2) -> basic_power b1 (R (float_of_int i2))
289 | (Zero, R f2) -> R 0.
290 | (Zero, Vec vec2) ->
291 let vec_zeros = Vec (new vector vec2#size (fun i -> Zero)) in
292 basic_power vec_zeros b2
293 | (Zero, Zero) -> basic_power (R 0.) (R 0.)
294 | (Zero, Error) -> Error
295 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
296 | (Error, _) -> Error;;
299 let rec basic_logic :
300 (bool -> bool -> bool) -> basic -> basic -> basic =
301 fun oper -> fun b1 -> fun b2 ->
303 | (Vec vec1, Vec vec2) ->
304 if vec1#size = vec2#size then
305 Vec (new vector vec1#size
306 (fun_binary (basic_logic oper) vec1#nth vec2#nth))
307 else raise (Basic_operation "vector size not matched.")
308 | (Vec vec1, Zero) ->
309 let vec_zeros = Vec (new vector vec1#size (fun i -> Zero)) in
310 basic_logic oper b1 vec_zeros
311 | (Vec vec1, _) -> raise (Basic_operation "vec1 logic sca2")
312 | (N i1, N i2) -> basic_of_bool (oper (basic_to_bool b1)
315 raise (Basic_operation "Float shouldn't be in logical oper.")
316 | (N i1, Vec vec2) -> raise (Basic_operation "f1 logic vec2")
317 | (N i1, Zero) -> basic_logic oper b1 (N 0)
318 | (N i1, Error) -> Error
320 raise (Basic_operation "Float shouldn't be in logical oper.")
321 | (Zero, N i2) -> basic_logic oper (N 0) b2
323 raise (Basic_operation "Float shouldn't be in logical oper.")
324 | (Zero, Vec vec2) ->
325 let vec_zeros = Vec (new vector vec2#size (fun i -> Zero)) in
326 basic_logic oper vec_zeros b2
327 | (Zero, Zero) -> basic_logic oper (N 0) (N 0)
328 | (Zero, Error) -> Error
329 | (Error, Vec vec2) -> raise (Basic_operation "Error logic vec2")
330 | (Error, _) -> Error;;
332 let basic_and = basic_logic (&&);;
333 let basic_or = basic_logic (||);;
334 let basic_xor = basic_logic xor;;
336 let rec basic_adjust : (float -> float) -> basic -> basic =
339 |N i -> R (float_of_int i)
341 |Vec vec -> Vec (new vector vec#size
342 (fun_unary (basic_adjust oper) vec#nth))
346 let basic_floor = basic_adjust floor;;
347 let basic_ceil = basic_adjust ceil;;
348 let basic_rint = basic_adjust rint;;
350 let rec basic_int : basic -> basic =
354 |R f -> N (int_of_float f)
355 |Vec vec -> Vec (new vector vec#size
356 (fun_unary basic_int vec#nth))
360 let rec basic_float : basic -> basic =
363 | N i -> R (float_of_int i)
365 | Vec vec -> Vec (new vector vec#size
366 (fun_unary basic_float vec#nth))
370 let rec basic_abs : basic -> basic =
374 | R f -> R (abs_float f)
375 | Vec vec -> Vec (new vector vec#size
376 (fun_unary basic_abs vec#nth))
381 let rec basic_unary : (float -> float) -> basic -> basic =
385 |N i -> R (oper (float_of_int i))
387 |Vec vec -> Vec (new vector vec#size
388 (fun_unary (basic_unary oper) vec#nth))
392 let basic_sin : basic -> basic = basic_unary sin;;
393 let basic_asin : basic -> basic = basic_unary asin;;
394 let basic_cos : basic -> basic = basic_unary cos;;
395 let basic_acos : basic -> basic = basic_unary acos;;
396 let basic_tan : basic -> basic = basic_unary tan;;
397 let basic_atan : basic -> basic = basic_unary atan;;
398 let basic_exp : basic -> basic = basic_unary exp;;
399 let basic_ln : basic -> basic = basic_unary log;;
400 let basic_lg : basic -> basic = basic_unary log10;;
402 let rec basic_atan2 : basic -> basic -> basic =
406 | (N i1, N i2) -> basic_atan2
407 (R (float_of_int i1)) (R (float_of_int i2))
408 | (N i1, R f2) -> basic_atan2 (R (float_of_int i1)) v2
409 | (N i1, Zero) -> basic_atan2 (R (float_of_int i1)) (R 0.)
410 | (N i1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
411 | (N i1, Error) -> Error
413 | (R f1, N i2) -> basic_atan2 v1 (R (float_of_int i2))
414 | (R f1, R f2) -> R (atan2 f1 f2)
415 | (R f1, Zero) -> basic_atan2 v1 (R 0.)
416 | (R f1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
417 | (R f1, Error) -> Error
419 | (Vec vec1, Vec vec2) -> Vec (new vector vec1#size
420 (fun_binary basic_atan2 vec1#nth vec2#nth))
421 | (Vec vec1, Zero) -> Vec (new vector vec1#size
422 (fun i -> basic_atan2 (vec1#nth i) Zero))
423 | (Vec vec1, _) -> raise (Basic_operation "atan2 vec sca.")
425 | (Zero, N i2) -> basic_atan2 (R 0.) (R (float_of_int i2))
426 | (Zero, R f2) -> basic_atan2 (R 0.) v2
427 | (Zero, Vec vec2) -> Vec (new vector vec2#size
428 (fun_unary (basic_atan2 Zero) vec2#nth))
429 | (Zero, Zero) -> basic_atan2 (R 0.) (R 0.)
430 | (Zero, Error) -> Error
432 | (Error, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
433 | (Error, _) -> Error;;
436 let rec basic_sqrt v = match v with
438 if i >= 0 then R (sqrt (float_of_int i))
439 else raise (Basic_operation "sqrt parameter < 0.")
441 if f >= 0. then R (sqrt f)
442 else raise (Basic_operation "sqrt parameter < 0.")
443 |Vec vec -> Vec (new vector vec#size (fun_unary basic_sqrt vec#nth))
448 let rec basic_mod : basic -> basic -> basic =
452 | (N i1, N i2) -> N (i1 mod i2)
454 raise (Basic_operation "b1 mod b2: b2 cannot be float.")
456 raise (Basic_operation "b1 mod b2: b1 cannot be float.")
457 | (N i1, Vec vec2) ->
458 raise (Basic_operation "Scalar_Vector: sca mod vec.")
460 raise (Basic_operation "b1 mod b2: b2 cannot be zero.")
461 | (N i1, Error) -> Error
462 | (Vec vec1, Vec vec2) ->
463 if vec1#size = vec2#size then
464 Vec (new vector vec1#size
465 (fun_binary basic_mod vec1#nth vec2#nth))
466 else raise (Basic_operation "vector size not matched.")
468 raise (Basic_operation "Vector_Scalar: vec mod sca.")
469 | (Zero, Vec vec2) ->
470 basic_mod (Vec (new vector vec2#size (fun i -> Zero))) b2
471 | (Zero, _) -> basic_mod (N 0) b2
472 | (Error, Vec vec2) ->
473 raise (Basic_operation "Scalar_Vector: sca mod vec.")
474 | (Error, _) -> Error;;
476 let rec basic_mod_float :
477 (float -> float -> float) -> basic -> basic -> basic =
478 fun oper -> fun b1 -> fun b2 ->
480 | (R f1, R f2) -> R (oper f1 f2)
482 raise (Basic_operation "b1 mod_float b2: b2 cannot be int.")
484 raise (Basic_operation "b1 mod_float b2: b1 cannot be int.")
485 | (R f1, Vec vec2) ->
486 raise (Basic_operation "Scalar_Vector: sca mod_float vec.")
488 raise (Basic_operation "b1 mod_float b2: b2 cannot be zero.")
489 | (R f1, Error) -> Error
490 | (Vec vec1, Vec vec2) ->
491 if vec1#size = vec2#size then
492 Vec (new vector vec1#size
493 (fun_binary (basic_mod_float oper) vec1#nth vec2#nth))
494 else raise (Basic_operation "vector size not matched.")
496 raise (Basic_operation "Vector_Scalaire: vec mod_float sca.")
497 | (Zero, Vec vec2) ->
498 basic_mod_float oper (Vec (new vector vec2#size (fun i -> Zero))) b2
499 | (Zero, _) -> basic_mod_float oper (R 0.) b2
500 | (Error, Vec vec2) ->
501 raise (Basic_operation "Scalaire_Vector: int mod_float vec.")
502 | (Error, _) -> Error;;
504 let basic_fmod = basic_mod_float mod_float;;
505 let basic_remainder = basic_mod_float remainder_float;;
507 let rec basic_compare_zero :
508 ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> basic -> basic =
509 fun oper1 -> fun oper2 -> fun v ->
511 |N i -> if oper1 i 0 then N 1 else N 0
512 |R f -> if oper2 f 0. then N 1 else N 0
514 Vec (new vector vec#size
515 (fun_unary (basic_compare_zero oper1 oper2) vec#nth ))
516 |Zero -> basic_compare_zero oper1 oper2 (N 0)
519 let basic_gt_zero = basic_compare_zero (>) (>);;
520 let basic_lt_zero = basic_compare_zero (<) (<);;
521 let basic_geq_zero = basic_compare_zero (>=) (>=);;
522 let basic_leq_zero = basic_compare_zero (<=) (<=);;
523 let basic_eq_zero = basic_compare_zero (=) (=);;
524 let basic_neq_zero = basic_compare_zero (<>) (<>);;
526 let basic_compare : (basic -> basic) -> basic -> basic -> basic =
527 fun oper -> fun b1 -> fun b2 -> oper (b1 -~ b2);;
529 let basic_gt = basic_compare basic_gt_zero;;
530 let basic_lt = basic_compare basic_lt_zero;;
531 let basic_geq = basic_compare basic_geq_zero;;
532 let basic_leq = basic_compare basic_leq_zero;;
533 let basic_eq = basic_compare basic_eq_zero;;
534 let basic_neq = basic_compare basic_neq_zero;;
536 let basic_max : basic -> basic -> basic =
539 let compare = basic_gt_zero (b1 -~ b2) in
543 else if i = 0 then b2
544 else raise (Basic_operation "compare result not bool.")
546 let basics = Array.init vec#size vec#nth in
547 let sum = basic_to_int (Array.fold_left basic_add Zero basics) in
548 if sum = vec#size then b1
549 else if sum = 0 then b2
552 | _ -> raise (Basic_operation "compare result not bool.");;
555 let basic_min : basic -> basic -> basic =
558 let compare = basic_gt_zero (b1 -~ b2) in
562 else if i = 0 then b1
563 else raise (Basic_operation "compare result not bool.")
565 let basics = Array.init vec#size vec#nth in
566 let sum = basic_to_int (Array.fold_left basic_add Zero basics) in
567 if sum = vec#size then b2
568 else if sum = 0 then b1
571 | _ -> raise (Basic_operation "compare result not bool.");;