How to resolve the algorithm Dot product step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Dot product step by step in the Picat programming language

Table of Contents

Problem Statement

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:

If implementing the dot product of two vectors directly:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Dot product step by step in the Picat programming language

Source code in the picat programming language

go =>
  L1 = [1, 3, -5],
  L2 = [4, -2, -1],

  println(dot_product=dot_product(L1,L2)),
  catch(println(dot_product([1,2,3,4],[1,2,3])),E, println(E)),
  nl.

dot_product(L1,L2) = _, L1.length != L2.length  => 
  throw($dot_product_not_same_length(L1,L2)).
dot_product(L1,L2) = sum([L1[I]*L2[I] : I in 1..L1.length]).

  

You may also check:How to resolve the algorithm Check that file exists step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Kosaraju step by step in the jq programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Unison programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Insitux programming language