<commit_before># cython: boundscheck=False
# cython: wraparound=False
# cython: embedsignature=True

from numpy.linalg import LinAlgError
from numpy import array, empty
from numpy import float64, int32

######################################################################

from numpy cimport float64_t, int32_t
from libc.math cimport log, sqrt, fabs

cdef extern from "cblas.h":
    float64_t cblas_ddot(int32_t N, float64_t *X, int32_t incX, float64_t *Y, int32_t incY)

cdef extern from "clapack.h":
    int32_t dpotrf_(char *uplo, int32_t *n, float64_t *a, int32_t *lda, int32_t *info)
    int32_t dpotrs_(char *uplo, int32_t *n, int32_t *nrhs, float64_t *a, int32_t *lda, float64_t *b, int32_t *ldb, int32_t *info)
    int32_t dgetrf_(int32_t *m, int32_t *n, float64_t *a, int32_t *lda, int32_t *ipiv, int32_t *info)

######################################################################

cdef char UPLO = 'L'

######################################################################

cdef void value_error(str msg) except *:
    raise ValueError(msg)

cdef void linalg_error(str msg) except *:
    raise LinAlgError(msg)

cpdef int cho_factor(float64_t[::1, :] C, float64_t[::1, :] L) except -1:
    r"""
    Compute the Cholesky factorization of matrix :math:`C` and store
    the result in :math:`L`. The lower part will contain the
    factorization; upper values could be anything.

    Parameters
    ----------
    C : float64_t[::1, :]
        :math:`n\times n` input matrix
    L : float64_t[::1, :]
        :math:`n\times n` output matrix

    Returns
    -------
    out : 0 on success, -1 on failure

    """

    cdef int32_t n = C.shape[0]
    cdef int32_t info
    cdef int i, j

    if C.shape[1] != n:
        value_error("C is not square")
    if L.shape[0] != n or L.shape[1] != n:
        value_error("invalid shape for L")

    if &C[0, 0] != &L[0, 0]:
        L[:, :] = C[:, :]

    dpotrf_(&UPLO, &n, &L[0, 0], &n, &info)

    if info < 0:
        value_error("illegal value")
    elif info > 0:
        linalg_error("matrix is not positive definite")

    return 0


cpdef int cho_solve_vec(float64_t[::1, :] L, float64_t[::1] b, float64_t[::1] x) except -1:
    r"""
    Solve the equation :math:`Ax=b`, where :math:`A` is a matrix and
    :math:`x` and :math:`b` are vectors.

    Parameters
    ----------
    L : float64_t[::1, :]
        :math:`n\times n` lower-triangular Cholesky factor of :math:`A`
    b : float64_t[::1]
        :math:`n` input vector
    x : float64_t[::1]
        :math:`n` output vector

    Returns
    -------
    out : 0 on success, -1 on failure

    """

    cdef int32_t n = L.shape[0]
    cdef int32_t nrhs = 1
    cdef int32_t info
    cdef int i

    if L.shape[1] != n:
        value_error("L is not square")
    if b.shape[0] != n:
        value_error("b has invalid size")
    if x.shape[0] != n:
        value_error("x has invalid size")

    if &x[0] != &b[0]:
        x[:] = b[:]

    dpotrs_(&UPLO, &n, &nrhs, &L[0, 0], &n, &x[0], &n, &info)

    if info < 0:
        value_error("illegal value")

    return 0


cpdef int cho_solve_mat(float64_t[::1, :] L, float64_t[::1, :] B, float64_t[::1, :] X) except -1:
    r"""
    Solve the equation :math:`AX=B`, where :math:`A`, :math:`X`, and
    :math:`B` are matrices.

    Parameters
    ----------
    L : float64_t[::1, :]
        :math:`n\times n` lower-triangular Cholesky factor of :math:`A`
    B : float64_t[::1, :]
        :math:`n\times n` input matrix
    X : float64_t[::1, :]
        :math:`n\times n` output matrix

    Returns
    -------
    out : 0 on success, -1 on failure

    """

    cdef int32_t n = L.shape[0]
    cdef int32_t nrhs = B.shape[1]
    cdef int32_t info
    cdef int i, j

    if L.shape[1] != n:
        value_error("L is not square")
    if B.shape[0] != n or B.shape[1] != n:
        value_error("B has invalid shape")
    if X.shape[0] != n or X.shape[1] != n:
        value_error("X has invalid shape")

    if &X[0, 0] != &B[0, 0]:
        X[:, :] = B[:, :]

    dpotrs_(&UPLO, &n, &nrhs, &L[0, 0], &n, &X[0, 0], &n, &info)

    if info < 0:
        value_error("illegal value")

    return 0


cpdef float64_t logdet(float64_t[::1, :] L) except? -1:
    r"""
    Compute the log determinant of a matrix :math:`A` from its
    lower-triangular Cholesky factor :math:`L`.

    Parameters
    ----------
    L : float64_t[::1, :]
        :math:`n\times n` lower-triangular Cholesky factor of :math:`A`

    Returns
    -------
    out : log-determinant

    """
<commit_msg>Remove trailing underscore from clapack functions<commit_after># cython: boundscheck=False
# cython: wraparound=False
# cython: embedsignature=True

