The subtype matriz triangular
is only applicable to square matrices. Thus, any triangular matrix - upper or lower - will necessarily always be a square matrix.
According to this definition a matrix is triangular superior
, if and only if, the square matrix has elements Aij = 0 for all i > j.
As my intention is to show how to assemble and display an upper triangular matrix of any order, then we can use the following algorithm below:
import numpy as np
while True:
try:
m = int(input('Digite a ordem da matriz: '))
if m < 1:
print('\033[31mValor INVÁLIDO! Digite apenas valores maiores que "0"!\033[m')
else:
break
except ValueError:
print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')
lista = list()
for i in range(1, m + 1):
linha = list()
for j in range(1, m + 1):
if i > j:
linha.append(0)
else:
while True:
try:
v = int(input(f'Digite o {j}º valor da {i}ª linha: '))
break
except ValueError:
print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')
linha.append(v)
lista.append(linha)
matriz = np.array(lista)
print(f'\033[32mA matriz Triangular superior é:\n{matriz}\033[m')
Note that to mount the algorithm I imported the library numpay
. This, in turn, had previously been installed.
Note that the first block While True
, captures and treats the typed value that represents the matrix order.
Later, the algorithm performs block nesting for
. It is, through this nesting of blocks for
, which will be mounted a list of lists containing all the entered values. Then the method array
library numpy
organizes the spatial presentation of the list, producing a matrix in tabular form.
What is the mistake that is happening? Could you edit your question and format the code so that we can better understand your problem?
– fernandosavio
Revise line 4 of your code. It is in it that you are giving the error that you did not put in the question. Compare what you did on this line with the others and you’ll see the problem.
– Woss