3 Description: basic data type in the vectorial faust interpreter.
5 Created: 31/05/2013 Modified: 17/07/2013
10 exception Convert_Error of string;;
11 exception Basic_operation of string;;
16 let faust_max = 2147483647;;
17 let faust_min = -2147483648;;
20 (* Functional operations *)
22 let fun_unary oper f = fun x -> oper (f x);;
23 let fun_binary oper f g = fun x -> oper (f x) (g x);;
24 let fun_ternary oper f g h = fun x -> oper (f x) (g x) (h x);;
26 (* basic operations *)
28 let memorize : int -> (index -> basic) -> (index -> basic) =
31 let memory_array = Array.create size Error in
32 let index_array = Array.create size false in
33 let vec_mem : index -> basic =
35 if i >= 0 && i < size then (
36 if index_array.(i) then
40 let () = memory_array.(i) <- result in
41 let () = index_array.(i) <- true in
43 else raise (Invalid_argument "vector overflow.") in
46 class vector : int -> (index -> basic) -> vector_type =
47 fun (size_init : int) ->
48 fun (vec_init : index -> basic) ->
51 val vec = memorize size_init vec_init
56 let rec basic_to_int : basic -> int =
60 |R f -> int_of_float f
62 raise (Convert_Error "basic_to_int : vector.")
64 |Error -> raise (Convert_Error "basic_to_int : Error");;
67 let basic_to_float : basic -> float =
70 |N i -> float_of_int i
73 raise (Convert_Error "basic_to_float : vector.")
78 let basic_to_float_array : basic -> float array =
82 let result : basic array =
83 Array.init vec#size vec#nth in
84 Array.map basic_to_float result
85 |_ -> [| (basic_to_float v)|];;
88 let basic_to_string : basic -> string =
91 |N i1 -> "N " ^ (string_of_int i1)
92 |R f1 -> "R " ^ (string_of_float f1)
97 (* VALUE OPERATIONS *)
99 let rec basic_normalize : basic -> basic =
101 let n = 2. ** float_of_int (faust_bits) in
104 if i > faust_max then
106 (n *. floor (((float_of_int i) +. n/.2.)/.n)))
107 else if i < faust_min then
109 (n *. floor ((n/.2. -. (float_of_int i) -. 1.)/.n)))
112 if f > float_of_int (faust_max) then
113 R (f -. (n *. floor ((f +. n/.2.)/.n)))
114 else if f < float_of_int (faust_min) then
115 R (f +. (n *. floor ((n/.2. -. f -. 1.)/.n)))
118 Vec (new vector vec#size
119 (fun_unary basic_normalize vec#nth))
124 let rec basic_add : basic -> basic -> basic =
130 | (Vec vec1, Vec vec2) ->
131 if vec1#size = vec2#size then
132 Vec (new vector vec1#size
133 (fun_binary basic_add vec1#nth vec2#nth))
134 else raise (Basic_operation "vector size not matched.")
135 | (Vec vec1, _) -> raise (Basic_operation "vec1 +~ sca2")
137 | (N i1, N i2) -> basic_normalize (N (i1 + i2))
138 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) +. f2))
139 | (N i1, Vec vec2) -> raise (Basic_operation "i1 +~ vec2")
140 | (N i1, Error) -> Error
142 | (R f1, N i2) -> basic_normalize (R (f1 +. (float_of_int i2)))
143 | (R f1, R f2) -> basic_normalize (R (f1 +. f2))
144 | (R f1, Vec vec2) -> raise (Basic_operation "f1 +~ vec2")
145 | (R f1, Error) -> Error
147 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
148 | (Error, _) -> Error;;
151 let (+~) b1 b2 = basic_add b1 b2;;
154 let rec basic_neg : basic -> basic =
159 |Vec vec -> Vec (new vector vec#size (fun_unary basic_neg vec#nth))
164 let basic_sub : basic -> basic -> basic =
167 basic_add b1 (basic_neg b2);;
170 let (-~) b1 b2 = basic_sub b1 b2;;
173 let rec basic_mul : basic -> basic -> basic =
177 | (Vec vec1, Vec vec2) ->
178 if vec1#size = vec2#size then
179 Vec (new vector vec1#size
180 (fun_binary basic_mul vec1#nth vec2#nth))
181 else raise (Basic_operation "vector size not matched.")
182 | (Vec vec1, Zero) ->
183 Vec (new vector vec1#size
184 (fun_unary (basic_mul Zero) vec1#nth))
185 | (Vec vec1, _) -> raise (Basic_operation "vec1 *~ sca2")
187 | (N i1, N i2) -> basic_normalize (N (i1 * i2))
188 | (N i1, R f2) -> basic_normalize (R ((float_of_int i1) *. f2))
189 | (N i1, Vec vec2) -> raise (Basic_operation "i1 *~ vec2")
190 | (N i1, Zero) -> N 0
191 | (N i1, Error) -> Error
193 | (R f1, N i2) -> basic_normalize (R (f1 *. (float_of_int i2)))
194 | (R f1, R f2) -> basic_normalize (R (f1 *. f2))
195 | (R f1, Vec vec2) -> raise (Basic_operation "f1 *~ vec2")
196 | (R f1, Zero) -> R 0.
197 | (R f1, Error) -> Error
199 | (Zero, N i2) -> N 0
200 | (Zero, R f2) -> R 0.
201 | (Zero, Vec vec2) ->
202 Vec (new vector vec2#size
203 (fun i -> basic_mul Zero (vec2#nth i)))
204 | (Zero, Zero) -> Zero
205 | (Zero, Error) -> Error
207 | (Error, Vec vec2) -> raise (Basic_operation "Error +~ vec2")
208 | (Error, _) -> Error;;
211 let ( *~ ) b1 b2 = basic_mul b1 b2;;
214 let rec basic_recip : basic -> basic =
217 |N i -> basic_recip (R (float_of_int i))
218 |R f -> if f = 0. then Error else R (1./.f)
219 |Vec vec -> Vec (new vector vec#size
220 (fun_unary basic_recip vec#nth))
225 let basic_div : basic -> basic -> basic =
228 basic_mul b1 (basic_recip b2);;
231 let (/~) b1 b2 = basic_div b1 b2;;
234 let rec basic_zero : basic -> basic =
239 |Vec vec -> Vec (new vector vec#size
240 (fun_unary basic_zero vec#nth))
245 let rec basic_floor : basic -> basic =
248 |N i -> R (float_of_int i)
250 |Vec vec -> Vec (new vector vec#size
251 (fun_unary basic_floor vec#nth))
256 let rec basic_int : basic -> basic =
260 |R f -> N (int_of_float f)
261 |Vec vec -> Vec (new vector vec#size
262 (fun_unary basic_int vec#nth))
267 let rec basic_unary : (float -> float) -> basic -> basic =
271 |N i -> R (oper (float_of_int i))
273 |Vec vec -> Vec (new vector vec#size
274 (fun_unary (basic_unary oper) vec#nth))
279 let basic_sin : basic -> basic = basic_unary sin;;
280 let basic_cos : basic -> basic = basic_unary cos;;
281 let basic_atan : basic -> basic = basic_unary atan;;
284 let rec basic_atan2 : basic -> basic -> basic =
288 | (N i1, N i2) -> basic_atan2
289 (R (float_of_int i1)) (R (float_of_int i2))
290 | (N i1, R f2) -> basic_atan2 (R (float_of_int i1)) v2
291 | (N i1, Zero) -> basic_atan2 (R (float_of_int i1)) (R 0.)
292 | (N i1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
293 | (N i1, Error) -> Error
295 | (R f1, N i2) -> basic_atan2 v1 (R (float_of_int i2))
296 | (R f1, R f2) -> R (atan2 f1 f2)
297 | (R f1, Zero) -> basic_atan2 v1 (R 0.)
298 | (R f1, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
299 | (R f1, Error) -> Error
301 | (Vec vec1, Vec vec2) -> Vec (new vector vec1#size
302 (fun_binary basic_atan2 vec1#nth vec2#nth))
303 | (Vec vec1, Zero) -> Vec (new vector vec1#size
304 (fun i -> basic_atan2 (vec1#nth i) Zero))
305 | (Vec vec1, _) -> raise (Basic_operation "atan2 vec sca.")
307 | (Zero, N i2) -> basic_atan2 (R 0.) (R (float_of_int i2))
308 | (Zero, R f2) -> basic_atan2 (R 0.) v2
309 | (Zero, Vec vec2) -> Vec (new vector vec2#size
310 (fun_unary (basic_atan2 Zero) vec2#nth))
311 | (Zero, Zero) -> basic_atan2 (R 0.) (R 0.)
312 | (Zero, Error) -> Error
314 | (Error, Vec vec2) -> raise (Basic_operation "atan2 sca vec.")
315 | (Error, _) -> Error;;
318 let rec basic_sqrt v = match v with
320 if i >= 0 then R (sqrt (float_of_int i))
321 else raise (Basic_operation "sqrt parameter < 0.")
323 if f >= 0. then R (sqrt f)
324 else raise (Basic_operation "sqrt parameter < 0.")
325 |Vec vec -> Vec (new vector vec#size (fun_unary basic_sqrt vec#nth))
330 let rec basic_mod : basic -> basic -> basic =
334 | (N i1, N i2) -> N (i1 mod i2)
335 | (N i1, R f2) -> basic_mod b1 (N (int_of_float f2))
336 | (N i1, Vec vec2) ->
337 raise (Basic_operation "Scalaire_Vector: int mod vec.")
339 raise (Basic_operation "b1 mod b2: b2 cannot be zero.")
340 | (N i1, Error) -> Error
342 | (R f1, _) -> basic_mod (N (int_of_float f1)) b2
344 | (Vec vec1, Vec vec2) ->
345 if vec1#size = vec2#size then
346 Vec (new vector vec1#size (fun_binary basic_mod vec1#nth vec2#nth))
347 else raise (Basic_operation "vector size not matched.")
349 raise (Basic_operation "Vector_Scalaire: vec mod int.")
351 | (Zero, Vec vec2) ->
352 basic_mod (Vec (new vector vec2#size (fun i -> Zero))) b2
353 | (Zero, _) -> basic_mod (N 0) b2
355 | (Error, Vec vec2) ->
356 raise (Basic_operation "Scalaire_Vector: int mod vec.")
357 | (Error, _) -> Error;;
360 let rec basic_larger_than_zero : basic -> basic =
363 |N i -> if i > 0 then N 1 else N 0
364 |R f -> if f > 0. then N 1 else N 0
366 Vec (new vector vec#size
367 (fun_unary basic_larger_than_zero vec#nth ))
372 let basic_larger : basic -> basic -> basic =
375 basic_larger_than_zero (b1 -~ b2);;
378 let basic_smaller : basic -> basic -> basic =
381 basic_larger_than_zero (b2 -~ b1);;