# 12: 06 In-Class Assignment - Matrix Multiply

12: 06 In-Class Assignment - Matrix Multiply

## Question: Instructions: As part of the completion of the course, a.

Instructions:
As part of the completion of the course, a major project must be completed. The project demonstrates the ability to apply the various skills learned in class to solve a practical problem.
Projects must be approved, and meet the following criteria:
1. Complexity of the project shall be significantly greater than a typical homework assignment.
2. The project must utilize multiple concepts learned in class and minimally contain array or vector manipulation, user defined classes, and file IO.
3. The scope of work of the project must be consistent with a two month timeline.
Project Proposal:
The project proposal shall state the goal of the program and contain a brief outline of the planned functionality of the program. Key features of the program should be mentioned, as well as a description of how the user will interact with the program (simple menus, text instructions, arrow keys, etc.)

Progress Report:
The progress report is to convey the current status of the project including what has been achieved, and the plan for completion.

Matrix calculation
Project Description:
The proposed program shall implement a matrix calculator which take these actions: add, multiply and transpose. The program will need to test whether the input matrix is legal or illegal and consider if this matrix could be added, multiplied or transposed
Matrix is read from file, result will be written to a new file
Topics Covered:
• Two Dimensional Arrays
o Matrix item numbers are stored in a 2D array
• Classes
o Matrix will be stored as a class with method that showing the matrix, doing the calculation
• Functions
o We will use a lot of function to test the matrix when we want to add, multiply or transpose the matrix
o Multiply function also ask for several smaller functions
• File IO
o Matrices is read from file

Progress Report
1. Challenge Definition
a. We need to do calculation on matrix including : add, multiply and transpose
b. The program will need to test whether the input matrix is legal or illegal and consider if this matrix could be added, multiplied or transposed
c. Matrix is read from file, result will be written to a new file
2. Describe current progress, problems encountered
a. Current progress:
i. I complete transposed method
ii. I haven’t implemented method to read matrix from file as well as other method
b. Problem encountered
i. I have problems while reading the input file
ii. I also have problems with adding and multiplying implementation
3. Describe future plan for completion (tasks that require completion, and the strategy to complete them)
b. Strategy to complete them

## Assignments Related to Lectures and Readings

This is one of over 2,400 courses on OCW. Explore materials for this course in the pages linked along the left.

MIT OpenCourseWare is a free & open publication of material from thousands of MIT courses, covering the entire MIT curriculum.

No enrollment or registration. Freely browse and use OCW materials at your own pace. There's no signup, and no start or end dates.

Knowledge is your reward. Use OCW to guide your own life-long learning, or to teach others. We don't offer credit or certification for using OCW.

Made for sharing. Download files for later. Send to friends and colleagues. Modify, remix, and reuse (just remember to cite OCW as the source.)

MIT OpenCourseWare is an online publication of materials from over 2,500 MIT courses, freely sharing knowledge with learners and educators around the world. Learn more »

Massachusetts Institute of Technology

OK, this is a very peculiar way to do this, but if that's the assignment, there's nothing to be done for it.

I take that back. It's a ridiculous way to do it. Are you certain that you're supposed to have three different matrices in a single object? Normally, I would assume that what is desired is to have each object represent a single matrix, with the multiplication method taking a Matrix as an argument. doing it this way is absurdly limiting, and simply bad design.

If I were in a course where a professor handed out this assignment, I would walk out, because the instructor obviously doesn't know how to write object-oriented programs.

I suspect, however, that there's a simple miscommunication involved. I don't normally ask this, but could you post the exact assignment for us?

The prompt asks me to have the user enter each row of the matrix one by one. I am not sure how to do this. Also, the prompt states
At minimum, the program will:
Use a class to hold 3 matricies of type private and use member function, only use non-inline functions and no friend functions allowed.
Have a member function called mult that will perform the actal matrix multiplication
Have a member function that inputs the data into the two input arrays from the keyboard using screen prompt above
Have a member function or functions that initialize any program arrays
The program will ask the user if he/she wishes to run the program again.