from numpy.linalg import LinAlgError
from numpy import array, empty
from numpy import float64, int32

######################################################################

from numpy cimport float64_t, int32_t
from libc.math cimport log, sqrt, fabs

cdef extern from "cblas.h":
    float64_t cblas_ddot(int32_t N, float64_t *X, int32_t incX, float64_t *Y, int32_t incY)

cdef extern from "clapack.h":
    int32_t dpotrf(char *uplo, int32_t *n, float64_t *a, int32_t *lda, int32_t *info)
    int32_t dpotrs(char *uplo, int32_t *n, int32_t *nrhs, float64_t *a, int32_t *lda, float64_t *b, int32_t *ldb, int32_t *info)

######################################################################

cdef char UPLO = 'L'

######################################################################

cdef void value_error(str msg) except *:
    raise ValueError(msg)

cdef void linalg_error(str msg) except *:
    raise LinAlgError(msg)

cpdef int cho_factor(float64_t[::1, :] C, float64_t[::1, :] L) except -1:
    r"""
    Compute the Cholesky factorization of matrix :math:`C` and store
    the result in :math:`L`. The lower part will contain the
    factorization; upper values could be anything.

    Parameters
    ----------
    C : float64_t[::1, :]
        :math:`n\times n` input matrix
    L : float64_t[::1, :]
        :math:`n\times n` output matrix

    Returns
    -------
    out : 0 on success, -1 on failure

    """

    cdef int32_t n = C.shape[0]
    cdef int32_t info
    cdef int i, j

    if C.shape[1] != n:
        value_error("C is not square")
    if L.shape[0] != n or L.shape[1] != n:
        value_error("invalid shape for L")

    if &C[0, 0] != &L[0, 0]:
        L[:, :] = C[:, :]

    dpotrf(&UPLO, &n, &L[0, 0], &n, &info)

    if info < 0:
        value_error("illegal value")
    elif info > 0:
        linalg_error("matrix is not positive definite")

    return 0


cpdef int cho_solve_vec(float64_t[::1, :] L, float64_t[::1] b, float64_t[::1] x) except -1:
    r"""
    Solve the equation :math:`Ax=b`, where :math:`A` is a matrix and
    :math:`x` and :math:`b` are vectors.

    Parameters
    ----------
    L : float64_t[::1, :]
        :math:`n\times n` lower-triangular Cholesky factor of :math:`A`
    b : float64_t[::1]
        :math:`n` input vector
    x : float64_t[::1]
        :math:`n` output vector

    Returns
    -------
    out : 0 on success, -1 on failure

    """

    cdef int32_t n = L.shape[0]
    cdef int32_t nrhs = 1
    cdef int32_t info
    cdef int i

    if L.shape[1] != n:
        value_error("L is not square")
    if b.shape[0] != n:
        value_error("b has invalid size")
    if x.shape[0] != n:
        value_error("x has invalid size")

    if &x[0] != &b[0]:
        x[:] = b[:]

    dpotrs(&UPLO, &n, &nrhs, &L[0, 0], &n, &x[0], &n, &info)

    if info < 0:
        value_error("illegal value")

    return 0


cpdef int cho_solve_mat(float64_t[::1, :] L, float64_t[::1, :] B, float64_t[::1, :] X) except -1:
    r"""
    Solve the equation :math:`AX=B`, where :math:`A`, :math:`X`, and
    :math:`B` are matrices.

    Parameters
    ----------
    L : float64_t[::1, :]
        :math:`n\times n` lower-triangular Cholesky factor of :math:`A`
    B : float64_t[::1, :]
        :math:`n\times n` input matrix
    X : float64_t[::1, :]
        :math:`n\times n` output matrix

    Returns
    -------
    out : 0 on success, -1 on failure

    """

    cdef int32_t n = L.shape[0]
    cdef int32_t nrhs = B.shape[1]
    cdef int32_t info
    cdef int i, j

    if L.shape[1] != n:
        value_error("L is not square")
    if B.shape[0] != n or B.shape[1] != n:
        value_error("B has invalid shape")
    if X.shape[0] != n or X.shape[1] != n:
        value_error("X has invalid shape")

    if &X[0, 0] != &B[0, 0]:
        X[:, :] = B[:, :]

    dpotrs(&UPLO, &n, &nrhs, &L[0, 0], &n, &X[0, 0], &n, &info)

    if info < 0:
        value_error("illegal value")

    return 0


cpdef float64_t logdet(float64_t[::1, :] L) except? -1:
    r"""
    Compute the log determinant of a matrix :math:`A` from its
    lower-triangular Cholesky factor :math:`L`.

    Parameters
    ----------
    L : float64_t[::1, :]
        :math:`n\times n` lower-triangular Cholesky factor of :math:`A`

    Returns
    -------
    out : log-determinant

    """

{'fraction_non_alphanumeric': 0.20093302232589136, 'fraction_numerical': 0.039320226591136286, 'mean_word_length': 2.629181781539702, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6416137', 'n_tokens_mistral': 3611, 'n_tokens_neox': 3397, 'n_words': 958}