How to resolve the algorithm Vector products step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Vector products step by step in the Haskell programming language

Table of Contents

Problem Statement

A vector is defined as having three dimensions as being represented by an ordered collection of three numbers:   (X, Y, Z). If you imagine a graph with the   x   and   y   axis being at right angles to each other and having a third,   z   axis coming out of the page, then a triplet of numbers,   (X, Y, Z)   would represent a point in the region,   and a vector from the origin to the point. Given the vectors: then the following common vector products are defined:

Given the three vectors:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vector products step by step in the Haskell programming language

Explanation:

The code you provided is written in Haskell programming language. It defines types and functions for performing vector and scalar operations in three-dimensional space.

Type Definitions:

  • Vector a: A type alias representing a vector as a list of elements of type a.
  • Scalar a: A type alias representing a scalar value of type a.

Data and Functions:

  • a, b, c, d: Sample vectors of integers.

  • dot: Computes the dot product of two vectors. It works for vectors of equal dimension, and returns a scalar value.

  • cross: Computes the cross product of two three-dimensional vectors. It returns a three-dimensional vector.

  • scalarTriple: Computes the scalar triple product of three vectors. It is the dot product of the first vector with the cross product of the second and third vectors.

  • vectorTriple: Computes the vector triple product of three vectors. It is the cross product of the first vector with the cross product of the second and third vectors.

Main Function:

  • main: The entry point of the program. It calculates and prints the results of various vector and scalar operations for the given sample vectors a, b, c, and d.

  • mapM_ putStrLn: Applies putStrLn (which prints a string) to each element of a list.

  • <>: The <> operator is used for string concatenation.

Alternate Version (Optional):

The second part of the code provided is an alternative version using the Either data type to handle potential errors in vector and scalar operations.

  • dotProduct: Computes the dot product, returning either a Left error message or a Right containing the result.

  • crossProduct: Computes the cross product, returning either a Left error message or a Right containing the result.

  • scalarTriple: Computes the scalar triple product using crossProduct and dotProduct, returning either a Left error message or a Right containing the result.

  • vectorTriple: Computes the vector triple product using crossProduct and crossProduct, returning either a Left error message or a Right containing the result.

  • main: The entry point of the program using the Either-based functions. It prints the results with error handling.

  • sh: Converts an Either value to a string, with the error message prefixed by " => " or the success value prefixed by " = ".

The alternative version allows you to gracefully handle errors that may arise from invalid vector operations, such as different dimensions or non-three-dimensional cross products.

Source code in the haskell programming language

import Data.Monoid ((<>))

type Vector a = [a]

type Scalar a = a

a, b, c, d :: Vector Int
a = [3, 4, 5]

b = [4, 3, 5]

c = [-5, -12, -13]

d = [3, 4, 5, 6]

dot
  :: (Num t)
  => Vector t -> Vector t -> Scalar t
dot u v
  | length u == length v = sum $ zipWith (*) u v
  | otherwise = error "Dotted Vectors must be of equal dimension."

cross
  :: (Num t)
  => Vector t -> Vector t -> Vector t
cross u v
  | length u == 3 && length v == 3 =
    [ u !! 1 * v !! 2 - u !! 2 * v !! 1
    , u !! 2 * head v - head u * v !! 2
    , head u * v !! 1 - u !! 1 * head v
    ]
  | otherwise = error "Crossed Vectors must both be three dimensional."

scalarTriple
  :: (Num t)
  => Vector t -> Vector t -> Vector t -> Scalar t
scalarTriple q r s = dot q $ cross r s

vectorTriple
  :: (Num t)
  => Vector t -> Vector t -> Vector t -> Vector t
vectorTriple q r s = cross q $ cross r s

main :: IO ()
main =
  mapM_
    putStrLn
    [ "a . b     = " <> show (dot a b)
    , "a x b     = " <> show (cross a b)
    , "a . b x c = " <> show (scalarTriple a b c)
    , "a x b x c = " <> show (vectorTriple a b c)
    , "a . d     = " <> show (dot a d)
    ]


dotProduct
  :: Num a
  => [a] -> [a] -> Either String a
dotProduct xs ys
  | length xs /= length ys =
    Left "Dot product not defined - vectors differ in dimension."
  | otherwise = Right (sum $ zipWith (*) xs ys)

crossProduct
  :: Num a
  => [a] -> [a] -> Either String [a]
crossProduct xs ys
  | 3 /= length xs || 3 /= length ys =
    Left "crossProduct is defined only for 3d vectors."
  | otherwise = Right [x2 * y3 - x3 * y2, x3 * y1 - x1 * y3, x1 * y2 - x2 * y1]
  where
    [x1, x2, x3] = xs
    [y1, y2, y3] = ys

scalarTriple
  :: Num a
  => [a] -> [a] -> [a] -> Either String a
scalarTriple q r s = crossProduct r s >>= dotProduct q

vectorTriple
  :: Num a
  => [a] -> [a] -> [a] -> Either String [a]
vectorTriple q r s = crossProduct r s >>= crossProduct q

-- TEST ---------------------------------------------------
a = [3, 4, 5]

b = [4, 3, 5]

c = [-5, -12, -13]

d = [3, 4, 5, 6]

main :: IO ()
main =
  mapM_ putStrLn $
  zipWith
    (++)
    ["a . b", "a x b", "a . b x c", "a x b x c", "a . d", "a . (b x d)"]
    [ sh $ dotProduct a b
    , sh $ crossProduct a b
    , sh $ scalarTriple a b c
    , sh $ vectorTriple a b c
    , sh $ dotProduct a d
    , sh $ scalarTriple a b d
    ]

sh
  :: Show a
  => Either String a -> String
sh = either (" => " ++) ((" = " ++) . show)


  

You may also check:How to resolve the algorithm Conway's Game of Life step by step in the SQL programming language
You may also check:How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Java programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the Neko programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the D programming language