Re: vector dot multiply

Pierre Weis (weis@pauillac.inria.fr)
Thu, 8 Jun 1995 20:29:15 +0200 (MET DST)

From: Pierre Weis <weis@pauillac.inria.fr>
Message-Id: <199506081829.UAA01903@pauillac.inria.fr>
Subject: Re: vector dot multiply
To: osman.buyukisik@ae.ge.com (U-E59264-Osman Buyukisik)
Date: Thu, 8 Jun 1995 20:29:15 +0200 (MET DST)
In-Reply-To: <199506081716.NAA01690@thomas.ge.com> from "U-E59264-Osman Buyukisik" at Jun 8, 95 01:16:29 pm

> 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
----------------------------------------------------------------------------