<commit_before>// ------------------ //
//  Standard Solver   //
// ------------------ //

template<typename VectorType, typename MatrixType>
auto Standard::solve(const MatrixType &A,const VectorType &v)
    -> VectorType
{
    return A.solve(v);
}

// -------------------------  //
//  Conjugate Gradient Solver //
// -------------------------  //

ConjugateGradient::ConjugateGradient(double tol, int verbosity_)
    : tolerance(tol), verbosity(verbosity_)
{
    // Nothing to do here.
}

template
<
typename VectorType,
typename MatrixType,
template <LogType> class Log
>
auto ConjugateGradient::solve(const MatrixType &A,
                              const VectorType &v)
    -> VectorType
{
    using RealType = typename VectorType::RealType;

    Log<LogType::SOL_CG> log(verbosity);

    unsigned int n = v.rows();
    RealType tol, alpha, beta, rnorm, xnorm;
    VectorType x, r, p, xnew, rnew, pnew;

    x = v;
    r = A * x - v;
    p = -1.0 * r;
    xnorm = x.norm();
    rnorm = r.norm();

    log.init(tolerance, rnorm, xnorm);

    int i = 0;
    while (rnorm / xnorm > tolerance)
    {
        alpha = dot(r, r) / dot(p, A * p);
        xnew  = x + alpha *     p;
        rnew  = r + alpha * A * p;
        beta  = dot(rnew, rnew) / dot(r, r);
        pnew  = beta * p - rnew;

        x = xnew; xnorm = x.norm();
        r = rnew; rnorm = r.norm();
        p = pnew;

        i++;
        if (i % 10 == 0)
            log.step(i, rnorm / xnorm);
    }
    log.finalize(i);

    return x;
}

// -----------------------  //
//  Preconditioned Solver   //
// -----------------------  //

template<typename Solver, typename Transformation>
PreconditionedSolver<Solver, Transformation>::
PreconditionedSolver(Solver s, Transformation t)
    : solver(s), transformation(t)
{
    // Nothin to do here.
}

template<typename Solver, typename Transformation>
    template<typename VectorType, typename MatrixType>
auto PreconditionedSolver<Solver, Transformation>::solve(const MatrixType &A,
                                                         const VectorType &v)
    -> VectorType<commit_msg>Fixed residual computation in CG.<commit_after>// ------------------ //
//  Standard Solver   //
// ------------------ //

template<typename VectorType, typename MatrixType>
auto Standard::solve(const MatrixType &A,const VectorType &v)
    -> VectorType
{
    return A.solve(v);
}

// -------------------------  //
//  Conjugate Gradient Solver //
// -------------------------  //

ConjugateGradient::ConjugateGradient(double tol, int verbosity_)
    : verbosity(verbosity_), tolerance(tol)
{
    // Nothing to do here.
}

template
<
typename VectorType,
typename MatrixType,
template <LogType> class Log
>
auto ConjugateGradient::solve(const MatrixType &A,
                              const VectorType &v)
    -> VectorType
{
    using RealType = typename VectorType::RealType;

    Log<LogType::SOL_CG> log(verbosity);

    RealType tol, alpha, beta, rnorm, vnorm;
    VectorType x, r, p, xnew, rnew, pnew;

    x = v;
    r = A * x - v;
    p = -1.0 * r;
    vnorm = v.norm();
    rnorm = r.norm();

    log.init(tolerance, rnorm, vnorm);

    int i = 0;
    while (rnorm / vnorm > tolerance)
    {
        alpha = dot(r, r) / dot(p, A * p);
        xnew  = x + alpha *     p;
        rnew  = A * x - v;
        beta  = dot(rnew, rnew) / dot(r, r);
        pnew  = beta * p - rnew;

        x = xnew;
        r = rnew; rnorm = r.norm();
        p = pnew;

        i++;
        if (i % 10 == 0)
            log.step(i, rnorm);
    }
    log.finalize(i);

    return x;
}

// -----------------------  //
//  Preconditioned Solver   //
// -----------------------  //

template<typename Solver, typename Transformation>
PreconditionedSolver<Solver, Transformation>::
PreconditionedSolver(Solver s, Transformation t)
    : solver(s), transformation(t)
{
    // Nothin to do here.
}

template<typename Solver, typename Transformation>
    template<typename VectorType, typename MatrixType>
auto PreconditionedSolver<Solver, Transformation>::solve(const MatrixType &A,
                                                         const VectorType &v)
    -> VectorType
{'fraction_non_alphanumeric': 0.1849364355960662, 'fraction_numerical': 0.002878388102662509, 'mean_word_length': 1.7652519893899203, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2738229', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1295, 'n_words': 339}