Note: We can only operate diagonals of a matrix if it is SQUARE.
In other words, it only makes sense to speak in diagonals if the number of rows in the matrix is igual
to the number of columns. Otherwise, it makes no sense to speak in diagonals.
When we have a matrix A(1 x 1)
, we say that matrix A is quadrada e de ordem "1"
and when we have a matrix B(2 x 2)
, we say that matrix B is quadrada e de ordem "2"
and so, successively.
So when we’re operating with square matrices, we just specify your ordem
to immediately subtender the number of rows and columns which are therefore equal.
When the questioner says that he wishes to calculate the soma dos elementos da diagonal principal
he is referring to trait of the matrix and when it says it wants to calculate the soma dos elementos da diagonal secundária
he is referring to anti-trafficking of the matrix.
Both the traço
like the anti-traço
can only be calculated if the matrix is square.
To solve this question we can implement the following algorithm...
import numpy as np
# Capturando e tratando a ordem da matriz quadrada.
while True:
try:
m = int(input('Digite a ordem da matriz: '))
if m < 1:
print('\033[31mValor INVÁLIDO! Digite inteiros maiores que "0"!\033[m')
else:
break
except ValueError:
print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')
# Montando a matriz quadrada de ordem "m".
matrizTemp = list()
for c in range(1, m + 1):
linha = list()
for i in range(1, m + 1):
# Capturando e tratando os valores de cada elemento da matriz.
while True:
try:
valor = int(input(f'Digite o {i}º elemento de {c}ª linha: '))
break
except ValueError:
print('\033[31mValor INVÁLIDO! Digite apenas inteiros!\033[m')
linha.append(valor)
matrizTemp.append(linha)
# Organizando de forma espacial a matriz.
matriz = np.array(matrizTemp)
matriz_in = np.fliplr(matriz)
# Calculando o traço e o antitraço da matriz.
traco = np.trace(matriz)
antitraco = np.trace(matriz_in)
# Exibindo os resultados.
print(f'\033[32mA matriz gerada é:\n{matriz}')
print(f'A soma dos elementos da diagonal principal é: {traco}')
print(f'A soma dos elementos da diagonal secundária é: {antitraco}\033[m')
See here the functioning of the algorithm.
Note that when we run the algorithm we receive the message; Digite a ordem da matriz
. Right now we must enter a number inteiro
and maior que zero
. from that moment we must enter each element of the matrix and then press enter
.
After we have typed all the elements of the matrix the algorithm will mount and display the matrix, in addition to calculating its traço
and anti-traço
respectively.
See here the meaning of the term fliplr
used in the code...
matriz_in = np.fliplr(matriz)
...also note here the applications of the term trace
used in codes...
traco = np.trace(matriz)
and..
antitraco = np.trace(matriz_in)
Observing
Realize that the bigger the ordem
of the square matrix, the largest will be the total de elementos
to be inserted into the matrix.
Related: https://answall.com/questions/183508
– Costamilam