Chris_T avatar Chris_T committed 0676cef

Added [Vec.map].

Comments (0)

Files changed (2)

   if incx > 0 then ref ofsx, ofsx + n * incx
   else ref (ofsx - (n - 1) * incx), ofsx + incx
 
+let vec_map_str = "Vec.map"
+
+let map f ?n ?ofsy ?incy ?y ?ofsx ?incx x =
+  let ofsx, incx = get_vec_geom vec_map_str x_str ofsx incx in
+  let ofsy, incy = get_vec_geom vec_map_str y_str ofsy incy in
+  let n = get_dim_vec vec_map_str x_str ofsx incx x n_str n in
+  let min_dim_y = ofsy + (n - 1) * abs incy in
+  let y, ofsy, incy =
+    match y with
+    | Some y -> check_vec vec_map_str y_str y min_dim_y; y, ofsy, incy
+    | None -> create min_dim_y, 1, 1 in
+  let i_ref, last_i = get_i_ref_last ~incx ~ofsx ~n in
+  let j_ref = ref(if incy > 0 then ofsy else min_dim_y) in
+  while !i_ref <> last_i do
+    y.{!j_ref} <- f x.{!i_ref};
+    i_ref := !i_ref + incx;
+    j_ref := !j_ref + incy;
+  done;
+  y
+
 let vec_iter_str = "Vec.iter"
 
 let iter f ?n ?ofsx ?incx (x : vec) =
 
 (** {6 Iterators over vectors} *)
 
+val map :
+  (num_type -> num_type) ->
+  ?n : int ->
+  ?ofsy : int ->
+  ?incy : int ->
+  ?y : vec ->
+  ?ofsx : int ->
+  ?incx : int ->
+  vec
+  -> vec
+(** [map f ?n ?ofsx ?incx x] @return a new matrix resulting from the
+    application of [f] to all elements of [x].
+    @param n default = greater n s.t. [ofsx+(n-1)(abs incx) <= dim x]
+    @param ofsx default = 1
+    @param incx default = 1
+    @param y default = new vector with [ofsy+(n-1)(abs incy)] rows
+    @param ofsx default = 1
+    @param incx default = 1 *)
+
 val iter :
   (num_type -> unit) ->
   ?n : int ->
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.