def LAPACK_solve_ls_with_QR(A, b):
    # Ref: https://stackoverflow.com/questions/21970510/solving-a-linear-system-with-lapacks-dgeqrf
    # The corresponding procedure in LAPACK is https://www.netlib.org/lapack/lug/node40.html
    qr, tau, work, info = dgeqrf(A)
    cq, work, info = dormqr('L', 'T', qr, tau, b, qr.shape[0])
    x_qr, info = dtrtrs(qr, cq)
    return x_qr[0:A.shape[1]]
Ejemplo n.º 2
0
def residual_variable_projection(matrix: np.ndarray, data: np.ndarray) \
        -> typing.Tuple[typing.List[str], np.ndarray]:
    """Calculates the conditionaly linear parameters and residual with the variable projection
    method.

    Parameters
    ----------
    matrix :
        The model matrix.
    data : np.ndarray
        The data to analyze.
    """
    # TODO: Reference Kaufman paper

    # Kaufman Q2 step 3
    qr, tau, _, _ = lapack.dgeqrf(matrix)

    # Kaufman Q2 step 4
    temp, _, _ = lapack.dormqr("L", "T", qr, tau, data, max(1, matrix.shape[1]),
                               overwrite_c=0)

    clp, _ = lapack.dtrtrs(qr, temp)

    for i in range(matrix.shape[1]):
        temp[i] = 0

    # Kaufman Q2 step 5

    residual, _, _ = lapack.dormqr("L", "N", qr, tau, temp, max(1, matrix.shape[1]),
                                   overwrite_c=0)
    return clp[:matrix.shape[1]], residual
Ejemplo n.º 3
0
def residual_variable_projection(
        matrix: np.ndarray,
        data: np.ndarray) -> typing.Tuple[typing.List[str], np.ndarray]:
    """Calculates the conditionally linear parameters and residual with the variable projection
    method.

    Parameters
    ----------
    matrix :
        The model matrix.
    data : np.ndarray
        The data to analyze.
    """
    # TODO: Reference Kaufman paper

    # Kaufman Q2 step 3
    qr, tau, _, _ = lapack.dgeqrf(matrix)

    # Kaufman Q2 step 4
    temp, _, _ = lapack.dormqr("L",
                               "T",
                               qr,
                               tau,
                               data,
                               max(1, matrix.shape[1]),
                               overwrite_c=0)

    clp, _ = lapack.dtrtrs(qr, temp)

    for i in range(matrix.shape[1]):
        temp[i] = 0

    # Kaufman Q2 step 5

    residual, _, _ = lapack.dormqr("L",
                                   "N",
                                   qr,
                                   tau,
                                   temp,
                                   max(1, matrix.shape[1]),
                                   overwrite_c=0)
    return clp[:matrix.shape[1]], residual
Ejemplo n.º 4
0
def calc_eq_g(l, B):
    L = B.shape[0]
    order = (np.arange(L) + l) % L
    Q, jpvt, tau, work, info = dgeqp3(B[order[0]] if L % 2 ==
                                      1 else np.dot(B[order[1]], B[order[0]]))
    d = Q.diagonal().copy()
    d[d == 0.0] = 1.0
    T = ((np.triu(Q).T / d).T)[:, jpvt.argsort()]
    for m in range((1 if L % 2 == 1 else 2), L, 2):
        W, work, info = dormqr("R", "N", Q, tau,
                               np.dot(B[order[m + 1]], B[order[m]]),
                               work.shape[0])
        W *= d
        jpvt = (W * W).sum(0).argsort()[::-1]
        Q, tau, work, info = dgeqrf(W[:, jpvt])
        d[...] = Q.diagonal()
        d[d == 0.0] = 1.0
        T = np.dot((np.triu(Q).T / d).T, T[jpvt, :])

    N = B.shape[1]
    invDb = np.zeros((N, N))
    for i in range(N):
        invDb[i, i] = 1.0 / d[i] if np.abs(d[i]) > 1.0 else 1.0
    invDbQT, work, info = dormqr("R", "T", Q, tau, invDb, work.shape[0])

    for i in range(N):
        if np.abs(d[i]) <= 1.0:
            T[i, :] *= d[i]

    T += invDbQT
    T_LU, piv, info = dgetrf(T)
    sign = 1
    for i in range(N):
        if (T_LU[i, i] < 0) ^ (piv[i] != i) ^ (invDb[i, i] < 0) ^ (tau[i] > 0):
            sign *= -1

    G, info = dgetrs(T_LU, piv, invDbQT)
    return G, sign