<commit_before>#ifndef __STAN__MATH__MATRIX_HPP__
#define __STAN__MATH__MATRIX_HPP__

#define EIGEN_DENSEBASE_PLUGIN "stan/math/EigenDenseBaseAddons.hpp"
#include <Eigen/Dense>

#include <stdexcept>
#include <vector>


namespace stan {
  
  namespace math {

    /**
     * This is a traits structure for Eigen matrix types.
     *
     * @tparam T Underlying scalar type.
     */ 
    template<typename T>
    struct EigenType {

      /** Type of scalar. 
       */
      typedef T scalar;

      /**
       * Type of Eigen matrix.
       */
      typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>  matrix;

      /**
       * Type of Eigen column vector.
       */
      typedef Eigen::Matrix<T,Eigen::Dynamic,1>  vector;

      /**
       * Type of Eigen row vector.
       */
      typedef Eigen::Matrix<T,1,Eigen::Dynamic>  row_vector;

      /**
       * Type of Eigen diagonal matrix.
       */
      typedef Eigen::DiagonalMatrix<T,Eigen::Dynamic> diagonal_matrix;

      /**
       * Type of Eigen array shaped like a (column) vector.
       */
      typedef Eigen::Array<T,1,Eigen::Dynamic> array_vector;
      
      /**
       * Type of Eigen array shaped like a row vector.
       */
      typedef Eigen::Array<T,Eigen::Dynamic,1> array_row_vector;

      /**
       * Type of Eigen array shaped like a matrix.
       */
      typedef Eigen::Array<T,Eigen::Dynamic,Eigen::Dynamic> array_matrix;
    };      
    
    /**
     * Type for matrix of double values.
     */
    typedef EigenType<double>::matrix matrix_d;

    /**
     * Type for (column) vector of double values.
     */
    typedef EigenType<double>::vector vector_d;

    /**
     * Type for (row) vector of double values.
     */
    typedef EigenType<double>::row_vector row_vector_d;

    namespace {

      template <typename T>
      void resize(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos],dims[pos+1]);
      }

