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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Dot product step by step in the Fantom 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 Fantom programming language

Source code in the fantom programming language

class DotProduct
{
  static Int dotProduct (Int[] a, Int[] b)
  {
    Int result := 0
    [a.size,b.size].min.times |i|
    {
      result += a[i] * b[i]
    }
    return result
  }

  public static Void main ()
  {
    Int[] x := [1,2,3,4]
    Int[] y := [2,3,4]

    echo ("Dot product of $x and $y is ${dotProduct(x, y)}")
  }
}

  

You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the zkl programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the OCaml programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the C++ programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the Haskell programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the C# programming language