## Status quo

As of May 2018 (numpy 1.15, relevant pull request and commit) the matrix class docstring contains the following note:

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

And at the same time a PendingDeprecationWarning has been added to matrix.__new__ . Unfortunately, deprecation warnings are (almost always) silenced by default, so most end-users of numpy will not see this strong hint.

Finally, the numpy roadmap as of November 2018 mentions multiple related topics as one of the "tasks and features [the numpy community] will be investing resources in":

This work assignment in operator overloading .I need to use operators * , [][] , = , + , - , << on objects of type matrix for example add to matrix using this code: m=m+s .

I already sent the code to my teacher but I still want your opinion so I can improve the next code.

I have been told to "throw exceptions rather than asserts" and to "make your base class destructor virtual". What is the right way to do it? I never used exception before and not familiar with the concept of virtual destructor.

Prefer a single allocation instead of doing multiple allocations in the constructor, it would be simpler to do only a single allocation. This is both faster and simpler

@Edward wrote this, but is it possible to allocate 2 dimensional array with an allocation?

Another thing I didn't understand is what to do when main is trying to use the function illegally for example add 2 matrix that not in the same size. I created a new object and gave him the same data as one then called the function and returned it. m=m+s in this example, if m and s are not in the same size I just returned new object with the values of m . Is it the right way?

## 3. Systems of Linear Equations with Many Solutions¶

When we solve a system of equations of the form $Ax=b$, we mentioned that we may have three outcomes:

Assume that we have $m$ equations and $n$ unkowns.

Case 1 $m < n$, we do not have enough equations, there will be only TWO outcomes: no solution, or infinity many solutions.

Case 2 $m = n$, we may have all THREE outcomes. If the determinate is nonzero, we have a unique solution, otherwise, we have to decide the outcome based on the augmented matrix.

Case 3 $m>n$, we have more equations than the number of unknowns. That means there will be redundant equations (we can remove them) or conflict equations (no solution). We may have all THREE outcomes.

We talked about several methods for solving the system of equations. The most general one is the Gauss-Jordan or Gaussian elimination, which works for all three cases. Note that Jacobian and Gauss-Seidel can not work on Case 1 and Case 3.

We will focus on the Gaussian elimination. After the Gaussian elimiation, we look at the last several rows (could be zero) with all zeros except the last column.

