Чтобы найти произведение диагональных элементов массива NumPy, вы можете использовать различные методы. Вот несколько подходов:
Метод 1: использование функции diagonalNumPy
import numpy as np
# Create a NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Get the diagonal elements
diagonal_elements = np.diagonal(arr)
# Find the multiplication of the diagonal elements
result = np.prod(diagonal_elements)
print("Multiplication of diagonal elements:", result)
Метод 2. Использование цикла
import numpy as np
# Create a NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Find the shape of the array
rows, cols = arr.shape
# Initialize the product variable
product = 1
# Iterate over the diagonal elements and multiply them
for i in range(min(rows, cols)):
product *= arr[i, i]
print("Multiplication of diagonal elements:", product)
Метод 3: использование функции numpy.trace
import numpy as np
# Create a NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Find the trace of the array
trace = np.trace(arr)
print("Multiplication of diagonal elements:", trace)
Обратите внимание, что в примерах кода предполагается, что у вас установлен NumPy.