To generate usable all-doubles versions of functions from the Stan program we would ideally generate a separate file. That means in
stan/generator.hpp we add a function called
generate_function_cpp that’s like
generate_cpp except that it only calls:
generate_version_comment(out); generate_includes(out); /// is including math.hpp enough? that should be simpler. generate_start_namespace(function_namespace, out); generate_usings(out); generate_typedefs(out); generate_globals(out); generate_functions(prog.function_decl_defs_, out); generate_end_namespace(out);
To generate functions that could be used with Rcpp’s sourceCpp function they need to be in the global namespace so the above parts could remain as they currently are. We would need a function at the end to instantiate all the function templates in a pre-specified namespace from within (
generate_template_instantiations or similar).
I think this means adding two more flags to the top level function for calling the code generator. The first flag would control whether the Stan program is called to generate a program for the whole model or just for the functions. The second flag would control what namespace the functions end up in (sourceCpp needs the global namespace but that’s generally discouraged). If the code is generated in and built into an R package I don’t think it would need to live in the global namespace either.
I can sort out the details here and get it to run/test but I want to get input on whether this is in line with your preferences (especially Bob and anybody else who deals with the code generator).