Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file owl_utils.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259# 1 "src/base/misc/owl_utils.ml"(*
* OWL - an OCaml numerical library for scientific computing
* Copyright (c) 2016-2018 Liang Wang <liang.wang@cl.cam.ac.uk>
*)(** Helper functions used in the library *)includeOwl_utils_convincludeOwl_utils_ndarraymoduleStack=Owl_utils_stackmoduleArray=Owl_utils_array(* Adjust the index according to the [0, m). m is the boundary, i can be negative. *)letadjust_indexim=ifi>=0&&i<mthenielseifi<0&&i>=-mtheni+melseraiseOwl_exception.INDEX_OUT_OF_BOUND(* 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(* save a marshalled object to a file *)letmarshal_to_filexf=lets=Marshal.to_stringx[]inleth=open_outfinoutput_stringhs;close_outh(* load a marshalled object from a file *)letmarshal_from_filef=leth=open_infinlets=really_input_stringh(in_channel_lengthh)inMarshal.from_strings0(* 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(* pretty-print an array to string *)letstring_of_array?(prefix="")?(suffix="")?(sep=",")string_of_xx=lets=Array.to_listx|>List.mapstring_of_x|>String.concatsepinPrintf.sprintf"%s%s%s"prefixssuffix(* 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(* 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)(* read a file of a given path *)letread_file?(trim=true)f=leth=open_infinlets=Stack.make()in(trywhiletruedoletl=matchtrimwith|true->input_lineh|>String.trim|false->input_linehinStack.pushsl;donewithEnd_of_file->());close_inh;Stack.to_arraysletread_file_stringf=letic=open_infinletn=in_channel_lengthicinlets=Bytes.createninreally_inputics0n;close_inic;Bytes.to_strings(* write a file of a given path *)letwrite_filefs=leth=open_outfinPrintf.fprintfh"%s"s;close_outh(** 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"(* ends here *)