<commit_before>// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2011 <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "sparse.h"
#include <Eigen/SparseCore>

template<typename Solver, typename Rhs, typename DenseMat, typename DenseRhs>
void check_sparse_solving(Solver& solver, const typename Solver::MatrixType& A, const Rhs& b, const DenseMat& dA, const DenseRhs& db)
{
  typedef typename Solver::MatrixType Mat;
  typedef typename Mat::Scalar Scalar;

  DenseRhs refX = dA.lu().solve(db);
  {
    Rhs x(b.rows(), b.cols());
    Rhs oldb = b;

    solver.compute(A);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: factorization failed (check_sparse_solving)\n";
      exit(0);
      return;
    }
    x = solver.solve(b);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: solving failed\n";
      return;
    }
    VERIFY(oldb.isApprox(b) && "sparse solver testing: the rhs should not be modified!");

    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
    x.setZero();
    // test the analyze/factorize API
    solver.analyzePattern(A);
    solver.factorize(A);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: factorization failed (check_sparse_solving)\n";
      exit(0);
      return;
    }
    x = solver.solve(b);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: solving failed\n";
      return;
    }
    VERIFY(oldb.isApprox(b) && "sparse solver testing: the rhs should not be modified!");

    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
  }
  
  // test dense Block as the result and rhs:
  {
    DenseRhs x(db.rows(), db.cols());
    DenseRhs oldb(db);
    x.setZero();
    x.block(0,0,x.rows(),x.cols()) = solver.solve(db.block(0,0,db.rows(),db.cols()));
    VERIFY(oldb.isApprox(db) && "sparse solver testing: the rhs should not be modified!");
    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
  }
}

template<typename Solver, typename Rhs>
void check_sparse_solving_real_cases(Solver& solver, const typename Solver::MatrixType& A, const Rhs& b, const Rhs& refX)
{
  typedef typename Solver::MatrixType Mat;
  typedef typename Mat::Scalar Scalar;
  typedef typename Mat::RealScalar RealScalar;
  
  Rhs x(b.rows(), b.cols());
  
  solver.compute(A);
  if (solver.info() != Success)
  {
    std::cerr << "sparse solver testing: factorization failed (check_sparse_solving_real_cases)\n";
    exit(0);
    return;
  }<commit_msg>Extend unit tests to check uncompressed sparse inputs in sparse solvers<commit_after>// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2011 <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "sparse.h"
#include <Eigen/SparseCore>

template<typename Solver, typename Rhs, typename DenseMat, typename DenseRhs>
void check_sparse_solving(Solver& solver, const typename Solver::MatrixType& A, const Rhs& b, const DenseMat& dA, const DenseRhs& db)
{
  typedef typename Solver::MatrixType Mat;
  typedef typename Mat::Scalar Scalar;

  DenseRhs refX = dA.lu().solve(db);
  {
    Rhs x(b.rows(), b.cols());
    Rhs oldb = b;

    solver.compute(A);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: factorization failed (check_sparse_solving)\n";
      exit(0);
      return;
    }
    x = solver.solve(b);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: solving failed\n";
      return;
    }
    VERIFY(oldb.isApprox(b) && "sparse solver testing: the rhs should not be modified!");

    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
    x.setZero();
    // test the analyze/factorize API
    solver.analyzePattern(A);
    solver.factorize(A);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: factorization failed (check_sparse_solving)\n";
      exit(0);
      return;
    }
    x = solver.solve(b);
    if (solver.info() != Success)
    {
      std::cerr << "sparse solver testing: solving failed\n";
      return;
    }
    VERIFY(oldb.isApprox(b) && "sparse solver testing: the rhs should not be modified!");

    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
  }
  
  // test dense Block as the result and rhs:
  {
    DenseRhs x(db.rows(), db.cols());
    DenseRhs oldb(db);
    x.setZero();
    x.block(0,0,x.rows(),x.cols()) = solver.solve(db.block(0,0,db.rows(),db.cols()));
    VERIFY(oldb.isApprox(db) && "sparse solver testing: the rhs should not be modified!");
    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
  }
  
  // test uncompressed inputs
  {
    Mat A2 = A;
    A2.reserve((ArrayXf::Random(A.outerSize())+2).template cast<typename Mat::Index>().eval());
    solver.compute(A2);
    Rhs x = solver.solve(b);
    VERIFY(x.isApprox(refX,test_precision<Scalar>()));
  }
}

template<typename Solver, typename Rhs>
void check_sparse_solving_real_cases(Solver& solver, const typename Solver::MatrixType& A, const Rhs& b, const Rhs& refX)
{
  typedef typename Solver::MatrixType Mat;
  typedef typename Mat::Scalar Scalar;
  typedef typename Mat::RealScalar RealScalar;
  
  Rhs x(b.rows(), b.cols());
  
  solver.compute(A);
  if (solver.info() != Success)
  {
    std::cerr << "sparse solver testing: factorization failed (check_sparse_solving_real_cases)\n";
    exit(0);
    return;
  }
{'fraction_non_alphanumeric': 0.1456930778525807, 'fraction_numerical': 0.005869152425340929, 'mean_word_length': 3.498447204968944, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 23, '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': '2185103', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1988, 'n_words': 522}