<commit_before>#ifndef __STAN__AGRAD__FWD__MATRIX__COLUMNS_MDIVIDE_RIGHT_HPP__
#define __STAN__AGRAD__FWD__MATRIX__COLUMNS_MDIVIDE_RIGHT_HPP__

#include <vector>
#include <stan/math/matrix/Eigen.hpp>
#include <stan/math/matrix/typedefs.hpp>
#include <stan/math/matrix/validate_multiplicable.hpp>
#include <stan/math/matrix/validate_square.hpp>
#include <stan/agrad/fwd/fvar.hpp>
#include <stan/agrad/fwd/matrix/typedefs.hpp>
#include <stan/agrad/fwd/matrix/inverse.hpp>
#include <stan/agrad/fwd/matrix/multiply.hpp>

namespace stan {
  namespace agrad {

    template <typename T1, typename T2, int R1,int C1,int R2,int C2>
    inline 
    Eigen::Matrix<fvar<typename stan::return_type<T1,T2>::type>,R1,C2>
    mdivide_right(const Eigen::Matrix<fvar<T1>,R1,C1> &b,
                 const Eigen::Matrix<fvar<T2>,R2,C2> &A) {
      
      stan::math::validate_square(A,"mdivide_left");
      stan::math::validate_multiplicable(b,A,"mdivide_left");
      
      return stan::agrad::multiply(b, stan::agrad::inverse(A));
    }

    template <typename T1, typename T2, int R1,int C1,int R2,int C2>
    inline 
    Eigen::Matrix<fvar<typename stan::return_type<T1,T2>::type>,R1,C2>
    mdivide_right(const Eigen::Matrix<fvar<T1>,R1,C1> &b,
                 const Eigen::Matrix<T2,R2,C2> &A) {
      
      stan::math::validate_square(A,"mdivide_left");
      stan::math::validate_multiplicable(b,A,"mdivide_left");
      
      return stan::agrad::multiply(b,stan::agrad::inverse(to_fvar(A)));
    }

    template <typename T1, typename T2, int R1,int C1,int R2,int C2>
    inline 
    Eigen::Matrix<fvar<typename stan::return_type<T1,T2>::type>,R1,C2>
    mdivide_right(const Eigen::Matrix<T1,R1,C1> &b,
                 const Eigen::Matrix<fvar<T2>,R2,C2> &A) {
      
      stan::math::validate_square(A,"mdivide_left");
      stan::math::validate_multiplicable(b,A,"mdivide_left");
      
      return stan::agrad::multiply(b,stan::agrad::inverse(A));
    }
  }
}
#endif<commit_msg>cleaned up mdivide_right in fvar<commit_after>#ifndef __STAN__AGRAD__FWD__MATRIX__COLUMNS_MDIVIDE_RIGHT_HPP__
#define __STAN__AGRAD__FWD__MATRIX__COLUMNS_MDIVIDE_RIGHT_HPP__

#include <vector>
#include <stan/math/matrix/Eigen.hpp>
#include <stan/math/matrix/typedefs.hpp>
#include <stan/math/matrix/validate_multiplicable.hpp>
#include <stan/math/matrix/validate_square.hpp>
#include <stan/agrad/fwd/fvar.hpp>
#include <stan/agrad/fwd/matrix/typedefs.hpp>
#include <stan/agrad/fwd/matrix/inverse.hpp>
#include <stan/agrad/fwd/matrix/multiply.hpp>

namespace stan {
  namespace agrad {

    template <typename T, int R1,int C1,int R2,int C2>
    inline 
    Eigen::Matrix<fvar<T>,R1,C2>
    mdivide_right(const Eigen::Matrix<fvar<T>,R1,C1> &A,
                 const Eigen::Matrix<fvar<T>,R2,C2> &b) {
      
      using stan::math::multiply;      
      using stan::math::mdivide_right;
      stan::math::validate_square(b,"mdivide_right");
      stan::math::validate_multiplicable(A,b,"mdivide_right");

      Eigen::Matrix<T,R1,C2> A_mult_inv_b(A.rows(),b.cols());
      Eigen::Matrix<T,R1,C2> deriv_A_mult_inv_b(A.rows(),b.cols());
      Eigen::Matrix<T,R2,C2> deriv_b_mult_inv_b(b.rows(),b.cols());
      Eigen::Matrix<T,R1,C1> val_A(A.rows(),A.cols()); 
      Eigen::Matrix<T,R1,C1> deriv_A(A.rows(),A.cols()); 
      Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols()); 
      Eigen::Matrix<T,R2,C2> deriv_b(b.rows(),b.cols()); 

