Install docker
brew install --cask docker
Clone the repository and build the docker image (work in the file with dockerfile
)
git clone
cd LinearAlgebraLibrary
docker build -t algebra .
Run the container, /usr/src/app
is your working directory in the container.
docker run -it -d --name LinearAlgebra -v ./:/usr/src/app algebra /bin/bash
Compile and run the unit tests
cd /usr/src/app/src
g++ -I ../include -o unitTest main.cpp hw1.cpp unit_test.cpp -lgtest -lgtest_main -pthread
./unitTest
Copy hw1.h
and hw1.cpp
to your project.
Write the code in main.cpp
to use the library.
#include "hw1.h"
Example:
Matrix matrix{algebra::zeros(5, 6)};
Matrix matrix{algebra::random(4, 4, -5, 7)};
Matrix inverse{algebra::inverse(matrix)};
Other usage examples can be found in unit_test.cpp
.
In this homework we are going to implement a Linear Algebra library for c++.
In this library we are going to implement different functions to allow our users to easilly work and manipulate different matrices.
We will be implementing all our functions in hw1.cpp
and hw1.h
. remember you should put all declarations in the .h
and all the implementations in the .cpp
file.
In order to have a matrix in c++ we need a 2D vector. We use the following line in order to reduce our syntax.
using Matrix = std::vector<std::vector<double>>;
From now on we can use the keyword Matrix instead of defining the 2D vector everytime. So, go ahead and put the above line in your code.
note. Define all your functions in a namespace called algebra
.
Now, lets discuss each functions and see how they should be implemented.
-
zeros implement this function so that it will create a
n x m
matrix with all elements equal to zero.Matrix zeros(size_t n, size_t m)
-
ones implement this function so that it will create a
n x m
matrix with all elements equal to one.Matrix ones(size_t n, size_t m)
-
random implement this function so that it will create a
n x m
matrix with all elements a random number betweenmin
andmax
.Matrix random(size_t n, size_t m, double min, double max)
note. to generate a random number you are not allowed to use
rand
andsrand
functions. instead, use the<random>
library first introduced in c++11. -
show implement this function so that it will display the matrix in a beautiful way.
void show(const Matrix& matrix)
note. to display your matrices use the
<iomanip>
library. when showing the numbers, each element of the matrix should have exactly 3 decimal places. -
multiply - scalar number
implement this function so that it multiplies the
matrix
into the constant scalarc
.Matrix multiply(const Matrix& matrix, double c)
-
multiply - matrix
implement this function so that it multiplies the
matrix1
intomatrix2
. (this is not an element-wise multiplication)Matrix multiply(const Matrix& matrix1, const Matrix& matrix2)
-
sum - scalar number
implement this function so that it adds the constant number
c
to every element ofmatrix
.Matrix sum(const Matrix& matrix, double c)
-
sum - matrix
implement this function so that it adds 2 matrices to each other.
Matrix sum(const Matrix& matrix1, const Matrix& matrix2)
-
transpose implement this function so that it will generate the transpose matrix of the input
matrix
.Matrix transpose(const Matrix& matrix)
-
minor implement this function so that it will create the minor of the input
matrix
with respect to nth row and mth column.Matrix minor(const Matrix& matrix, size_t n, size_t m)
note. the indices in c++ start from 0
not 1, for example:
-
determinant implement this function so that it calculates the determinant of the input
matrix
.double determinant(const Matrix& matrix)
you should only use the minor approach to calculate the determinant, any other methods (such as upper-triangular or ...) is not allowed.
-
inverse implement this function so that it generates the
matrix
's inverse.Matrix inverse(const Matrix& matrix)
-
concatenate implement this function so that it will concatenate
matrix1
andmatrix2
along the specified axis. (axis=0
: on top of each other |axis=1
: alongside each other).Matrix concatenate(const Matrix& matrix1, const Matrix& matrix2, int axis=0)
-
elementary row operations we want to implement elementary row operation (ERO) functionality for our library. as you know we have 3 EROs:
1)
swapping two rows.2)
multiplying a row into a constant number.3)
multiplying a row into a constant number and add it to another row.So, lets define the following 3 functions to do this.
-
swap swaps r1th row with r2th.
Matrix ero_swap(const Matrix& matrix, size_t r1, size_t r2)
-
multiply multiplies every element in rth row with constant number
c
.Matrix ero_multiply(const Matrix& matrix, size_t r, double c)
-
sum adds r1th x c into r2th row.
Matrix ero_sum(const Matrix& matrix, size_t r1, double c, size_t r2)
-
-
upper triangular implement this function so that it will calculate the upper triangular form of the
matrix
using the ERO operations.Matrix upper_triangular(const Matrix& matrix)
note. to make a upper triangular matrix you have to zero out all the elements which are below the main diagonal, using the elemnts on the main diagonal. consider the following example:
use the same trick to zero out
l
, usingj
like the following.
bonus question what happens if we come across a zero on our main diagonal for example
a
orj
in the example above? solve this problem.The last unit-test is for the bonus question. don't worry too much if it fails.