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 AA, and its elements are referred to by their row and column indices, such as AijA_{ij} for the element in the ithi^{th} row and jthj^{th} column. The size of a matrix is given by its number of rows and columns, and it is often written as m×nm \times n, where mm is the number of rows and nn is the number of columns. For example, here's a square matrix (having equal rows and columns) of dimension 3×3.3 \times 3.

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
Demo of the matrix calculator

Let’s review the input file, Matrices.txt, to better understand these concepts.

4 --> Total Number of Matrices
2 2 --> Number of Rows and Columns of the First Matrix
1 2
4 5
2 2 --> Number of Rows and Columns of the Second Matrix
7 8
2 2
3 3 --> Number of Rows and Columns of the Third Matrix
4 7 5
9 8 5
3 7 2
4 3 --> Number of Rows and Columns of the Fourth Matrix
4 6 5
3 3 4
5 8 9
4 5 3
Structure of the input file

Here are the instructions for interacting with the code output:

  1. Click the “Run” button to start the execution.

  2. The calculator will display the matrices it reads from the file. The sample output is shown below:

M1:
1 2
4 5
M2:
7 8
2 2
M3:
4 7 5
9 8 5
3 7 2
M4:
4 6 5
3 3 4
5 8 9
4 5 3
Sample matrix output
  1. Then, the following menu will be displayed:

Menu
1. Binary Arithmetic
2. Accumulation Assignments
3. Unary Scalar Arithmetic
4. Unary Increment/Decrement
5. Unary Operations
6. Comparison Of Matrices
7. Rotation Of Matrices
8. Exit
Sample menu
  1. 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 2
4 5
M2:
7 8
2 2
M3:
4 7 5
9 8 5
3 7 2
M4:
4 6 5
3 3 4
5 8 9
4 5 3
_______________________________
Menu
1. Binary Arithmetic
2. Accumulation Assignments
3. Unary Scalar Arithmetic
4. Unary Increment/Decrement
5. Unary Operations
6. Comparison Of Matrices
7. Rotation Of Matrices
8. Exit
Choice: 9
Invalid choice...!
Output for invalid choice
  1. Let’s say we entered 1. At this stage, the program will ask us to enter input in a specific way (M3=M1+M2 M3 = M1 +M2), as displayed in the example below:

Matrices
_______________________________
M1:
1 2
4 5
M2:
7 8
2 2
M3:
4 7 5
9 8 5
3 7 2
M4:
4 6 5
3 3 4
5 8 9
4 5 3
_______________________________
Menu
1. Binary Arithmetic
2. Accumulation Assignments
3. Unary Scalar Arithmetic
4. Unary Increment/Decrement
5. Unary Operations
6. Comparison Of Matrices
7. Rotation Of Matrices
8. Exit
Choice: 9
Invalid choice...!
Sample menu of the matrix calculator

Note: Here, Mi is a matrix that stores the sum of the Mj and Mk matrices, and * could be any arithmetic operator (*, +, -, /, %).

  1. 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 Mi =MjM_i\space\odot=M_j, ii and jj could be any two indexes, and \odot could be (+,,,/).(+, -, *, /).

  • For unary scalar arithmetic, enter 3.

    Choice: 3
    Enter Expression Mi = Mj ⊙ number (⊙: + or - or * or /):
    M1 = M2 + 3
    

    In Mi=MjnumberM_i=M_j\odot number, ii and jj could be any two indexes, numbernumber could be any integer, and and \odot 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 2×22 \times 2 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
    
  1. 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 10
6 7
M2:
7 8
2 2
M3:
4 7 5
9 8 5
3 7 2
M4:
4 6 5
3 3 4
5 8 9
4 5 3
_______________________________
Menu
1. Binary Arithmetic
2. Accumulation Assignments
3. Unary Scalar Arithmetic
4. Unary Increment/Decrement
5. Unary Operations
6. Comparison Of Matrices
7. Rotation Of Matrices
8. Exit
_______________________________
Choice: 8
Output of the program and termination

Note: For the sake of simplicity, we have only included four matrices. We can see them in the Matrices.txt file.