44#include < cmdstan/arguments/argument_parser.hpp>
55#include < cmdstan/arguments/arg_sample.hpp>
66#include < cmdstan/file.hpp>
7- #include < stan/callbacks/unique_stream_writer.hpp>
87#include < stan/callbacks/json_writer.hpp>
98#include < stan/callbacks/writer.hpp>
109#include < stan/io/dump.hpp>
@@ -580,18 +579,14 @@ std::vector<std::vector<double>> get_uparams_r(
580579 */
581580void services_log_prob_grad (const stan::model::model_base &model, bool jacobian,
582581 std::vector<std::vector<double >> ¶ms_set,
583- int sig_figs, std::ostream &output_stream) {
584- // header row
585- output_stream << std::setprecision (sig_figs) << " lp__," ;
586- std::vector<std::string> p_names;
582+ stan::callbacks::writer &output) {
583+ // header
584+ std::vector<std::string> p_names{" lp__" };
587585 model.unconstrained_param_names (p_names, false , false );
588- for (size_t i = 0 ; i < p_names.size (); ++i) {
589- output_stream << " g_" << p_names[i];
590- if (i == p_names.size () - 1 )
591- output_stream << " \n " ;
592- else
593- output_stream << " ," ;
594- }
586+ std::transform (p_names.begin () + 1 , p_names.end (), p_names.begin () + 1 ,
587+ [](std::string s) { return " g_" + s; });
588+ output (p_names);
589+
595590 // data row(s)
596591 std::vector<int > dummy_params_i;
597592 double lp;
@@ -604,10 +599,9 @@ void services_log_prob_grad(const stan::model::model_base &model, bool jacobian,
604599 lp = stan::model::log_prob_grad<true , false >(model, params,
605600 dummy_params_i, gradients);
606601 }
607- output_stream << lp << " ," ;
608- std::copy (gradients.begin (), gradients.end () - 1 ,
609- std::ostream_iterator<double >(output_stream, " ," ));
610- output_stream << gradients.back () << " \n " ;
602+ // unfortunate: var.grad clears the vector, so need to insert lp afterwards
603+ gradients.insert (gradients.begin (), lp);
604+ output (gradients);
611605 }
612606}
613607
0 commit comments