If one element from the corresponding right hand side is not zero, we have that $equals some nonzero number, which is impossible. Therefore, there is no solution. E.g.,$left[ egin 1 & 0 & 0 0 & 1 & 0 0 & 0 & 1 0 & 0 & 0 end , middlevert , egin 2 3 4 5 end ight] $In this case, we say that the system is inconsistent. Later in the semester we will look into methods that try to find a "good enough" solution for an inconsistant system (regression). Otherwise, we remove all the rows with all zeros (which is the same as removing redundant equations). If the number of remaining equations is the same as the number of unknowns, the rref is an identity matrix, and we have unique solution. E.g.,$left[ egin 1 & 0 & 0 0 & 1 & 0 0 & 0 & 1 0 & 0 & 0 end , middlevert , egin 2 3 4 0 end ight] Rightarrow left[ egin 1 & 0 & 0 0 & 1 & 0 0 & 0 & 1 end , middlevert , egin 2 3 4 end ight] $If the number of remaining equations is less than the number of unknonws, we have infinite many solutions. Consider the following three examples:$left[ egin 1 & 0 & 0 0 & 1 & 0 0 & 0 & 0 end , middlevert , egin 2 3 0 end ight] Rightarrow left[ egin 1 & 0 & 0 0 & 1 & 0 end , middlevert , egin 2 3 end ight] Rightarrow x= [2, 3, x_3]^ op $where$x_3$is a free variable.$left[ egin 1 & 2 & 0 0 & 0 & 1 0 & 0 & 0 end , middlevert , egin 2 3 0 end ight] Rightarrow left[ egin 1 & 2 & 0 0 & 0 & 1 end , middlevert , egin 2 3 end ight] Rightarrow x= [2-2x_2, x_2, 3]$where$x_2$is a free variable.$left[ egin 1 & 2 & 0 & 1 0 & 0 & 1 & 3 0 & 0 & 0 & 0 end , middlevert , egin 2 5 0 end ight] Rightarrow left[ egin 1 & 2 & 0 & 1 0 & 0 & 1 & 3 end , middlevert , egin 2 5 end ight] Rightarrow x= [2-2x_2-x_4, x_2, 5-3x_4, x_4]$where$x_2$and$x_4$are free variables. ✅ **QUESTION:** Assume that the system is consistent, explain why the number of equations can not be larger than the number of unknowns after the redundant equations are removed? Put the solution to the above question here. ✅ **DO THIS:** If there are two solutions for$Ax=b$, that is$Ax=b$and$Ax'=b$while$x eq x'$. Check that$A(cx+(1-c)x') = b$for any real number$c$. Therefore, if we have two different solutions, we have infinite many solutions. Put the solution to the above question here. If$Ax=b$and$Ax'=b$, then we have$A(x-x')=0$. If$x$is a particular solution to$Ax=b$, then all the solutions to$Ax=b$are$ Av=0>$. The solution for$Ax=0$is always a subspace. After removing the redundant rows, if the number of equations is the same as the number of unknowns, we have a unique solution. If the difference between this two number is 1, all the solutions lie on a line. If the difference is 2, all the solutions lie on a 2-D plane. ✅ **QUESTION:** What is the solution to the following set of linear equations in augmented matrix form?$A = left[ egin -2 & 4 & 8 1 & -2 & 4 4 & -8 & 16 end , middlevert , egin 0 0 0 end ight] \$

Put the solution to the above question here.

## 12: 06 In-Class Assignment - Matrix Multiply

I'm writing a program just to show that I can use operators in matrices. I've done it for addition and subtraction but I'm struggling to apply operator * to multiply two 3x3 matrices. Any help would be appreciated, thank you.

The errors lie line 169 :
- you need to take line "i" of the left matrix, not "j"
- you're supposed to multiply the values, not add them
- the results should be accumulated, but SetMatrixData only stores the newest one.

To improve readability, you should define a 2nd Matrix::operator():
double operator ()( int m, int n) const

Using it and the fact that your constructor initializes the matrix elements to 0, this line could be rewritten Temp1(i,j) += (* this )(i,k) * x(k,j)

The error that toum pointed out is a good reason to use names something like Row & Col not i & j. As you have found out these errors are hard to see.

I have seen this lots of times before - do a find replace and the error is much easier to see. It is good to use meaningful names.

You seem to have some of these:

Although you make adjustments in your functions, IMO it might be better if you always worked from 0

This way you don't have to adjust any subscripts, and avoid the possibility of overstepping the array.

You have all your code in 1 file - it is much better practice to put the class declaration in a header file, with the functions definitions in a .cpp file. If you are using an IDE, is there a class wizard which creates the files and can create functions stubs from declarations? My IDE (KDevelop on Linux) does that, I am not sure if yours does.

## Next Steps

Now that we have explored the basic usage of the Eigen library, as well as glimpsed at the higher functionality, we are ready to carry out Numerical Linear Algebra (NLA) with Eigen as the basis (excuse the pun) for our code. This will help us solve problems in quantitative trading (such as using the Cholesky Decomposition for correlated asset paths) and derivatives pricing (solving Black-Scholes via implicit Finite Difference Methods and tridiagonal LR decomposition).

### QSAlpha

Join the QSAlpha research platform that helps fill your strategy research pipeline, diversifies your portfolio and improves your risk-adjusted returns for increased profitability.