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

triangular solver #91

Open
samotto1 opened this issue Oct 20, 2020 · 6 comments
Open

triangular solver #91

samotto1 opened this issue Oct 20, 2020 · 6 comments
Labels
feature_request Request for a feature

Comments

@samotto1
Copy link

Feature request

Hi,

I would like to be able to solve symmetric positive-definite linear systems in Numba using Cholesky factorization. While the Cholesky factorization is implemented, there is no triangular solver! It would be wonderful if a triangular solver could be implemented so that I can take advantage of the Cholesky function. Thanks.

Sam

@gmarkall gmarkall added the feature_request Request for a feature label Oct 20, 2020
@gmarkall
Copy link
Member

Thanks for the request! Do you have an executable example of the function you'd like to work please? Or can you perhaps point towards the particular functions / libraries you'd like to see supported?

@stuartarchibald
Copy link
Contributor

I don't think a back solver is part of NumPy: https://numpy.org/doc/stable/reference/routines.linalg.html, it does exist in SciPy though https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve_triangular.html#scipy.linalg.solve_triangular, another option is to go via https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.cho_factor.html#scipy.linalg.cho_factor and https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.cho_solve.html#scipy.linalg.cho_solve, or just https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve.html#scipy.linalg.solve which will select an optimal solve method if you tell the routine that your system is indeed S.P.D. I don't think this is something Numba should implement as it is out of scope. May be something for https://github.com/numba/numba-scipy, eventually.

If you really want to bind to LAPACK routines to do this from within JIT compiled code then there's an example of doing so in this issue numba/numba#5301, it's not particularly easy but works.

@gmarkall
Copy link
Member

Closing this issue as no further info provided recently - please feel free to open with more specific information about the request.

@agramfort
Copy link

agramfort commented Dec 7, 2022

I have the same request. Here is a gist to replicate the need

import numpy as np
from scipy import linalg
from numba import njit

# Build 3 random symmetric positive matrices
np.random.seed(0)
A = np.random.randn(3, 30, 30)
A = 0.5 * (A @ np.swapaxes(A, -1, -2))
B = np.random.randn(3, 30, 10)
AinvB = np.linalg.solve(A, B)

@njit
def cholesky_solve(A, B):
    # Compute the Cholesky decomposition
    L = np.linalg.cholesky(A)
    AinvB = np.empty((3, 30, 10))
    for i in range(3):
        AinvB[i, :, :] = linalg.cho_solve(
            (L[i], True), B[i]
        )
    return AinvB

np.testing.assert_allclose(cholesky_solve(A, B), np.linalg.solve(A, B))

it would be great to see this working. Thanks

@stuartarchibald stuartarchibald transferred this issue from numba/numba Dec 7, 2022
@stuartarchibald
Copy link
Contributor

I transferred this in from numba/numba (was issue 6394) as it's a request for supporting a feature in scipy.

@agramfort
Copy link

@gmarkall @stuartarchibald any chance this can be looked into soon? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature_request Request for a feature
Projects
None yet
Development

No branches or pull requests

4 participants