      template <typename T>
      void resize(Eigen::Matrix<T,Eigen::Dynamic,1>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos]);
      }

      template <typename T>
      void resize(Eigen::Matrix<T,1,Eigen::Dynamic>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos]);
      }


      void resize(double x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        // no-op
      }

      template <typename T>
      void resize(std::vector<T>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos]);
        ++pos;
        if (pos >= dims.size()) return; // skips lowest loop to scalar
        for (size_t i = 0; i < x.size(); ++i)
          resize(x[i],dims,pos);
      }

    }

    /**
     * Recursively resize the specified vector of vectors,
     * which must bottom out at scalar values, Eigen vectors
     * or Eigen matrices.
     *
     * @param x Array-like object to resize.
     * @param dims New dimensions.
     * @tparam T Type of object being resized.
     */
    template <typename T>
    inline void resize(T& x, std::vector<size_t> dims) {
      resize(x,dims,0U);
    }

    // polymorphic gets with bounds checking


    namespace {
      
      inline
      void check_range(size_t max,
                       size_t i, 
                       const char* msg,
                       size_t idx) {
        if (i < 1 || i > max) {
          std::stringstream s;
          s << "INDEX OPERATOR [] OUT OF BOUNDS"
            << "; index=" << i
            << "; lower bound=1"
            << "; upper bound=" << max
            << "; index position=" << idx
            << "; " << msg
            << std::endl;
          throw std::out_of_range(s.str());
        }
      }

    }

    /**
     * Return a reference to the value of the specified vector at the
     * specified base-one index.  If the index is out of range, throw
     * a <code>std::out_of_range</code> exception with the specified
     * error message and index indicated.
     *<commit_msg>Make check_range() conditional on NDEBUG.<commit_after>#ifndef __STAN__MATH__MATRIX_HPP__
#define __STAN__MATH__MATRIX_HPP__

#define EIGEN_DENSEBASE_PLUGIN "stan/math/EigenDenseBaseAddons.hpp"
#include <Eigen/Dense>

#include <stdexcept>
#include <vector>


namespace stan {
  
  namespace math {

    /**
     * This is a traits structure for Eigen matrix types.
     *
     * @tparam T Underlying scalar type.
     */ 
    template<typename T>
    struct EigenType {

      /** Type of scalar. 
       */
      typedef T scalar;

      /**
       * Type of Eigen matrix.
       */
      typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>  matrix;

      /**
       * Type of Eigen column vector.
       */
      typedef Eigen::Matrix<T,Eigen::Dynamic,1>  vector;

      /**
       * Type of Eigen row vector.
       */
      typedef Eigen::Matrix<T,1,Eigen::Dynamic>  row_vector;

      /**
       * Type of Eigen diagonal matrix.
       */
      typedef Eigen::DiagonalMatrix<T,Eigen::Dynamic> diagonal_matrix;

      /**
       * Type of Eigen array shaped like a (column) vector.
       */
      typedef Eigen::Array<T,1,Eigen::Dynamic> array_vector;
      
      /**
       * Type of Eigen array shaped like a row vector.
       */
      typedef Eigen::Array<T,Eigen::Dynamic,1> array_row_vector;

      /**
       * Type of Eigen array shaped like a matrix.
       */
      typedef Eigen::Array<T,Eigen::Dynamic,Eigen::Dynamic> array_matrix;
    };      
    
    /**
     * Type for matrix of double values.
     */
    typedef EigenType<double>::matrix matrix_d;

    /**
     * Type for (column) vector of double values.
     */
    typedef EigenType<double>::vector vector_d;

    /**
     * Type for (row) vector of double values.
     */
    typedef EigenType<double>::row_vector row_vector_d;

    namespace {

      template <typename T>
      void resize(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos],dims[pos+1]);
      }

      template <typename T>
      void resize(Eigen::Matrix<T,Eigen::Dynamic,1>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos]);
      }

      template <typename T>
      void resize(Eigen::Matrix<T,1,Eigen::Dynamic>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos]);
      }


      void resize(double x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        // no-op
      }

      template <typename T>
      void resize(std::vector<T>& x, 
                  const std::vector<size_t>& dims, 
                  size_t pos) {
        x.resize(dims[pos]);
        ++pos;
        if (pos >= dims.size()) return; // skips lowest loop to scalar
        for (size_t i = 0; i < x.size(); ++i)
          resize(x[i],dims,pos);
      }

    }

    /**
     * Recursively resize the specified vector of vectors,
     * which must bottom out at scalar values, Eigen vectors
     * or Eigen matrices.
     *
     * @param x Array-like object to resize.
     * @param dims New dimensions.
     * @tparam T Type of object being resized.
     */
    template <typename T>
    inline void resize(T& x, std::vector<size_t> dims) {
      resize(x,dims,0U);
    }

    // polymorphic gets with bounds checking


    namespace {
      
      inline
      void check_range(size_t max,
                       size_t i, 
                       const char* msg,
                       size_t idx) {
#ifndef NDEBUG
        if (i < 1 || i > max) {
          std::stringstream s;
          s << "INDEX OPERATOR [] OUT OF BOUNDS"
            << "; index=" << i
            << "; lower bound=1"
            << "; upper bound=" << max
            << "; index position=" << idx
            << "; " << msg
            << std::endl;
          throw std::out_of_range(s.str());
        }
#endif
      }

    }

    /**
     * Return a reference to the value of the specified vector at the
     * specified base-one index.  If the index is out of range, throw
     * a <code>std::out_of_range</code> exception with the specified
     * error message and index indicated.
     *
{'fraction_non_alphanumeric': 0.1182480037576327, 'fraction_numerical': 0.0025833724753405356, 'mean_word_length': 1.784243216737496, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 69, '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': '6854095', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2522, 'n_words': 679}