Assignment 2
Assignment 2
Assignment 2
Dr. Ming Ming Tan
McMaster University
1 Background
A vector is a mathematical entity that has direction and magnitude. A
vector can be identified with a point in Euclidean space. A point in 3-
dimensional Euclidean space can be represented with Cartesian coordinates
as a triple (3-tuple) V = (a, b, c) of real numbers where a is the x-coordinate,
b is the y-coordinate, and c is the z-coordinate of the point, respectively. (A
point in 3-dimensional Euclidean space could also be represented in other
ways such as with polar coordinates.)
Suppose V = (a, b, c) and V 0 = (a0 , b0 , c0 ) are two vectors represented by
points in 3-dimensional Euclidean space. V is the zero vector if a = b = c =
0. The scalar product of a real number r and √ V is the vector (r ∗a, r ∗b, r ∗c).
The magnitude of V is the real number a2 + b2 + c2 . The sum of V and
V 0 is the vector (a + a0 , b + b0 , c + c0 ). The difference of V and V 0 is the sum
of V and the scalar multiple of -1 and V 0 . The distance between V and V 0
is the magnitude of the difference of V and V 0 . The dot product of V and
V 0 is the real number a ∗ a0 + b ∗ b0 + c ∗ c0 . The vectors V and V 0 are said to
1
be orthogonal if the dot product of the two vectors is equal to 0. A list of
vectors is called pairwise orthogonal if each pairing of them is orthogonal. In
other words, a list of vectors [V1 , V2 , . . . , Vn ] is pairwise orthogonal if Vi and
Vj are orthogonal (i.e., their dot product is equal to 0) for all i 6= j, i, j =
1, . . . , n. As an example, [(1, −1, 0), (1, 1, 0), (0, 0, 0)] is pairwise orthogonal
but [(1, −1, 0), (−1, 1, 0), (1, 1, 0)] is not pairwise orthogonal.
2 Assignment 2
The purpose of this assignment is to create a Haskell module for the vector
space of 3-dimensional vectors whose coordinates are of type Double.
2.1 Requirements
1. The name of your Haskell file is Assign 2 YourMacID.hs where Your-
MacID is your actual MacID.
2. Your name, MacID, the date, and “Assignment 2” are given in com-
ments at the top of your file.
3. The first uncommented line of the file should be
5. The file includes a constant named vecZero of type Vector that im-
plements the zero vector constant.
6. The file includes a function named vecScalarProd of type Double ->
Vector -> Vector that implements the scalar product function.
7. The file includes a function named vecSum of type Vector -> Vector
-> Vector that implements the sum function.
8. The file includes a function named vecMagnitude of type Vector ->
Double that implements the magnitude function.
9. The file includes a function named vecF of type Vector -> [Vector]
-> [Double] such that vecF v vs equals a list whose ith entry is the
distance between the vector v and the i-th vector in the list vs.
10. The file includes a function named vecDot of type Vector -> Vector
-> Double that implements the dot product function.
11. The file includes a function named vecOrthogonal of type [Vector]
-> Bool such that vecOrthogonal vs returns True if vs is empty or
if vs is pairwise orthogonal, returns False otherwise.
2
12. Your file can be imported into GHCi and all of your functions perform
correctly.
2.2 Testing
Include in your file a test plan for the functions vecScalarProd, vecSum,
vecMagnitude, and vecF. The test plan must include at least three test
cases for each function. Each test case should have following form:
3
Function: Name of the function being tested.
Test Case Number: The number of the test case.
Input: Inputs for function.
Expected Output: Expected output for the function.
Actual Output: Actual output for the function.
The test plan should be at the bottom of your file in a comment region
beginning with a {- line and ending with a -} line.
3.1 Requirements
1. The name of your Haskell file is Assign 2 ExtraCredit YourMacID.hs
where YourMacID is your actual MacID.
2. Your name, MacID, the date, and “Assignment 2 Extra Credit” are
given in comments at the top of your file.
4. The file contains the following type definitions and type class defini-
tion:
4
6. The file includes a function named vecF of type (Floating
a,VectorSpace v) => v a -> [v a] -> [a] such that vecF x y
equals a list whose ith entry is the distance between x and the ith entry
of the list y. Use the functions defined in the type class VectorSpace
to define vecF.
7. Your file successfully loads into GHCi and all of your functions perform
correctly.
3.2 Testing
Include in your file a test plan (as described above) for the functions
vecScalarProd, vecSum, vecMagnitude, and vecF. The test plan must in-
clude at least three test cases for each function.