OperarMatrices.py provides a looping console menu that lets you perform three matrix operations: addition, subtraction, and multiplication. You enter matrix dimensions and values interactively, and the program prints the result before returning you to the menu.
How to run
os).
Menu overview
When the program starts, you see the following menu on every iteration:0 at any time to exit. After each operation, the terminal is cleared with os.system('cls') (Windows) and the menu is shown again.
os.system('cls') only clears the terminal on Windows. On Linux or macOS the clear command has no effect and the menu simply reprints below the previous output.Functions
IngresarMatriz(T1, T2=None)
IngresarMatriz(T1, T2=None)
Prompts you to fill a matrix element by element. When only
T1 is passed, it creates a square T1 × T1 matrix. When both T1 and T2 are passed, it creates a rectangular T1 × T2 matrix (T1 rows, T2 columns).MostrarMatriz(matriz)
MostrarMatriz(matriz)
Prints the result matrix with a header. The
*m unpacking removes brackets and commas from each row for clean console output.SumarMatriz(matriz)
SumarMatriz(matriz)
Accumulates values from
matriz into the shared matrizRes result matrix. Both matrices must be square with the same size T.The first matrix in an addition or subtraction session is always passed to
SumarMatriz, regardless of the selected operation. This seeds matrizRes with real values instead of operating against a zero matrix.RestarMatriz(matriz)
RestarMatriz(matriz)
Subtracts
matriz values from matrizRes. Only called for matrices 2, 3, … in a subtraction session.MultiplicarMatriz(matriz, T1, T2)
MultiplicarMatriz(matriz, T1, T2)
Multiplies
Pmatriz (the first matrix, stored as a global) by matriz (the second) and writes the result into matrizRes. Uses three nested loops implementing the standard dot-product algorithm.Operations
- Addition
- Subtraction
- Multiplication
How addition works
The program prompts for how many matrices to add (minimum 2), then the shared sizeT, and then collects each matrix one by one. The first matrix is always added to the zero-initialised matrizRes; subsequent matrices are also added.You must enter at least 2 matrices. The program will keep re-prompting if you enter a value less than 2.
Walkthrough
Important constraints
| Operation | Constraint |
|---|---|
| Addition | Minimum 2 matrices; all must be the same square size T × T |
| Subtraction | Minimum 2 matrices; all must be the same square size T × T |
| Multiplication | Exactly 2 matrices; columns of first must equal rows of second |