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

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

def dot(x; y):
  reduce range(0;x|length) as $i (0; . + x[$i] * y[$i]);

def SIGMA( f ): reduce .[] as $o (0; . + ($o | f )) ;

dot( [1, 3, -5]; [4, -2, -1]) # => 3

[ {"x": 1, "y": 4},  {"x": 3, "y": -2},  {"x": -5, "y": -1} ]
  | SIGMA( .x * .y ) # => 3

  

You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the C# programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Pascal programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Julia programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Erlang programming language
You may also check:How to resolve the algorithm Atomic updates step by step in the PARI/GP programming language