Final Project
Final Project
Tecnológica de Durango
Departamento de Mecatrónica y
Energías Renovables
Algebra Lineal
Unidad III
Final Project
Introduction .................................................................................................................................... 2
Description of the program ...................................................................... 3
1
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
Team Members:
The program is a valuable tool for students, professionals and anyone who
wants to learn and understand matrix operations interactively. With an intuitive
graphical interface and a wide range of functions, it is easy to learn, verify and
experiment with matrix math.
2
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
Let's start breaking down the code part by part to see how it works from the
inside out
1.-Imports
1. import tkinter as tk
2. from tkinter import ttk, messagebox, scrolledtext
These lines import the necessary modules from Tkinter for building the GUI
application.
3
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
4
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
It assigns a specific pattern of values to each letter and stores the corresponding
matrices in the 'matrices_minusculas' and 'matrices_minusculas' dictionary.
3.1.-How it works
This part of the code initializes matrices for each letter of the alphabet, both
uppercase and lowercase.
3. value_upper = 28: This variable initializes the starting value for the
matrices of uppercase letters.
5. MatricesSumaDeterminante.matrices_mayusculas[chr(c)]: This
assigns a 3x3 matrix to each uppercase letter in the
‘matrices_mayusculas’ dictionary.
5
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
9. MatricesSumaDeterminante.matrices_minusculas[chr(c)]: This
assigns a 3x3 matrix to each lowercase letter in the
‘matrices_minusculas’ dictionary.
10. value_lower += 1: This increments the value for the next matrix.
In summary, this part of the code creates 3x3 matrices for each letter of the
alphabet, both uppercase and lowercase, and stores them in separate dictionaries
matrices_mayusculas and matrices_minusculas.
6
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This line assigns a 3x3 matrix to the key 'ñ' in the ‘matrices_minusculas’
dictionary. The values in the matrix are arbitrary integers.
Similarly, this line assigns a 3x3 matrix to the key 'Ñ' in the
‘matrices_mayusculas’ dictionary.
These matrices are added to handle the special cases of "ñ" and "Ñ" in the input
strings. They follow the same pattern as the other matrices, with arbitrary integer
values.
7
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
return result
This part of the code defines a static method called sum_matrices. Let's break
down its syntax and functionality:
• result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]: This line initializes a 3x3 matrix
called result with all elements set to zero. This matrix will store the sum of
all matrices in the input list.
• for matrix in matrices:: This line starts a loop that iterates over each
matrix in the matrices list.
8
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
• for i in range(3):: This line starts a loop that iterates over the rows of the
matrix (indices 0, 1, and 2).
• for j in range(3):: This line starts a loop that iterates over the columns of
the matrix (indices 0, 1, and 2).
• return result: Once all matrices in the input list have been summed up,
the resulting matrix result is returned.
9
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This part of the code defines a static method called determinant within the
MatricesSumaDeterminante class.
10
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This part of the code defines a function named adjugate that calculates the
adjugate matrix of a given 3x3 matrix. Let's break down its syntax and how it
works:
def adjugate(matrix):
11
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
• adj = [
• [matrix[1][1] * matrix[2][2] - matrix[1][2] *
matrix[2][1], -(matrix[0][1] * matrix[2][2] - matrix[0][2] *
matrix[2][1]), matrix[0][1] * matrix[1][2] - matrix[0][2] *
matrix[1][1]],
• [-(matrix[1][0] * matrix[2][2] - matrix[1][2] *
matrix[2][0]), matrix[0][0] * matrix[2][2] - matrix[0][2] *
matrix[2][0], -(matrix[0][0] * matrix[1][2] - matrix[0][2] *
matrix[1][0])],
• [matrix[1][0] * matrix[2][1] - matrix[1][1] *
matrix[2][0], -(matrix[0][0] * matrix[2][1] - matrix[0][1] *
matrix[2][0]), matrix[0][0] * matrix[1][1] - matrix[0][1] *
matrix[1][0]]
• ]
• This block initializes the variable ‘adj´ with a nested list comprehension that
calculates each element of the adjugate matrix based on the elements of
the input ‘matrix’. It follows the formula for computing the adjugate matrix
of a 3x3 matrix.
• return adj
12
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
adj = MatricesSumaDeterminante.adjugate(matrix)
inv = [[adj[i][j] / det for j in range(3)] for i in range(3)]
return inv
This part of the code defines a static method named ‘inverse’ within the
‘MatricesSumaDeterminante’ class. It computes the inverse of a given 3x3
matrix:
@staticmethod
def inverse(matrix):
• def inverse(matrix):: This line defines the inverse method, which takes
a single argument matrix, representing the 3x3 matrix for which the
inverse will be calculated.
• det = MatricesSumaDeterminante.determinant(matrix)
• This line calculates the determinant of the input matrix using the
determinant method defined within the MatricesSumaDeterminante
class.
13
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
if det == 0:
return None # La matriz no es invertible
• adj = MatricesSumaDeterminante.adjugate(matrix)
• The method calculates the adjugate matrix of the input matrix using the
adjugate method defined within the MatricesSumaDeterminante class.
• This line computes the elements of the inverse matrix by dividing each
element of the adjugate matrix by the determinant.
• return inv
14
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
def __init__(self):
self.initialize_matrices()
self.root = tk.Tk()
self.root.title("Calculadora de Determinante")
ruta_icono = "icon.ico"
try:
self.root.iconbitmap(ruta_icono)
except:
print("a")
self.root.geometry("530x330") # Ancho x Alto
This code initializes the matrices and creates the main application window for the
determinant calculator
@staticmethod
def transpose(matrix):
return [[matrix[j][i] for j in range(3)] for i in range(3)]
• This line returns a new 2D list, which represents the transpose of the input
matrix. It uses a list comprehension to iterate over the rows and columns of
the input matrix and construct the transpose matrix accordingly.
15
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
def __init__(self):
self.initialize_matrices()
self.root = tk.Tk()
self.root.title("Calculadora de Determinante")
ruta_icono = "icon.ico"
try:
self.root.iconbitmap(ruta_icono)
except:
print("a")
self.root.geometry("530x330") # Ancho x Alto
• This code block defines the __init__ method, which serves as the
constructor for the MatricesSumaDeterminante class. It initializes the
class attributes and creates the main window for the calculator application
using the Tkinter library.
• ruta_icono = "icon.ico": It defines the path to the icon file for the
application window.
16
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This part of the code sets a style for the window using the ‘ttk.Style()’
class from the tkinter library. It then specifies the theme to be used for the
style, which is "clam" in this case.
The ttk.Style() class allows you to define and customize the appearance of
widgets in your tkinter application. By calling
self.style.theme_use("clam"), the code sets the theme of the
application to "clam", which is one of the predefined themes available in
tkinter.
17
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
18
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
2. Label (self.label): Displays a text prompt for the user to enter a name.
3. Entry (self.entry): Provides a text entry field where the user can input a
name. It is binded to the <Return> key, so pressing Enter triggers the
self.calculate method.
19
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
matrices_input = []
This part of the code creates a matrix of labels to represent a table in the
graphical user interface (GUI). Here's what each part does:
20
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
3. Label for Adjoint (‘self.label_adj’): This label displays the text "Adjoint".
It's positioned at row 2 and column 1 of the grid layout within
self.main_frame.
21
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
result_str = ""
This part of the code processes each character in the input string and
converts it into its corresponding matrix. Here's how it works:
22
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
3. Result String:
23
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This part of the code iterates over each matrix in the matrices_input list and
appends information about each matrix to the result_str string.
• Then, it iterates over each row in the matrix and appends the values
of each row, separated by spaces, to result_str.
3. Result String:
24
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
15.-Add debug messages to see which letters and matrices are being
used
# Agregar mensajes de depuración para ver qué letras y matrices se están
utilizando
print("Letras ingresadas:", [letter for letter, _ in
matrices_input])
print("Matrices utilizadas:", [matrix for _, matrix in
matrices_input])
This part was added, more to help programmers see in the terminal which arrays
were being used, making it easier to detect errors in code execution.
This part of the code sums up all the matrices provided as input, displays the
resultant matrix in text form, and visualizes it using Tkinter labels.
25
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This section of the code calculates the determinant of the resultant matrix
(sum_matrix) obtained from the sum of input matrices. Here's what each line
does:
1. det = self.determinant(sum_matrix): It calculates the determinant of
the sum_matrix by calling the determinant method defined in the class,
passing the sum_matrix as an argument. The determinant value is
assigned to the variable det.
2. result_str += f"\nLa determinante de la matriz resultante es:
{det}\n": It appends a string to the result_str variable, indicating the
determinant of the resultant matrix. The determinant value (det) is
formatted into the string using an f-string.
3. self.label_det["text"] = "Determinante: " + str(det): It updates the
text displayed on a label (label_det) in the GUI to show the calculated
determinant value. The str(det) converts the determinant value to a string,
and it's concatenated with the label text "Determinante: ".
26
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This part of the code calculates the adjugate (or adjoint) matrix of the resultant
matrix (sum_matrix). Here's a breakdown:
1. adj = self.adjugate(sum_matrix): It calculates the adjugate matrix of
the sum_matrix by calling the adjugate method defined in the class,
passing the sum_matrix as an argument. The resulting adjugate matrix is
stored in the variable adj.
2. result_str += "Matriz adjunta de la matriz resultante:\n": It
appends a string to the result_str variable, indicating that the following
lines will display the adjugate matrix of the resultant matrix.
3. for row in adj: and result_str += ' '.join(map(str, row)) + '\n':
These lines iterate over each row of the adjugate matrix (adj) and convert
each row's elements to strings, joining them with spaces. This forms a row
of the adjugate matrix, which is appended to the result_str.
4. for fila in range(3): and for columna in range(3):: These nested loops
iterate over the rows and columns of the adjugate matrix.
5. Inside these loops, it creates tk.Label widgets to display each element of
the adjugate matrix visually in the GUI. Each label is configured with the
corresponding element from the adjugate matrix (adj[fila][columna]).
These labels are then positioned in a grid layout within the matriz_adj
frame, representing the adjugate matrix visually.
27
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
This part of the code calculates the adjugate (or adjoint) matrix of the resultant
matrix (sum_matrix). Here's a breakdown:
1. adj = self.adjugate(sum_matrix): It calculates the adjugate matrix of
the sum_matrix by calling the adjugate method defined in the class,
passing the sum_matrix as an argument. The resulting adjugate matrix is
stored in the variable adj.
2. result_str += "Matriz adjunta de la matriz resultante:\n": It
appends a string to the result_str variable, indicating that the following
lines will display the adjugate matrix of the resultant matrix.
3. for row in adj: and result_str += ' '.join(map(str, row)) + '\n':
These lines iterate over each row of the adjugate matrix (adj) and convert
each row's elements to strings, joining them with spaces. This forms a row
of the adjugate matrix, which is appended to the result_str.
4. for fila in range(3): and for columna in range(3):: These nested loops
iterate over the rows and columns of the adjugate matrix.
5. Inside these loops, it creates tk.Label widgets to display each element of
the adjugate matrix visually in the GUI. Each label is configured with the
corresponding element from the adjugate matrix (adj[fila][columna]).
These labels are then positioned in a grid layout within the matriz_adj
frame, representing the adjugate matrix visually.
28
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
20.- Calculate the transpose of the resulting matrix and finalize the code.
# Calcular la transpuesta de la matriz resultante
trans = self.transpose(sum_matrix)
result_str += "Matriz transpuesta de la matriz resultante:\n"
for row in trans:
result_str += ' '.join(map(str, row)) + '\n'
else:
messagebox.showerror("Error", "No se ingresaron caracteres
válidos.")
self.result_text.delete(1.0, tk.END)
self.result_text.insert(tk.END, result_str)
def run(self):
self.root.mainloop()
if __name__ == "__main__":
programa = MatricesSumaDeterminante()
programa.run()
29
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
30
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
operating guidelines
1.- First open the program, is recommended create an direct access after
downloading it
31
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
3.- In the input space next to “Ingrese un Nombre” place a name, which can
include both uppercase and lowercase words (do not enter special characters [, . ""
; : ... @ ", or accented words ] since it will mark an error in the console and take
the character as null, as well as characters with dieresis or belonging to other
alphabets than the basic Latin alphabet)
4.- Click on the "Calcular" button, and the program will show you different
matrices that are made with the entered name (individual matrix of each letter, the
sum of matrices, determinant of the resulting matrix, adjoint matrix, transposed
matrix and inverse matrix).
32
Técnico Superior Universitario en Mecatrónica
Algebra Lineal
Unidad III
33