w06-bsc-g01-class-basics
Class basics : Matrix
In this example, we explore the basics of Classes in C++. For this
purpose, we have converted the matrix
pointer implemented in the
last few examples into a class Matrix
. The class interface
matrix.hpp
and the implementation matrix.cpp
are in folder
tehpc/src
. Please go through the two files and observe its
various member variables and member functions.
An example on how to usage the class Matrix
and its various
functions is added in the folder tehpc/basics
.
Go into tehpc/basics/
. Consider the following code:
g01_class_basics.cpp
Compile the code with
- load appropriate modules by performing the following command in your terminal:
source tehpc/tools/tehpc_load_modules.sh
. - consider
tehpc/basics/CMakeLists.txt
and make sure thatg01_class_basics
is included in compilation - go into the
tehpc/build/
folder (possibly in another terminal) - type:
cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DTEHPC_BASICS:STRING=ON ..
(you only have to do this the very first time). - type:
make
(do this whenever you want to recompile). - To run the complied code, go into
tehpc/build/basics/
. - type:
./g01_class_basics
.
Obeserve the output and the implementation in the file
g01_class_basics.cpp
and answer the following questions as comments direclty on your MR.
-
You will see that to define a vector we have used the type
std::vector<double>
from#include <vector>
. How is it different from thedouble*
type that we have been using till now to define a vector? What are the advantages ofstd::vector
overdouble*
? Isstd::vector
a class? -
In the example
g01_class_basics.cpp
we are performing various matrix operations such as initializing a matrix to value 1, matrix-scalar multiplication, matrix-vector multiplication, matrix-matrix multiplication and printing matrix usingstd::cout
. What are the specific member functions inmatrix.hpp
that allow us to perform all these matrix operations? What is this practice generally referred to as and how is it useful? Check the member functioninverse
inmatrix.cpp
. How is it different from the functioninverseMatrix
implemented in the last exercise session, check the filebasics/newton_raphson_function.cpp
. -
The matrix class defined in
matrix.hpp
usesoperator()(UInt i, UInt j)
to access and to store values at row i and column j. What is so special about the way entries are stored and is there an advantage of storing the values in that manner?