Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: vector dot multiply
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pascal Nicolas <pn@u...>
Subject: Re: vector dot multiply
> 
> Hi,
>  What would be 1). elegant
>                2). efficient  
>  way to write a "dot multiply" function in caml-light? 
> This is what I came up with but I am hoping for a better one :
> 
> let dot a b = let rec dot_aux a b i sum =
> if i< vect_length a then
>    dot_aux a b (i+1) (sum +. (a.(i) *. b.(i)) )
>  else
>    sum
> in
>  dot_aux a b 0 0.0;;
> 
In order to avoid to (re)compute the length of a at each recursive call, you
can modify a little your function as follows

 let dot a b = let rec dot_aux a b i sum L =
 if i < L then
    dot_aux a b (i+1) (sum +. (a.(i) *. b.(i))) L
 else
    sum
 in dot_aux a b 0 0.0 (vect_length a) ;;


--
Pascal NICOLAS                                       LERIA  Université d'ANGERS
Tél : (33) 41 73 54 20             2, Bd Lavoisier 49045 ANGERS cedex 01 FRANCE
E Mail : pn@univ-angers.fr
WWW Url : http://www.univ-angers.fr/~pn/nicolas.html