Matrix Calculator (Overview and Demo)
Get familiar with the matrix calculator.
We'll cover the following
Before making the matrix calculator, let’s briefly recap matrices and their arithmetic.
Matrix
In mathematics, a matrix is a rectangular array of numbers, symbols, or expressions arranged in rows and columns. Matrices are often used to represent linear transformations and systems of linear equations. They have many applications in various fields, such as physics, engineering, economics, computer graphics, and data science/machine learning.
A matrix is usually denoted by a capital letter, such as
In this lesson, we’ll look at an executable demo of the matrix calculator using classes, along with a sample program that can be used as a guideline for testing the demo. This will allow us to gain a better understanding of what we’ll be required to build by the end of the section, which is the complete implementation of the matrix calculator through operator overloading.
Matrix calculator demo
Let’s run the demo program below to obtain a comprehensive understanding of a menu-based matrix calculator. The code has been hidden intentionally to focus solely on understanding the calculator’s functionality, which will help prepare us for what’s to come in the case study.
4 2 2 1 2 4 5 2 2 7 8 2 2 3 3 4 7 5 9 8 5 3 7 2 4 3 4 6 5 3 3 4 5 8 9 4 5 3
Let’s review the input file, Matrices.txt
, to better understand these concepts.
4 --> Total Number of Matrices2 2 --> Number of Rows and Columns of the First Matrix1 24 52 2 --> Number of Rows and Columns of the Second Matrix7 82 23 3 --> Number of Rows and Columns of the Third Matrix4 7 59 8 53 7 24 3 --> Number of Rows and Columns of the Fourth Matrix4 6 53 3 45 8 94 5 3
Here are the instructions for interacting with the code output:
Click the “Run” button to start the execution.
The calculator will display the matrices it reads from the file. The sample output is shown below:
M1:1 24 5M2:7 82 2M3:4 7 59 8 53 7 2M4:4 6 53 3 45 8 94 5 3
Then, the following menu will be displayed:
Menu1. Binary Arithmetic2. Accumulation Assignments3. Unary Scalar Arithmetic4. Unary Increment/Decrement5. Unary Operations6. Comparison Of Matrices7. Rotation Of Matrices8. Exit
Now, we’ll need to input our choice in the form of an integer (1–8) because we have only eight options. If we enter any other number, the program will display the
Invalid choice...!
message, and it’ll display the matrices and ask for the input again, as shown below
Matrices_______________________________M1:1 24 5M2:7 82 2M3:4 7 59 8 53 7 2M4:4 6 53 3 45 8 94 5 3_______________________________Menu1. Binary Arithmetic2. Accumulation Assignments3. Unary Scalar Arithmetic4. Unary Increment/Decrement5. Unary Operations6. Comparison Of Matrices7. Rotation Of Matrices8. ExitChoice: 9Invalid choice...!
Let’s say we entered 1. At this stage, the program will ask us to enter input in a specific way (
), as displayed in the example below:
Matrices_______________________________M1:1 24 5M2:7 82 2M3:4 7 59 8 53 7 2M4:4 6 53 3 45 8 94 5 3_______________________________Menu1. Binary Arithmetic2. Accumulation Assignments3. Unary Scalar Arithmetic4. Unary Increment/Decrement5. Unary Operations6. Comparison Of Matrices7. Rotation Of Matrices8. ExitChoice: 9Invalid choice...!
Note: Here,
Mi
is a matrix that stores the sum of theMj
andMk
matrices, and*
could be any arithmetic operator (*
,+
,-
,/
,%
).
Here are some sample examples to run:
-
For testing accumulation based operator, enter 2.
Choice: 2 Enter Expression Mi ⊙= Mj (⊙: + or - or * or /): M1 += M2
In , and could be any two indexes, and could be
-
For unary scalar arithmetic, enter 3.
Choice: 3 Enter Expression Mi = Mj ⊙ number (⊙: + or - or * or /): M1 = M2 + 3
In , and could be any two indexes, could be any integer, and and could be
-
For pre/post increment/decrement, enter 4.
Choice: 4 Enter Expression ⊙Mi or Mi⊙ (⊙: ++ or -- ): ++M1
-
For testing unary operations (e.g., negative of a matrix, transpose, and inverse of a matrix), enter 5.
Choice: 5 Enter Expression Mi = ⊙Mj (-[Unary Minus] or ![Transpose] or ~[Inverse]): M1 = -M2
-
For testing comparison of matrices (equal or not equal matrices, subset, superset, etc.), enter 6.
Choice: 6 Enter Expression Mi Mj (Check equality): M1 M2
-
For testing the rotation of matrices, enter 7.
Choice: 7 Enter Mi>Degree (for clockwise rotation) and Mi<Degree (for counterclockwise rotation): M1 > 90
We’ll see the result of said operation, and the program will ask which option to perform again and again until we enter 8, and the program terminates.
Matrices_______________________________M1:8 106 7M2:7 82 2M3:4 7 59 8 53 7 2M4:4 6 53 3 45 8 94 5 3_______________________________Menu1. Binary Arithmetic2. Accumulation Assignments3. Unary Scalar Arithmetic4. Unary Increment/Decrement5. Unary Operations6. Comparison Of Matrices7. Rotation Of Matrices8. Exit_______________________________Choice: 8
Note: For the sake of simplicity, we have only included four matrices. We can see them in the
Matrices.txt
file.