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: Pierre Weis <weis@p...>
Subject: Re: vector dot multiply
> Hi,
>  What would be 1). elegant
>                2). efficient  
>  way to write a "dot multiply" function in caml-light? 

You're recursive version seems good (except that you (re)compute
the vect_length of vector a at each recursive call). You may prefer an
imperative version (as yours, this function assumes a and b to have the
same length):

let dot a b =
  let s = ref 0.0 in
  for i = 0 to vect_length a - 1 do
   s := a.(i) *. b.(i) +. !s
  done;
  !s;;

> Also, is there a similar construct to Haskell array/list comprehensions?

No. This is very difficult to get in a strict language, where these
lists cannot be computed as necessary as is done in lazy languages...

Pierre Weis
----------------------------------------------------------------------------
WWW Home Page: http://pauillac.inria.fr/~weis
Projet Cristal
INRIA, BP 105, F-78153 Le Chesnay Cedex (France)
E-mail: Pierre.Weis@inria.fr
Telephone: +33 1 39 63 55 98
Fax: +33 1 39 63 53 30
----------------------------------------------------------------------------