np.eyes(), np.identity()
xxxxxxxxxx
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
xxxxxxxxxx
class Solution(object):
def diagonalSum(self, array):
"""
:type array: List[List[int]]
:rtype: int
"""
n = len(array)
primary = 0
secondary = 0;
for i in range(0, n):
primary += array[i][i]
secondary += array[i][n-i-1]
if (n % 2 == 0): return primary + secondary
else: return primary + secondary - array[n//2][n//2]
xxxxxxxxxx
import numpy as np
a = np.matrix([[1, 1, 1],
[1, 2, 3],
[3, 3, 3]])
prod_diag = a.diagonal().prod()
print(prod_diag)
# gives 6 as answer