Last active
November 29, 2021 20:43
-
-
Save akochepasov/6ae2623bcca6732171455c4f72fa548b to your computer and use it in GitHub Desktop.
matrix, band, BLAS, MKL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def diagonal_form(a, upper = 1, lower= 1): | |
""" | |
a is a numpy square matrix | |
this function converts a square matrix to diagonal ordered form | |
returned matrix in ab shape which can be used directly for scipy.linalg.solve_banded | |
""" | |
n = a.shape[1] | |
assert(np.all(a.shape ==(n,n))) | |
ab = np.zeros((2*n-1, n)) | |
for i in range(n): | |
ab[i,(n-1)-i:] = np.diagonal(a,(n-1)-i) | |
for i in range(n-1): | |
ab[(2*n-2)-i,:i+1] = np.diagonal(a,i-(n-1)) | |
mid_row_inx = int(ab.shape[0]/2) | |
upper_rows = [mid_row_inx - i for i in range(1, upper+1)] | |
upper_rows.reverse() | |
upper_rows.append(mid_row_inx) | |
lower_rows = [mid_row_inx + i for i in range(1, lower+1)] | |
keep_rows = upper_rows+lower_rows | |
ab = ab[keep_rows,:] | |
return ab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment