Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a function to convert a matrix into diagonal ordered form (ab) solve_banded #8362

Open
Khalilsqu opened this issue Feb 2, 2018 · 1 comment · May be fixed by #11344
Open

a function to convert a matrix into diagonal ordered form (ab) solve_banded #8362

Khalilsqu opened this issue Feb 2, 2018 · 1 comment · May be fixed by #11344
Labels
enhancement A new feature or improvement scipy.linalg

Comments

@Khalilsqu
Copy link

Khalilsqu commented Feb 2, 2018

I was using scipy function scipy.linalg.solve_banded and had trouble to convert a banded matrix to ab form (diagonal ordered form). I created the following function which simply can convert any matrix into ab shape.

upper and lower kwargs are same as l_and_u in solve_banded function of scipy.
I am just wondering if the created function could be associated into solve_banded function of scipy so there will be no need to convert banded matrix into ab shape.

Kind regards

import numpy as np
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
@czgdp1807
Copy link
Member

Yes, your function works correctly. Just revert the order of upper and lower so that it becomes easy to use.

@perimosocordiae perimosocordiae added enhancement A new feature or improvement scipy.linalg labels Dec 12, 2018
@JDkuba JDkuba linked a pull request Jan 11, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.linalg
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants