Matrix with two diagonals: Difference between revisions

Line 1,702:
 
=={{header|Python}}==
===Pure Python===
<lang python>'''Matrix with two diagonals'''
 
Line 1,761 ⟶ 1,762:
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1</pre>
 
===NumPy===
<lang Python>import numpy as np
 
def diagdiag(n):
"""
Create double a diagonal matrix
 
Args:
n (int): number of rows.
 
Returns:
a (numpy matrix): double diagonal matrix.
 
"""
d = np.eye(n)
a = d + np.fliplr(d)
if n % 2:
k = (n - 1) // 2
a[k, k] = 1
return a
 
print(diagdiag(7))</lang>
{{Output}}
[[1. 0. 0. 0. 0. 0. 1.]
[0. 1. 0. 0. 0. 1. 0.]
[0. 0. 1. 0. 1. 0. 0.]
[0. 0. 0. 1. 0. 0. 0.]
[0. 0. 1. 0. 1. 0. 0.]
[0. 1. 0. 0. 0. 1. 0.]
[1. 0. 0. 0. 0. 0. 1.]]
 
=={{header|Raku}}==