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 (* VALUE OPERATIONS *)
127 let rec basic_normalize : basic -> basic =
129 let n = 2. ** float_of_int (faust_bits) in
132 if i > faust_max then
134 (n *. floor (((float_of_int i) +. n/.2.)/.n)))
135 else if i < faust_min then
137 (n *. floor ((n/.2. -. (float_of_int i) -. 1.)/.n)))
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)))
146 Vec (new vector vec#size
147 (fun_unary basic_normalize vec#nth))
152 let rec basic_add : basic -> basic -> basic =
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")
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
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
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")
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
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
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
235 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
236 | (Error, _) -> Error;;
239 let ( *~ ) b1 b2 = basic_mul b1 b2;;
242 let rec basic_recip : basic -> basic =
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))
253 let basic_div : basic -> basic -> basic =
256 basic_mul b1 (basic_recip b2);;
259 let (/~) b1 b2 = basic_div b1 b2;;
262 let rec basic_zero : basic -> basic =
267 |Vec vec -> Vec (new vector vec#size
268 (fun_unary basic_zero vec#nth))
273 let rec basic_power : basic -> basic -> basic =
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")
287 | (N i1, _) -> basic_power (R (float_of_int i1)) b2
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
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
303 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
304 | (Error, _) -> Error;;
307 let rec basic_logic :
308 (bool -> bool -> bool) -> basic -> basic -> basic =
309 fun oper -> fun b1 -> fun b2 ->
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")
321 | (N i1, N i2) -> oper (basic_to_bool b1) (basic_to_bool b2)
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
329 raise (Basic_operation "Float shouldn't be in logical oper.")
331 | (Zero, N i2) -> basic_logic oper (N 0) b2
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
340 | (Error, Vec vec2) -> raise (Basic_operation "Error logic vec2")
341 | (Error, _) -> Error;;
343 let basic_and = basic_logic (&&);;
344 let basic_or = basic_logic (||);;
345 let basic_xor = basic_logic xor;;
347 let rec basic_adjust : (float -> float) -> basic -> basic =
350 |N i -> R (float_of_int i)
352 |Vec vec -> Vec (new vector vec#size
353 (fun_unary basic_floor vec#nth))
357 let basic_floor = basic_adjust floor;;
358 let basic_ceil = basic_adjust ceil;;
359 let basic_rint = basic_adjust rint;;
361 let rec basic_int : basic -> basic =
365 |R f -> N (int_of_float f)
366 |Vec vec -> Vec (new vector vec#size
367 (fun_unary basic_int vec#nth))
371 let rec basic_float : basic -> basic =
374 | N i -> R (float_of_int i)
376 | Vec vec -> Vec (new vector vec#size
377 (fun_unary basic_float vec#nth))
382 let rec basic_unary : (float -> float) -> basic -> basic =
386 |N i -> R (oper (float_of_int i))
388 |Vec vec -> Vec (new vector vec#size
389 (fun_unary (basic_unary oper) vec#nth))
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;;
399 let rec basic_atan2 : basic -> basic -> basic =
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
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
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.")
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
429 | (Error, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
430 | (Error, _) -> Error;;
433 let rec basic_sqrt v = match v with
435 if i >= 0 then R (sqrt (float_of_int i))
436 else raise (Basic_operation "sqrt parameter < 0.")
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))
445 let rec basic_mod : basic -> basic -> basic =
449 | (N i1, N i2) -> N (i1 mod i2)
451 raise (Basic_operation "b1 mod b2: b2 cannot be float.")
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.")
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.")
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;;
473 let rec basic_fmod : basic -> basic -> basic =
477 | (R f1, R f2) -> R (mod_float f1 f2)
479 raise (Basic_operation "b1 fmod b2: b2 cannot be int.")
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.")
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.")
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;;
502 let rec basic_larger_than_zero : basic -> basic =
505 |N i -> if i > 0 then N 1 else N 0
506 |R f -> if f > 0. then N 1 else N 0
508 Vec (new vector vec#size
509 (fun_unary basic_larger_than_zero vec#nth ))
514 let basic_larger : basic -> basic -> basic =
517 basic_larger_than_zero (b1 -~ b2);;
520 let basic_smaller : basic -> basic -> basic =
523 basic_larger_than_zero (b2 -~ b1);;
526 let basic_max : basic -> basic -> basic =
529 let compare = basic_larger_than_zero (b1 -~ b2) in
533 else if i = 0 then b2
534 else raise (Basic_operation "compare result not bool.")
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
542 | _ -> raise (Basic_operation "compare result not bool.");;
545 let basic_min : basic -> basic -> basic =
548 let compare = basic_larger_than_zero (b1 -~ b2) in
552 else if i = 0 then b1
553 else raise (Basic_operation "compare result not bool.")
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
561 | _ -> raise (Basic_operation "compare result not bool.");;