A
A
andreika_big2021-06-05 14:09:52
Python
andreika_big, 2021-06-05 14:09:52

Python nxn matrix determinant?

How to write a program to find the determinant of an nxn matrix without third-party libraries?
Here is the code

import pandas as pd

a, b, c = 0, 0, 0
print('Выберите действие от 1 до 6: 1 сложение, 2 вычитание, 3 умножение, 4 деление, 5 транспонирование и 6 определитель')
a = int(input())
if a < 5:
  print('введите числа')
  b = int(input())
  c = int(input())
  if a == 1:
    print(b+c)
  if a == 2:
    print(b-c)
  if a == 3:
    print(b*c)
  if a == 4:
    print(b-c)
else:
  print('введите размерность матрицы')
  b = int(input())
  c = int(input())
  mat = {}
  for row in range(b):
    print(f'Строка {row}')
    for column in range(c):
      mat[row, column] = input()
  if a == 5:
    for column in range(c):
        str = ''
        for row in range(b):
          str += mat[row, column] + ', '
        print(str[:-2])
  if a == 6:
    # создаем df для удобства
    df = pd.DataFrame()
    for row in range(b):
      for column in range(c):
        df.loc[row, column] = int(mat[row, column])

    # получаем матрицу треугольного вида
    for column in range(b):
      for row in range(column+1, b):
        df.loc[row, :] = df.loc[column, :] * (- df.loc[row, column] /  df.loc[column, column]) + df.loc[row, :]

    # Находим детерминант
    det = 1
    for row in range(b):
      det *= df.loc[row, row]

    print(det)


But it does not work correctly, I do not understand what the error is, help me fix it, I
will
be
grateful ] = df.loc[column, :] * (- df.loc[row, column] / df.loc[column, column]) + df.loc[row, :]
nan

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-07
@andreika_big

Somewhere in the calculations it turned out NaN - Not A Number.
Could divide by zero, I no longer see operations that could generate NaN.
Check the values ​​​​of the matrix in steps, what else can I say.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question