      for (int j = 0; j < A.cols(); j++) {
        for(int i = 0; i < A.rows(); i++) {
          val_A(i,j) = A(i,j).val_;
          deriv_A(i,j) = A(i,j).d_;
        }
      }

      for (int j = 0; j < b.cols(); j++) {
        for(int i = 0; i < b.rows(); i++) {
          val_b(i,j) = b(i,j).val_;
          deriv_b(i,j) = b(i,j).d_;
        }
      }

      A_mult_inv_b = mdivide_right(val_A, val_b);
      deriv_A_mult_inv_b = mdivide_right(deriv_A, val_b);
      deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);

      Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
      deriv = deriv_A_mult_inv_b - multiply(A_mult_inv_b, deriv_b_mult_inv_b);

      return stan::agrad::to_fvar(A_mult_inv_b, deriv);
    }

    template <typename T, int R1,int C1,int R2,int C2>
    inline 
    Eigen::Matrix<fvar<typename stan::return_type<T,double>::type>,R1,C2>
    mdivide_right(const Eigen::Matrix<fvar<T>,R1,C1> &A,
                  const Eigen::Matrix<double,R2,C2> &b) {
      
      using stan::math::multiply;      
      using stan::math::mdivide_right;
      stan::math::validate_square(b,"mdivide_right");
      stan::math::validate_multiplicable(A,b,"mdivide_right");

      Eigen::Matrix<T,R2,C2> deriv_b_mult_inv_b(b.rows(),b.cols());
      Eigen::Matrix<T,R1,C1> val_A(A.rows(),A.cols()); 
      Eigen::Matrix<T,R1,C1> deriv_A(A.rows(),A.cols());

      for (int j = 0; j < A.cols(); j++) {
        for(int i = 0; i < A.rows(); i++) {
          val_A(i,j) = A(i,j).val_;
          deriv_A(i,j) = A(i,j).d_;
        }
      }

      return stan::agrad::to_fvar(mdivide_right(val_A, b), 
                                  mdivide_right(deriv_A, b));
    }

    template <typename T, int R1,int C1,int R2,int C2>
    inline 
    Eigen::Matrix<fvar<typename stan::return_type<T,double>::type>,R1,C2>
    mdivide_right(const Eigen::Matrix<double,R1,C1> &A,
                  const Eigen::Matrix<fvar<T>,R2,C2> &b) {
      
      using stan::math::multiply;      
      using stan::math::mdivide_right;
      stan::math::validate_square(b,"mdivide_right");
      stan::math::validate_multiplicable(A,b,"mdivide_right");

      Eigen::Matrix<typename stan::return_type<T,double>::type,R1,C2> 
        A_mult_inv_b(A.rows(),b.cols());
      Eigen::Matrix<T,R2,C2> deriv_b_mult_inv_b(b.rows(),b.cols());
      Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols()); 
      Eigen::Matrix<T,R2,C2> deriv_b(b.rows(),b.cols()); 

      for (int j = 0; j < b.cols(); j++) {
        for(int i = 0; i < b.rows(); i++) {
          val_b(i,j) = b(i,j).val_;
          deriv_b(i,j) = b(i,j).d_;
        }
      }

      A_mult_inv_b = mdivide_right(A, val_b);
      deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);

      Eigen::Matrix<typename stan::return_type<T,double>::type,R1,C2> 
        deriv(A.rows(), b.cols());
      deriv = -multiply(A_mult_inv_b, deriv_b_mult_inv_b);

      return stan::agrad::to_fvar(A_mult_inv_b, deriv);
    }
  }
}
#endif
{'fraction_non_alphanumeric': 0.17434365665775822, 'fraction_numerical': 0.018550542367552273, 'mean_word_length': 3.5540443808160345, 'pattern_counts': {'":': 0, '<': 90, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1680255', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2611, 'n_words': 332}