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

Published on 12 May 2024 09:40 PM

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

Source code in the clojure programming language

(defn dot-product [& matrix]
  {:pre [(apply == (map count matrix))]}
  (apply + (apply map * matrix)))

(defn dot-product2 [x y]
 (->> (interleave x y)
      (partition 2 2)
      (map #(apply * %))
      (reduce +)))

(defn dot-product3
  "Dot product of vectors. Tested on version 1.8.0."
  [v1 v2]
  {:pre [(= (count v1) (count v2))]}
  (reduce + (map * v1 v2)))

;Example Usage
(println (dot-product [1 3 -5] [4 -2 -1]))
(println (dot-product2 [1 3 -5] [4 -2 -1]))
(println (dot-product3 [1 3 -5] [4 -2 -1]))


  

You may also check:How to resolve the algorithm String length step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Holidays related to Easter step by step in the Tcl programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Hy programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Batch File programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the Perl programming language