Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file owl_utils.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263# 1 "src/base/misc/owl_utils.ml"(*
* OWL - OCaml Scientific and Engineering Computing
* Copyright (c) 2016-2018 Liang Wang <liang.wang@cl.cam.ac.uk>
*)(** Helper functions used in the library *)openOwl_typesincludeOwl_utils_ndarraymoduleStack=Owl_utils_stackmoduleArray=Owl_utils_array(* Computes a left fold over a range of integers from a to b (inclusive) *)letrange_foldab~f~init=letrecgoaccx=ifx>bthenaccelsego(faccx)(x+1)ingoinitaletarray_reversex=letd=Array.lengthx-1inletn=d/2infori=0tondolett=x.(i)inx.(i)<-x.(d-i);x.(d-i)<-t;done(* insert element [a] at position [i] in [x] *)letarray_insertxia=letn=Array.lengthxinassert(i<=n);lety=Array.make(n+1)ainArray.blitx0y0i;Array.blitxiy(i+1)(n-i);y(* get the suffix a file name *)letget_suffixs=letparts=String.split_on_char'.'sinList.(nthparts(lengthparts-1))letcount_dupl=matchlwith|[]->[]|hd::tl->letacc,x,c=List.fold_left(fun(acc,x,c)y->ify=xthenacc,x,c+1else(x,c)::acc,y,1)([],hd,1)tlin(x,c)::acc(* search the list given a value, return the position of its first occurrence *)letlist_searchxl=letrec_searchxlc=matchlwith|[]->raiseNot_found|hd::tl->ifhd=xthencelse_searchxtl(c+1)in_searchxl0(* flatten an array2 then convert to array1 *)letarray2_to_array1x=letopenBigarrayinletm=Array2.dim1xinletn=Array2.dim2xinletc=m*ninletx=genarray_of_array2xinreshape_1xc(* iter function for ['a array array] type *)letaarr_iterfx=Array.iter(Array.iterf)x(* iteri function for ['a array array] type *)letaarr_iterifx=Array.iteri(funiy->Array.iteri(funjz->fijz)y)x(* map function for ['a array array] type *)letaarr_mapfx=Array.map(Array.mapf)x(* mapi function for ['a array array] type *)letaarr_mapifx=Array.mapi(funiy->Array.mapi(funjz->fijz)y)x(* map2 function for ['a array array] type, x and y must have the same shape. *)letaarr_map2fxy=Array.map2(Array.map2f)xy(* map2i function for ['a array array] type, x and y must have the same shape. *)letaarr_map2ifx0x1=Array.map2i(funiy0y1->Array.map2i(funjz0z1->fijz0z1)y0y1)x0x1(* map3i function for ['a array array] type, all must have the same shape. *)letaarr_map3ifx0x1x2=Array.init(Array.lengthx0)(funi->Array.init(Array.lengthx0.(i))(funj->fijx0.(i).(j)x1.(i).(j)x2.(i).(j)))(* map3 function for ['a array array] type, all must have the same shape. *)letaarr_map3fx0x1x2=Array.init(Array.lengthx0)(funi->Array.init(Array.lengthx0.(i))(funj->fx0.(i).(j)x1.(i).(j)x2.(i).(j)))(* map4 function for ['a array array] type, all must have the same shape. *)letaarr_map4fx0x1x2x3=Array.init(Array.lengthx0)(funi->Array.init(Array.lengthx0.(i))(funj->fx0.(i).(j)x1.(i).(j)x2.(i).(j)x3.(i).(j)))(* convert array of array to list of list, shape remains the same *)letaarr2llssx=Array.mapArray.to_listx|>Array.to_list(* convert list of list to array of array, shape remains the same *)letllss2aarrx=List.mapArray.of_listx|>Array.of_list(* fold function for ['a array array] type, by flatten the array *)letaarr_foldfax=leta=refainArray.iter(Array.iter(funb->a:=f!ab))x;!a(* make a matrix of array array type, fill with value a *)letaarr_matrixmna=Array.initm(fun_->Array.makena)(* extend passed in array by appending n slots *)letarray1_extendxn=letopenBigarrayinletm=Array1.dimxinlety=Array1.(create(kindx)c_layout(m+n))inletz=Array1.suby0minArray1.blitxz;y(* make a copy of the passed in array1 *)letarray1_copyx=letopenBigarrayinlety=Array1.(create(kindx)c_layout(dimx))inArray1.blitxy;y(* map function for ['a array array array] type *)letaaarrr_mapfx=Array.map(Array.map(Array.mapf))x(*
``squeeze_continuous_dims shape axes`` first groups the int elements in the
``shape`` array sequentially, depending on whether the index of that element
is contained in the array ``axes``, then computes the product of each group and
returns them in an int array. Assume ``axes`` only contains non-negative
integers.
*)letsqueeze_continuous_dimsshapeaxes=letndim=Array.lengthshapeinletnew_shape=Array.makendim1inletaxes=Array.sort_fill~min:0~max:(ndim-1)~fill:(-1)axesinletflag=ref(axes.(0)=0)inletprod=ref1inletcount=ref0infori=0tondim-1doif((axes.(i)=i)=!flag)then(prod:=!prod*shape.(i))else(new_shape.(!count)<-!prod;prod:=shape.(i);count:=!count+1;flag:=not!flag;)done;new_shape.(!count)<-!prod;Array.subnew_shape0(!count+1)(* format time period into human-readable format *)letformat_timet=ift<60.thenPrintf.sprintf"%02is"(int_of_floatt)elseift>=60.&&t<3600.then(letm=int_of_float(t/.60.)inlets=(int_of_floatt)mod60inPrintf.sprintf"%02im%02is"ms)else(leth=int_of_float(t/.3600.)inletm=int_of_float(t/.60.)mod60inPrintf.sprintf"%ih%02im"hm)(** measure the time spent in a function in millisecond *)lettimef=lett=Unix.gettimeofday()inf();(Unix.gettimeofday()-.t)*.1000.(** TODO: return the the distance between [1.0] and the next larger representable
floating-point value. *)leteps:typeab.(a,b)Bigarray.kind->float=letopenBigarrayinfunction|Float32->2.**(-23.)|Float64->2.**(-52.)|Complex32->2.**(-23.)|Complex64->2.**(-52.)|_->failwith"owl_utils:eps"letnum_typ_to_strx=matchxwith|F32->"F32"|F64->"F64"|C32->"C32"|C64->"C64"letnum_typ_of_strx=matchxwith|"F32"->F32|"F64"->F64|"C32"->C32|"C64"->C64|_->failwith"num_typ_of_str"letlongest_stringstrings=Array.fold_left(funaccs->maxacc(String.lengths))0stringsletpad_stringssides_maxstrings=Array.map(funs->lets_len=String.lengthsinlets_len=max0(s_max-s_len)inlets_pad=String.makes_len' 'inmatchsidewith|`Left->s_pad^s|`Right->s^s_pad)strings(* ends here *)