Matrix with two diagonals: Difference between revisions
Content added Content deleted
Line 1,702: | Line 1,702: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Pure Python=== |
|||
<lang python>'''Matrix with two diagonals''' |
<lang python>'''Matrix with two diagonals''' |
||
Line 1,761: | Line 1,762: | ||
0 1 0 0 0 0 1 0 |
0 1 0 0 0 0 1 0 |
||
1 0 0 0 0 0 0 1</pre> |
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}}== |
=={{header|Raku}}== |