Issue
Using numpy, I want to create a superdiagonal matrix that is only almost square. It has extra zeros to the right or left of the square part. The code snippet below give me the desired result, but it is a little tricky to read, and the matrix type seems to me common enough that there should be an idiomatic way to construct it.
What is the simplest way to construct 'padded eyes' as below, in numpy?
import numpy as np
size = 5
pad_width = 3
left_padded_eye = np.block([np.zeros((size,pad_width)),np.eye(size)])
right_padded_eye = np.block([np.eye(size),np.zeros((size,pad_width))])
Solution
np.eye
can do that directly
>>> size = 5
>>> pad_width = 3
>>> np.eye(size, size+pad_width, pad_width)
array([[0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 1.]])
>>> np.eye(size, size+pad_width)
array([[1., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0.]])
Answered By - Paul Panzer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.