Dot product

From Rosetta Code
Revision as of 09:50, 26 February 2010 by rosettacode>Dsnouck (Added Fortran)
Task
Dot product
You are encouraged to solve this task according to the task description, using any language you may know.

Create a function/use an in-built function, to compute the dot product, also known as the scalar product of two vectors. If possible, make the vectors of arbitrary length.

As an example, compute the dot product of the vectors [1, 3, -5] and [4, -2, -1].

If implementing the dot product of two vectors directly, each vector must be the same length; multiply corresponding terms from each vector then sum the results to produce the answer.

Clojure

Works with: Clojure version 1.1

Preconditions are new in 1.1. The actual code also works in older Clojure versions.

<lang clojure>(defn dot-product [c1 c2]

 {:pre [(== (count c1) (count c2))]}
 (reduce + (map * c1 c2))
 )

(println (dot-product [1 3 -5] [4 -2 -1]))</lang>

Factor

The built-in word v. is used to compute the dot product. It doesn't enforce that the vectors are of the same length, so here's a wrapper.

<lang factor>USING: kernel math.vectors sequences ;

dot-product ( u v -- w )
   2dup [ length ] bi@ =
   [ v. ] [ "Vector lengths must be equal" throw ] if ;</lang>
( scratchpad ) { 1 3 -5 } { 4 -2 -1 } dot-product .
3

Fortran

Works with: Fortran version 90 and later

<lang fortran>program test_dot_product

 write (*, '(i0)') dot_product ((/1, 3, -5/), (/4, -2, -1/))

end program test_dot_product</lang> Output: <lang>3</lang>

Haskell

<lang haskell>dotp a b | length a == length b = sum (zipWith (*) a b)

        | otherwise = error "Vector sizes must match"

main = print $ dotp [1, 3, -5] [4, -2, -1] -- prints 3</lang>

J

<lang j> 1 3 _5 +/ . * 4 _2 _1 3

  dotp=: +/ . *                  NB. Or defined as a verb (function)
  1 3 _5  dotp 4 _2 _1

3</lang> The conjunction . applies generally to matricies and arrays of higher dimensions and can be used with verbs (functions) other than sum ( +/ ) and product ( * ).

<lang logo>to dotprod :a :b

 output apply "sum (map "product :a :b)

end

show dotprod [1 3 -5] [4 -2 -1]  ; 3</lang>

Oz

Vectors are represented as lists in this example. <lang oz>declare

 fun {DotProduct Xs Ys}
    {Length Xs} = {Length Ys} %% assert
    {List.foldL {List.zip Xs Ys Number.'*'} Number.'+' 0}
 end

in

 {Show {DotProduct [1 3 ~5] [4 ~2 ~1]}}</lang>

Perl

<lang perl>sub dotprod (\@\@) {

       my($vec_a, $vec_b) = @_;
       die "they must have the same size\n" unless @$vec_a == @$vec_b;
       my $sum = 0;
       $sum += $vec_a->[$_]*$vec_b->[$_] for 0..$#$vec_a;
       return $sum;

}

my @vec_a = (1,3,-5); my @vec_b = (4,-2,-1);

print dotprod(@vec_a,@vec_b), "\n"; # 3</lang>

PL/I

<lang PL/I> get (n); begin;

  declare (A(n), B(n)) float;
  declare dot_product float;
  get list (A);
  get list (B);
  dot_product = sum(a*b);
  put (dot_product);

end; </lang>

Python

<lang python>def dotp(a,b):

   assert len(a) == len(b), 'Vector sizes must match'
   return sum(aterm * bterm for aterm,bterm in zip(a, b))

if __name__ == '__main__':

   a, b = [1, 3, -5], [4, -2, -1]
   assert dotp(a,b) == 3</lang>

Tcl

Library: tcllib

<lang tcl>package require math::linearalgebra

set a {1 3 -5} set b {4 -2 -1} set dotp [::math::linearalgebra::dotproduct $a $b] proc pp vec {return \[[join $vec ,]\]} puts "[pp $a] \u2219 [pp $b] = $dotp"</lang> Output:

[1,3,-5] ∙ [4,-2,-1] = 3.0

TI-89 BASIC

dotP([1, 3, –5], [4, –2, –1])
    3