Saturday 18 June 2022

Chapter 24 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Exercise 12

Implement
Matrix<double> operator*(Matrix<double,2>&, Matrix<double>&);
and
Matrix<double, N> operator+(Matrix<double, N>&, Matrix<double, N>&)
If you need to, look up the mathematical definitions in a textbook.


So the first one is already supplied on page 913. Here he refers to it as a vector, but a 1 dimensional matrix is a vector.

For the second I discovered you can't use Bjarne's code to initialise a 3d matrix with a 3d array as it will cause a heap corruption. So I created a little function to use with the inbuilt apply to quickly populate the 3d matrix.

Being able to add a matrix of any incoming dimension was a bit of a head scratcher until I came across this post:

std::transform takes in an iterator to the beginning and end of your container, then uses a function object on each element. Matrix doesn't have iterators but it does supply a pointer to the beginning of the data. So I supplied transform with the raw data pointers, the size of the matrix and a new matrix to put the data into to. This way we don't have to care how many dimensions there actually are.

And with that chapter 24 is done! For once I didn't take 3 months to finish a chapter. Really looking forward to the embedded systems one (which is next) and the final chapter on C programming.

No comments:

Post a Comment