Matrix
and Vector
classes
from programming assignment 3 suffer
from a few design flaws. In particular:
Vector
objects is identical to the code for
adding and subtracting two \(n \times 1\) or
\(1 \times n\) Matrix
objects).
Matrix
class contains "unnecessary" methods
(e.g., to pre-multiply and post-multiply a Matrix
by a Vector
).
Matrix
and an
\(n \times n\) Matrix
a
Vector
or a \(1 \times n\) Matrix
?).
To overcome these shortcomings,
in this assignment if you need a row Vector
you
will use a \(1 \times n\) Matrix
and if you
need a column Vector
you will use a
\(m \times 1\) Matrix
instead.
However, you will still have a Vector
"class" because
it will contain the friend functions that are specific to
Vector
objects.
This assignment is about specialization in C++ (and, to a lesser extent, about templates). It does not use any new topics from our study of vector/matrix arithmetic.
Note that, as is traditional in many applications including computer graphics, we are now only considering column vectors. (If you need a row vector for some reason you can use a \(1 \times n\) matrix.)
Matrix
class from
from programming assignment 3.
Next, you should make the following modifications to the copy:
Matrix
.
Vector
.
(It isn't necessary since a Vector
"is a"
Matrix
in this version.)
double dot(const Matrix& a, const Matrix& b)
that calculates the dot product (more commonly known as the
scalar product) of two Matrix
objects.
(As with vectors, the dot/scalar product is the sum of the
component-wise products of the elements. The dot/scalar product
is only defined for matrices that have the same size.)
double get(int i) const
and another with the
signature
double& operator()(int i)
that return a
particular element of the calling Matrix
if it contains
a single row or single column. They must throw an
out_of_range
if i
is out of range and
length_error
is the Matrix
is neither
a single row or single column.
Matrix getColumn(int c) const
that returns
a column of the calling Matrix
.
Matrix multiply(const Vector& a, const Vector& b)
.
(It isn't necessary since a Vector
"is a"
Matrix
in this version.)
Matrix operator|(const Matrix& a, const Matrix& b)
that concatenates the columns of the a
and the
columns of b
.
Vector operator*(const Vector& v, const Matrix& m)
and
Vector operator*(const Matrix& m, const Vector& v)
.
(They aren't necessary since a Vector
"is a"
Matrix
in this version.)
void setValues(const Matrix& other);
that
is used by the copy constructor and the assignment operator.
assertEquals()
method that can be passed
an object that has an operator==()
function.
It also includes an assertThrows()
method that makes use
of lambda expressions.
pa4.zip
that contains
just Vector.h
, Vector.cpp
,
Matrix.h
, Matrix.cpp
using Autolab.
Vector
class specializes
the Matrix
class, it does not contain any additional
methods (other than constructors), nor does it override any methods
in the Matrix
class. In fact, the Vector
class only exists to add friend functionality to the Matrix
class.
One could, instead, have a Vector
class that "really"
specializes the Matrix
class and use distinct
Matrix
and Vector
objects. However, such
a design would have several awkward aspects. You should ask yourself
the following questions about such a design.
Vector
object
and a Matrix
? What are the reasonable options?
Matrix
to either a column or row Vector
? What is the
type of the result?
Vector
class in order to provide type safety?
Matrix
and Vector
classes.
The parameter(s) of the templates will be the size(s) of the
objects (i.e., the number of rows and columns).
Copyright 2020