diff --git a/src/frontend/Ast.ml b/src/frontend/Ast.ml index 89c13eddab..dcf1006832 100644 --- a/src/frontend/Ast.ml +++ b/src/frontend/Ast.ml @@ -116,7 +116,7 @@ type typed_lval = (typed_expression, typed_expr_meta) lval_with (** Statement shapes, where we substitute untyped_expression and untyped_statement for 'e and 's respectively to get untyped_statement and typed_expression and typed_statement to get typed_statement *) -type ('e, 's, 'l, 'f) statement = +type ('e, 's, 'l, 'f, 'c) statement = | Assignment of { assign_lhs: 'l ; assign_op: assignmentoperator @@ -155,6 +155,7 @@ type ('e, 's, 'l, 'f) statement = | FunDef of { returntype: Middle.UnsizedType.returntype ; funname: identifier + ; closure: 'c ; arguments: (Middle.UnsizedType.autodifftype * Middle.UnsizedType.t * identifier) list @@ -176,13 +177,20 @@ type statement_returntype = | AnyReturnType [@@deriving sexp, hash, compare] -type ('e, 'm, 'l, 'f) statement_with = - {stmt: ('e, ('e, 'm, 'l, 'f) statement_with, 'l, 'f) statement; smeta: 'm} +type ('e, 'm, 'l, 'f, 'c) statement_with = + { stmt: ('e, ('e, 'm, 'l, 'f, 'c) statement_with, 'l, 'f, 'c) statement + ; smeta: 'm } +[@@deriving sexp, compare, map, hash] + +type closure_info = + { clname: string + ; captures: + (Middle.UnsizedType.autodifftype * Middle.UnsizedType.t * string) list } [@@deriving sexp, compare, map, hash] (** Untyped statements, which have location_spans as meta-data *) type untyped_statement = - (untyped_expression, located_meta, untyped_lval, unit) statement_with + (untyped_expression, located_meta, untyped_lval, unit, bool) statement_with [@@deriving sexp, compare, map, hash] let mk_untyped_statement ~stmt ~loc : untyped_statement = {stmt; smeta= {loc}} @@ -198,7 +206,8 @@ type typed_statement = ( typed_expression , stmt_typed_located_meta , typed_lval - , fun_kind ) + , fun_kind + , closure_info option ) statement_with [@@deriving sexp, compare, map, hash] @@ -246,7 +255,7 @@ let rec untyped_statement_of_typed_statement {stmt; smeta} = map_statement untyped_expression_of_typed_expression untyped_statement_of_typed_statement untyped_lvalue_of_typed_lvalue (fun _ -> ()) - stmt + Option.is_some stmt ; smeta= {loc= smeta.loc} } (** Forgetful function from typed to untyped programs *) diff --git a/src/frontend/Ast_to_Mir.ml b/src/frontend/Ast_to_Mir.ml index 34ce9c1976..b7ecc8b8c9 100644 --- a/src/frontend/Ast_to_Mir.ml +++ b/src/frontend/Ast_to_Mir.ml @@ -1,6 +1,8 @@ open Core_kernel open Middle +let closures = ref String.Map.empty + (* XXX fix exn *) let unwrap_return_exn = function | Some (UnsizedType.ReturnType ut) -> ut @@ -72,6 +74,16 @@ and trans_expr {Ast.expr; Ast.emeta} = | Variable {name; _} -> Var name | IntNumeral x -> Lit (Int, format_number x) | RealNumeral x -> Lit (Real, format_number x) + | FunApp + ( Ast.StanLib + , {name= "integrate_ode_bdf"; _} + , ({emeta= {type_= UFun (fargs, _, _); _}; _} :: _ as args) ) + when 2 + List.length fargs = List.length args -> + FunApp + ( Fun_kind.StanLib + , "integrate_ode_bdf" + , trans_exprs args + @ Expr.Helpers.[float 1e-10; float 1e-10; float 1e8] ) | FunApp (fn_kind, {name; _}, args) |CondDistApp (fn_kind, {name; _}, args) -> FunApp (trans_fn_kind fn_kind, name, trans_exprs args) @@ -467,6 +479,9 @@ let%expect_test "dist name suffix" = let rec trans_stmt ud_dists (declc : decl_context) (ts : Ast.typed_statement) = let stmt_typed = ts.stmt and smeta = ts.smeta.loc in + let trans_stmt_ad = + trans_stmt ud_dists {dconstrain= None; dadlevel= AutoDiffable} + in let trans_stmt = trans_stmt ud_dists {declc with dconstrain= None} in let trans_single_stmt s = match trans_stmt s with @@ -612,10 +627,52 @@ let rec trans_stmt ud_dists (declc : decl_context) (ts : Ast.typed_statement) = ; meta= smeta } in Stmt.Helpers.[ensure_var (for_each bodyfn) iteratee' smeta] - | Ast.FunDef _ -> + | Ast.FunDef {closure= None; _} -> raise_s [%message "Found function definition statement outside of function block"] + | Ast.FunDef + {returntype; funname; closure= Some {clname; captures}; arguments; body} + -> + let type_ = + UnsizedType.UFun + (List.map arguments ~f:(fun (a, t, _) -> (a, t)), returntype, Closure) + in + ( match + Map.add !closures ~key:clname + ~data: + { Program.cdrt= + ( match returntype with + | Void -> None + | ReturnType ut -> Some ut ) + ; cdcaptures= List.map captures ~f:(fun (a, t, n) -> (a, n, t)) + ; cdargs= List.map arguments ~f:trans_arg + ; cdbody= trans_stmt_ad body |> unwrap_block_or_skip } + with + | `Ok x -> closures := x + | `Duplicate -> () ) ; + [ Stmt. + { Fixed.pattern= + Decl + { decl_adtype= declc.dadlevel + ; decl_id= funname.name + ; decl_type= Unsized type_ } + ; meta= smeta } + ; { pattern= + Assignment + ( (funname.name, type_, []) + , Expr. + { pattern= + FunApp + ( CompilerInternal + , Internal_fun.to_string FnMakeClosure + , Helpers.str clname + :: List.map captures ~f:(fun (adlevel, type_, id) -> + { Fixed.pattern= Var id + ; meta= {Typed.Meta.type_; adlevel; loc= smeta} + } ) ) + ; meta= {type_; adlevel= declc.dadlevel; loc= smeta} } ) + ; meta= smeta } ] | Ast.VarDecl {decl_type; transformation; identifier; initial_value; is_global} -> ignore is_global ; @@ -631,7 +688,7 @@ let rec trans_stmt ud_dists (declc : decl_context) (ts : Ast.typed_statement) = let trans_fun_def ud_dists (ts : Ast.typed_statement) = match ts.stmt with - | Ast.FunDef {returntype; funname; arguments; body} -> + | Ast.FunDef {returntype; funname; closure= None; arguments; body} -> [ Program. { fdrt= (match returntype with Void -> None | ReturnType ut -> Some ut) @@ -765,7 +822,8 @@ let trans_prog filename (p : Ast.typed_program) : Program.Typed.t = let transform_inits = gen_from_block {declc with dconstrain= Some Unconstrain} Parameters in - { functions_block= map (trans_fun_def ud_dists) functionblock + { closures= !closures + ; functions_block= map (trans_fun_def ud_dists) functionblock ; input_vars ; prepare_data ; log_prob diff --git a/src/frontend/Canonicalize.ml b/src/frontend/Canonicalize.ml index 7772b61e59..ef099a9f38 100644 --- a/src/frontend/Canonicalize.ml +++ b/src/frontend/Canonicalize.ml @@ -89,7 +89,7 @@ let rec repair_syntax_stmt user_dists {stmt; smeta} = { stmt= map_statement repair_syntax_expr (repair_syntax_stmt user_dists) - repair_syntax_lval ident stmt + repair_syntax_lval ident ident stmt ; smeta } let rec replace_deprecated_expr {expr; emeta} = @@ -140,7 +140,12 @@ let rec replace_deprecated_stmt {stmt; smeta} = { assign_lhs= replace_deprecated_lval l ; assign_op= Assign ; assign_rhs= replace_deprecated_expr e } - | FunDef {returntype; funname= {name; id_loc}; arguments; body} -> + | FunDef + { returntype + ; funname= {name; id_loc} + ; closure + ; arguments + ; body } -> FunDef { returntype ; funname= @@ -148,11 +153,12 @@ let rec replace_deprecated_stmt {stmt; smeta} = Option.value ~default:name (String.Table.find deprecated_userdefined name) ; id_loc } + ; closure ; arguments ; body= replace_deprecated_stmt body } | _ -> map_statement replace_deprecated_expr replace_deprecated_stmt - replace_deprecated_lval ident stmt + replace_deprecated_lval ident ident stmt in {stmt; smeta} @@ -209,7 +215,7 @@ let rec parens_stmt {stmt; smeta} = ; lower_bound= keep_parens lower_bound ; upper_bound= keep_parens upper_bound ; loop_body= parens_stmt loop_body } - | _ -> map_statement no_parens parens_stmt parens_lval ident stmt + | _ -> map_statement no_parens parens_stmt parens_lval ident ident stmt in {stmt; smeta} diff --git a/src/frontend/Pretty_printing.ml b/src/frontend/Pretty_printing.ml index f7b2a1955e..26cb717310 100644 --- a/src/frontend/Pretty_printing.ml +++ b/src/frontend/Pretty_printing.ml @@ -66,7 +66,7 @@ and pp_unsizedtype ppf = function let ut2, d = unwind_array_type ut in let array_str = "[" ^ String.make d ',' ^ "]" in Fmt.(suffix (const string array_str) pp_unsizedtype ppf ut2) - | UFun (argtypes, rt) -> + | UFun (argtypes, rt, _) -> Fmt.pf ppf "{|@[(%a) => %a@]|}" Fmt.(list ~sep:comma_no_break pp_argtype) argtypes pp_returntype rt @@ -354,7 +354,8 @@ and pp_statement ppf ({stmt= s_content; _} as ss) = with_hbox ppf (fun () -> Fmt.pf ppf "%a %a%a%a;" pp_transformed_type (pst, trans) pp_identifier id pp_array_dims es pp_init init ) - | FunDef {returntype= rt; funname= id; arguments= args; body= b} -> ( + | FunDef {returntype= rt; funname= id; arguments= args; body= b; closure= _} + -> ( Fmt.pf ppf "%a %a(" pp_returntype rt pp_identifier id ; with_box ppf 0 (fun () -> Fmt.pf ppf "%a" (Fmt.list ~sep:Fmt.comma pp_args) args ) ; diff --git a/src/frontend/Semantic_check.ml b/src/frontend/Semantic_check.ml index 5c493d2b32..a700d1e403 100644 --- a/src/frontend/Semantic_check.ml +++ b/src/frontend/Semantic_check.ml @@ -165,10 +165,9 @@ let semantic_check_assignmentoperator op = Validate.ok op (* Probably nothing to do here *) let semantic_check_autodifftype at = Validate.ok at -(* Probably nothing to do here *) -let rec semantic_check_unsizedtype : UnsizedType.t -> unit Validate.t = +let rec semantic_check_unsizedtype ~loc : UnsizedType.t -> unit Validate.t = function - | UFun (l, rt) -> + | UFun (l, rt, _) -> (* fold over argument types accumulating errors with initial state given by validating the return type *) List.fold @@ -176,16 +175,18 @@ let rec semantic_check_unsizedtype : UnsizedType.t -> unit Validate.t = Validate.( apply_const (apply_const v0 (semantic_check_autodifftype at)) - (semantic_check_unsizedtype ut)) ) - ~init:(semantic_check_returntype rt) + (semantic_check_unsizedtype ~loc ut)) ) + ~init:(semantic_check_returntype ~loc rt) l - | UArray ut -> semantic_check_unsizedtype ut + | UArray ut -> semantic_check_unsizedtype ~loc ut | _ -> Validate.ok () -and semantic_check_returntype : UnsizedType.returntype -> unit Validate.t = - function +and semantic_check_returntype ~loc : UnsizedType.returntype -> unit Validate.t + = function | Void -> Validate.ok () - | ReturnType ut -> semantic_check_unsizedtype ut + | ReturnType (UFun _) -> + Validate.error (Semantic_error.invalid_return_type loc) + | ReturnType ut -> semantic_check_unsizedtype ~loc ut (* -- Indentifiers ---------------------------------------------------------- *) let reserved_keywords = @@ -272,10 +273,10 @@ let mk_fun_app ~is_cond_dist (x, y, z) = let semantic_check_fn_normal ~is_cond_dist ~loc id es = Validate.( match Symbol_table.look vm id.name with - | Some (_, UnsizedType.UFun (_, Void)) -> + | Some (_, UnsizedType.UFun (_, Void, _)) -> Semantic_error.returning_fn_expected_nonreturning_found loc id.name |> error - | Some (_, UFun (listedtypes, rt)) + | Some (_, UFun (listedtypes, rt, _)) when not (UnsizedType.check_compatible_arguments_mod_conv id.name listedtypes (get_arg_types es)) -> @@ -284,7 +285,7 @@ let semantic_check_fn_normal ~is_cond_dist ~loc id es = |> Semantic_error.illtyped_userdefined_fn_app loc id.name listedtypes rt |> error - | Some (_, UFun (_, ReturnType ut)) -> + | Some (_, UFun (_, ReturnType ut, _)) -> mk_typed_expression ~expr:(mk_fun_app ~is_cond_dist (UserDefined, id, es)) ~ad_level:(lub_ad_e es) ~type_:ut ~loc @@ -329,7 +330,8 @@ let semantic_check_reduce_sum ~is_cond_dist ~loc id es = UnsizedType.UFun ( ((_, sliced_arg_fun_type) as sliced_arg_fun) :: (_, UInt) :: (_, UInt) :: fun_args - , ReturnType UReal ); _ }; _ } + , ReturnType UReal + , _ ); _ }; _ } :: sliced :: {emeta= {type_= UInt; _}; _} :: args when arg_match sliced_arg_fun sliced && List.mem Stan_math_signatures.reduce_sum_slice_types @@ -860,20 +862,20 @@ let semantic_check_nrfn_target ~loc ~cf id = let semantic_check_nrfn_normal ~loc id es = Validate.( match Symbol_table.look vm id.name with - | Some (_, UFun (listedtypes, Void)) + | Some (_, UFun (listedtypes, Void, _)) when UnsizedType.check_compatible_arguments_mod_conv id.name listedtypes (get_arg_types es) -> mk_typed_statement ~stmt:(NRFunApp (UserDefined, id, es)) ~return_type:NoReturnType ~loc |> ok - | Some (_, UFun (listedtypes, Void)) -> + | Some (_, UFun (listedtypes, Void, _)) -> es |> List.map ~f:type_of_expr_typed |> Semantic_error.illtyped_userdefined_fn_app loc id.name listedtypes Void |> error - | Some (_, UFun (_, ReturnType _)) -> + | Some (_, UFun (_, ReturnType _, _)) -> Semantic_error.nonreturning_fn_expected_returning_found loc id.name |> error | Some _ -> @@ -1059,7 +1061,7 @@ let semantic_check_sampling_distribution ~loc id arguments = |> Option.value_map ~default:false ~f:is_real_rt and valid_arg_types_for_suffix suffix = match Symbol_table.look vm (name ^ suffix) with - | Some (Functions, UFun (listedtypes, ReturnType UReal)) -> + | Some (Functions, UFun (listedtypes, ReturnType UReal, _)) -> UnsizedType.check_compatible_arguments_mod_conv name listedtypes argumenttypes | _ -> false @@ -1089,7 +1091,7 @@ let cumulative_density_is_defined id arguments = |> Option.value_map ~default:false ~f:is_real_rt and valid_arg_types_for_suffix suffix = match Symbol_table.look vm (name ^ suffix) with - | Some (Functions, UFun (listedtypes, ReturnType UReal)) -> + | Some (Functions, UFun (listedtypes, ReturnType UReal, _)) -> UnsizedType.check_compatible_arguments_mod_conv name listedtypes argumenttypes | _ -> false @@ -1273,18 +1275,20 @@ and semantic_check_while ~loc ~cf e s = ue us (* -- For Statements -------------------------------------------------------- *) -and semantic_check_loop_body ~cf loop_var loop_var_ty loop_body = - Symbol_table.begin_scope vm ; - let is_fresh_var = check_fresh_variable loop_var false in - Symbol_table.enter vm loop_var.name (cf.current_block, loop_var_ty) ; - (* Check that function args and loop identifiers are not modified in - function. (passed by const ref) *) - Symbol_table.set_read_only vm loop_var.name ; - let us = - semantic_check_statement {cf with loop_depth= cf.loop_depth + 1} loop_body - |> Validate.apply_const is_fresh_var - in - Symbol_table.end_scope vm ; us +and semantic_check_loop_body ~cf loop_var loop_var_ty = + Symbol_table.with_scope vm (fun loop_body -> + let is_fresh_var = check_fresh_variable loop_var false in + Symbol_table.enter vm loop_var.name (cf.current_block, loop_var_ty) ; + (* Check that function args and loop identifiers are not modified in + function. (passed by const ref) *) + Symbol_table.set_read_only vm loop_var.name ; + let us = + semantic_check_statement + {cf with loop_depth= cf.loop_depth + 1} + loop_body + |> Validate.apply_const is_fresh_var + in + us ) and semantic_check_for ~loc ~cf loop_var lower_bound_e upper_bound_e loop_body = @@ -1371,14 +1375,14 @@ and try_compute_block_statement_returntype loc srt1 srt2 = Validate.ok AnyReturnType and semantic_check_block ~loc ~cf stmts = - Symbol_table.begin_scope vm ; (* Any statements after a break or continue or return or reject do not count for the return type. *) let validated_stmts = - List.map ~f:(semantic_check_statement cf) stmts |> Validate.sequence + stmts + |> Symbol_table.with_scope vm (List.map ~f:(semantic_check_statement cf)) + |> Validate.sequence in - Symbol_table.end_scope vm ; Validate.( validated_stmts >>= fun xs -> @@ -1486,12 +1490,12 @@ and semantic_check_var_decl ~loc ~cf sized_ty trans id init is_global = mk_typed_statement ~stmt ~loc ~return_type:NoReturnType )) (* -- Function definitions -------------------------------------------------- *) -and semantic_check_fundef_overloaded ~loc id arg_tys rt = +and semantic_check_fundef_overloaded ~loc is_closure id arg_tys rt = Validate.( (* User defined functions cannot be overloaded *) - if Symbol_table.check_is_unassigned vm id.name then + if (not is_closure) && Symbol_table.check_is_unassigned vm id.name then match Symbol_table.look vm id.name with - | Some (Functions, UFun (arg_tys', rt')) + | Some (Functions, UFun (arg_tys', rt', _)) when arg_tys' = arg_tys && rt' = rt -> ok () | _ -> @@ -1502,11 +1506,12 @@ and semantic_check_fundef_overloaded ~loc id arg_tys rt = else check_fresh_variable id (List.length arg_tys = 0)) (** WARNING: side effecting *) -and semantic_check_fundef_decl ~loc id body = +and semantic_check_fundef_decl ~loc is_closure id body = Validate.( match body with | {stmt= Skip; _} -> - if Symbol_table.check_is_unassigned vm id.name then + if is_closure then error @@ Semantic_error.invalid_forward_decl loc + else if Symbol_table.check_is_unassigned vm id.name then error @@ Semantic_error.fn_decl_without_def loc else let () = Symbol_table.set_is_unassigned vm id.name in @@ -1515,6 +1520,17 @@ and semantic_check_fundef_decl ~loc id body = Symbol_table.set_is_assigned vm id.name ; ok ()) +and semantic_check_fundef_suffix ~loc is_closure id = + Validate.( + let is_special = + List.exists + ~f:(fun x -> String.is_suffix id.name ~suffix:x) + ["_log"; "_lpdf"; "_lpmf"; "_lcdf"; "_lccdf"; "_rng"; "_lp"] + in + if is_closure && is_special then + error @@ Semantic_error.invalid_special_fn loc + else ok ()) + and semantic_check_fundef_dist_rt ~loc id return_ty = Validate.( let is_dist = @@ -1583,77 +1599,95 @@ and semantic_check_fundef_return_tys ~loc id return_type body = if Symbol_table.check_is_unassigned vm id.name || check_of_compatible_return_type return_type body.smeta.return_type - then ok () + then ok body else error @@ Semantic_error.incompatible_return_types loc) -and semantic_check_fundef ~loc ~cf return_ty id args body = +and semantic_check_fundef ~loc ~cf return_ty id is_closure args body = let uargs = List.map args ~f:(fun (at, ut, id) -> Validate.( semantic_check_autodifftype at - |> apply_const (semantic_check_unsizedtype ut) + |> apply_const (semantic_check_unsizedtype ~loc ut) |> apply_const (semantic_check_identifier id) |> map ~f:(fun at -> (at, ut, id))) ) |> Validate.sequence in + let clname = if is_closure then Symbol_table.new_clname vm else "" in Validate.( uargs |> apply_const (semantic_check_identifier id) - |> apply_const (semantic_check_returntype return_ty) + |> apply_const (semantic_check_returntype ~loc return_ty) >>= fun uargs -> let urt = return_ty in let uarg_types = List.map ~f:(fun (w, y, _) -> (w, y)) uargs in let uarg_identifiers = List.map ~f:(fun (_, _, z) -> z) uargs in let uarg_names = List.map ~f:(fun x -> x.name) uarg_identifiers in - semantic_check_fundef_overloaded ~loc id uarg_types urt - |> apply_const (semantic_check_fundef_decl ~loc id body) - >>= fun _ -> + semantic_check_fundef_overloaded ~loc is_closure id uarg_types urt + |> apply_const (semantic_check_fundef_decl ~loc is_closure id body) + >>= fun () -> (* WARNING: SIDE EFFECTING *) - Symbol_table.enter vm id.name (Functions, UFun (uarg_types, urt)) ; - (* Check that function args and loop identifiers are not modified in - function. (passed by const ref)*) - List.iter ~f:(Symbol_table.set_read_only vm) uarg_names ; - semantic_check_fundef_dist_rt ~loc id urt + Symbol_table.enter vm id.name + ( cf.current_block + , UFun (uarg_types, urt, if is_closure then Closure else Function) + ) ; + Symbol_table.set_read_only vm id.name ; + semantic_check_fundef_suffix ~loc:id.id_loc is_closure id + |> apply_const (semantic_check_fundef_dist_rt ~loc id urt) |> apply_const (semantic_check_pdf_fundef_first_arg_ty ~loc id uarg_types) |> apply_const (semantic_check_pmf_fundef_first_arg_ty ~loc id uarg_types) - >>= fun _ -> - (* WARNING: SIDE EFFECTING *) - Symbol_table.begin_scope vm ; - List.map ~f:(fun x -> check_fresh_variable x false) uarg_identifiers + >>= Symbol_table.with_closure vm (fun captures () -> + List.map + ~f:(fun x -> + Symbol_table.set_read_only vm x.name ; + check_fresh_variable x false ) + uarg_identifiers + |> sequence + |> map ~f:(List.iter ~f:Fn.id) + |> apply_const + (semantic_check_fundef_distinct_arg_ids ~loc uarg_names) + >>= fun () -> + (* TODO: Bob was suggesting that function arguments must be allowed to + shadow user defined functions but not library functions. + Should we allow for that? + *) + (* We treat DataOnly arguments as if they are data and AutoDiffable arguments + as if they are parameters, for the purposes of type checking. + *) + (* WARNING: SIDE EFFECTING *) + let _ : unit = + List.iter2_exn ~f:(Symbol_table.enter vm) uarg_names + (List.map + ~f:(function + | UnsizedType.DataOnly, ut -> (Data, ut) + | AutoDiffable, ut -> (Param, ut)) + uarg_types) + and context = + { cf with + in_fun_def= true + ; in_rng_fun_def= String.is_suffix id.name ~suffix:"_rng" + ; in_lp_fun_def= String.is_suffix id.name ~suffix:"_lp" + ; in_returning_fun_def= urt <> Void } + in + semantic_check_statement context body + >>= fun ub -> + semantic_check_fundef_return_tys ~loc id urt ub + |> map ~f:(fun ub -> (ub, Set.to_list !captures)) ) + >>= fun (ub, captures) -> + List.filter_map captures ~f:(fun name -> + match Symbol_table.look vm name with + | None | Some (Functions, UFun (_, _, Function)) -> None + | Some (block, type_) -> + Symbol_table.set_read_only vm name ; + Some (ok (calculate_autodifftype cf block type_, type_, name)) ) |> sequence - |> apply_const (semantic_check_fundef_distinct_arg_ids ~loc uarg_names) - >>= fun _ -> - (* TODO: Bob was suggesting that function arguments must be allowed to - shadow user defined functions but not library functions. - Should we allow for that? - *) - (* We treat DataOnly arguments as if they are data and AutoDiffable arguments - as if they are parameters, for the purposes of type checking. - *) - (* WARNING: SIDE EFFECTING *) - let _ : unit Base.List.Or_unequal_lengths.t = - List.iter2 ~f:(Symbol_table.enter vm) uarg_names - (List.map - ~f:(function - | UnsizedType.DataOnly, ut -> (Data, ut) - | AutoDiffable, ut -> (Param, ut)) - uarg_types) - and context = - { cf with - in_fun_def= true - ; in_rng_fun_def= String.is_suffix id.name ~suffix:"_rng" - ; in_lp_fun_def= String.is_suffix id.name ~suffix:"_lp" - ; in_returning_fun_def= urt <> Void } - in - let body' = semantic_check_statement context body in - body' - >>= fun ub -> - semantic_check_fundef_return_tys ~loc id urt ub - |> map ~f:(fun _ -> - (* WARNING: SIDE EFFECTING *) - Symbol_table.end_scope vm ; + |> map ~f:(fun captures -> let stmt = - FunDef {returntype= urt; funname= id; arguments= uargs; body= ub} + FunDef + { returntype= urt + ; funname= id + ; closure= (if is_closure then Some {clname; captures} else None) + ; arguments= uargs + ; body= ub } in mk_typed_statement ~return_type:NoReturnType ~loc ~stmt )) @@ -1693,8 +1727,8 @@ and semantic_check_statement cf (s : Ast.untyped_statement) : ; is_global } -> semantic_check_var_decl ~loc ~cf st transformation identifier initial_value is_global - | FunDef {returntype; funname; arguments; body} -> - semantic_check_fundef ~loc ~cf returntype funname arguments body + | FunDef {returntype; funname; closure; arguments; body} -> + semantic_check_fundef ~loc ~cf returntype funname closure arguments body (* == Untyped programs ====================================================== *) @@ -1767,9 +1801,11 @@ let semantic_check_program let upb = semantic_check_ostatements_in_block ~cf Param pb in let utpb = semantic_check_ostatements_in_block ~cf TParam tpb in (* Model top level variables only assigned and read in model *) - Symbol_table.begin_scope vm ; - let umb = semantic_check_ostatements_in_block ~cf Model mb in - Symbol_table.end_scope vm ; + let umb = + Symbol_table.with_scope vm + (semantic_check_ostatements_in_block ~cf Model) + mb + in let ugb = semantic_check_ostatements_in_block ~cf GQuant gb in let mk_typed_prog ufb udb utdb upb utpb umb ugb : Ast.typed_program = { functionblock= ufb diff --git a/src/frontend/Semantic_error.ml b/src/frontend/Semantic_error.ml index a354a0d5fc..4951d8f0de 100644 --- a/src/frontend/Semantic_error.ml +++ b/src/frontend/Semantic_error.ml @@ -101,7 +101,8 @@ module TypeError = struct [ [ UnsizedType.UFun ( List.hd_exn args :: (AutoDiffable, UInt) :: (AutoDiffable, UInt) :: List.tl_exn args - , ReturnType UReal ) ] + , ReturnType UReal + , Function ) ] ; first; [UInt]; rest ] in Fmt.pf ppf @@ -186,7 +187,7 @@ module TypeError = struct signatures:%a\n\ @[Instead supplied arguments of incompatible type: %a.@]" name UnsizedType.pp - (UFun (listed_tys, return_ty)) + (UFun (listed_tys, return_ty, Function)) Fmt.(list UnsizedType.pp ~sep:comma) arg_tys | IllTypedBinaryOperator (op, lt, rt) -> @@ -297,11 +298,13 @@ module StatementError = struct | ProbMassNonIntVariate of UnsizedType.t option | DuplicateArgNames | IncompatibleReturnType + | InvalidForwardDecl + | SpecialClosure + | InvalidReturnType let pp ppf = function | CannotAssignToReadOnly name -> - Fmt.pf ppf - "Cannot assign to function argument or loop identifier '%s'." name + Fmt.pf ppf "Cannot assign to read-only identifier '%s'." name | CannotAssignToGlobal name -> Fmt.pf ppf "Cannot assign to global variable '%s' declared in previous blocks." @@ -396,6 +399,13 @@ For example, "target += normal_lpdf(y, 0, 1)" should become "y ~ normal(0, 1)." Fmt.pf ppf "Function bodies must contain a return statement of correct type in \ every branch." + | InvalidForwardDecl -> + Fmt.pf ppf "Cannot declare a closure without definition." + | SpecialClosure -> + Fmt.pf ppf + "Cannot declare a closure with suffix _lpdf, _lpmf, _log, _lcdf, \ + _lccdf, _rng or _lp." + | InvalidReturnType -> Fmt.pf ppf "Return type cannot be a function type." end type t = @@ -588,3 +598,11 @@ let duplicate_arg_names loc = let incompatible_return_types loc = StatementError (loc, StatementError.IncompatibleReturnType) + +let invalid_forward_decl loc = + StatementError (loc, StatementError.InvalidForwardDecl) + +let invalid_special_fn loc = StatementError (loc, StatementError.SpecialClosure) + +let invalid_return_type loc = + StatementError (loc, StatementError.InvalidReturnType) diff --git a/src/frontend/Semantic_error.mli b/src/frontend/Semantic_error.mli index 5765b2332b..cfc7da889a 100644 --- a/src/frontend/Semantic_error.mli +++ b/src/frontend/Semantic_error.mli @@ -108,3 +108,6 @@ val prob_density_non_real_variate : val prob_mass_non_int_variate : Location_span.t -> UnsizedType.t option -> t val duplicate_arg_names : Location_span.t -> t val incompatible_return_types : Location_span.t -> t +val invalid_forward_decl : Location_span.t -> t +val invalid_special_fn : Location_span.t -> t +val invalid_return_type : Location_span.t -> t diff --git a/src/frontend/Symbol_table.ml b/src/frontend/Symbol_table.ml index a6d975ae21..0a1cfcac5d 100644 --- a/src/frontend/Symbol_table.ml +++ b/src/frontend/Symbol_table.ml @@ -10,6 +10,9 @@ type 'a state = ; scopedepth: int ref ; readonly: (string, unit) Hashtbl.t ; isunassigned: (string, unit) Hashtbl.t + ; locals: (string, unit) Hashtbl.t ref + ; captures: String.Set.t ref + ; closures: String.Set.t ref ; globals: (string, unit) Hashtbl.t } let initialize () = @@ -18,17 +21,23 @@ let initialize () = ; scopedepth= ref 0 ; readonly= String.Table.create () ; isunassigned= String.Table.create () + ; locals= ref (String.Table.create ()) + ; captures= ref String.Set.empty + ; closures= ref String.Set.empty ; globals= String.Table.create () } let enter s str ty = - let _ : [`Duplicate | `Ok] = - if !(s.scopedepth) = 0 then Hashtbl.add s.globals ~key:str ~data:() - else `Ok - in + if !(s.scopedepth) = 0 then + ignore (Hashtbl.add s.globals ~key:str ~data:() : [`Duplicate | `Ok]) ; + let _ : [`Duplicate | `Ok] = Hashtbl.add !(s.locals) ~key:str ~data:() in let _ : [`Duplicate | `Ok] = Hashtbl.add s.table ~key:str ~data:ty in Stack.push s.stack str -let look s str = Hashtbl.find s.table str +let look s str = + ( match Hashtbl.find !(s.locals) str with + | None -> s.captures := Set.add !(s.captures) str + | Some () -> () ) ; + Hashtbl.find s.table str let begin_scope s = s.scopedepth := !(s.scopedepth) + 1 ; @@ -41,34 +50,55 @@ let end_scope s = (* we pop the stack down to where we entered the current scope and remove all variables defined since from the var map *) Hashtbl.remove s.table (Stack.top_exn s.stack) ; Hashtbl.remove s.readonly (Stack.top_exn s.stack) ; + Hashtbl.remove !(s.locals) (Stack.top_exn s.stack) ; Hashtbl.remove s.isunassigned (Stack.top_exn s.stack) ; - let _ : string = Stack.pop_exn s.stack in - () + Stack.pop_exn s.stack |> (ignore : string -> unit) done ; - let _ : string = Stack.pop_exn s.stack in - () + Stack.pop_exn s.stack |> (ignore : string -> unit) + +let with_scope s f x = + begin_scope s ; + let x = f x in + end_scope s ; x + +let with_closure s f x = + let locals = !(s.locals) in + let captures = !(s.captures) in + s.locals := String.Table.create () ; + s.captures := String.Set.empty ; + begin_scope s ; + let x = f s.captures x in + end_scope s ; + s.locals := locals ; + s.captures := captures ; + x let set_read_only s str = - let _ : [`Duplicate | `Ok] = Hashtbl.add s.readonly ~key:str ~data:() in - () + Hashtbl.add s.readonly ~key:str ~data:() + |> (ignore : [`Duplicate | `Ok] -> unit) let get_read_only s str = - match Hashtbl.find s.readonly str with Some () -> true | _ -> false + Option.( + is_some (Hashtbl.find s.readonly str) + || not (is_some (Hashtbl.find !(s.locals) str))) let set_is_assigned s str = Hashtbl.remove s.isunassigned str let set_is_unassigned s str = - let _ : [`Duplicate | `Ok] = - if Hashtbl.mem s.isunassigned str then `Ok - else Hashtbl.add s.isunassigned ~key:str ~data:() - in - () + Hashtbl.add s.isunassigned ~key:str ~data:() + |> (ignore : [`Duplicate | `Ok] -> unit) let check_is_unassigned s str = Hashtbl.mem s.isunassigned str let check_some_id_is_unassigned s = not (Hashtbl.length s.isunassigned = 0) +let is_global s str = Option.is_some (Hashtbl.find s.globals str) -let is_global s str = - match Hashtbl.find s.globals str with Some _ -> true | _ -> false +let new_clname vm = + let clname = + let n = Set.length !(vm.closures) in + if n < 10 then Fmt.strf "closure0%d" n else Fmt.strf "closure%d" n + in + vm.closures := Set.add !(vm.closures) clname ; + clname let unsafe_clear_symbol_table s = Hashtbl.clear s.table ; @@ -76,4 +106,7 @@ let unsafe_clear_symbol_table s = s.scopedepth := 0 ; Hashtbl.clear s.readonly ; Hashtbl.clear s.isunassigned ; + s.captures := String.Set.empty ; + s.closures := String.Set.empty ; + Hashtbl.clear !(s.locals) ; Hashtbl.clear s.globals diff --git a/src/frontend/Symbol_table.mli b/src/frontend/Symbol_table.mli index 60dae7583a..1b9a8e35d3 100644 --- a/src/frontend/Symbol_table.mli +++ b/src/frontend/Symbol_table.mli @@ -18,6 +18,10 @@ val begin_scope : 'a state -> unit val end_scope : 'a state -> unit (** Used to end a local scope, purging the symbol table of all symbols added in that scope *) +val with_scope : 'a state -> ('b -> 'c) -> 'b -> 'c + +val with_closure : 'a state -> (Core_kernel.String.Set.t ref -> 'b -> 'c) -> 'b -> 'c + val set_read_only : 'a state -> string -> unit (** Used to add a read only label to an identifier *) @@ -41,3 +45,5 @@ val is_global : 'a state -> string -> bool val unsafe_clear_symbol_table : 'a state -> unit (** Used to clear the whole symbol table *) + +val new_clname : 'a state -> string diff --git a/src/frontend/parser.mly b/src/frontend/parser.mly index 3c80f535e9..f5244934ba 100644 --- a/src/frontend/parser.mly +++ b/src/frontend/parser.mly @@ -176,6 +176,19 @@ function_def: { grammar_logger "function_def" ; {stmt=FunDef {returntype = rt; funname = name; + closure = false; + arguments = args; body=b;}; + smeta={loc=Location_span.of_positions_exn $startpos $endpos} + } + } + +closure_def: + | FUNCTIONBLOCK rt=return_type name=decl_identifier + LPAREN args=separated_list(COMMA, arg_decl) RPAREN b=statement + { + grammar_logger "function_def" ; + {stmt=FunDef {returntype = rt; funname = name; + closure = true; arguments = args; body=b;}; smeta={loc=Location_span.of_positions_exn $startpos $endpos} } @@ -192,15 +205,27 @@ arg_decl: { grammar_logger "arg_decl" ; match od with None -> (UnsizedType.AutoDiffable, ut, id) | _ -> (DataOnly, ut, id) } +arg_type: + | od=option(DATABLOCK) ut=unsized_type + { grammar_logger "arg_type" ; + match od with None -> (UnsizedType.AutoDiffable, ut) | _ -> (DataOnly, ut) } + +type_args: + | LPAREN args=separated_list(COMMA, arg_type) RPAREN + { grammar_logger "type_args" ; + args } + unsized_type: - | bt=basic_type ud=option(unsized_dims) + | bt=basic_type ud=option(unsized_dims) fa=option(type_args) { grammar_logger "unsized_type" ; let rec reparray n x = if n <= 0 then x else reparray (n-1) (UnsizedType.UArray x) in let size = - match ud with Some d -> 1 + d | None -> 0 - in - reparray size bt } + match ud with Some d -> 1 + d | None -> 0 in + let t = reparray size bt in + match fa with + | None -> t + | Some args -> UnsizedType.UFun (args, UnsizedType.ReturnType t, Closure) } basic_type: | INT @@ -635,9 +660,13 @@ vardecl_or_statement: { grammar_logger "vardecl_or_statement_statement" ; s } | v=var_decl { grammar_logger "vardecl_or_statement_vardecl" ; v } + | f=closure_def + { grammar_logger "vardecl_or_statement_vardecl" ; f } top_vardecl_or_statement: | s=statement { grammar_logger "top_vardecl_or_statement_statement" ; s } | v=top_var_decl { grammar_logger "top_vardecl_or_statement_top_vardecl" ; v } + | f=closure_def + { grammar_logger "vardecl_or_statement_vardecl" ; f } diff --git a/src/middle/Internal_fun.ml b/src/middle/Internal_fun.ml index e889bedf49..8306464da2 100644 --- a/src/middle/Internal_fun.ml +++ b/src/middle/Internal_fun.ml @@ -4,6 +4,7 @@ type t = | FnLength | FnMakeArray | FnMakeRowVec + | FnMakeClosure | FnNegInf | FnReadData (* XXX move these to a backend specific file?*) @@ -19,6 +20,9 @@ type t = | FnReject | FnResizeToMatch | FnNaN + | FnZeroAdjoint + | FnGetAdjoint + | FnGetVariPtr [@@deriving sexp] let to_string x = Sexp.to_string (sexp_of_t x) ^ "__" diff --git a/src/middle/Program.ml b/src/middle/Program.ml index 5b93470ff4..2bd11525ef 100644 --- a/src/middle/Program.ml +++ b/src/middle/Program.ml @@ -5,6 +5,13 @@ open Helpers type fun_arg_decl = (UnsizedType.autodifftype * string * UnsizedType.t) list [@@deriving sexp, hash, map] +type 'a closure_def = + { cdrt: UnsizedType.t option + ; cdcaptures: (UnsizedType.autodifftype * string * UnsizedType.t) list + ; cdargs: (UnsizedType.autodifftype * string * UnsizedType.t) list + ; cdbody: 'a } +[@@deriving compare, hash, map, sexp, map, fold] + type 'a fun_def = { fdrt: UnsizedType.t option ; fdname: string @@ -42,8 +49,19 @@ type 'e outvar = ; out_trans: 'e transformation } [@@deriving sexp, map, hash, fold] +module StringMap = struct + include String.Map + + let map f = map ~f + + let fold f init = + let f ~key:_ ~data accum = f accum data in + fold ~init ~f +end + type ('a, 'b) t = - { functions_block: 'b fun_def list + { closures: 'b closure_def StringMap.t + ; functions_block: 'b fun_def list ; input_vars: (string * 'a SizedType.t) list ; prepare_data: 'b list (* data & transformed data decls and statements *) ; log_prob: 'b list (*assumes data & params are in scope and ready*) diff --git a/src/middle/Stan_math_signatures.ml b/src/middle/Stan_math_signatures.ml index ab740be3e6..f26a1a614d 100644 --- a/src/middle/Stan_math_signatures.ml +++ b/src/middle/Stan_math_signatures.ml @@ -1,7 +1,7 @@ (** The signatures of the Stan Math library, which are used for type checking *) open Core_kernel -(* The "dimensionality" (bad name?) is supposed to help us represent the +(** The "dimensionality" (bad name?) is supposed to help us represent the vectorized nature of many Stan functions. It allows us to represent when a function argument can be just a real or matrix, or some common forms of vectorization over reals. This captures the most commonly used forms in our @@ -139,9 +139,7 @@ let mk_declarative_sig (fnkinds, name, args) = let full_lpdf = [Lpdf; Rng; Ccdf; Cdf] let full_lpmf = [Lpmf; Rng; Ccdf; Cdf] - let reduce_sum_functions = ["reduce_sum"; "reduce_sum_static"] - let is_reduce_sum_fn f = List.mem ~equal:String.equal reduce_sum_functions f let distributions = @@ -318,7 +316,7 @@ let get_sigs name = let name = Utils.stdlib_distribution_name name in Hashtbl.find_multi stan_math_signatures name |> List.sort ~compare -let pp_math_sig ppf (rt, args) = UnsizedType.pp ppf (UFun (args, rt)) +let pp_math_sig ppf (rt, args) = UnsizedType.pp ppf (UFun (args, rt, Function)) let pp_math_sigs ppf name = (Fmt.list ~sep:Fmt.cut pp_math_sig) ppf (get_sigs name) @@ -431,7 +429,8 @@ let () = , UFun ( [ (AutoDiffable, UVector); (AutoDiffable, UVector) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType UVector ) ) + , ReturnType UVector + , Function ) ) ; (AutoDiffable, UVector); (AutoDiffable, UVector) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] ) ; add_qualified @@ -441,7 +440,8 @@ let () = , UFun ( [ (AutoDiffable, UVector); (AutoDiffable, UVector) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType UVector ) ) + , ReturnType UVector + , Function ) ) ; (AutoDiffable, UVector); (AutoDiffable, UVector) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt); (DataOnly, UReal) ; (DataOnly, UReal); (DataOnly, UReal) ] ) ; @@ -972,7 +972,8 @@ let () = ( [ (AutoDiffable, UReal); (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType UReal ) ) + , ReturnType UReal + , Function ) ) ; (AutoDiffable, UReal); (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] ) ; @@ -984,7 +985,8 @@ let () = ( [ (AutoDiffable, UReal); (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType UReal ) ) + , ReturnType UReal + , Function ) ) ; (AutoDiffable, UReal); (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt); (DataOnly, UReal) ] @@ -998,7 +1000,8 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) @@ -1013,7 +1016,8 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) @@ -1028,7 +1032,8 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) @@ -1044,7 +1049,8 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) @@ -1059,13 +1065,70 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) + ; (AutoDiffable, UArray UReal) + ; (AutoDiffable, UReal) + ; (AutoDiffable, UArray UReal) + ; (AutoDiffable, UArray UReal) + ; (DataOnly, UArray UReal); (DataOnly, UArray UInt); (DataOnly, UReal) + ; (DataOnly, UReal); (DataOnly, UReal) ] ) ; + add_qualified + ( "integrate_ode_bdf" + , ReturnType (UArray UVector) + , [ ( AutoDiffable + , UFun + ( [ (AutoDiffable, UReal) + ; (AutoDiffable, UVector) + ; (AutoDiffable, UArray UReal) + ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] + , ReturnType (UVector) + , Function ) ) + ; (AutoDiffable, UVector) + ; (AutoDiffable, UReal) + ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) + ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] ) ; + add_qualified + ( "integrate_ode_bdf" + , ReturnType (UArray (UVector)) + , [ ( AutoDiffable + , UFun + ( [ (AutoDiffable, UReal) + ; (AutoDiffable, UVector) + ; (AutoDiffable, UArray UReal) + ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] + , ReturnType (UVector) + , Function ) ) + ; (AutoDiffable, UVector) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt); (DataOnly, UReal) ; (DataOnly, UReal); (DataOnly, UReal) ] ) ; + add_qualified + ( "integrate_ode_bdf" + , ReturnType (UArray UVector) + , [ ( AutoDiffable + , UFun + ( [(AutoDiffable, UReal); (AutoDiffable, UVector)] + , ReturnType UVector + , Function ) ) + ; (AutoDiffable, UVector) + ; (AutoDiffable, UReal) + ; (AutoDiffable, UArray UReal) ] ) ; + add_qualified + ( "integrate_ode_bdf" + , ReturnType (UArray UVector) + , [ ( AutoDiffable + , UFun + ( [(AutoDiffable, UReal); (AutoDiffable, UVector)] + , ReturnType (UVector) + , Function ) ) + ; (AutoDiffable, UVector) + ; (AutoDiffable, UReal) + ; (AutoDiffable, UArray UReal) + ; (DataOnly, UReal); (DataOnly, UReal); (DataOnly, UReal) ] ) ; add_qualified ( "integrate_ode_rk45" , ReturnType (UArray (UArray UReal)) @@ -1075,7 +1138,8 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) @@ -1090,7 +1154,8 @@ let () = ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UArray UReal) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType (UArray UReal) ) ) + , ReturnType (UArray UReal) + , Function ) ) ; (AutoDiffable, UArray UReal) ; (AutoDiffable, UReal) ; (AutoDiffable, UArray UReal) @@ -1178,7 +1243,8 @@ let () = , UFun ( [ (AutoDiffable, UVector); (AutoDiffable, UVector) ; (DataOnly, UArray UReal); (DataOnly, UArray UInt) ] - , ReturnType UVector ) ) + , ReturnType UVector + , Function ) ) ; (AutoDiffable, UVector) ; (AutoDiffable, UArray UVector) ; (DataOnly, UArray (UArray UReal)) diff --git a/src/middle/Stmt.ml b/src/middle/Stmt.ml index d5b5e6c721..4cfdae6bf6 100644 --- a/src/middle/Stmt.ml +++ b/src/middle/Stmt.ml @@ -261,14 +261,11 @@ module Helpers = struct in let loopvar, reset = Gensym.enter () in let lower = Expr.Helpers.loop_bottom in - let stmt = - Fixed.Pattern.Block - [bodyfn (Expr.Helpers.add_int_index iteratee (idx loopvar))] - in + let body = bodyfn (Expr.Helpers.add_int_index iteratee (idx loopvar)) in reset () ; - let body = Fixed.{meta; pattern= stmt} in + let body = Fixed.{body with pattern= Pattern.Block [body]} in let pattern = Fixed.Pattern.For {loopvar; lower; upper; body} in - Fixed.{meta; pattern} + Fixed.{body with pattern} let rec for_each bodyfn iteratee smeta = let len (e : Expr.Typed.t) = diff --git a/src/middle/Stmt.mli b/src/middle/Stmt.mli index f6a2527d0c..c3a4ab2449 100644 --- a/src/middle/Stmt.mli +++ b/src/middle/Stmt.mli @@ -125,34 +125,37 @@ module Helpers : sig val mkfor : Expr.Typed.t - -> (Expr.Typed.t -> Located.t) + -> (Expr.Typed.t -> (Expr.Typed.Meta.t, 'a) Fixed.t) -> Expr.Typed.t -> Location_span.t - -> Located.t + -> (Expr.Typed.Meta.t, 'a) Fixed.t val for_each : - (Expr.Typed.t -> Located.t) -> Expr.Typed.t -> Location_span.t -> Located.t + (Expr.Typed.t -> (Expr.Typed.Meta.t, 'a) Fixed.t) + -> Expr.Typed.t + -> Location_span.t + -> (Expr.Typed.Meta.t, 'a) Fixed.t val for_scalar : Expr.Typed.t SizedType.t - -> (Expr.Typed.t -> Located.t) + -> (Expr.Typed.t -> (Expr.Typed.Meta.t, 'a) Fixed.t) -> Expr.Typed.t -> Location_span.t - -> Located.t + -> (Expr.Typed.Meta.t, 'a) Fixed.t val for_scalar_inv : Expr.Typed.t SizedType.t - -> (Expr.Typed.t -> Located.t) + -> (Expr.Typed.t -> (Expr.Typed.Meta.t, 'a) Fixed.t) -> Expr.Typed.t -> Location_span.t - -> Located.t + -> (Expr.Typed.Meta.t, 'a) Fixed.t val for_eigen : Expr.Typed.t SizedType.t - -> (Expr.Typed.t -> Located.t) + -> (Expr.Typed.t -> (Expr.Typed.Meta.t, 'a) Fixed.t) -> Expr.Typed.t -> Location_span.t - -> Located.t + -> (Expr.Typed.Meta.t, 'a) Fixed.t val assign_indexed : UnsizedType.t diff --git a/src/middle/UnsizedType.ml b/src/middle/UnsizedType.ml index 48eb0229a9..de415d4a82 100644 --- a/src/middle/UnsizedType.ml +++ b/src/middle/UnsizedType.ml @@ -8,9 +8,11 @@ type t = | URowVector | UMatrix | UArray of t - | UFun of (autodifftype * t) list * returntype + | UFun of (autodifftype * t) list * returntype * functiontype | UMathLibraryFunction +and functiontype = Function | Closure + and autodifftype = DataOnly | AutoDiffable and returntype = Void | ReturnType of t [@@deriving compare, hash, sexp] @@ -36,7 +38,7 @@ let rec pp ppf = function let ty, depth = unsized_array_depth ut in let commas = String.make depth ',' in Fmt.pf ppf "%a[%s]" pp ty commas - | UFun (argtypes, rt) -> + | UFun (argtypes, rt, _) -> Fmt.pf ppf {|@[(%a) => %a@]|} Fmt.(list pp_fun_arg ~sep:comma) argtypes pp_returntype rt @@ -61,14 +63,17 @@ let check_of_same_type_mod_conv name t1 t2 = else match (t1, t2) with | UReal, UInt -> true - | UFun (l1, rt1), UFun (l2, rt2) -> + | UFun (l1, rt1, _), UFun (l2, rt2, _) -> ( rt1 = rt2 - && List.for_all - ~f:(fun x -> x = true) - (List.map2_exn - ~f:(fun (at1, ut1) (at2, ut2) -> - ut1 = ut2 && autodifftype_can_convert at2 at1 ) - l1 l2) + && + match + List.for_all2 + ~f:(fun (at1, ut1) (at2, ut2) -> + ut1 = ut2 && autodifftype_can_convert at2 at1 ) + l1 l2 + with + | List.Or_unequal_lengths.Ok ok -> ok + | Unequal_lengths -> false ) | _ -> t1 = t2 let rec check_of_same_type_mod_array_conv name t1 t2 = diff --git a/src/stan_math_backend/Expression_gen.ml b/src/stan_math_backend/Expression_gen.ml index b1123ec70b..c52838d72f 100644 --- a/src/stan_math_backend/Expression_gen.ml +++ b/src/stan_math_backend/Expression_gen.ml @@ -48,6 +48,12 @@ let rec local_scalar ut ad = | _, UnsizedType.DataOnly | UInt, AutoDiffable -> stantype_prim_str ut | _, AutoDiffable -> "local_scalar_t__" +let rec captured_scalar ut ad = + match (ut, ad) with + | UnsizedType.UArray t, _ -> captured_scalar t ad + | _, UnsizedType.DataOnly | UInt, AutoDiffable -> stantype_prim_str ut + | _, AutoDiffable -> "captured_t__" + let minus_one e = { e with Expr.Fixed.pattern= @@ -94,7 +100,7 @@ let%expect_test "promote_unsized" = let rec pp_unsizedtype_custom_scalar ppf (scalar, ut) = match ut with - | UnsizedType.UInt | UReal -> string ppf scalar + | UnsizedType.UInt | UReal | UFun _ -> string ppf scalar | UArray t -> pf ppf "std::vector<%a>" pp_unsizedtype_custom_scalar (scalar, t) | UMatrix -> pf ppf "Eigen::Matrix<%s, -1, -1>" scalar @@ -106,6 +112,10 @@ let pp_unsizedtype_local ppf (adtype, ut) = let s = local_scalar ut adtype in pp_unsizedtype_custom_scalar ppf (s, ut) +let pp_unsizedtype_captured ppf (adtype, ut) = + let s = captured_scalar ut adtype in + pp_unsizedtype_custom_scalar ppf (s, ut) + let pp_expr_type ppf e = pp_unsizedtype_local ppf Expr.Typed.(adlevel_of e, type_of e) @@ -262,8 +272,7 @@ and read_data ut ppf es = match ut with | UnsizedType.UArray UInt -> "i" | UArray UReal -> "r" - | UInt | UReal | UVector | URowVector | UMatrix | UArray _ - |UFun (_, _) + | UInt | UReal | UVector | URowVector | UMatrix | UArray _ | UFun _ |UMathLibraryFunction -> raise_s [%message "Can't ReadData of " (ut : UnsizedType.t)] in @@ -274,8 +283,8 @@ and gen_fun_app ppf fname es = let default ppf es = let to_var s = Expr.{Fixed.pattern= Var s; meta= Typed.Meta.empty} in let convert_hof_vars = function - | {Expr.Fixed.pattern= Var name; meta= {Expr.Typed.Meta.type_= UFun _; _}} - as e -> + | { Expr.Fixed.pattern= Var name + ; meta= {Expr.Typed.Meta.type_= UFun (_, _, Function); _} } as e -> { e with pattern= FunApp (StanLib, name ^ functor_suffix_select fname, []) } @@ -299,10 +308,17 @@ and gen_fun_app ppf fname es = (fname, f :: x :: y :: dat :: datint :: msgs :: tl) | true, "integrate_1d", f :: a :: b :: theta :: x_r :: x_i :: tl -> (fname, f :: a :: b :: theta :: x_r :: x_i :: msgs :: tl) - | ( true + | ( _ , "integrate_ode_bdf" - , f :: y0 :: t0 :: ts :: theta :: x :: x_int :: tl ) - |( true + , f + :: ({meta= {type_= UArray UReal; _}; _} as y0) + :: t0 :: ts :: theta :: x_r :: x_i :: tl ) -> + ( "integrate_ode_bdf" + , f :: y0 :: t0 :: ts :: theta :: x_r :: x_i :: msgs :: tl ) + | _, "integrate_ode_bdf", f :: y0 :: t0 :: ts :: tl -> + let args, tols = List.split_n tl (List.length tl - 3) in + ("ode_bdf_tol", (f :: y0 :: t0 :: ts :: tols) @ (msgs :: args)) + | ( true , "integrate_ode_adams" , f :: y0 :: t0 :: ts :: theta :: x :: x_int :: tl ) |( true @@ -338,12 +354,19 @@ and pp_constrain_funapp constrain_or_un_str ppf = function and pp_user_defined_fun ppf (f, es) = let extra_args = suffix_args f @ ["pstream__"] in let sep = if List.is_empty es then "" else ", " in + let convert_hof_vars = function + | { Expr.Fixed.pattern= Var name + ; meta= {Expr.Typed.Meta.type_= UFun (_, _, Function); _} } as e -> + {e with pattern= FunApp (StanLib, name ^ functor_suffix_select "", [])} + | e -> e + in + let es = List.map ~f:convert_hof_vars es in pf ppf "@[%s(@,%a%s)@]" (demangle_propto_name true f) (list ~sep:comma pp_expr) es (sep ^ String.concat ~sep:", " extra_args) -and pp_compiler_internal_fn ut f ppf es = +and pp_compiler_internal_fn (meta : Expr.Typed.Meta.t) f ppf es = let pp_array_literal ppf es = let pp_add_method ppf () = pf ppf ")@,.add(" in pf ppf "stan::math::array_builder<%a>()@,.add(%a)@,.array()" @@ -355,17 +378,27 @@ and pp_compiler_internal_fn ut f ppf es = match Internal_fun.of_string_opt f with | Some FnMakeArray -> pp_array_literal ppf es | Some FnMakeRowVec -> ( - match ut with + match meta.type_ with | UnsizedType.URowVector -> pf ppf "stan::math::to_row_vector(@,%a)" pp_array_literal es | UMatrix -> pf ppf "stan::math::to_matrix(@,%a)" pp_array_literal es | _ -> raise_s [%message - "Unexpected type for row vector literal" (ut : UnsizedType.t)] ) + "Unexpected type for row vector literal" + (meta.type_ : UnsizedType.t)] ) + | Some FnMakeClosure -> ( + match es with + | {Expr.Fixed.pattern= Lit (Str, clname); _} :: captures -> + let scalar_t = + if meta.adlevel = AutoDiffable then "local_scalar_t__" else "double" + in + pf ppf "@[make_%s__<%s>(@,%a)@]" clname scalar_t + (list ~sep:comma pp_expr) captures + | _ -> raise_s [%message "Bad closure " (es : Expr.Typed.t list)] ) | Some FnConstrain -> pp_constrain_funapp "constrain" ppf es | Some FnUnconstrain -> pp_constrain_funapp "free" ppf es - | Some FnReadData -> read_data ut ppf es + | Some FnReadData -> read_data meta.type_ ppf es | Some FnReadParam -> ( match es with | {Expr.Fixed.pattern= Lit (Str, base_type); _} :: dims -> @@ -406,7 +439,7 @@ and pp_expr ppf Expr.Fixed.({pattern; meta} as e) = | Lit (_, s) -> pf ppf "%s" s | FunApp (StanLib, f, es) -> gen_fun_app ppf f es | FunApp (CompilerInternal, f, es) -> - pp_compiler_internal_fn meta.type_ (stan_namespace_qualify f) ppf es + pp_compiler_internal_fn meta (stan_namespace_qualify f) ppf es | FunApp (UserDefined, f, es) -> pp_user_defined_fun ppf (f, es) | EAnd (e1, e2) -> pp_logical_op ppf "&&" e1 e2 | EOr (e1, e2) -> pp_logical_op ppf "||" e1 e2 diff --git a/src/stan_math_backend/Stan_math_code_gen.ml b/src/stan_math_backend/Stan_math_code_gen.ml index 9b2e8e9dd2..f7ada2ad80 100644 --- a/src/stan_math_backend/Stan_math_code_gen.ml +++ b/src/stan_math_backend/Stan_math_code_gen.ml @@ -46,26 +46,24 @@ let rec contains_int = function | UArray t -> contains_int t | _ -> false -(** Detect if argument requires C++ template *) -let arg_needs_template = function - | UnsizedType.DataOnly, _, _ -> false - | _, _, t when contains_int t -> false - | _ -> true - (** Print template arguments for C++ functions that need templates @param args A pack of `Program.fun_arg_decl` containing functions to detect templates. @return A list of arguments with template parameter names added. *) -let maybe_templated_arg_types (args : Program.fun_arg_decl) = - List.mapi args ~f:(fun i a -> - match arg_needs_template a with - | true -> Some (sprintf "T%d__" i) - | false -> None ) +let maybe_templated_arg_types scalar (args : Program.fun_arg_decl) = + List.mapi args ~f:(fun i -> function + | _, _, UnsizedType.UFun _ -> + Some + ( if scalar then sprintf "typename T%d__::captured_scalar_t__" i + else sprintf "T%d__" i ) + | UnsizedType.DataOnly, _, _ -> None + | _, _, t when contains_int t -> None + | _ -> Some (sprintf "T%d__" i) ) let%expect_test "arg types templated correctly" = [(AutoDiffable, "xreal", UReal); (DataOnly, "yint", UInt)] - |> maybe_templated_arg_types |> List.filter_opt |> String.concat ~sep:"," - |> print_endline ; + |> maybe_templated_arg_types false + |> List.filter_opt |> String.concat ~sep:"," |> print_endline ; [%expect {| T0__ |}] (** Print the code for promoting stan real types @@ -88,7 +86,8 @@ let pp_promoted_scalar ppf args = in promote_args_chunked ppf List.( - chunks_of ~length:5 (filter_opt (maybe_templated_arg_types args))) + chunks_of ~length:5 + (filter_opt (maybe_templated_arg_types true args))) (** Pretty-prints a function's return-type, taking into account templated argument promotion.*) @@ -100,6 +99,19 @@ let pp_returntype ppf arg_types rt = | Some ut -> pf ppf "%a@," pp_unsizedtype_custom_scalar (scalar, ut) | None -> pf ppf "void@," +let pp_returntype_closure ppf (arg_types, (rt, promote)) = + let scalar = + if promote then + strf "typename boost::math::tools::promote_args::type" + pp_promoted_scalar arg_types + else strf "%a" pp_promoted_scalar arg_types + in + match rt with + | Some ut when contains_int ut -> + pf ppf "%a@," pp_unsizedtype_custom_scalar ("int", ut) + | Some ut -> pf ppf "%a@," pp_unsizedtype_custom_scalar (scalar, ut) + | None -> pf ppf "void@," + (** [pp_located_error ppf (pp_body_block, body_block, err_msg)] surrounds [body_block] with a C++ try-catch that will rethrow the error with the proper source location from the [body_block] (required to be a [stmt_loc Block] variant). @@ -138,7 +150,7 @@ let typename = ( ^ ) "typename " @param fdargs A sexp list of strings representing C++ types. *) let get_templates_and_args fdargs = - let argtypetemplates = maybe_templated_arg_types fdargs in + let argtypetemplates = maybe_templated_arg_types false fdargs in ( List.filter_opt argtypetemplates , List.map ~f:(fun a -> strf "%a" pp_arg a) @@ -157,7 +169,7 @@ let pp_template_decorator ppf = function Refactor this please - one idea might be to have different functions for printing user defined distributions vs rngs vs regular functions. *) -let pp_fun_def ppf Program.({fdrt; fdname; fdargs; fdbody; _}) +let pp_fun_def ppf Program.({fdrt; fdname; fdargs; fdbody; _}) is_closure funs_used_in_reduce_sum = let is_lp = is_user_lp fdname in let is_dist = is_user_dist fdname in @@ -200,6 +212,15 @@ let pp_fun_def ppf Program.({fdrt; fdname; fdargs; fdbody; _}) in pf ppf "%s(@[%a@]) " name (list ~sep:comma string) arg_strs in + let pp_ode_sig ppf name = + pp_template_decorator ppf templates ; + pp_returntype ppf fdargs fdrt ; + let arg_strs = + let two, rest = List.split_n args 2 in + two @ ["std::ostream* pstream__"] @ rest + in + pf ppf "%s(@[%a@]) " name (list ~sep:comma string) arg_strs + in let pp_sig_rs ppf name = if is_dist then pp_template_decorator ppf (List.tl_exn templates) else pp_template_decorator ppf templates ; @@ -216,26 +237,333 @@ let pp_fun_def ppf Program.({fdrt; fdname; fdargs; fdbody; _}) pp_sig ppf fdname ; match Stmt.Fixed.(fdbody.pattern) with | Skip -> pf ppf ";@ " - | _ -> ( + | _ -> pp_block ppf (pp_body, fdbody) ; - pf ppf "@,@,struct %s%s {@,%a const @,{@,return %a;@,}@,};@," fdname - functor_suffix pp_sig "operator()" pp_call_str - ( fdname - , List.map ~f:(fun (_, name, _) -> name) fdargs @ extra @ ["pstream__"] - ) ; - if String.Set.mem funs_used_in_reduce_sum fdname then - (* Produces the reduce_sum functors that has the pstream argument - as the third and not last argument *) - match fdargs with - | (_, slice, _) :: (_, start, _) :: (_, end_, _) :: rest -> - pf ppf "@,@,struct %s%s {@,%a const @,{@,return %a;@,}@,};@," - fdname reduce_sum_functor_suffix pp_sig_rs "operator()" - pp_call_str - ( (if is_dist then fdname ^ "" else fdname) - , slice :: (start ^ " + 1") :: (end_ ^ " + 1") - :: List.map ~f:(fun (_, name, _) -> name) rest + if not is_closure then ( + let pp_admethods ppf () = + pf ppf + "void set_zero_adjoints() const { }@ void \ + accumulate_adjoints(double*) const { }@ void save_varis(vari**) \ + const { }" + in + let pp_subclass ppf () = + pf ppf + "using ValueOf__ = %s%s;@ using DeepCopy__ = ValueOf__;@ using \ + captured_scalar_t__ = double;" + fdname functor_suffix + in + let pp_ode ppf () = + if not (is_dist || is_lp || is_rng || List.length args < 3) then + pf ppf "%a const @,{@,return %a;@,}@,%a@,%a" pp_ode_sig + "operator()" pp_call_str + ( fdname + , List.map ~f:(fun (_, name, _) -> name) fdargs @ extra @ ["pstream__"] ) - | _ -> raise_s [%message "impossible!"] ) + pp_admethods () pp_subclass () + in + pf ppf "@,@,struct %s%s {@,%s@,%a const @,{@,return %a;@,}@,%a};@," + fdname functor_suffix "const static int num_vars__ = 0;" pp_sig + "operator()" pp_call_str + ( fdname + , List.map ~f:(fun (_, name, _) -> name) fdargs + @ extra @ ["pstream__"] ) + pp_ode () ; + if String.Set.mem funs_used_in_reduce_sum fdname then + (* Produces the reduce_sum functors that has the pstream argument + as the third and not last argument *) + match fdargs with + | (_, slice, _) :: (_, start, _) :: (_, end_, _) :: rest -> + pf ppf "@,@,struct %s%s {@,%a const @,{@,return %a;@,}@,};@," + fdname reduce_sum_functor_suffix pp_sig_rs "operator()" + pp_call_str + ( (if is_dist then fdname ^ "" else fdname) + , slice :: (start ^ " + 1") :: (end_ ^ " + 1") + :: List.map ~f:(fun (_, name, _) -> name) rest + @ extra @ ["pstream__"] ) + | _ -> raise_s [%message "impossible!"] ) + +let function_from_closure clname Program.({cdrt; cdcaptures; cdargs; cdbody}) = + Program. + { fdname= clname ^ "_impl__" + ; fdrt= cdrt + ; fdargs= cdargs @ cdcaptures + ; fdbody= cdbody + ; fdloc= Location_span.empty } + +let pp_closure_defs ppf closures = + let numbered = List.mapi ~f:(fun i x -> (i, x)) in + let pp_type ppf = function + | i, (_, UnsizedType.UFun _) -> pf ppf "F%d__&" i + | _, (ad, ut) when UnsizedType.is_scalar_type ut -> + pp_unsizedtype_captured ppf (ad, ut) + | _, (ad, ut) -> pf ppf "%a&" pp_unsizedtype_captured (ad, ut) + in + let pp_valuetype ppf = function + | i, (_, UnsizedType.UFun _) -> pf ppf "typename F%d__::ValueOf__" i + | _, (UnsizedType.AutoDiffable, ut) -> + pp_unsizedtype_captured ppf (DataOnly, ut) + | _, (_, ut) when UnsizedType.is_scalar_type ut -> + pp_unsizedtype_captured ppf (DataOnly, ut) + | _, (DataOnly, ut) -> pf ppf "%a&" pp_unsizedtype_captured (DataOnly, ut) + in + let pp_copytype ppf = function + | i, (_, UnsizedType.UFun _) -> pf ppf "typename F%d__::DeepCopy__" i + | _, (UnsizedType.AutoDiffable, ut) -> + pp_unsizedtype_captured ppf (AutoDiffable, ut) + | _, (DataOnly, ut) when UnsizedType.is_scalar_type ut -> + pp_unsizedtype_captured ppf (DataOnly, ut) + | _, (DataOnly, ut) -> pf ppf "%a&" pp_unsizedtype_captured (DataOnly, ut) + in + let pp_args ppf captures = + (list ~sep:comma (fun ppf (i, (ad, id, ut)) -> + pf ppf "const %a %s" pp_type (i, (ad, ut)) id )) + ppf (numbered captures) + in + let pp_members ppf (pp_type, captures) = + (list ~sep:cut (fun ppf (i, (ad, id, ut)) -> + pf ppf "const %a %s;" pp_type (i, (ad, ut)) id )) + ppf (numbered captures) + in + let pp_ctor ppf (clname, captures) = + let pp_count ppf captures = + if List.is_empty captures then pf ppf "0" + else + (list + ~sep:(fun ppf () -> pf ppf "@ + ") + (fun ppf -> function + | _, _, UnsizedType.(UReal | UInt) -> pf ppf "1" + | _, n, UFun _ -> pf ppf "%s__.num_vars__" n + | _, n, _ -> pf ppf "num_elements(%s__)" n )) + ppf captures + in + pf ppf + "@[%s__(@[%a@])@ : \ + %a%snum_vars__(@[stan::is_var::value ?@ %a : 0@]) { }@]" + clname + (list ~sep:comma (fun ppf (i, (ad, id, ut)) -> + pf ppf "const %a %s__" pp_type (i, (ad, ut)) id )) + (numbered captures) + (list ~sep:comma (fun ppf (_, id, _) -> pf ppf "%s(%s__)" id id)) + captures + (if List.is_empty captures then "" else ", ") + pp_count + (List.filter + ~f:(fun (ad, _, _) -> ad = UnsizedType.AutoDiffable) + captures) + in + let pp_op ppf (rt, name, captures, args) = + let argtypetemplates, s_args = get_templates_and_args args in + let pp_ode ppf () = + if List.length s_args > 2 then ( + let two, tl = List.split_n s_args 2 in + pp_template_decorator ppf List.(map ~f:typename argtypetemplates) ; + pf ppf + "@[%aoperator()(@[%a, std::ostream* pstream__, %a@]) const \ + {@ @[return %a;@]@,}@]@," + pp_returntype_closure (args, rt) (list ~sep:comma text) two + (list ~sep:comma text) tl pp_call_str + ( name ^ "_impl__" + , List.map ~f:(fun (_, n, _) -> n) (args @ captures) @ ["pstream__"] + ) ) + in + pp_template_decorator ppf List.(map ~f:typename argtypetemplates) ; + pf ppf + "@[%aoperator()(@[%a%sstd::ostream* pstream__@]) const {@ \ + @[return %a;@]@,}@]@,%a" + pp_returntype_closure (args, rt) (list ~sep:comma text) s_args + (if List.is_empty args then "" else ", ") + pp_call_str + ( name ^ "_impl__" + , List.map ~f:(fun (_, n, _) -> n) (args @ captures) @ ["pstream__"] ) + pp_ode () + in + let pp_valueof ppf (rt, name, captures, args) = + let pp_init ppf (name, captures) = + let pp_items ppf captures = + if List.is_empty captures then pf ppf "" + else + pf ppf " : %a" + (list ~sep:comma (fun ppf (ad, n, ut) -> + if + ad = UnsizedType.AutoDiffable + && not (UnsizedType.is_fun_type ut) + then pf ppf "%s(value_of(init.%s))" n n + else pf ppf "%s(init.%s)" n n )) + captures + in + pf ppf "@[ValueOf_cl__(const %s__& init)%a { }@]@," name pp_items + captures ; + pf ppf "@[ValueOf_cl__(const DeepCopy_cl__& init)%a { }@]" pp_items + captures + in + pf ppf + "@[class ValueOf_cl__ {@ %a@ public:@ const static int num_vars__ \ + = 0;@ %a@ %a@ void set_zero_adjoints() const { }@ void \ + accumulate_adjoints(double*) const { }@ void save_varis(vari**) const \ + { }@ using captured_scalar_t__ = double;@ using ValueOf__ = \ + ValueOf_cl__;@ using DeepCopy__ = ValueOf_cl__;@ };@]@ using ValueOf__ \ + = ValueOf_cl__;" + pp_members (pp_valuetype, captures) pp_init (name, captures) pp_op + ((rt, false), name, captures, args) + in + let to_var adlevel type_ id = + Expr.{Fixed.pattern= Var id; meta= {Typed.Meta.empty with type_; adlevel}} + in + let rec for_each_scalar bodyfn (iteratee : Expr.Typed.t) = + let bodyfn' = + match iteratee.meta.type_ with + | UArray _ -> for_each_scalar bodyfn + | _ -> bodyfn + in + Stmt.Helpers.for_each bodyfn' iteratee iteratee.meta.loc + in + let ad_filter = function + | UnsizedType.DataOnly, _, _ -> None + | AutoDiffable, id, type_ -> Some (id, type_) + in + let pp_zeroadjoints ppf captures = + let pp_loop ppf (id, type_) = + let f v = + Stmt.Helpers.internal_nrfunapp FnZeroAdjoint [v] + Stmt.Numbered.Meta.empty + in + if UnsizedType.is_fun_type type_ then pf ppf "%s.set_zero_adjoints();" id + else pp_statement ppf (for_each_scalar f (to_var AutoDiffable type_ id)) + in + pf ppf + "@[void set_zero_adjoints() const {@ if \ + (stan::is_var::value) {@ %a@ }@ }@]" + (list ~sep:cut pp_loop) + (List.filter_map captures ~f:ad_filter) + in + let swrap pattern = Stmt.{Fixed.pattern; meta= Numbered.Meta.empty} in + let pos = Expr.{Fixed.pattern= Var "pos__"; meta= Typed.Meta.empty} in + let indexed v i = + Expr.{Fixed.pattern= Indexed (v, [Single i]); meta= Typed.Meta.empty} + in + let pp_accumulateadjoints ppf captures = + let pp_loop ppf (id, ut) = + let adj v = + Expr.Helpers.internal_funapp FnGetAdjoint [v] Expr.Typed.Meta.empty + in + let go v = + SList + [ Assignment + ( ("ptr", UReal, [Single pos]) + , Expr.Helpers.binop + (indexed (to_var DataOnly (UArray UReal) "ptr") pos) + Plus (adj v) ) + |> swrap + ; Assignment (("pos__", UInt, []), Expr.Helpers.(binop pos Plus one)) + |> swrap ] + |> swrap + in + if UnsizedType.is_fun_type ut then + pf ppf + "%s.accumulate_adjoints(ptr + (pos__ - 1));@,pos__ += %s.num_vars__;" + id id + else pp_statement ppf (for_each_scalar go (to_var AutoDiffable ut id)) + in + pf ppf + "@[void accumulate_adjoints(double* ptr) const {@ if \ + (stan::is_var::value) {@ size_t pos__ = 1;@ %a@ }@ }@]" + (list ~sep:cut pp_loop) + (List.filter_map captures ~f:ad_filter) + in + let pp_savevaris ppf captures = + let pp_loop ppf (id, ut) = + let vari v = + Expr.Helpers.internal_funapp FnGetVariPtr [v] Expr.Typed.Meta.empty + in + let go v = + SList + [ Assignment (("ptr", UInt, [Single pos]), vari v) |> swrap + ; Assignment (("pos__", UInt, []), Expr.Helpers.(binop pos Plus one)) + |> swrap ] + |> swrap + in + if UnsizedType.is_fun_type ut then + pf ppf "%s.save_varis(ptr + (pos__ - 1));@,pos__ += %s.num_vars__;" id + id + else pp_statement ppf (for_each_scalar go (to_var AutoDiffable ut id)) + in + pf ppf + "@[void save_varis(vari** ptr) const {@ if \ + (stan::is_var::value) {@ size_t pos__ = 1;@ %a@ }@ }@]" + (list ~sep:cut pp_loop) + (List.filter_map captures ~f:ad_filter) + in + let pp_deepcopy ppf (rt, id, captures, args) = + let pp_init ppf (id, captures) = + let pp_items ppf captures = + (list ~sep:comma (fun ppf (ad, n, ut) -> + if + ad = UnsizedType.AutoDiffable + && not (UnsizedType.is_fun_type ut) + then pf ppf "%s(deep_copy_vars(init.%s))" n n + else pf ppf "%s(init.%s)" n n )) + ppf captures + in + pf ppf + "@[DeepCopy_cl__(const %s__& init) : %a%s \ + num_vars__(init.num_vars__) { }@]" + id pp_items captures + (if List.is_empty captures then "" else ", ") + in + pf ppf + "@[class DeepCopy_cl__ {@ %a@ public:@ %a@ %a@ %a@ const int \ + num_vars__;@ using captured_scalar_t__ = captured_t__;@ %a@ using \ + DeepCopy__ = DeepCopy_cl__;@ };@]@ using DeepCopy__ = DeepCopy_cl__;@ \ + using ValueOf__ = typename DeepCopy__::ValueOf__;" + pp_members (pp_copytype, captures) pp_init (id, captures) pp_op + ((rt, true), id, captures, args) + pp_accumulateadjoints captures pp_valueof (rt, id, captures, args) + in + let pp_admethods ppf captures = + pp_zeroadjoints ppf captures ; + cut ppf () ; + pp_accumulateadjoints ppf captures ; + cut ppf () ; + pp_savevaris ppf captures + in + let f ~key ~data:Program.({cdrt; cdargs; cdcaptures; cdbody}) = + cut ppf () ; + pp_fun_def ppf + Program. + { fdname= key ^ "_impl__" + ; fdrt= cdrt + ; fdargs= cdargs @ cdcaptures + ; fdbody= {cdbody with Stmt.Fixed.pattern= Skip} + ; fdloc= Location_span.empty } + true String.Set.empty ; + pf ppf + "@,%a@[class %s__ {@,%a@ public:@ %s@ const int num_vars__;@ %a@ \ + %a@ %a@ %a@]@,};@,%a auto make_%s__(%a) {@,return %s__<%a>(%a);@,}" + pp_template_decorator + ( "typename captured_t__" + :: List.filter_mapi cdcaptures ~f:(fun i (_, _, ut) -> + if UnsizedType.is_fun_type ut then Some (strf "typename F%d__" i) + else None ) ) + key pp_members (pp_type, cdcaptures) + "using captured_scalar_t__ = captured_t__;" pp_ctor (key, cdcaptures) + pp_op + ((cdrt, true), key, cdcaptures, cdargs) + pp_deepcopy + (cdrt, key, cdcaptures, cdargs) + pp_admethods cdcaptures pp_template_decorator + ( "typename captured_t__" + :: List.filter_mapi cdcaptures ~f:(fun i (_, _, ut) -> + if UnsizedType.is_fun_type ut then Some (strf "typename F%d__" i) + else None ) ) + key pp_args cdcaptures key (list ~sep:comma string) + ( "captured_t__" + :: List.filter_mapi cdcaptures ~f:(fun i (_, _, ut) -> + if UnsizedType.is_fun_type ut then Some (strf "F%d__" i) else None + ) ) + (list ~sep:comma string) + (List.map cdcaptures ~f:(fun (_, id, _) -> id)) + in + String.Map.iteri ~f closures let version = "// Code generated by %%NAME%% %%VERSION%%" let includes = "#include " @@ -329,6 +657,7 @@ let pp_ctor ppf p = let rec top_level_decls Stmt.Fixed.({pattern; _}) = match pattern with + | Decl {decl_type= Unsized (UFun _); _} -> [None] | Decl d -> [Some (d.decl_id, Type.to_unsized d.decl_type, UnsizedType.DataOnly)] | SList stmts -> @@ -689,6 +1018,13 @@ using namespace stan::math; |} *) let custom_functions = {| +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -787,18 +1123,25 @@ let pp_prog ppf (p : Program.Typed.t) = (* First, do some transformations on the MIR itself before we begin printing it.*) let p, s = Locations.prepare_prog p in let pp_fun_def_with_rs_list ppf fblock = - pp_fun_def ppf fblock (fun_used_in_reduce_sum p) + pp_fun_def ppf fblock false (fun_used_in_reduce_sum p) in let reduce_sum_struct_decl = String.Set.map ~f:(fun x -> "struct " ^ x ^ reduce_sum_functor_suffix ^ ";") (fun_used_in_reduce_sum p) in - pf ppf "@[@ %s@ %s@ namespace %s {@ %s@ %s@ %a@ %s@ %a@ %a@ }@ @]" version - includes (namespace p) custom_functions usings Locations.pp_globals s + pf ppf "@[@ %s@ %s@ namespace %s {@ %s@ %s@ %a@ %s@ %a@ %a@ %a@ %a@ }@ @]" + version includes (namespace p) custom_functions usings Locations.pp_globals + s (String.concat ~sep:"\n" (String.Set.elements reduce_sum_struct_decl)) + pp_closure_defs p.closures (list ~sep:cut pp_fun_def_with_rs_list) - p.functions_block pp_model p ; + p.functions_block + (list ~sep:cut (fun ppf fd -> pp_fun_def ppf fd true String.Set.empty)) + (Map.fold ~init:[] + ~f:(fun ~key ~data accum -> function_from_closure key data :: accum) + p.closures) + pp_model p ; pf ppf "@,typedef %s_namespace::%s stan_model;@," p.prog_name p.prog_name ; pf ppf {| diff --git a/src/stan_math_backend/Statement_gen.ml b/src/stan_math_backend/Statement_gen.ml index e90fe7fe9d..f03b00ff7d 100644 --- a/src/stan_math_backend/Statement_gen.ml +++ b/src/stan_math_backend/Statement_gen.ml @@ -103,6 +103,8 @@ let rec pp_statement (ppf : Format.formatter) | Block _ | SList _ | Decl _ | Skip | Break | Continue -> () | _ -> Locations.pp_smeta ppf meta ) ; match pattern with + | Assignment ((vident, UFun _, []), rhs) -> + pf ppf "@[auto %s = %a;@]" vident pp_expr rhs | Assignment ((vident, _, []), ({meta= Expr.Typed.Meta.({type_= UInt; _}); _} as rhs)) |Assignment ((vident, _, []), ({meta= {type_= UReal; _}; _} as rhs)) -> @@ -203,6 +205,7 @@ let rec pp_statement (ppf : Format.formatter) pp_for_loop ppf (loopvar, lower, upper, pp_statement, body) | Block ls -> pp_block ppf (pp_stmt_list, ls) | SList ls -> pp_stmt_list ppf ls + | Decl {decl_adtype= _; decl_id= _; decl_type= Unsized (UFun _)} -> () | Decl {decl_adtype; decl_id; decl_type} -> pp_possibly_sized_decl ppf (decl_id, decl_type, decl_adtype) diff --git a/src/stan_math_backend/Transform_Mir.ml b/src/stan_math_backend/Transform_Mir.ml index 23f767b9e9..f9742bf455 100644 --- a/src/stan_math_backend/Transform_Mir.ml +++ b/src/stan_math_backend/Transform_Mir.ml @@ -631,10 +631,22 @@ let trans_prog (p : Program.Typed.t) = ; meta= Expr.Typed.Meta.empty } ) ; meta= Location_span.empty } ] ) in + let recreate_closures = + (* Closures are not default-constructible so they cannot be + private class members like all other transformed data. + The workaround is to recreate them at the start of each block. *) + List.filter_map p.prepare_data ~f:(function + | Stmt.Fixed.({pattern= Decl {decl_type= Unsized (UFun _); _}; _}) as d + -> + Some d + | Stmt.Fixed.({pattern= Assignment ((_, UFun _, _), _); _}) as d -> + Some d + | _ -> None ) + in let p = { p with functions_block - ; log_prob + ; log_prob= recreate_closures @ log_prob ; prog_name= escape_name p.prog_name ; prepare_data= init_pos @@ -646,7 +658,7 @@ let trans_prog (p : Program.Typed.t) = @ ( add_validate_dims p.output_vars p.transform_inits |> add_reads constrained_params data_read ) @ List.map ~f:gen_write free_params - ; generate_quantities } + ; generate_quantities= recreate_closures @ generate_quantities } in Program.( p diff --git a/test/integration/bad/closures/closure_lp.stan b/test/integration/bad/closures/closure_lp.stan new file mode 100644 index 0000000000..6b97d05d98 --- /dev/null +++ b/test/integration/bad/closures/closure_lp.stan @@ -0,0 +1,10 @@ +parameters { + real y; +} +model { + functions + real foo_lp(real x) { + return x + 1; + } + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/closure_lpdf b/test/integration/bad/closures/closure_lpdf new file mode 100644 index 0000000000..36465326e4 --- /dev/null +++ b/test/integration/bad/closures/closure_lpdf @@ -0,0 +1,10 @@ +parameters { + real y; +} +model { + functions + real foo_lpdf(real x) { + return x + 1; + } + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/closure_lpmf b/test/integration/bad/closures/closure_lpmf new file mode 100644 index 0000000000..71068c69f6 --- /dev/null +++ b/test/integration/bad/closures/closure_lpmf @@ -0,0 +1,10 @@ +parameters { + real y; +} +model { + functions + real foo_lpmf(int x) { + return x + 1; + } + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/closure_rng.stan b/test/integration/bad/closures/closure_rng.stan new file mode 100644 index 0000000000..aa21b729bf --- /dev/null +++ b/test/integration/bad/closures/closure_rng.stan @@ -0,0 +1,10 @@ +parameters { + real y; +} +model { + functions + real foo_rng(real x) { + return x + 1; + } + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/dune b/test/integration/bad/closures/dune new file mode 100644 index 0000000000..856a7fccef --- /dev/null +++ b/test/integration/bad/closures/dune @@ -0,0 +1 @@ +(include ../dune) diff --git a/test/integration/bad/closures/forward-declaration.stan b/test/integration/bad/closures/forward-declaration.stan new file mode 100644 index 0000000000..3b97e4c7b7 --- /dev/null +++ b/test/integration/bad/closures/forward-declaration.stan @@ -0,0 +1,12 @@ +parameters { + real y; +} +model { + functions + real foo(real x); + functions + real foo(real x) { + return x + 1; + } + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/mutate1.stan b/test/integration/bad/closures/mutate1.stan new file mode 100644 index 0000000000..89173a0048 --- /dev/null +++ b/test/integration/bad/closures/mutate1.stan @@ -0,0 +1,12 @@ +parameters { + real y; +} +model { + real x = 0.0; + functions + real foo(real z) { + x = 2; + return x + z; + } + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/mutate2.stan b/test/integration/bad/closures/mutate2.stan new file mode 100644 index 0000000000..22b1dbe0f8 --- /dev/null +++ b/test/integration/bad/closures/mutate2.stan @@ -0,0 +1,12 @@ +parameters { + real y; +} +model { + real x = 0.0; + functions + real foo(real z) { + return x + z; + } + x = 2; + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/returntype.stan b/test/integration/bad/closures/returntype.stan new file mode 100644 index 0000000000..4a2089434b --- /dev/null +++ b/test/integration/bad/closures/returntype.stan @@ -0,0 +1,8 @@ +functions { + real foo(real x) { + return 2*x; + } + real(real) bar(real x) { + return foo; + } +} \ No newline at end of file diff --git a/test/integration/bad/closures/shadowing.stan b/test/integration/bad/closures/shadowing.stan new file mode 100644 index 0000000000..8537bc99e5 --- /dev/null +++ b/test/integration/bad/closures/shadowing.stan @@ -0,0 +1,19 @@ +functions { + real foo(real x); + real bar(real y) { + functions + real foo(real z) { + return z - 1; + } + return foo(y); + } + real foo(real x) { + return x + 1; + } +} +parameters { + real y; +} +model { + y ~ normal(0,1); +} \ No newline at end of file diff --git a/test/integration/bad/closures/stanc.expected b/test/integration/bad/closures/stanc.expected new file mode 100644 index 0000000000..df56181bf0 --- /dev/null +++ b/test/integration/bad/closures/stanc.expected @@ -0,0 +1,98 @@ + $ ../../../../../install/default/bin/stanc closure_lp.stan + +Semantic error in 'closure_lp.stan', line 6, column 7 to column 13: + ------------------------------------------------- + 4: model { + 5: functions + 6: real foo_lp(real x) { + ^ + 7: return x + 1; + 8: } + ------------------------------------------------- + +Cannot declare a closure with suffix _lpdf, _lpmf, _log, _lcdf, _lccdf, _rng or _lp. + + $ ../../../../../install/default/bin/stanc closure_rng.stan + +Semantic error in 'closure_rng.stan', line 6, column 7 to column 14: + ------------------------------------------------- + 4: model { + 5: functions + 6: real foo_rng(real x) { + ^ + 7: return x + 1; + 8: } + ------------------------------------------------- + +Cannot declare a closure with suffix _lpdf, _lpmf, _log, _lcdf, _lccdf, _rng or _lp. + + $ ../../../../../install/default/bin/stanc forward-declaration.stan + +Semantic error in 'forward-declaration.stan', line 5, column 2 to line 6, column 19: + ------------------------------------------------- + 3: } + 4: model { + 5: functions + ^ + 6: real foo(real x); + 7: functions + ------------------------------------------------- + +Cannot declare a closure without definition. + + $ ../../../../../install/default/bin/stanc mutate1.stan + +Semantic error in 'mutate1.stan', line 8, column 6 to column 12: + ------------------------------------------------- + 6: functions + 7: real foo(real z) { + 8: x = 2; + ^ + 9: return x + z; + 10: } + ------------------------------------------------- + +Cannot assign to read-only identifier 'x'. + + $ ../../../../../install/default/bin/stanc mutate2.stan + +Semantic error in 'mutate2.stan', line 10, column 2 to column 8: + ------------------------------------------------- + 8: return x + z; + 9: } + 10: x = 2; + ^ + 11: y ~ normal(0,1); + 12: } + ------------------------------------------------- + +Cannot assign to read-only identifier 'x'. + + $ ../../../../../install/default/bin/stanc returntype.stan + +Semantic error in 'returntype.stan', line 5, column 4 to line 7, column 5: + ------------------------------------------------- + 3: return 2*x; + 4: } + 5: real(real) bar(real x) { + ^ + 6: return foo; + 7: } + ------------------------------------------------- + +Return type cannot be a function type. + + $ ../../../../../install/default/bin/stanc shadowing.stan + +Semantic error in 'shadowing.stan', line 5, column 9 to column 12: + ------------------------------------------------- + 3: real bar(real y) { + 4: functions + 5: real foo(real z) { + ^ + 6: return z - 1; + 7: } + ------------------------------------------------- + +Identifier 'foo' is already in use. + diff --git a/test/integration/bad/compound-assign/stanc.expected b/test/integration/bad/compound-assign/stanc.expected index 9d70e1f414..c932444943 100644 --- a/test/integration/bad/compound-assign/stanc.expected +++ b/test/integration/bad/compound-assign/stanc.expected @@ -75,19 +75,7 @@ Semantic error in 'plus_equals_bad_var_lhs.stan', line 3, column 4 to column 14: 5: } ------------------------------------------------- -Ill-typed arguments supplied to assignment operator +=: lhs has type (real) => real and rhs has type real. Available signatures: -(int, int) => void -(real, int) => void -(real, real) => void -(vector, int) => void -(vector, real) => void -(vector, vector) => void -(row_vector, int) => void -(row_vector, real) => void -(row_vector, row_vector) => void -(matrix, int) => void -(matrix, real) => void -(matrix, matrix) => void +Cannot assign to read-only identifier 'foo'. $ ../../../../../install/default/bin/stanc plus_equals_bad_var_lhs2.stan diff --git a/test/integration/bad/for_loops/stanc.expected b/test/integration/bad/for_loops/stanc.expected index 1db1ebbdf6..2cc2667178 100644 --- a/test/integration/bad/for_loops/stanc.expected +++ b/test/integration/bad/for_loops/stanc.expected @@ -10,7 +10,7 @@ Semantic error in 'assign_to_loop_var1.stan', line 5, column 18 to column 24: 7: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc assign_to_loop_var2.stan @@ -24,7 +24,7 @@ Semantic error in 'assign_to_loop_var2.stan', line 5, column 18 to column 24: 7: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc assign_to_loop_var3.stan @@ -38,7 +38,7 @@ Semantic error in 'assign_to_loop_var3.stan', line 6, column 6 to column 15: 8: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc assign_to_loop_var4.stan @@ -52,7 +52,7 @@ Semantic error in 'assign_to_loop_var4.stan', line 5, column 6 to column 12: 7: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc assign_to_loop_var5.stan @@ -66,7 +66,7 @@ Semantic error in 'assign_to_loop_var5.stan', line 5, column 6 to column 14: 7: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc assign_to_loop_var6.stan @@ -80,7 +80,7 @@ Semantic error in 'assign_to_loop_var6.stan', line 4, column 4 to column 12: 6: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc assign_to_loop_var7.stan @@ -108,7 +108,7 @@ Semantic error in 'for_statements_bad_indices0.stan', line 5, column 20 to colum 7: return 0; ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc for_statements_bad_indices1.stan @@ -136,7 +136,7 @@ Semantic error in 'for_statements_bad_indices2.stan', line 5, column 20 to colum 7: return 0; ------------------------------------------------- -Cannot assign to function argument or loop identifier 'v'. +Cannot assign to read-only identifier 'v'. $ ../../../../../install/default/bin/stanc for_statements_bad_indices3.stan diff --git a/test/integration/bad/ode/adams/stanc.expected b/test/integration/bad/ode/adams/stanc.expected index 183615c738..95692c7e80 100644 --- a/test/integration/bad/ode/adams/stanc.expected +++ b/test/integration/bad/ode/adams/stanc.expected @@ -283,6 +283,10 @@ Semantic error in 'bad_x_var_type_control.stan', line 26, column 10 to line 32, ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[], real, real, int. diff --git a/test/integration/bad/ode/stanc.expected b/test/integration/bad/ode/stanc.expected index 232aa4d20b..8c139ec08e 100644 --- a/test/integration/bad/ode/stanc.expected +++ b/test/integration/bad/ode/stanc.expected @@ -11,6 +11,10 @@ Semantic error in 'bad_bdf_control_function_return.stan', line 13, column 6 to l ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real, real[], real, real[], real[], real[], int[], int, int, int. @@ -44,6 +48,10 @@ Semantic error in 'bad_fun_type_bdf.stan', line 27, column 10 to line 33, column ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real[], real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[]. @@ -61,6 +69,10 @@ Semantic error in 'bad_fun_type_bdf_control.stan', line 27, column 10 to line 33 ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real[], real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[], real, real, int. @@ -128,6 +140,10 @@ Semantic error in 'bad_t_type_bdf.stan', line 26, column 10 to line 32, column 2 ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], matrix, real[], real[], real[], int[]. @@ -145,6 +161,10 @@ Semantic error in 'bad_t_type_bdf_control.stan', line 26, column 10 to line 32, ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], matrix, real[], real[], real[], int[], real, real, int. @@ -212,6 +232,10 @@ Semantic error in 'bad_theta_type_bdf.stan', line 27, column 10 to line 33, colu ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[,], real[], int[]. @@ -229,6 +253,10 @@ Semantic error in 'bad_theta_type_bdf_control.stan', line 27, column 10 to line ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[,], real[], int[], real, real, int. @@ -296,6 +324,10 @@ Semantic error in 'bad_ts_type_bdf.stan', line 26, column 10 to line 32, column ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[,], real[], real[], int[]. @@ -313,6 +345,10 @@ Semantic error in 'bad_ts_type_bdf_control.stan', line 26, column 10 to line 32, ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[,], real[], real[], int[], real, real, int. @@ -380,6 +416,10 @@ Semantic error in 'bad_x_int_type_bdf.stan', line 26, column 10 to line 32, colu ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[,]. @@ -397,6 +437,10 @@ Semantic error in 'bad_x_int_type_bdf_control.stan', line 26, column 10 to line ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[,], real, real, int. @@ -464,6 +508,10 @@ Semantic error in 'bad_x_type_bdf.stan', line 27, column 10 to line 33, column 2 ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], vector[], int[]. @@ -481,6 +529,10 @@ Semantic error in 'bad_x_type_bdf_control.stan', line 27, column 10 to line 33, ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], vector[], int[], real, real, int. @@ -548,6 +600,10 @@ Semantic error in 'bad_x_var_type_bdf.stan', line 26, column 10 to line 32, colu ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[]. @@ -565,6 +621,10 @@ Semantic error in 'bad_x_var_type_bdf_control.stan', line 26, column 10 to line ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[], real, real[], real[], real[], int[], real, real, int. @@ -632,6 +692,10 @@ Semantic error in 'bad_y_type_bdf.stan', line 26, column 10 to line 32, column 2 ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[,], real, real[], real[], real[], int[]. @@ -649,6 +713,10 @@ Semantic error in 'bad_y_type_bdf_control.stan', line 26, column 10 to line 32, ------------------------------------------------- Ill-typed arguments supplied to function 'integrate_ode_bdf'. Available signatures: +((real, vector) => vector, vector, real, real[]) => vector[] +((real, vector) => vector, vector, real, real[], data real, data real, data real) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[]) => vector[] +((real, vector, real[], data real[], data int[]) => vector, vector, real, real[], real[], data real[], data int[], data real, data real, data real) => vector[] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[]) => real[,] ((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], data real[], data int[], data real, data real, data real) => real[,] Instead supplied arguments of incompatible type: (real, real[], real[], real[], int[]) => real[], real[,], real, real[], real[], real[], int[], real, real, int. diff --git a/test/integration/bad/stanc.expected b/test/integration/bad/stanc.expected index 65157d459b..6ac093155b 100644 --- a/test/integration/bad/stanc.expected +++ b/test/integration/bad/stanc.expected @@ -1171,7 +1171,7 @@ Semantic error in 'functions-bad13.stan', line 3, column 4 to column 10: 5: } ------------------------------------------------- -Cannot assign to function argument or loop identifier 'x'. +Cannot assign to read-only identifier 'x'. $ ../../../../install/default/bin/stanc functions-bad14.stan diff --git a/test/integration/good/code-gen/cl.expected b/test/integration/good/code-gen/cl.expected index 73642f2a5f..b07a601aa3 100644 --- a/test/integration/good/code-gen/cl.expected +++ b/test/integration/good/code-gen/cl.expected @@ -4,6 +4,13 @@ #include namespace optimize_glm_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -285,6 +292,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class optimize_glm_model : public model_base_crtp { private: diff --git a/test/integration/good/code-gen/closure_1.stan b/test/integration/good/code-gen/closure_1.stan new file mode 100644 index 0000000000..fc0ad19fd4 --- /dev/null +++ b/test/integration/good/code-gen/closure_1.stan @@ -0,0 +1,28 @@ +data { + int N; + real x; + real y[N]; + vector[N] v; +} +transformed data { + functions + real foo(real z, row_vector r) { + return z + sum(r) + x + sum(y) + sum(v); + } +} +parameters { + real p; + real pa[N]; + vector[N] pv; +} +model { + functions + real bar(real z, row_vector r) { + return z + sum(r) + x + sum(y) + sum(v) + + p + sum(pa) + sum(pv); + } + target += foo(0.0,[1.0]); + target += foo(p,[1.0]); + target += bar(0.0,[1.0]); + target += bar(p,[1.0]); +} diff --git a/test/integration/good/code-gen/closure_2.stan b/test/integration/good/code-gen/closure_2.stan new file mode 100644 index 0000000000..b79f46b6fa --- /dev/null +++ b/test/integration/good/code-gen/closure_2.stan @@ -0,0 +1,30 @@ +transformed data { + functions + real foo(real x) { + return x; + } + functions + real bar(real y) { + return foo(y); + } +} +parameters { + real alpha; +} +transformed parameters { + functions + real baz(real y) { + return foo(y); + } + functions + real goo(real s) { + functions + real gar(real b) { + return b; + } + return gar(s); + } + real s1 = bar(1.0); + real s2 = baz(1.0); + real s3 = goo(1.0); +} diff --git a/test/integration/good/code-gen/closure_3.stan b/test/integration/good/code-gen/closure_3.stan new file mode 100644 index 0000000000..72a9bbef26 --- /dev/null +++ b/test/integration/good/code-gen/closure_3.stan @@ -0,0 +1,27 @@ +functions { + real foo(real(real) g, real x) { + functions + real g2(real y) { + return g(y) * x; + } + return g2(1.0); + } +} +transformed data { + real x = 2.0; + functions + real bar(real y) { + return x*y; + } + real z = foo(bar, 1.0); +} +parameters { + real p; +} +transformed parameters { + functions + real baz(real y) { + return p*y; + } + real w = foo(baz, 1.0); +} \ No newline at end of file diff --git a/test/integration/good/code-gen/cpp.expected b/test/integration/good/code-gen/cpp.expected index dca4ef8a53..84ff235744 100644 --- a/test/integration/good/code-gen/cpp.expected +++ b/test/integration/good/code-gen/cpp.expected @@ -1,9 +1,2672 @@ + $ ../../../../../install/default/bin/stanc --print-cpp closure_1.stan + +// Code generated by %%NAME%% %%VERSION%% +#include +namespace closure_1_model_namespace { + +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + +template +std::vector resize_to_match__(std::vector& dst, const std::vector& src) { + dst.resize(src.size()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.rows(), src.cols()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.size()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.size()); + return dst; +} +std::vector to_doubles__(std::initializer_list x) { + return x; +} + +std::vector to_vars__(std::initializer_list x) { + return x; +} + +inline void validate_positive_index(const char* var_name, const char* expr, + int val) { + if (val < 1) { + std::stringstream msg; + msg << "Found dimension size less than one in simplex declaration" + << "; variable=" << var_name << "; dimension size expression=" << expr + << "; expression value=" << val; + std::string msg_str(msg.str()); + throw std::invalid_argument(msg_str.c_str()); + } +} + +inline void validate_unit_vector_index(const char* var_name, const char* expr, + int val) { + if (val <= 1) { + std::stringstream msg; + if (val == 1) { + msg << "Found dimension size one in unit vector declaration." + << " One-dimensional unit vector is discrete" + << " but the target distribution must be continuous." + << " variable=" << var_name << "; dimension size expression=" << expr; + } else { + msg << "Found dimension size less than one in unit vector declaration" + << "; variable=" << var_name << "; dimension size expression=" << expr + << "; expression value=" << val; + } + std::string msg_str(msg.str()); + throw std::invalid_argument(msg_str.c_str()); + } +} + + +using std::istream; +using std::string; +using std::stringstream; +using std::vector; +using std::pow; +using stan::io::dump; +using stan::math::lgamma; +using stan::model::model_base_crtp; +using stan::model::rvalue; +using stan::model::cons_list; +using stan::model::index_uni; +using stan::model::index_max; +using stan::model::index_min; +using stan::model::index_min_max; +using stan::model::index_multi; +using stan::model::index_omni; +using stan::model::nil_index_list; +using namespace stan::math; + +static int current_statement__ = 0; +static const std::vector locations_array__ = {" (found before start of program)", + " (in 'closure_1.stan', line 14, column 4 to column 11)", + " (in 'closure_1.stan', line 15, column 4 to column 15)", + " (in 'closure_1.stan', line 16, column 4 to column 17)", + " (in 'closure_1.stan', line 8, column 4 to line 11, column 5)", + " (in 'closure_1.stan', line 19, column 4 to line 23, column 5)", + " (in 'closure_1.stan', line 24, column 4 to column 29)", + " (in 'closure_1.stan', line 25, column 4 to column 27)", + " (in 'closure_1.stan', line 26, column 4 to column 29)", + " (in 'closure_1.stan', line 27, column 4 to column 27)", + " (in 'closure_1.stan', line 2, column 4 to column 10)", + " (in 'closure_1.stan', line 3, column 4 to column 11)", + " (in 'closure_1.stan', line 4, column 4 to column 14)", + " (in 'closure_1.stan', line 5, column 4 to column 16)", + " (in 'closure_1.stan', line 10, column 8 to column 48)", + " (in 'closure_1.stan', line 9, column 35 to line 11, column 5)", + " (in 'closure_1.stan', line 21, column 8 to line 22, column 40)", + " (in 'closure_1.stan', line 20, column 35 to line 23, column 5)"}; + + + +template +stan::promote_args_t +closure00_impl__(const T0__& z, const Eigen::Matrix& r, + const Eigen::Matrix& v, const double& x, + const std::vector& y, std::ostream* pstream__) ; + +template +class closure00__ { + const Eigen::Matrix& v; + const double x; + const std::vector& y; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure00__(const Eigen::Matrix& v__, const double x__, + const std::vector& y__) : v(v__), x(x__), + y(y__), num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& z, const Eigen::Matrix& + r, std::ostream* pstream__) const { + return closure00_impl__(z, r, v, x, y, pstream__); + } + + class DeepCopy_cl__ { + const Eigen::Matrix& v; + const double x; + const std::vector& y; + public: + DeepCopy_cl__(const closure00__& init) : v(init.v), x(init.x), + y(init.y), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& z, const Eigen::Matrix& + r, std::ostream* pstream__) const { + return closure00_impl__(z, r, v, x, y, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const Eigen::Matrix& v; + const double x; + const std::vector& y; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure00__& init) : v(init.v), x(init.x), + y(init.y) { } + ValueOf_cl__(const DeepCopy_cl__& init) : v(init.v), x(init.x), + y(init.y) { } + template + stan::promote_args_t + operator()(const T0__& z, const Eigen::Matrix& + r, std::ostream* pstream__) const { + return closure00_impl__(z, r, v, x, y, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure00__(const Eigen::Matrix& v, +const double x, +const std::vector& y) { +return closure00__(v, +x, +y); +} +template +stan::promote_args_t +closure01_impl__(const T0__& z, const Eigen::Matrix& r, + const T2__& p, const std::vector& pa, + const Eigen::Matrix& pv, + const Eigen::Matrix& v, const double& x, + const std::vector& y, std::ostream* pstream__) ; + +template +class closure01__ { + const captured_t__ p; + const std::vector& pa; + const Eigen::Matrix& pv; + const Eigen::Matrix& v; + const double x; + const std::vector& y; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure01__(const captured_t__ p__, const std::vector& pa__, + const Eigen::Matrix& pv__, + const Eigen::Matrix& v__, const double x__, + const std::vector& y__) : p(p__), pa(pa__), pv(pv__), + v(v__), x(x__), + y(y__), num_vars__(stan::is_var::value ? 1 + + num_elements(pa__) + num_elements(pv__) : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& z, const Eigen::Matrix& + r, std::ostream* pstream__) const { + return closure01_impl__(z, r, p, pa, pv, v, x, y, pstream__); + } + + class DeepCopy_cl__ { + const captured_t__ p; + const std::vector pa; + const Eigen::Matrix pv; + const Eigen::Matrix& v; + const double x; + const std::vector& y; + public: + DeepCopy_cl__(const closure01__& init) : p(deep_copy_vars(init.p)), + pa(deep_copy_vars(init.pa)), pv(deep_copy_vars(init.pv)), v(init.v), + x(init.x), y(init.y), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& z, const Eigen::Matrix& + r, std::ostream* pstream__) const { + return closure01_impl__(z, r, p, pa, pv, v, x, y, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + FnGetAdjoint__(p)); + pos__ = (pos__ + 1); + for (int sym1__ = 1; sym1__ <= stan::math::size(pa); ++sym1__) { + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + + FnGetAdjoint__(pa[(sym1__ - 1)])); + pos__ = (pos__ + 1);} + for (int sym1__ = 1; sym1__ <= stan::math::size(pv); ++sym1__) { + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + + FnGetAdjoint__(pv[(sym1__ - 1)])); + pos__ = (pos__ + 1);} + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const double p; + const std::vector pa; + const Eigen::Matrix pv; + const Eigen::Matrix& v; + const double x; + const std::vector& y; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure01__& init) : p(value_of(init.p)), + pa(value_of(init.pa)), pv(value_of(init.pv)), v(init.v), x(init.x), + y(init.y) { } + ValueOf_cl__(const DeepCopy_cl__& init) : p(value_of(init.p)), + pa(value_of(init.pa)), pv(value_of(init.pv)), v(init.v), x(init.x), + y(init.y) { } + template + stan::promote_args_t + operator()(const T0__& z, const Eigen::Matrix& + r, std::ostream* pstream__) const { + return closure01_impl__(z, r, p, pa, pv, v, x, y, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + FnZeroAdjoint__(p); + for (int sym1__ = 1; sym1__ <= stan::math::size(pa); ++sym1__) { + FnZeroAdjoint__(pa[(sym1__ - 1)]);} + for (int sym1__ = 1; sym1__ <= stan::math::size(pv); ++sym1__) { + FnZeroAdjoint__(pv[(sym1__ - 1)]);} + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + FnGetAdjoint__(p)); + pos__ = (pos__ + 1); + for (int sym1__ = 1; sym1__ <= stan::math::size(pa); ++sym1__) { + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + + FnGetAdjoint__(pa[(sym1__ - 1)])); + pos__ = (pos__ + 1);} + for (int sym1__ = 1; sym1__ <= stan::math::size(pv); ++sym1__) { + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + + FnGetAdjoint__(pv[(sym1__ - 1)])); + pos__ = (pos__ + 1);} + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + ptr[(pos__ - 1)] = FnGetVariPtr__(p); + pos__ = (pos__ + 1); + for (int sym1__ = 1; sym1__ <= stan::math::size(pa); ++sym1__) { + ptr[(pos__ - 1)] = FnGetVariPtr__(pa[(sym1__ - 1)]); + pos__ = (pos__ + 1);} + for (int sym1__ = 1; sym1__ <= stan::math::size(pv); ++sym1__) { + ptr[(pos__ - 1)] = FnGetVariPtr__(pv[(sym1__ - 1)]); + pos__ = (pos__ + 1);} + } + } +}; +template + auto make_closure01__(const captured_t__ p, +const std::vector& pa, +const Eigen::Matrix& pv, +const Eigen::Matrix& v, +const double x, +const std::vector& y) { +return closure01__(p, +pa, +pv, +v, +x, +y); +} + +template +stan::promote_args_t +closure01_impl__(const T0__& z, const Eigen::Matrix& r, + const T2__& p, const std::vector& pa, + const Eigen::Matrix& pv, + const Eigen::Matrix& v, const double& x, + const std::vector& y, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 16; + return (((((((z + sum(r)) + x) + sum(y)) + sum(v)) + p) + sum(pa)) + + sum(pv)); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure00_impl__(const T0__& z, const Eigen::Matrix& r, + const Eigen::Matrix& v, const double& x, + const std::vector& y, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 14; + return ((((z + sum(r)) + x) + sum(y)) + sum(v)); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +class closure_1_model : public model_base_crtp { + + private: + int pos__; + int N; + double x; + std::vector y; + Eigen::Matrix v; + + public: + ~closure_1_model() { } + + std::string model_name() const { return "closure_1_model"; } + + closure_1_model(stan::io::var_context& context__, + unsigned int random_seed__ = 0, + std::ostream* pstream__ = nullptr) : model_base_crtp(0) { + typedef double local_scalar_t__; + boost::ecuyer1988 base_rng__ = + stan::services::util::create_rng(random_seed__, 0); + (void) base_rng__; // suppress unused var warning + static const char* function__ = "closure_1_model_namespace::closure_1_model"; + (void) function__; // suppress unused var warning + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + pos__ = std::numeric_limits::min(); + + pos__ = 1; + context__.validate_dims("data initialization","N","int", + context__.to_vec()); + N = std::numeric_limits::min(); + + current_statement__ = 10; + N = context__.vals_i("N")[(1 - 1)]; + context__.validate_dims("data initialization","x","double", + context__.to_vec()); + x = std::numeric_limits::quiet_NaN(); + + current_statement__ = 11; + x = context__.vals_r("x")[(1 - 1)]; + current_statement__ = 12; + validate_non_negative_index("y", "N", N); + context__.validate_dims("data initialization","y","double", + context__.to_vec(N)); + y = std::vector(N, std::numeric_limits::quiet_NaN()); + + current_statement__ = 12; + assign(y, nil_index_list(), context__.vals_r("y"), + "assigning variable y"); + current_statement__ = 13; + validate_non_negative_index("v", "N", N); + context__.validate_dims("data initialization","v","double", + context__.to_vec(N)); + v = Eigen::Matrix(N); + stan::math::fill(v, std::numeric_limits::quiet_NaN()); + + { + std::vector v_flat__; + current_statement__ = 13; + assign(v_flat__, nil_index_list(), context__.vals_r("v"), + "assigning variable v_flat__"); + current_statement__ = 13; + pos__ = 1; + current_statement__ = 13; + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + current_statement__ = 13; + assign(v, cons_list(index_uni(sym1__), nil_index_list()), + v_flat__[(pos__ - 1)], "assigning variable v"); + current_statement__ = 13; + pos__ = (pos__ + 1);} + } + + current_statement__ = 4; + auto foo = make_closure00__(v, x, y); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + num_params_r__ = 0U; + + try { + num_params_r__ += 1; + current_statement__ = 2; + validate_non_negative_index("pa", "N", N); + num_params_r__ += N; + current_statement__ = 3; + validate_non_negative_index("pv", "N", N); + num_params_r__ += N; + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } + template + inline T__ log_prob(std::vector& params_r__, + std::vector& params_i__, + std::ostream* pstream__ = 0) const { + typedef T__ local_scalar_t__; + T__ lp__(0.0); + stan::math::accumulator lp_accum__; + static const char* function__ = "closure_1_model_namespace::log_prob"; +(void) function__; // suppress unused var warning + + stan::io::reader in__(params_r__, params_i__); + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + + try { + + current_statement__ = 4; + auto foo = make_closure00__(v, x, y); + local_scalar_t__ p; + p = DUMMY_VAR__; + + current_statement__ = 1; + p = in__.scalar(); + current_statement__ = 2; + validate_non_negative_index("pa", "N", N); + std::vector pa; + pa = std::vector(N, DUMMY_VAR__); + + current_statement__ = 2; + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + current_statement__ = 2; + assign(pa, cons_list(index_uni(sym1__), nil_index_list()), + in__.scalar(), "assigning variable pa");} + current_statement__ = 3; + validate_non_negative_index("pv", "N", N); + Eigen::Matrix pv; + pv = Eigen::Matrix(N); + stan::math::fill(pv, DUMMY_VAR__); + + current_statement__ = 3; + pv = in__.vector(N); + { + + current_statement__ = 5; + auto bar = make_closure01__(p, pa, pv, v, x, y); + current_statement__ = 6; + lp_accum__.add( + foo(0.0, stan::math::to_row_vector( + stan::math::array_builder().add(1.0).array()), pstream__)); + current_statement__ = 7; + lp_accum__.add( + foo(p, stan::math::to_row_vector( + stan::math::array_builder().add(1.0).array()), pstream__)); + current_statement__ = 8; + lp_accum__.add( + bar(0.0, stan::math::to_row_vector( + stan::math::array_builder().add(1.0).array()), pstream__)); + current_statement__ = 9; + lp_accum__.add( + bar(p, stan::math::to_row_vector( + stan::math::array_builder().add(1.0).array()), pstream__)); + } + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + lp_accum__.add(lp__); + return lp_accum__.sum(); + } // log_prob() + + template + inline void write_array(RNG& base_rng__, std::vector& params_r__, + std::vector& params_i__, + std::vector& vars__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true, + std::ostream* pstream__ = 0) const { + using local_scalar_t__ = double; + vars__.resize(0); + stan::io::reader in__(params_r__, params_i__); + static const char* function__ = "closure_1_model_namespace::write_array"; +(void) function__; // suppress unused var warning + + (void) function__; // suppress unused var warning + + double lp__ = 0.0; + (void) lp__; // dummy to suppress unused var warning + stan::math::accumulator lp_accum__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + + try { + + current_statement__ = 4; + auto foo = make_closure00__(v, x, y); + double p; + p = std::numeric_limits::quiet_NaN(); + + current_statement__ = 1; + p = in__.scalar(); + current_statement__ = 2; + validate_non_negative_index("pa", "N", N); + std::vector pa; + pa = std::vector(N, std::numeric_limits::quiet_NaN()); + + current_statement__ = 2; + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + current_statement__ = 2; + assign(pa, cons_list(index_uni(sym1__), nil_index_list()), + in__.scalar(), "assigning variable pa");} + current_statement__ = 3; + validate_non_negative_index("pv", "N", N); + Eigen::Matrix pv; + pv = Eigen::Matrix(N); + stan::math::fill(pv, std::numeric_limits::quiet_NaN()); + + current_statement__ = 3; + pv = in__.vector(N); + vars__.push_back(p); + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + vars__.push_back(pa[(sym1__ - 1)]);} + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + vars__.push_back(pv[(sym1__ - 1)]);} + if (logical_negation((primitive_value(emit_transformed_parameters__) || + primitive_value(emit_generated_quantities__)))) { + return ; + } + if (logical_negation(emit_generated_quantities__)) { + return ; + } + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } // write_array() + + inline void transform_inits(const stan::io::var_context& context__, + std::vector& params_i__, + std::vector& vars__, + std::ostream* pstream__) const { + typedef double local_scalar_t__; + vars__.resize(0); + vars__.reserve(num_params_r__); + + try { + int pos__; + pos__ = std::numeric_limits::min(); + + pos__ = 1; + double p; + p = std::numeric_limits::quiet_NaN(); + + current_statement__ = 1; + p = context__.vals_r("p")[(1 - 1)]; + current_statement__ = 2; + validate_non_negative_index("pa", "N", N); + std::vector pa; + pa = std::vector(N, std::numeric_limits::quiet_NaN()); + + current_statement__ = 2; + assign(pa, nil_index_list(), context__.vals_r("pa"), + "assigning variable pa"); + current_statement__ = 3; + validate_non_negative_index("pv", "N", N); + Eigen::Matrix pv; + pv = Eigen::Matrix(N); + stan::math::fill(pv, std::numeric_limits::quiet_NaN()); + + { + std::vector pv_flat__; + current_statement__ = 3; + assign(pv_flat__, nil_index_list(), context__.vals_r("pv"), + "assigning variable pv_flat__"); + current_statement__ = 3; + pos__ = 1; + current_statement__ = 3; + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + current_statement__ = 3; + assign(pv, cons_list(index_uni(sym1__), nil_index_list()), + pv_flat__[(pos__ - 1)], "assigning variable pv"); + current_statement__ = 3; + pos__ = (pos__ + 1);} + } + vars__.push_back(p); + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + vars__.push_back(pa[(sym1__ - 1)]);} + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + vars__.push_back(pv[(sym1__ - 1)]);} + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } // transform_inits() + + inline void get_param_names(std::vector& names__) const { + + names__.resize(0); + names__.push_back("p"); + names__.push_back("pa"); + names__.push_back("pv"); + } // get_param_names() + + inline void get_dims(std::vector>& dimss__) const { + dimss__.resize(0); + std::vector dims__; + dimss__.push_back(dims__); + dims__.resize(0); + dims__.push_back(N); + dimss__.push_back(dims__); + dims__.resize(0); + dims__.push_back(N); + dimss__.push_back(dims__); + dims__.resize(0); + + } // get_dims() + + inline void constrained_param_names( + std::vector& param_names__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true) const { + + param_names__.push_back(std::string() + "p"); + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + { + param_names__.push_back(std::string() + "pa" + '.' + std::to_string(sym1__)); + }} + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + { + param_names__.push_back(std::string() + "pv" + '.' + std::to_string(sym1__)); + }} + if (emit_transformed_parameters__) { + + } + + if (emit_generated_quantities__) { + + } + + } // constrained_param_names() + + inline void unconstrained_param_names( + std::vector& param_names__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true) const { + + param_names__.push_back(std::string() + "p"); + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + { + param_names__.push_back(std::string() + "pa" + '.' + std::to_string(sym1__)); + }} + for (int sym1__ = 1; sym1__ <= N; ++sym1__) { + { + param_names__.push_back(std::string() + "pv" + '.' + std::to_string(sym1__)); + }} + if (emit_transformed_parameters__) { + + } + + if (emit_generated_quantities__) { + + } + + } // unconstrained_param_names() + + inline std::string get_constrained_sizedtypes() const { + stringstream s__; + s__ << "[{\"name\":\"p\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"pa\",\"type\":{\"name\":\"array\",\"length\":" << N << ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"pv\",\"type\":{\"name\":\"vector\",\"length\":" << N << "},\"block\":\"parameters\"}]"; + return s__.str(); + } // get_constrained_sizedtypes() + + inline std::string get_unconstrained_sizedtypes() const { + stringstream s__; + s__ << "[{\"name\":\"p\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"pa\",\"type\":{\"name\":\"array\",\"length\":" << N << ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"pv\",\"type\":{\"name\":\"vector\",\"length\":" << N << "},\"block\":\"parameters\"}]"; + return s__.str(); + } // get_unconstrained_sizedtypes() + + + // Begin method overload boilerplate + template + void write_array(RNG& base_rng__, + Eigen::Matrix& params_r, + Eigen::Matrix& vars, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true, + std::ostream* pstream = 0) const { + std::vector params_r_vec(params_r.size()); + for (int i = 0; i < params_r.size(); ++i) + params_r_vec[i] = params_r(i); + std::vector vars_vec; + std::vector params_i_vec; + write_array(base_rng__, params_r_vec, params_i_vec, vars_vec, + emit_transformed_parameters__, emit_generated_quantities__, pstream); + vars.resize(vars_vec.size()); + for (int i = 0; i < vars.size(); ++i) + vars(i) = vars_vec[i]; + } + + template + T_ log_prob(Eigen::Matrix& params_r, + std::ostream* pstream = 0) const { + std::vector vec_params_r; + vec_params_r.reserve(params_r.size()); + for (int i = 0; i < params_r.size(); ++i) + vec_params_r.push_back(params_r(i)); + std::vector vec_params_i; + return log_prob(vec_params_r, vec_params_i, pstream); + } + + void transform_inits(const stan::io::var_context& context, + Eigen::Matrix& params_r, + std::ostream* pstream__) const { + std::vector params_r_vec; + std::vector params_i_vec; + transform_inits(context, params_i_vec, params_r_vec, pstream__); + params_r.resize(params_r_vec.size()); + for (int i = 0; i < params_r.size(); ++i) + params_r(i) = params_r_vec[i]; + } + +}; +} +typedef closure_1_model_namespace::closure_1_model stan_model; + +#ifndef USING_R + +// Boilerplate +stan::model::model_base& new_model( + stan::io::var_context& data_context, + unsigned int seed, + std::ostream* msg_stream) { + stan_model* m = new stan_model(data_context, seed, msg_stream); + return *m; +} + +#endif + + + + $ ../../../../../install/default/bin/stanc --print-cpp closure_2.stan + +// Code generated by %%NAME%% %%VERSION%% +#include +namespace closure_2_model_namespace { + +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + +template +std::vector resize_to_match__(std::vector& dst, const std::vector& src) { + dst.resize(src.size()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.rows(), src.cols()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.size()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.size()); + return dst; +} +std::vector to_doubles__(std::initializer_list x) { + return x; +} + +std::vector to_vars__(std::initializer_list x) { + return x; +} + +inline void validate_positive_index(const char* var_name, const char* expr, + int val) { + if (val < 1) { + std::stringstream msg; + msg << "Found dimension size less than one in simplex declaration" + << "; variable=" << var_name << "; dimension size expression=" << expr + << "; expression value=" << val; + std::string msg_str(msg.str()); + throw std::invalid_argument(msg_str.c_str()); + } +} + +inline void validate_unit_vector_index(const char* var_name, const char* expr, + int val) { + if (val <= 1) { + std::stringstream msg; + if (val == 1) { + msg << "Found dimension size one in unit vector declaration." + << " One-dimensional unit vector is discrete" + << " but the target distribution must be continuous." + << " variable=" << var_name << "; dimension size expression=" << expr; + } else { + msg << "Found dimension size less than one in unit vector declaration" + << "; variable=" << var_name << "; dimension size expression=" << expr + << "; expression value=" << val; + } + std::string msg_str(msg.str()); + throw std::invalid_argument(msg_str.c_str()); + } +} + + +using std::istream; +using std::string; +using std::stringstream; +using std::vector; +using std::pow; +using stan::io::dump; +using stan::math::lgamma; +using stan::model::model_base_crtp; +using stan::model::rvalue; +using stan::model::cons_list; +using stan::model::index_uni; +using stan::model::index_max; +using stan::model::index_min; +using stan::model::index_min_max; +using stan::model::index_multi; +using stan::model::index_omni; +using stan::model::nil_index_list; +using namespace stan::math; + +static int current_statement__ = 0; +static const std::vector locations_array__ = {" (found before start of program)", + " (in 'closure_2.stan', line 12, column 2 to column 13)", + " (in 'closure_2.stan', line 2, column 2 to line 5, column 3)", + " (in 'closure_2.stan', line 6, column 2 to line 9, column 3)", + " (in 'closure_2.stan', line 15, column 2 to line 18, column 3)", + " (in 'closure_2.stan', line 19, column 2 to line 26, column 3)", + " (in 'closure_2.stan', line 27, column 2 to column 21)", + " (in 'closure_2.stan', line 28, column 2 to column 21)", + " (in 'closure_2.stan', line 29, column 2 to column 21)", + " (in 'closure_2.stan', line 4, column 4 to column 13)", + " (in 'closure_2.stan', line 3, column 19 to line 5, column 3)", + " (in 'closure_2.stan', line 8, column 4 to column 18)", + " (in 'closure_2.stan', line 7, column 19 to line 9, column 3)", + " (in 'closure_2.stan', line 17, column 4 to column 18)", + " (in 'closure_2.stan', line 16, column 19 to line 18, column 3)", + " (in 'closure_2.stan', line 21, column 4 to line 24, column 5)", + " (in 'closure_2.stan', line 25, column 4 to column 18)", + " (in 'closure_2.stan', line 20, column 19 to line 26, column 3)", + " (in 'closure_2.stan', line 23, column 6 to column 15)", + " (in 'closure_2.stan', line 22, column 21 to line 24, column 5)"}; + + + +template +stan::promote_args_t +closure00_impl__(const T0__& x, std::ostream* pstream__) ; + +template +class closure00__ { + + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure00__() : num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& x, std::ostream* pstream__) const { + return closure00_impl__(x, pstream__); + } + + class DeepCopy_cl__ { + + public: + DeepCopy_cl__(const closure00__& init) : num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& x, std::ostream* pstream__) const { + return closure00_impl__(x, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure00__& init) { } + ValueOf_cl__(const DeepCopy_cl__& init) { } + template + stan::promote_args_t + operator()(const T0__& x, std::ostream* pstream__) const { + return closure00_impl__(x, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure00__() { +return closure00__(); +} +template +stan::promote_args_t +closure01_impl__(const T0__& y, const T1__& foo, std::ostream* pstream__) ; + +template +class closure01__ { + const F0__& foo; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure01__(const F0__& foo__) + : foo(foo__), num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure01_impl__(y, foo, pstream__); + } + + class DeepCopy_cl__ { + const typename F0__::DeepCopy__ foo; + public: + DeepCopy_cl__(const closure01__& init) : foo(init.foo), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure01_impl__(y, foo, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const typename F0__::ValueOf__ foo; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure01__& init) : foo(init.foo) { } + ValueOf_cl__(const DeepCopy_cl__& init) : foo(init.foo) { } + template + stan::promote_args_t + operator()(const T0__& y, std::ostream* pstream__) const { + return closure01_impl__(y, foo, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure01__(const F0__& foo) { +return closure01__(foo); +} +template +stan::promote_args_t +closure02_impl__(const T0__& y, const T1__& foo, std::ostream* pstream__) ; + +template +class closure02__ { + const F0__& foo; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure02__(const F0__& foo__) + : foo(foo__), num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure02_impl__(y, foo, pstream__); + } + + class DeepCopy_cl__ { + const typename F0__::DeepCopy__ foo; + public: + DeepCopy_cl__(const closure02__& init) : foo(init.foo), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure02_impl__(y, foo, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const typename F0__::ValueOf__ foo; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure02__& init) : foo(init.foo) { } + ValueOf_cl__(const DeepCopy_cl__& init) : foo(init.foo) { } + template + stan::promote_args_t + operator()(const T0__& y, std::ostream* pstream__) const { + return closure02_impl__(y, foo, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure02__(const F0__& foo) { +return closure02__(foo); +} +template +stan::promote_args_t +closure03_impl__(const T0__& s, std::ostream* pstream__) ; + +template +class closure03__ { + + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure03__() : num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& s, std::ostream* pstream__) const { + return closure03_impl__(s, pstream__); + } + + class DeepCopy_cl__ { + + public: + DeepCopy_cl__(const closure03__& init) : num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& s, std::ostream* pstream__) const { + return closure03_impl__(s, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure03__& init) { } + ValueOf_cl__(const DeepCopy_cl__& init) { } + template + stan::promote_args_t + operator()(const T0__& s, std::ostream* pstream__) const { + return closure03_impl__(s, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure03__() { +return closure03__(); +} +template +stan::promote_args_t +closure04_impl__(const T0__& b, std::ostream* pstream__) ; + +template +class closure04__ { + + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure04__() : num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& b, std::ostream* pstream__) const { + return closure04_impl__(b, pstream__); + } + + class DeepCopy_cl__ { + + public: + DeepCopy_cl__(const closure04__& init) : num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& b, std::ostream* pstream__) const { + return closure04_impl__(b, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure04__& init) { } + ValueOf_cl__(const DeepCopy_cl__& init) { } + template + stan::promote_args_t + operator()(const T0__& b, std::ostream* pstream__) const { + return closure04_impl__(b, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure04__() { +return closure04__(); +} + +template +stan::promote_args_t +closure04_impl__(const T0__& b, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 18; + return b; + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure03_impl__(const T0__& s, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + + current_statement__ = 15; + auto gar = make_closure04__(); + current_statement__ = 16; + return gar(s, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure02_impl__(const T0__& y, const T1__& foo, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 13; + return foo(y, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure01_impl__(const T0__& y, const T1__& foo, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 11; + return foo(y, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure00_impl__(const T0__& x, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 9; + return x; + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +class closure_2_model : public model_base_crtp { + + private: + int pos__; + + public: + ~closure_2_model() { } + + std::string model_name() const { return "closure_2_model"; } + + closure_2_model(stan::io::var_context& context__, + unsigned int random_seed__ = 0, + std::ostream* pstream__ = nullptr) : model_base_crtp(0) { + typedef double local_scalar_t__; + boost::ecuyer1988 base_rng__ = + stan::services::util::create_rng(random_seed__, 0); + (void) base_rng__; // suppress unused var warning + static const char* function__ = "closure_2_model_namespace::closure_2_model"; + (void) function__; // suppress unused var warning + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + pos__ = std::numeric_limits::min(); + + pos__ = 1; + + current_statement__ = 2; + auto foo = make_closure00__(); + + current_statement__ = 3; + auto bar = make_closure01__(foo); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + num_params_r__ = 0U; + + try { + num_params_r__ += 1; + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } + template + inline T__ log_prob(std::vector& params_r__, + std::vector& params_i__, + std::ostream* pstream__ = 0) const { + typedef T__ local_scalar_t__; + T__ lp__(0.0); + stan::math::accumulator lp_accum__; + static const char* function__ = "closure_2_model_namespace::log_prob"; +(void) function__; // suppress unused var warning + + stan::io::reader in__(params_r__, params_i__); + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + + try { + + current_statement__ = 2; + auto foo = make_closure00__(); + + current_statement__ = 3; + auto bar = make_closure01__(foo); + local_scalar_t__ alpha; + alpha = DUMMY_VAR__; + + current_statement__ = 1; + alpha = in__.scalar(); + + current_statement__ = 4; + auto baz = make_closure02__(foo); + + current_statement__ = 5; + auto goo = make_closure03__(); + local_scalar_t__ s1; + s1 = DUMMY_VAR__; + + current_statement__ = 6; + s1 = bar(1.0, pstream__); + local_scalar_t__ s2; + s2 = DUMMY_VAR__; + + current_statement__ = 7; + s2 = baz(1.0, pstream__); + local_scalar_t__ s3; + s3 = DUMMY_VAR__; + + current_statement__ = 8; + s3 = goo(1.0, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + lp_accum__.add(lp__); + return lp_accum__.sum(); + } // log_prob() + + template + inline void write_array(RNG& base_rng__, std::vector& params_r__, + std::vector& params_i__, + std::vector& vars__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true, + std::ostream* pstream__ = 0) const { + using local_scalar_t__ = double; + vars__.resize(0); + stan::io::reader in__(params_r__, params_i__); + static const char* function__ = "closure_2_model_namespace::write_array"; +(void) function__; // suppress unused var warning + + (void) function__; // suppress unused var warning + + double lp__ = 0.0; + (void) lp__; // dummy to suppress unused var warning + stan::math::accumulator lp_accum__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + + try { + + current_statement__ = 2; + auto foo = make_closure00__(); + + current_statement__ = 3; + auto bar = make_closure01__(foo); + double alpha; + alpha = std::numeric_limits::quiet_NaN(); + + current_statement__ = 1; + alpha = in__.scalar(); + + + double s1; + s1 = std::numeric_limits::quiet_NaN(); + + double s2; + s2 = std::numeric_limits::quiet_NaN(); + + double s3; + s3 = std::numeric_limits::quiet_NaN(); + + vars__.push_back(alpha); + if (logical_negation((primitive_value(emit_transformed_parameters__) || + primitive_value(emit_generated_quantities__)))) { + return ; + } + current_statement__ = 4; + auto baz = make_closure02__(foo); + current_statement__ = 5; + auto goo = make_closure03__(); + current_statement__ = 6; + s1 = bar(1.0, pstream__); + current_statement__ = 7; + s2 = baz(1.0, pstream__); + current_statement__ = 8; + s3 = goo(1.0, pstream__); + vars__.push_back(s1); + vars__.push_back(s2); + vars__.push_back(s3); + if (logical_negation(emit_generated_quantities__)) { + return ; + } + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } // write_array() + + inline void transform_inits(const stan::io::var_context& context__, + std::vector& params_i__, + std::vector& vars__, + std::ostream* pstream__) const { + typedef double local_scalar_t__; + vars__.resize(0); + vars__.reserve(num_params_r__); + + try { + int pos__; + pos__ = std::numeric_limits::min(); + + pos__ = 1; + double alpha; + alpha = std::numeric_limits::quiet_NaN(); + + current_statement__ = 1; + alpha = context__.vals_r("alpha")[(1 - 1)]; + vars__.push_back(alpha); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } // transform_inits() + + inline void get_param_names(std::vector& names__) const { + + names__.resize(0); + names__.push_back("alpha"); + names__.push_back("s1"); + names__.push_back("s2"); + names__.push_back("s3"); + } // get_param_names() + + inline void get_dims(std::vector>& dimss__) const { + dimss__.resize(0); + std::vector dims__; + dimss__.push_back(dims__); + dims__.resize(0); + dimss__.push_back(dims__); + dims__.resize(0); + dimss__.push_back(dims__); + dims__.resize(0); + dimss__.push_back(dims__); + dims__.resize(0); + + } // get_dims() + + inline void constrained_param_names( + std::vector& param_names__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true) const { + + param_names__.push_back(std::string() + "alpha"); + if (emit_transformed_parameters__) { + param_names__.push_back(std::string() + "s1"); + param_names__.push_back(std::string() + "s2"); + param_names__.push_back(std::string() + "s3"); + } + + if (emit_generated_quantities__) { + + } + + } // constrained_param_names() + + inline void unconstrained_param_names( + std::vector& param_names__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true) const { + + param_names__.push_back(std::string() + "alpha"); + if (emit_transformed_parameters__) { + param_names__.push_back(std::string() + "s1"); + param_names__.push_back(std::string() + "s2"); + param_names__.push_back(std::string() + "s3"); + } + + if (emit_generated_quantities__) { + + } + + } // unconstrained_param_names() + + inline std::string get_constrained_sizedtypes() const { + stringstream s__; + s__ << "[{\"name\":\"alpha\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"s1\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"s2\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"s3\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"}]"; + return s__.str(); + } // get_constrained_sizedtypes() + + inline std::string get_unconstrained_sizedtypes() const { + stringstream s__; + s__ << "[{\"name\":\"alpha\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"s1\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"s2\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"s3\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"}]"; + return s__.str(); + } // get_unconstrained_sizedtypes() + + + // Begin method overload boilerplate + template + void write_array(RNG& base_rng__, + Eigen::Matrix& params_r, + Eigen::Matrix& vars, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true, + std::ostream* pstream = 0) const { + std::vector params_r_vec(params_r.size()); + for (int i = 0; i < params_r.size(); ++i) + params_r_vec[i] = params_r(i); + std::vector vars_vec; + std::vector params_i_vec; + write_array(base_rng__, params_r_vec, params_i_vec, vars_vec, + emit_transformed_parameters__, emit_generated_quantities__, pstream); + vars.resize(vars_vec.size()); + for (int i = 0; i < vars.size(); ++i) + vars(i) = vars_vec[i]; + } + + template + T_ log_prob(Eigen::Matrix& params_r, + std::ostream* pstream = 0) const { + std::vector vec_params_r; + vec_params_r.reserve(params_r.size()); + for (int i = 0; i < params_r.size(); ++i) + vec_params_r.push_back(params_r(i)); + std::vector vec_params_i; + return log_prob(vec_params_r, vec_params_i, pstream); + } + + void transform_inits(const stan::io::var_context& context, + Eigen::Matrix& params_r, + std::ostream* pstream__) const { + std::vector params_r_vec; + std::vector params_i_vec; + transform_inits(context, params_i_vec, params_r_vec, pstream__); + params_r.resize(params_r_vec.size()); + for (int i = 0; i < params_r.size(); ++i) + params_r(i) = params_r_vec[i]; + } + +}; +} +typedef closure_2_model_namespace::closure_2_model stan_model; + +#ifndef USING_R + +// Boilerplate +stan::model::model_base& new_model( + stan::io::var_context& data_context, + unsigned int seed, + std::ostream* msg_stream) { + stan_model* m = new stan_model(data_context, seed, msg_stream); + return *m; +} + +#endif + + + + $ ../../../../../install/default/bin/stanc --print-cpp closure_3.stan + +// Code generated by %%NAME%% %%VERSION%% +#include +namespace closure_3_model_namespace { + +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + +template +std::vector resize_to_match__(std::vector& dst, const std::vector& src) { + dst.resize(src.size()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.rows(), src.cols()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.size()); + return dst; +} + +template +Eigen::Matrix +resize_to_match__(Eigen::Matrix& dst, const Eigen::Matrix& src) { + dst.resize(src.size()); + return dst; +} +std::vector to_doubles__(std::initializer_list x) { + return x; +} + +std::vector to_vars__(std::initializer_list x) { + return x; +} + +inline void validate_positive_index(const char* var_name, const char* expr, + int val) { + if (val < 1) { + std::stringstream msg; + msg << "Found dimension size less than one in simplex declaration" + << "; variable=" << var_name << "; dimension size expression=" << expr + << "; expression value=" << val; + std::string msg_str(msg.str()); + throw std::invalid_argument(msg_str.c_str()); + } +} + +inline void validate_unit_vector_index(const char* var_name, const char* expr, + int val) { + if (val <= 1) { + std::stringstream msg; + if (val == 1) { + msg << "Found dimension size one in unit vector declaration." + << " One-dimensional unit vector is discrete" + << " but the target distribution must be continuous." + << " variable=" << var_name << "; dimension size expression=" << expr; + } else { + msg << "Found dimension size less than one in unit vector declaration" + << "; variable=" << var_name << "; dimension size expression=" << expr + << "; expression value=" << val; + } + std::string msg_str(msg.str()); + throw std::invalid_argument(msg_str.c_str()); + } +} + + +using std::istream; +using std::string; +using std::stringstream; +using std::vector; +using std::pow; +using stan::io::dump; +using stan::math::lgamma; +using stan::model::model_base_crtp; +using stan::model::rvalue; +using stan::model::cons_list; +using stan::model::index_uni; +using stan::model::index_max; +using stan::model::index_min; +using stan::model::index_min_max; +using stan::model::index_multi; +using stan::model::index_omni; +using stan::model::nil_index_list; +using namespace stan::math; + +static int current_statement__ = 0; +static const std::vector locations_array__ = {" (found before start of program)", + " (in 'closure_3.stan', line 19, column 4 to column 11)", + " (in 'closure_3.stan', line 12, column 4 to line 15, column 5)", + " (in 'closure_3.stan', line 22, column 4 to line 25, column 5)", + " (in 'closure_3.stan', line 26, column 4 to column 27)", + " (in 'closure_3.stan', line 11, column 4 to column 17)", + " (in 'closure_3.stan', line 16, column 4 to column 27)", + " (in 'closure_3.stan', line 3, column 8 to line 6, column 9)", + " (in 'closure_3.stan', line 7, column 8 to column 23)", + " (in 'closure_3.stan', line 2, column 35 to line 8, column 5)", + " (in 'closure_3.stan', line 5, column 12 to column 28)", + " (in 'closure_3.stan', line 4, column 24 to line 6, column 9)", + " (in 'closure_3.stan', line 14, column 8 to column 19)", + " (in 'closure_3.stan', line 13, column 21 to line 15, column 5)", + " (in 'closure_3.stan', line 24, column 8 to column 19)", + " (in 'closure_3.stan', line 23, column 21 to line 25, column 5)"}; + + + +template +stan::promote_args_t +closure00_impl__(const T0__& y, const T1__& g, const T2__& x, + std::ostream* pstream__) ; + +template +class closure00__ { + const F0__& g; + const captured_t__ x; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure00__(const F0__& g__, const captured_t__ x__) : g(g__), + x(x__), num_vars__(stan::is_var::value ? g__.num_vars__ + + 1 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure00_impl__(y, g, x, pstream__); + } + + class DeepCopy_cl__ { + const typename F0__::DeepCopy__ g; + const captured_t__ x; + public: + DeepCopy_cl__(const closure00__& init) : g(init.g), + x(deep_copy_vars(init.x)), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure00_impl__(y, g, x, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + g.accumulate_adjoints(ptr + (pos__ - 1)); + pos__ += g.num_vars__; + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + FnGetAdjoint__(x)); + pos__ = (pos__ + 1); + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const typename F0__::ValueOf__ g; + const double x; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure00__& init) : g(init.g), + x(value_of(init.x)) { } + ValueOf_cl__(const DeepCopy_cl__& init) : g(init.g), + x(value_of(init.x)) { } + template + stan::promote_args_t + operator()(const T0__& y, std::ostream* pstream__) const { + return closure00_impl__(y, g, x, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + g.set_zero_adjoints(); + FnZeroAdjoint__(x); + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + g.accumulate_adjoints(ptr + (pos__ - 1)); + pos__ += g.num_vars__; + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + FnGetAdjoint__(x)); + pos__ = (pos__ + 1); + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + g.save_varis(ptr + (pos__ - 1)); + pos__ += g.num_vars__; + ptr[(pos__ - 1)] = FnGetVariPtr__(x); + pos__ = (pos__ + 1); + } + } +}; +template + auto make_closure00__(const F0__& g, +const captured_t__ x) { +return closure00__(g, +x); +} +template +stan::promote_args_t +closure01_impl__(const T0__& y, const double& x, std::ostream* pstream__) ; + +template +class closure01__ { + const double x; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure01__(const double x__) + : x(x__), num_vars__(stan::is_var::value ? 0 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure01_impl__(y, x, pstream__); + } + + class DeepCopy_cl__ { + const double x; + public: + DeepCopy_cl__(const closure01__& init) : x(init.x), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure01_impl__(y, x, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const double x; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure01__& init) : x(init.x) { } + ValueOf_cl__(const DeepCopy_cl__& init) : x(init.x) { } + template + stan::promote_args_t + operator()(const T0__& y, std::ostream* pstream__) const { + return closure01_impl__(y, x, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + + } + } +}; +template + auto make_closure01__(const double x) { +return closure01__(x); +} +template +stan::promote_args_t +closure02_impl__(const T0__& y, const T1__& p, std::ostream* pstream__) ; + +template +class closure02__ { + const captured_t__ p; + public: + using captured_scalar_t__ = captured_t__; + const int num_vars__; + closure02__(const captured_t__ p__) + : p(p__), num_vars__(stan::is_var::value ? 1 : 0) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure02_impl__(y, p, pstream__); + } + + class DeepCopy_cl__ { + const captured_t__ p; + public: + DeepCopy_cl__(const closure02__& init) : p(deep_copy_vars(init.p)), num_vars__(init.num_vars__) { } + template + typename boost::math::tools::promote_args>::type + operator()(const T0__& y, std::ostream* pstream__) const { + return closure02_impl__(y, p, pstream__); + } + + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + FnGetAdjoint__(p)); + pos__ = (pos__ + 1); + } + } + const int num_vars__; + using captured_scalar_t__ = captured_t__; + class ValueOf_cl__ { + const double p; + public: + const static int num_vars__ = 0; + ValueOf_cl__(const closure02__& init) : p(value_of(init.p)) { } + ValueOf_cl__(const DeepCopy_cl__& init) : p(value_of(init.p)) { } + template + stan::promote_args_t + operator()(const T0__& y, std::ostream* pstream__) const { + return closure02_impl__(y, p, pstream__); + } + + void set_zero_adjoints() const { } + void accumulate_adjoints(double*) const { } + void save_varis(vari**) const { } + using captured_scalar_t__ = double; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = ValueOf_cl__; + }; + using ValueOf__ = ValueOf_cl__; + using DeepCopy__ = DeepCopy_cl__; + }; + using DeepCopy__ = DeepCopy_cl__; + using ValueOf__ = typename DeepCopy__::ValueOf__; + void set_zero_adjoints() const { + if (stan::is_var::value) { + FnZeroAdjoint__(p); + } + } + void accumulate_adjoints(double* ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + ptr[(pos__ - 1)] = (ptr[(pos__ - 1)] + FnGetAdjoint__(p)); + pos__ = (pos__ + 1); + } + } + void save_varis(vari** ptr) const { + if (stan::is_var::value) { + size_t pos__ = 1; + ptr[(pos__ - 1)] = FnGetVariPtr__(p); + pos__ = (pos__ + 1); + } + } +}; +template + auto make_closure02__(const captured_t__ p) { +return closure02__(p); +} +template +stan::promote_args_t +foo(const T0__& g, const T1__& x, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + + current_statement__ = 7; + auto g2 = make_closure00__(g, x); + current_statement__ = 8; + return g2(1.0, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} + +struct foo_functor__ { +const static int num_vars__ = 0; +template +stan::promote_args_t +operator()(const T0__& g, const T1__& x, std::ostream* pstream__) const +{ +return foo(g, x, pstream__); +} +}; + +template +stan::promote_args_t +closure02_impl__(const T0__& y, const T1__& p, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 14; + return (p * y); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure01_impl__(const T0__& y, const double& x, std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 12; + return (x * y); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +template +stan::promote_args_t +closure00_impl__(const T0__& y, const T1__& g, const T2__& x, + std::ostream* pstream__) { + using local_scalar_t__ = stan::promote_args_t; + const static bool propto__ = true; + (void) propto__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + current_statement__ = 10; + return (g(y, pstream__) * x); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + +} +class closure_3_model : public model_base_crtp { + + private: + int pos__; + double x; + double z; + + public: + ~closure_3_model() { } + + std::string model_name() const { return "closure_3_model"; } + + closure_3_model(stan::io::var_context& context__, + unsigned int random_seed__ = 0, + std::ostream* pstream__ = nullptr) : model_base_crtp(0) { + typedef double local_scalar_t__; + boost::ecuyer1988 base_rng__ = + stan::services::util::create_rng(random_seed__, 0); + (void) base_rng__; // suppress unused var warning + static const char* function__ = "closure_3_model_namespace::closure_3_model"; + (void) function__; // suppress unused var warning + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + try { + pos__ = std::numeric_limits::min(); + + pos__ = 1; + x = std::numeric_limits::quiet_NaN(); + + current_statement__ = 5; + x = 2.0; + + current_statement__ = 2; + auto bar = make_closure01__(x); + z = std::numeric_limits::quiet_NaN(); + + current_statement__ = 6; + z = foo(bar, 1.0, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + num_params_r__ = 0U; + + try { + num_params_r__ += 1; + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } + template + inline T__ log_prob(std::vector& params_r__, + std::vector& params_i__, + std::ostream* pstream__ = 0) const { + typedef T__ local_scalar_t__; + T__ lp__(0.0); + stan::math::accumulator lp_accum__; + static const char* function__ = "closure_3_model_namespace::log_prob"; +(void) function__; // suppress unused var warning + + stan::io::reader in__(params_r__, params_i__); + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + + try { + + current_statement__ = 2; + auto bar = make_closure01__(x); + local_scalar_t__ p; + p = DUMMY_VAR__; + + current_statement__ = 1; + p = in__.scalar(); + + current_statement__ = 3; + auto baz = make_closure02__(p); + local_scalar_t__ w; + w = DUMMY_VAR__; + + current_statement__ = 4; + w = foo(baz, 1.0, pstream__); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + lp_accum__.add(lp__); + return lp_accum__.sum(); + } // log_prob() + + template + inline void write_array(RNG& base_rng__, std::vector& params_r__, + std::vector& params_i__, + std::vector& vars__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true, + std::ostream* pstream__ = 0) const { + using local_scalar_t__ = double; + vars__.resize(0); + stan::io::reader in__(params_r__, params_i__); + static const char* function__ = "closure_3_model_namespace::write_array"; +(void) function__; // suppress unused var warning + + (void) function__; // suppress unused var warning + + double lp__ = 0.0; + (void) lp__; // dummy to suppress unused var warning + stan::math::accumulator lp_accum__; + local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN()); + (void) DUMMY_VAR__; // suppress unused var warning + + + try { + + current_statement__ = 2; + auto bar = make_closure01__(x); + double p; + p = std::numeric_limits::quiet_NaN(); + + current_statement__ = 1; + p = in__.scalar(); + + double w; + w = std::numeric_limits::quiet_NaN(); + + vars__.push_back(p); + if (logical_negation((primitive_value(emit_transformed_parameters__) || + primitive_value(emit_generated_quantities__)))) { + return ; + } + current_statement__ = 3; + auto baz = make_closure02__(p); + current_statement__ = 4; + w = foo(baz, 1.0, pstream__); + vars__.push_back(w); + if (logical_negation(emit_generated_quantities__)) { + return ; + } + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } // write_array() + + inline void transform_inits(const stan::io::var_context& context__, + std::vector& params_i__, + std::vector& vars__, + std::ostream* pstream__) const { + typedef double local_scalar_t__; + vars__.resize(0); + vars__.reserve(num_params_r__); + + try { + int pos__; + pos__ = std::numeric_limits::min(); + + pos__ = 1; + double p; + p = std::numeric_limits::quiet_NaN(); + + current_statement__ = 1; + p = context__.vals_r("p")[(1 - 1)]; + vars__.push_back(p); + } catch (const std::exception& e) { + stan::lang::rethrow_located(e, locations_array__[current_statement__]); + // Next line prevents compiler griping about no return + throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***"); + } + } // transform_inits() + + inline void get_param_names(std::vector& names__) const { + + names__.resize(0); + names__.push_back("p"); + names__.push_back("w"); + } // get_param_names() + + inline void get_dims(std::vector>& dimss__) const { + dimss__.resize(0); + std::vector dims__; + dimss__.push_back(dims__); + dims__.resize(0); + dimss__.push_back(dims__); + dims__.resize(0); + + } // get_dims() + + inline void constrained_param_names( + std::vector& param_names__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true) const { + + param_names__.push_back(std::string() + "p"); + if (emit_transformed_parameters__) { + param_names__.push_back(std::string() + "w"); + } + + if (emit_generated_quantities__) { + + } + + } // constrained_param_names() + + inline void unconstrained_param_names( + std::vector& param_names__, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true) const { + + param_names__.push_back(std::string() + "p"); + if (emit_transformed_parameters__) { + param_names__.push_back(std::string() + "w"); + } + + if (emit_generated_quantities__) { + + } + + } // unconstrained_param_names() + + inline std::string get_constrained_sizedtypes() const { + stringstream s__; + s__ << "[{\"name\":\"p\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"w\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"}]"; + return s__.str(); + } // get_constrained_sizedtypes() + + inline std::string get_unconstrained_sizedtypes() const { + stringstream s__; + s__ << "[{\"name\":\"p\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"w\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"}]"; + return s__.str(); + } // get_unconstrained_sizedtypes() + + + // Begin method overload boilerplate + template + void write_array(RNG& base_rng__, + Eigen::Matrix& params_r, + Eigen::Matrix& vars, + bool emit_transformed_parameters__ = true, + bool emit_generated_quantities__ = true, + std::ostream* pstream = 0) const { + std::vector params_r_vec(params_r.size()); + for (int i = 0; i < params_r.size(); ++i) + params_r_vec[i] = params_r(i); + std::vector vars_vec; + std::vector params_i_vec; + write_array(base_rng__, params_r_vec, params_i_vec, vars_vec, + emit_transformed_parameters__, emit_generated_quantities__, pstream); + vars.resize(vars_vec.size()); + for (int i = 0; i < vars.size(); ++i) + vars(i) = vars_vec[i]; + } + + template + T_ log_prob(Eigen::Matrix& params_r, + std::ostream* pstream = 0) const { + std::vector vec_params_r; + vec_params_r.reserve(params_r.size()); + for (int i = 0; i < params_r.size(); ++i) + vec_params_r.push_back(params_r(i)); + std::vector vec_params_i; + return log_prob(vec_params_r, vec_params_i, pstream); + } + + void transform_inits(const stan::io::var_context& context, + Eigen::Matrix& params_r, + std::ostream* pstream__) const { + std::vector params_r_vec; + std::vector params_i_vec; + transform_inits(context, params_i_vec, params_r_vec, pstream__); + params_r.resize(params_r_vec.size()); + for (int i = 0; i < params_r.size(); ++i) + params_r(i) = params_r_vec[i]; + } + +}; +} +typedef closure_3_model_namespace::closure_3_model stan_model; + +#ifndef USING_R + +// Boilerplate +stan::model::model_base& new_model( + stan::io::var_context& data_context, + unsigned int seed, + std::ostream* msg_stream) { + stan_model* m = new stan_model(data_context, seed, msg_stream); + return *m; +} + +#endif + + + $ ../../../../../install/default/bin/stanc --print-cpp eight_schools_ncp.stan // Code generated by %%NAME%% %%VERSION%% #include namespace eight_schools_ncp_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -105,6 +2768,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class eight_schools_ncp_model : public model_base_crtp { private: @@ -540,6 +3205,13 @@ stan::model::model_base& new_model( #include namespace mother_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -1088,6 +3760,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'mother.stan', line 309, column 78 to line 313, column 3)"}; + int foo(const int& n, std::ostream* pstream__) ; @@ -1116,6 +3789,7 @@ foo(const int& n, std::ostream* pstream__) { } struct foo_functor__ { +const static int num_vars__ = 0; int operator()(const int& n, std::ostream* pstream__) const { @@ -1169,6 +3843,7 @@ sho(const T0__& t, const std::vector& y, } struct sho_functor__ { +const static int num_vars__ = 0; template std::vector> @@ -1178,7 +3853,21 @@ operator()(const T0__& t, const std::vector& y, { return sho(t, y, theta, x, x_int, pstream__); } -}; +template +std::vector> +operator()(const T0__& t, const std::vector& y, + std::ostream* pstream__, const std::vector& theta, + const std::vector& x, const std::vector& x_int) const +{ +return sho(t, y, theta, x, x_int, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = sho_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; double foo_bar0(std::ostream* pstream__) { @@ -1200,6 +3889,7 @@ foo_bar0(std::ostream* pstream__) { } struct foo_bar0_functor__ { +const static int num_vars__ = 0; double operator()(std::ostream* pstream__) const { @@ -1228,6 +3918,7 @@ foo_bar1(const T0__& x, std::ostream* pstream__) { } struct foo_bar1_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const T0__& x, std::ostream* pstream__) const @@ -1258,6 +3949,7 @@ foo_bar2(const T0__& x, const T1__& y, std::ostream* pstream__) { } struct foo_bar2_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -1286,6 +3978,7 @@ foo_lpmf(const int& y, const T1__& lambda, std::ostream* pstream__) { } struct foo_lpmf_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const int& y, const T1__& lambda, std::ostream* pstream__) const @@ -1315,6 +4008,7 @@ foo_lcdf(const int& y, const T1__& lambda, std::ostream* pstream__) { } struct foo_lcdf_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const int& y, const T1__& lambda, std::ostream* pstream__) const @@ -1344,6 +4038,7 @@ foo_lccdf(const int& y, const T1__& lambda, std::ostream* pstream__) { } struct foo_lccdf_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const int& y, const T1__& lambda, std::ostream* pstream__) const @@ -1375,6 +4070,7 @@ foo_rng(const T0__& mu, const T1__& sigma, RNG& base_rng__, } struct foo_rng_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -1408,6 +4104,7 @@ unit_normal_lp(const T0__& u, T_lp__& lp__, T_lp_accum__& lp_accum__, } struct unit_normal_lp_functor__ { +const static int num_vars__ = 0; template void @@ -1670,6 +4367,7 @@ foo_1(const int& a, std::ostream* pstream__) { } struct foo_1_functor__ { +const static int num_vars__ = 0; int operator()(const int& a, std::ostream* pstream__) const { @@ -1714,6 +4412,7 @@ foo_2(const int& a, std::ostream* pstream__) { } struct foo_2_functor__ { +const static int num_vars__ = 0; int operator()(const int& a, std::ostream* pstream__) const { @@ -1742,6 +4441,7 @@ foo_3(const T0__& t, const int& n, std::ostream* pstream__) { } struct foo_3_functor__ { +const static int num_vars__ = 0; template std::vector> operator()(const T0__& t, const int& n, std::ostream* pstream__) const @@ -1771,6 +4471,7 @@ foo_lp(const T0__& x, T_lp__& lp__, T_lp_accum__& lp_accum__, } struct foo_lp_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -1805,6 +4506,7 @@ foo_4(const T0__& x, std::ostream* pstream__) { } struct foo_4_functor__ { +const static int num_vars__ = 0; template void operator()(const T0__& x, std::ostream* pstream__) const @@ -1870,6 +4572,7 @@ relative_diff(const T0__& x, const T1__& y, const T2__& max_, } struct relative_diff_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -1878,7 +4581,20 @@ operator()(const T0__& x, const T1__& y, const T2__& max_, const T3__& min_, { return relative_diff(x, y, max_, min_, pstream__); } -}; +template +stan::promote_args_t +operator()(const T0__& x, const T1__& y, std::ostream* pstream__, + const T2__& max_, const T3__& min_) const +{ +return relative_diff(x, y, max_, min_, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = relative_diff_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template Eigen::Matrix& shared_params, } struct foo_5_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> @@ -1916,7 +4633,22 @@ operator()(const Eigen::Matrix& shared_params, { return foo_5(shared_params, job_params, data_r, data_i, pstream__); } -}; +template +Eigen::Matrix, -1, 1> +operator()(const Eigen::Matrix& shared_params, + const Eigen::Matrix& job_params, + std::ostream* pstream__, const std::vector& data_r, + const std::vector& data_i) const +{ +return foo_5(shared_params, job_params, data_r, data_i, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = foo_5_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -1946,6 +4678,7 @@ foo_five_args(const T0__& x1, const T1__& x2, const T2__& x3, const T3__& x4, } struct foo_five_args_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t +stan::promote_args_t +operator()(const T0__& x1, const T1__& x2, std::ostream* pstream__, + const T2__& x3, const T3__& x4, const T4__& x5) const +{ +return foo_five_args(x1, x2, x3, x4, x5, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = foo_five_args_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2041,6 +4789,7 @@ covsqrt2corsqrt(const Eigen::Matrix& mat, const int& invert, } struct covsqrt2corsqrt_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> operator()(const Eigen::Matrix& mat, const int& invert, @@ -2091,6 +4840,7 @@ f0(const int& a1, const std::vector& a2, } struct f0_functor__ { +const static int num_vars__ = 0; template void @@ -2108,7 +4858,28 @@ operator()(const int& a1, const std::vector& a2, { return f0(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +void +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f0(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f0_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2148,6 +4919,7 @@ f1(const int& a1, const std::vector& a2, } struct f1_functor__ { +const static int num_vars__ = 0; template int @@ -2165,7 +4937,28 @@ operator()(const int& a1, const std::vector& a2, { return f1(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +int +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f1(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f1_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2205,6 +4998,7 @@ f2(const int& a1, const std::vector& a2, } struct f2_functor__ { +const static int num_vars__ = 0; template std::vector @@ -2222,7 +5016,28 @@ operator()(const int& a1, const std::vector& a2, { return f2(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f2(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f2_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2262,6 +5077,7 @@ f3(const int& a1, const std::vector& a2, } struct f3_functor__ { +const static int num_vars__ = 0; template std::vector> @@ -2279,7 +5095,28 @@ operator()(const int& a1, const std::vector& a2, { return f3(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f3(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f3_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2321,6 +5158,7 @@ f4(const int& a1, const std::vector& a2, } struct f4_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t& a2, { return f4(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +stan::promote_args_t> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f4(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f4_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2382,6 +5243,7 @@ f5(const int& a1, const std::vector& a2, } struct f5_functor__ { +const static int num_vars__ = 0; template std::vector& a2, { return f5(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector>> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f5(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f5_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2443,6 +5328,7 @@ f6(const int& a1, const std::vector& a2, } struct f6_functor__ { +const static int num_vars__ = 0; template std::vector& a2, { return f6(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector>>> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f6(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f6_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2504,6 +5413,7 @@ f7(const int& a1, const std::vector& a2, } struct f7_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix& a2, { return f7(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +Eigen::Matrix>, -1, 1> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f7(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f7_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2565,6 +5498,7 @@ f8(const int& a1, const std::vector& a2, } struct f8_functor__ { +const static int num_vars__ = 0; template std::vector& a2, { return f8(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector>, -1, 1>> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f8(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f8_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2624,28 +5581,52 @@ f9(const int& a1, const std::vector& a2, } } - -struct f9_functor__ { + +struct f9_functor__ { +const static int num_vars__ = 0; +template +std::vector>, -1, 1>>> +operator()(const int& a1, const std::vector& a2, + const std::vector>& a3, const T3__& a4, + const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12, + std::ostream* pstream__) const +{ +return f9(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} template std::vector>, -1, 1>>> operator()(const int& a1, const std::vector& a2, - const std::vector>& a3, const T3__& a4, - const std::vector& a5, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, const std::vector>& a6, const Eigen::Matrix& a7, const std::vector>& a8, const std::vector>>& a9, const Eigen::Matrix& a10, const std::vector>& a11, - const std::vector>>& a12, - std::ostream* pstream__) const + const std::vector>>& a12) const { return f9(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f9_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2687,6 +5668,7 @@ f10(const int& a1, const std::vector& a2, } struct f10_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix& a2, { return f10(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +Eigen::Matrix>, -1, -1> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f10(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f10_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2748,6 +5753,7 @@ f11(const int& a1, const std::vector& a2, } struct f11_functor__ { +const static int num_vars__ = 0; template std::vector& a2, { return f11(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector>, -1, -1>> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f11(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f11_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template @@ -2809,6 +5838,7 @@ f12(const int& a1, const std::vector& a2, } struct f12_functor__ { +const static int num_vars__ = 0; template std::vector& a2, { return f12(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); } -}; +template +std::vector>, -1, -1>>> +operator()(const int& a1, const std::vector& a2, + std::ostream* pstream__, const std::vector>& a3, + const T3__& a4, const std::vector& a5, + const std::vector>& a6, + const Eigen::Matrix& a7, + const std::vector>& a8, + const std::vector>>& a9, + const Eigen::Matrix& a10, + const std::vector>& a11, + const std::vector>>& a12) const +{ +return f12(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f12_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; void foo_6(std::ostream* pstream__) { @@ -2879,6 +5932,7 @@ foo_6(std::ostream* pstream__) { } struct foo_6_functor__ { +const static int num_vars__ = 0; void operator()(std::ostream* pstream__) const { @@ -2915,6 +5969,7 @@ matfoo(std::ostream* pstream__) { } struct matfoo_functor__ { +const static int num_vars__ = 0; Eigen::Matrix operator()(std::ostream* pstream__) const { @@ -2944,6 +5999,7 @@ vecfoo(std::ostream* pstream__) { } struct vecfoo_functor__ { +const static int num_vars__ = 0; Eigen::Matrix operator()(std::ostream* pstream__) const { @@ -2981,6 +6037,7 @@ vecmufoo(const T0__& mu, std::ostream* pstream__) { } struct vecmufoo_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> operator()(const T0__& mu, std::ostream* pstream__) const @@ -3024,6 +6081,7 @@ vecmubar(const T0__& mu, std::ostream* pstream__) { } struct vecmubar_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> operator()(const T0__& mu, std::ostream* pstream__) const @@ -3073,6 +6131,7 @@ algebra_system(const Eigen::Matrix& x, } struct algebra_system_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> @@ -3082,7 +6141,21 @@ operator()(const Eigen::Matrix& x, { return algebra_system(x, y, dat, dat_int, pstream__); } -}; +template +Eigen::Matrix, -1, 1> +operator()(const Eigen::Matrix& x, + const Eigen::Matrix& y, std::ostream* pstream__, + const std::vector& dat, const std::vector& dat_int) const +{ +return algebra_system(x, y, dat, dat_int, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = algebra_system_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template Eigen::Matrix& phi, } struct binomialf_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> @@ -3128,7 +6202,22 @@ operator()(const Eigen::Matrix& phi, { return binomialf(phi, theta, x_r, x_i, pstream__); } -}; +template +Eigen::Matrix, -1, 1> +operator()(const Eigen::Matrix& phi, + const Eigen::Matrix& theta, std::ostream* pstream__, + const std::vector& x_r, const std::vector& x_i) const +{ +return binomialf(phi, theta, x_r, x_i, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = binomialf_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class mother_model : public model_base_crtp { @@ -9067,6 +12156,13 @@ get_lp() function is deprecated. It will be removed in a future release. Use tar #include namespace motherHOF_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -9286,6 +12382,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'motherHOF.stan', line 29, column 39 to line 34, column 3)"}; + template std::vector> @@ -9325,6 +12422,7 @@ sho(const T0__& t, const std::vector& y, } struct sho_functor__ { +const static int num_vars__ = 0; template std::vector> @@ -9334,7 +12432,21 @@ operator()(const T0__& t, const std::vector& y, { return sho(t, y, theta, x, x_int, pstream__); } -}; +template +std::vector> +operator()(const T0__& t, const std::vector& y, + std::ostream* pstream__, const std::vector& theta, + const std::vector& x, const std::vector& x_int) const +{ +return sho(t, y, theta, x, x_int, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = sho_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template stan::promote_args_t& theta, } struct integrand_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -9369,7 +12482,21 @@ operator()(const T0__& x, const T1__& xc, const std::vector& theta, { return integrand(x, xc, theta, x_r, x_i, pstream__); } -}; +template +stan::promote_args_t +operator()(const T0__& x, const T1__& xc, std::ostream* pstream__, + const std::vector& theta, const std::vector& x_r, + const std::vector& x_i) const +{ +return integrand(x, xc, theta, x_r, x_i, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = integrand_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template Eigen::Matrix& shared_params, } struct foo_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> @@ -9407,7 +12535,22 @@ operator()(const Eigen::Matrix& shared_params, { return foo(shared_params, job_params, data_r, data_i, pstream__); } -}; +template +Eigen::Matrix, -1, 1> +operator()(const Eigen::Matrix& shared_params, + const Eigen::Matrix& job_params, + std::ostream* pstream__, const std::vector& data_r, + const std::vector& data_i) const +{ +return foo(shared_params, job_params, data_r, data_i, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = foo_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template Eigen::Matrix& shared_params, } struct goo_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> @@ -9445,7 +12589,22 @@ operator()(const Eigen::Matrix& shared_params, { return goo(shared_params, job_params, data_r, data_i, pstream__); } -}; +template +Eigen::Matrix, -1, 1> +operator()(const Eigen::Matrix& shared_params, + const Eigen::Matrix& job_params, + std::ostream* pstream__, const std::vector& data_r, + const std::vector& data_i) const +{ +return goo(shared_params, job_params, data_r, data_i, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = goo_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; template stan::promote_args_t @@ -9468,6 +12627,7 @@ map_rectfake(const T0__& x, std::ostream* pstream__) { } struct map_rectfake_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const T0__& x, std::ostream* pstream__) const @@ -9517,6 +12677,7 @@ algebra_system(const Eigen::Matrix& x, } struct algebra_system_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> @@ -9526,7 +12687,22 @@ operator()(const Eigen::Matrix& x, { return algebra_system(x, y, dat, dat_int, pstream__); } -}; +template +Eigen::Matrix, -1, 1> +operator()(const Eigen::Matrix& x, + const Eigen::Matrix& y, std::ostream* pstream__, + const std::vector& dat, const std::vector& dat_int) const +{ +return algebra_system(x, y, dat, dat_int, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = algebra_system_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class motherHOF_model : public model_base_crtp { @@ -10001,19 +13177,19 @@ class motherHOF_model : public model_base_crtp { current_statement__ = 65; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_d, t0, ts, theta_d, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 66; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_d, t0, ts, theta_p, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 67; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_p, t0, ts, theta_d, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 68; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_p, t0, ts, theta_p, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 69; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_d, t0, ts, theta_d, x, x_int, @@ -10397,19 +13573,19 @@ class motherHOF_model : public model_base_crtp { current_statement__ = 34; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_d, t0, ts, theta_d, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 35; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_d, t0, ts, theta_p, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 36; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_p, t0, ts, theta_d, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 37; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_p, t0, ts, theta_p, x, x_int, - pstream__), "assigning variable y_hat"); + pstream__, 1e-10, 1e-10, 100000000.), "assigning variable y_hat"); current_statement__ = 38; assign(y_hat, nil_index_list(), integrate_ode_bdf(sho_functor__(), y0_d, t0, ts, theta_d, x, x_int, @@ -11033,6 +14209,13 @@ STAN_REGISTER_MAP_RECT(9, motherHOF_model_namespace::goo_functor__) #include namespace optimize_glm_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -11314,6 +14497,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class optimize_glm_model : public model_base_crtp { private: @@ -12739,6 +15924,13 @@ stan::model::model_base& new_model( #include namespace reduce_sum_m1_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -12851,6 +16043,7 @@ static const std::vector locations_array__ = {" (found before start of p struct foo_lpdf_rsfunctor__; struct g_rsfunctor__; struct h_rsfunctor__; + template stan::promote_args_t g(const std::vector& y_slice, const int& start, const int& end, @@ -12879,6 +16072,7 @@ g(const std::vector& y_slice, const int& start, const int& end, } struct g_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector& y_slice, const int& start, @@ -12886,7 +16080,19 @@ operator()(const std::vector& y_slice, const int& start, { return g(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return g(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g_rsfunctor__ { @@ -12930,6 +16136,7 @@ h(const std::vector& y_slice, const int& start, const int& end, } struct h_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -12939,7 +16146,21 @@ operator()(const std::vector& y_slice, const int& start, { return h(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector& a) const +{ +return h(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h_rsfunctor__ { @@ -12974,6 +16195,7 @@ foo_lpdf(const std::vector& y_slice, const int& start, const int& end, } struct foo_lpdf_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector& y_slice, const int& start, @@ -12994,6 +16216,7 @@ return foo_lpdf(y_slice, start + 1, end + 1, pstream__); } }; + class reduce_sum_m1_model : public model_base_crtp { private: @@ -13391,6 +16614,13 @@ stan::model::model_base& new_model( #include namespace reduce_sum_m2_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -13633,6 +16863,7 @@ struct h5_rsfunctor__; struct h6_rsfunctor__; struct h7_rsfunctor__; struct h8_rsfunctor__; + template stan::promote_args_t g1(const std::vector& y_slice, const int& start, const int& end, @@ -13655,6 +16886,7 @@ g1(const std::vector& y_slice, const int& start, const int& end, } struct g1_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector& y_slice, const int& start, @@ -13662,7 +16894,19 @@ operator()(const std::vector& y_slice, const int& start, { return g1(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return g1(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g1_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g1_rsfunctor__ { @@ -13710,6 +16954,7 @@ g2(const std::vector>& y_slice, const int& start, } struct g2_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, @@ -13717,7 +16962,19 @@ operator()(const std::vector>& y_slice, { return g2(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return g2(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g2_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g2_rsfunctor__ { @@ -13765,6 +17022,7 @@ g3(const std::vector>& y_slice, const int& start, } struct g3_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, @@ -13772,7 +17030,19 @@ operator()(const std::vector>& y_slice, { return g3(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return g3(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g3_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g3_rsfunctor__ { @@ -13821,6 +17091,7 @@ g4(const std::vector>& y_slice, const int& start, } struct g4_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, @@ -13828,7 +17099,19 @@ operator()(const std::vector>& y_slice, { return g4(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return g4(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g4_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g4_rsfunctor__ { @@ -13882,6 +17165,7 @@ g5(const std::vector>& y_slice, const int& start, } struct g5_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, const int& start, @@ -13889,7 +17173,19 @@ operator()(const std::vector>& y_slice, const int& start, { return g5(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return g5(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g5_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g5_rsfunctor__ { @@ -13944,6 +17240,7 @@ g6(const std::vector>>& y_slice, } struct g6_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -13951,7 +17248,19 @@ operator()(const std::vector>>& y_slice, { return g6(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return g6(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g6_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g6_rsfunctor__ { @@ -14006,6 +17315,7 @@ g7(const std::vector>>& y_slice, } struct g7_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -14013,7 +17323,19 @@ operator()(const std::vector>>& y_slice, { return g7(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return g7(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g7_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g7_rsfunctor__ { @@ -14068,6 +17390,7 @@ g8(const std::vector>>& y_slice, } struct g8_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -14075,7 +17398,19 @@ operator()(const std::vector>>& y_slice, { return g8(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return g8(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g8_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g8_rsfunctor__ { @@ -14114,6 +17449,7 @@ h1(const std::vector& y, const int& start, const int& end, } struct h1_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14122,7 +17458,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h1(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector& a) const +{ +return h1(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h1_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h1_rsfunctor__ { @@ -14171,6 +17521,7 @@ h2(const std::vector& y, const int& start, const int& end, } struct h2_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14180,7 +17531,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h2(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return h2(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h2_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h2_rsfunctor__ { @@ -14230,6 +17595,7 @@ h3(const std::vector& y, const int& start, const int& end, } struct h3_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14239,7 +17605,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h3(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return h3(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h3_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h3_rsfunctor__ { @@ -14290,6 +17670,7 @@ h4(const std::vector& y, const int& start, const int& end, } struct h4_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14299,7 +17680,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h4(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return h4(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h4_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h4_rsfunctor__ { @@ -14356,6 +17751,7 @@ h5(const std::vector& y, const int& start, const int& end, } struct h5_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14364,7 +17760,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h5(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return h5(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h5_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h5_rsfunctor__ { @@ -14422,16 +17832,31 @@ h6(const std::vector& y, const int& start, const int& end, } struct h6_functor__ { +const static int num_vars__ = 0; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, const int& end, + const std::vector>>& a, + std::ostream* pstream__) const +{ +return h6(y, start, end, a, pstream__); +} template stan::promote_args_t -operator()(const std::vector& y, const int& start, const int& end, - const std::vector>>& a, - std::ostream* pstream__) const +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>>& a) const { return h6(y, start, end, a, pstream__); } -}; +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h6_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h6_rsfunctor__ { @@ -14490,6 +17915,7 @@ h7(const std::vector& y, const int& start, const int& end, } struct h7_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14499,7 +17925,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h7(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>>& a) const +{ +return h7(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h7_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h7_rsfunctor__ { @@ -14558,6 +17998,7 @@ h8(const std::vector& y, const int& start, const int& end, } struct h8_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -14567,7 +18008,21 @@ operator()(const std::vector& y, const int& start, const int& end, { return h8(y, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>>& a) const +{ +return h8(y, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = h8_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct h8_rsfunctor__ { @@ -14582,6 +18037,7 @@ return h8(y, start + 1, end + 1, a, pstream__); } }; + class reduce_sum_m2_model : public model_base_crtp { private: @@ -16459,6 +19915,13 @@ stan::model::model_base& new_model( #include namespace reduce_sum_m3_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -16794,6 +20257,7 @@ struct g7_rsfunctor__; struct g8_rsfunctor__; struct g9_rsfunctor__; struct s_rsfunctor__; + template stan::promote_args_t f1(const std::vector& y_slice, const int& start, const int& end, @@ -16816,6 +20280,7 @@ f1(const std::vector& y_slice, const int& start, const int& end, } struct f1_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector& y_slice, const int& start, @@ -16823,7 +20288,19 @@ operator()(const std::vector& y_slice, const int& start, { return f1(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return f1(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f1_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f1_rsfunctor__ { @@ -16858,6 +20335,7 @@ f1a(const std::vector& y_slice, const int& start, const int& end, } struct f1a_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector& y_slice, const int& start, @@ -16865,7 +20343,19 @@ operator()(const std::vector& y_slice, const int& start, { return f1a(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return f1a(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f1a_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f1a_rsfunctor__ { @@ -16900,6 +20390,7 @@ f2(const std::vector>& y_slice, const int& start, } struct f2_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, @@ -16907,7 +20398,19 @@ operator()(const std::vector>& y_slice, { return f2(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f2(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f2_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f2_rsfunctor__ { @@ -16942,6 +20445,7 @@ f3(const std::vector>& y_slice, const int& start, } struct f3_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, @@ -16949,7 +20453,19 @@ operator()(const std::vector>& y_slice, { return f3(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f3(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f3_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f3_rsfunctor__ { @@ -16984,6 +20500,7 @@ f4(const std::vector>& y_slice, const int& start, } struct f4_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, @@ -16991,7 +20508,19 @@ operator()(const std::vector>& y_slice, { return f4(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f4(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f4_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f4_rsfunctor__ { @@ -17026,6 +20555,7 @@ f5(const std::vector>& y_slice, const int& start, } struct f5_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>& y_slice, const int& start, @@ -17033,7 +20563,19 @@ operator()(const std::vector>& y_slice, const int& start, { return f5(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return f5(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f5_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f5_rsfunctor__ { @@ -17068,6 +20610,7 @@ f6(const std::vector>>& y_slice, } struct f6_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -17075,7 +20618,19 @@ operator()(const std::vector>>& y_slice, { return f6(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f6(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f6_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f6_rsfunctor__ { @@ -17110,6 +20665,7 @@ f7(const std::vector>>& y_slice, } struct f7_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -17117,7 +20673,19 @@ operator()(const std::vector>>& y_slice, { return f7(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f7(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f7_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f7_rsfunctor__ { @@ -17152,6 +20720,7 @@ f8(const std::vector>>& y_slice, } struct f8_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -17159,7 +20728,19 @@ operator()(const std::vector>>& y_slice, { return f8(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f8(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f8_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f8_rsfunctor__ { @@ -17193,13 +20774,25 @@ f9(const std::vector& y_slice, const int& start, const int& end, } struct f9_functor__ { +const static int num_vars__ = 0; double operator()(const std::vector& y_slice, const int& start, const int& end, std::ostream* pstream__) const { return f9(y_slice, start, end, pstream__); } -}; +double +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return f9(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f9_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f9_rsfunctor__ { @@ -17232,13 +20825,25 @@ f10(const std::vector>& y_slice, const int& start, } struct f10_functor__ { +const static int num_vars__ = 0; double operator()(const std::vector>& y_slice, const int& start, const int& end, std::ostream* pstream__) const { return f10(y_slice, start, end, pstream__); } -}; +double +operator()(const std::vector>& y_slice, const int& start, + std::ostream* pstream__, const int& end) const +{ +return f10(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f10_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f10_rsfunctor__ { @@ -17271,13 +20876,25 @@ f11(const std::vector>>& y_slice, } struct f11_functor__ { +const static int num_vars__ = 0; double operator()(const std::vector>>& y_slice, const int& start, const int& end, std::ostream* pstream__) const { return f11(y_slice, start, end, pstream__); } -}; +double +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f11(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f11_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f11_rsfunctor__ { @@ -17311,6 +20928,7 @@ f12(const std::vector>>& y_slice, } struct f12_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t operator()(const std::vector>>& y_slice, @@ -17318,7 +20936,19 @@ operator()(const std::vector>>& y_slice, { return f12(y_slice, start, end, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector>>& y_slice, + const int& start, std::ostream* pstream__, const int& end) const +{ +return f12(y_slice, start, end, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = f12_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct f12_rsfunctor__ { @@ -17354,6 +20984,7 @@ g1(const std::vector& y_slice, const int& start, const int& end, } struct g1_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17362,7 +20993,20 @@ operator()(const std::vector& y_slice, const int& start, { return g1(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, const T3__& a) const +{ +return g1(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g1_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g1_rsfunctor__ { @@ -17399,6 +21043,7 @@ g2(const std::vector& y_slice, const int& start, const int& end, } struct g2_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17408,7 +21053,21 @@ operator()(const std::vector& y_slice, const int& start, { return g2(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const Eigen::Matrix& a) const +{ +return g2(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g2_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g2_rsfunctor__ { @@ -17446,6 +21105,7 @@ g3(const std::vector& y_slice, const int& start, const int& end, } struct g3_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17455,7 +21115,21 @@ operator()(const std::vector& y_slice, const int& start, { return g3(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const Eigen::Matrix& a) const +{ +return g3(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g3_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g3_rsfunctor__ { @@ -17493,6 +21167,7 @@ g4(const std::vector& y_slice, const int& start, const int& end, } struct g4_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17502,7 +21177,21 @@ operator()(const std::vector& y_slice, const int& start, { return g4(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const Eigen::Matrix& a) const +{ +return g4(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g4_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g4_rsfunctor__ { @@ -17540,6 +21229,7 @@ g5(const std::vector& y_slice, const int& start, const int& end, } struct g5_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17549,7 +21239,21 @@ operator()(const std::vector& y_slice, const int& start, { return g5(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector& a) const +{ +return g5(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g5_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g5_rsfunctor__ { @@ -17587,6 +21291,7 @@ g6(const std::vector& y_slice, const int& start, const int& end, } struct g6_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17596,7 +21301,21 @@ operator()(const std::vector& y_slice, const int& start, { return g6(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return g6(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g6_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g6_rsfunctor__ { @@ -17634,6 +21353,7 @@ g7(const std::vector& y_slice, const int& start, const int& end, } struct g7_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17643,7 +21363,21 @@ operator()(const std::vector& y_slice, const int& start, { return g7(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return g7(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g7_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g7_rsfunctor__ { @@ -17681,6 +21415,7 @@ g8(const std::vector& y_slice, const int& start, const int& end, } struct g8_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17690,7 +21425,21 @@ operator()(const std::vector& y_slice, const int& start, { return g8(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return g8(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g8_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g8_rsfunctor__ { @@ -17728,6 +21477,7 @@ g9(const std::vector& y_slice, const int& start, const int& end, } struct g9_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17737,7 +21487,21 @@ operator()(const std::vector& y_slice, const int& start, { return g9(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>& a) const +{ +return g9(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g9_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g9_rsfunctor__ { @@ -17776,6 +21540,7 @@ g10(const std::vector& y_slice, const int& start, const int& end, } struct g10_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17786,7 +21551,21 @@ operator()(const std::vector& y_slice, const int& start, { return g10(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>>& a) const +{ +return g10(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g10_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g10_rsfunctor__ { @@ -17825,6 +21604,7 @@ g11(const std::vector& y_slice, const int& start, const int& end, } struct g11_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17835,7 +21615,21 @@ operator()(const std::vector& y_slice, const int& start, { return g11(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>>& a) const +{ +return g11(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g11_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g11_rsfunctor__ { @@ -17874,6 +21668,7 @@ g12(const std::vector& y_slice, const int& start, const int& end, } struct g12_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -17884,7 +21679,21 @@ operator()(const std::vector& y_slice, const int& start, { return g12(y_slice, start, end, a, pstream__); } -}; +template +stan::promote_args_t +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, + const std::vector>>& a) const +{ +return g12(y_slice, start, end, a, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = g12_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct g12_rsfunctor__ { @@ -17951,6 +21760,7 @@ s(const std::vector& y_slice, const int& start, const int& end, } struct s_functor__ { +const static int num_vars__ = 0; template & y_slice, const int& start, return s(y_slice, start, end, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, pstream__); } -}; +template +stan::promote_args_t>> +operator()(const std::vector& y_slice, const int& start, + std::ostream* pstream__, const int& end, const int& a, + const T4__& b, const Eigen::Matrix& c, + const Eigen::Matrix& d, + const Eigen::Matrix& e, const std::vector& f, + const std::vector& g, + const std::vector>& h, + const std::vector>& i, + const std::vector>& j, + const std::vector>& k, + const std::vector>& l, + const std::vector>>& m, + const std::vector>>& n, + const std::vector>>& o, + const std::vector>>& p, + const std::vector>>& q) const +{ +return s(y_slice, start, end, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, + q, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = s_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; struct s_rsfunctor__ { @@ -18395,6 +22237,7 @@ r(std::ostream* pstream__) { } struct r_functor__ { +const static int num_vars__ = 0; double operator()(std::ostream* pstream__) const { @@ -18402,6 +22245,7 @@ return r(pstream__); } }; + class reduce_sum_m3_model : public model_base_crtp { private: @@ -20773,6 +24617,13 @@ stan::model::model_base& new_model( #include namespace tilde_block_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -20870,6 +24721,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class tilde_block_model : public model_base_crtp { private: @@ -21181,6 +25034,13 @@ stan::model::model_base& new_model( #include namespace truncate_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -21291,6 +25151,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class truncate_model : public model_base_crtp { private: @@ -21688,6 +25550,13 @@ stan::model::model_base& new_model( #include namespace user_constrain_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -21781,6 +25650,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'user_constrain.stan', line 2, column 36 to line 4, column 3)"}; + template stan::promote_args_t @@ -21803,6 +25673,7 @@ lb_constrain(const T0__& x, const T1__& y, std::ostream* pstream__) { } struct lb_constrain_functor__ { +const static int num_vars__ = 0; template stan::promote_args_t @@ -21812,6 +25683,7 @@ return lb_constrain(x, y, pstream__); } }; + class user_constrain_model : public model_base_crtp { private: diff --git a/test/integration/good/code-gen/mir.expected b/test/integration/good/code-gen/mir.expected index fe483769c2..10ef5bce37 100644 --- a/test/integration/good/code-gen/mir.expected +++ b/test/integration/good/code-gen/mir.expected @@ -1,5 +1,6 @@ $ ../../../../../install/default/bin/stanc --debug-mir mother.stan -((functions_block +((closures ()) + (functions_block (((fdrt (UInt)) (fdname foo) (fdargs ((AutoDiffable n UInt))) (fdbody ((pattern Skip) (meta ))) (fdloc )) ((fdrt (UInt)) (fdname foo) (fdargs ((AutoDiffable n UInt))) @@ -5281,7 +5282,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x)) (meta ((type_ UVector) (loc ) (adlevel DataOnly)))) @@ -5303,7 +5304,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x)) (meta ((type_ UVector) (loc ) (adlevel DataOnly)))) @@ -5331,7 +5332,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x)) (meta ((type_ UVector) (loc ) (adlevel DataOnly)))) @@ -5359,7 +5360,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -5381,7 +5382,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -5409,7 +5410,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -5431,7 +5432,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -6591,7 +6592,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (DataOnly (UArray UReal)) (DataOnly (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var tmp)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -7892,7 +7893,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x)) (meta ((type_ UVector) (loc ) (adlevel DataOnly)))) @@ -7914,7 +7915,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x)) (meta ((type_ UVector) (loc ) (adlevel DataOnly)))) @@ -7942,7 +7943,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x)) (meta ((type_ UVector) (loc ) (adlevel DataOnly)))) @@ -7970,7 +7971,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -7992,7 +7993,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -8020,7 +8021,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) @@ -8042,7 +8043,7 @@ (UFun ((AutoDiffable UVector) (AutoDiffable UVector) (AutoDiffable (UArray UReal)) (AutoDiffable (UArray UInt))) - (ReturnType UVector))) + (ReturnType UVector) Function)) (loc ) (adlevel AutoDiffable)))) ((pattern (Var x_p)) (meta ((type_ UVector) (loc ) (adlevel AutoDiffable)))) diff --git a/test/integration/good/compiler-optimizations/cpp.expected b/test/integration/good/compiler-optimizations/cpp.expected index 8fe8c20566..6ba8fd0cb8 100644 --- a/test/integration/good/compiler-optimizations/cpp.expected +++ b/test/integration/good/compiler-optimizations/cpp.expected @@ -4,6 +4,13 @@ #include namespace ad_level_failing_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -123,6 +130,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'ad-level-failing.stan', line 15, column 31 to line 25, column 3)"}; + template std::vector> @@ -189,6 +197,7 @@ simple_SIR(const T0__& t, const std::vector& y, } struct simple_SIR_functor__ { +const static int num_vars__ = 0; template std::vector> @@ -198,7 +207,22 @@ operator()(const T0__& t, const std::vector& y, { return simple_SIR(t, y, theta, x_r, x_i, pstream__); } -}; +template +std::vector> +operator()(const T0__& t, const std::vector& y, + std::ostream* pstream__, const std::vector& theta, + const std::vector& x_r, const std::vector& x_i) const +{ +return simple_SIR(t, y, theta, x_r, x_i, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = simple_SIR_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class ad_level_failing_model : public model_base_crtp { @@ -939,6 +963,13 @@ stan::model::model_base& new_model( #include namespace copy_fail_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -1081,6 +1112,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'copy_fail.stan', line 45, column 4 to column 15)"}; + int first_capture(const std::vector& y_i, std::ostream* pstream__) { using local_scalar_t__ = double; @@ -1121,6 +1153,7 @@ first_capture(const std::vector& y_i, std::ostream* pstream__) { } struct first_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -1178,6 +1211,7 @@ last_capture(const std::vector& y_i, std::ostream* pstream__) { } struct last_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -1375,6 +1409,7 @@ prob_uncaptured(const int& nind, const int& n_occasions, } struct prob_uncaptured_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> @@ -1384,7 +1419,22 @@ operator()(const int& nind, const int& n_occasions, { return prob_uncaptured(nind, n_occasions, p, phi, pstream__); } -}; +template +Eigen::Matrix, -1, -1> +operator()(const int& nind, const int& n_occasions, std::ostream* pstream__, + const Eigen::Matrix& p, + const Eigen::Matrix& phi) const +{ +return prob_uncaptured(nind, n_occasions, p, phi, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = prob_uncaptured_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class copy_fail_model : public model_base_crtp { @@ -3205,6 +3255,13 @@ stan::model::model_base& new_model( #include namespace dce_fail_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -3344,6 +3401,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class dce_fail_model : public model_base_crtp { private: @@ -5017,6 +5076,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_experiment_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -5115,6 +5181,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_experiment_model : public model_base_crtp { private: @@ -5399,6 +5467,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_experiment2_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -5496,6 +5571,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_experiment2_model : public model_base_crtp { private: @@ -5779,6 +5856,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -5879,6 +5963,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_fail_model : public model_base_crtp { private: @@ -6485,6 +6571,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail2_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -6583,6 +6676,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_fail2_model : public model_base_crtp { private: @@ -7016,6 +7111,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail3_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -7142,6 +7244,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_fail3_model : public model_base_crtp { private: @@ -8809,6 +8913,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail4_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -8932,6 +9043,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_fail4_model : public model_base_crtp { private: @@ -9758,6 +9871,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail5_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -9904,6 +10024,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'expr-prop-fail5.stan', line 47, column 4 to column 15)"}; + int first_capture(const std::vector& y_i, std::ostream* pstream__) { using local_scalar_t__ = double; @@ -9944,6 +10065,7 @@ first_capture(const std::vector& y_i, std::ostream* pstream__) { } struct first_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -10001,6 +10123,7 @@ last_capture(const std::vector& y_i, std::ostream* pstream__) { } struct last_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -10198,6 +10321,7 @@ prob_uncaptured(const int& nind, const int& n_occasions, } struct prob_uncaptured_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> @@ -10207,7 +10331,22 @@ operator()(const int& nind, const int& n_occasions, { return prob_uncaptured(nind, n_occasions, p, phi, pstream__); } -}; +template +Eigen::Matrix, -1, -1> +operator()(const int& nind, const int& n_occasions, std::ostream* pstream__, + const Eigen::Matrix& p, + const Eigen::Matrix& phi) const +{ +return prob_uncaptured(nind, n_occasions, p, phi, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = prob_uncaptured_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class expr_prop_fail5_model : public model_base_crtp { @@ -11883,6 +12022,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail6_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -12091,6 +12237,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'expr-prop-fail6.stan', line 81, column 42 to line 143, column 3)"}; + int first_capture(const std::vector& y_i, std::ostream* pstream__) { using local_scalar_t__ = double; @@ -12131,6 +12278,7 @@ first_capture(const std::vector& y_i, std::ostream* pstream__) { } struct first_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -12188,6 +12336,7 @@ last_capture(const std::vector& y_i, std::ostream* pstream__) { } struct last_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -12395,6 +12544,7 @@ prob_uncaptured(const Eigen::Matrix& p, } struct prob_uncaptured_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> @@ -13012,6 +13162,7 @@ js_super_lp(const std::vector>& y, } struct js_super_lp_functor__ { +const static int num_vars__ = 0; template void @@ -13028,6 +13179,7 @@ return js_super_lp(y, first, last, p, phi, psi, nu, chi, lp__, lp_accum__, } }; + class expr_prop_fail6_model : public model_base_crtp { private: @@ -15883,6 +16035,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail7_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -15997,6 +16156,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_fail7_model : public model_base_crtp { private: @@ -17571,6 +17732,13 @@ stan::model::model_base& new_model( #include namespace expr_prop_fail8_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -17678,6 +17846,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class expr_prop_fail8_model : public model_base_crtp { private: @@ -18457,6 +18627,13 @@ stan::model::model_base& new_model( #include namespace fails_test_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -18599,6 +18776,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'fails-test.stan', line 45, column 4 to column 15)"}; + int first_capture(const std::vector& y_i, std::ostream* pstream__) { using local_scalar_t__ = double; @@ -18639,6 +18817,7 @@ first_capture(const std::vector& y_i, std::ostream* pstream__) { } struct first_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -18696,6 +18875,7 @@ last_capture(const std::vector& y_i, std::ostream* pstream__) { } struct last_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -18893,6 +19073,7 @@ prob_uncaptured(const int& nind, const int& n_occasions, } struct prob_uncaptured_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> @@ -18902,7 +19083,22 @@ operator()(const int& nind, const int& n_occasions, { return prob_uncaptured(nind, n_occasions, p, phi, pstream__); } -}; +template +Eigen::Matrix, -1, -1> +operator()(const int& nind, const int& n_occasions, std::ostream* pstream__, + const Eigen::Matrix& p, + const Eigen::Matrix& phi) const +{ +return prob_uncaptured(nind, n_occasions, p, phi, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = prob_uncaptured_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class fails_test_model : public model_base_crtp { @@ -20723,6 +20919,13 @@ stan::model::model_base& new_model( #include namespace inlining_fail2_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -20928,6 +21131,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'inlining-fail2.stan', line 156, column 4 to column 26)"}; + int first_capture(const std::vector& y_i, std::ostream* pstream__) { using local_scalar_t__ = double; @@ -20968,6 +21172,7 @@ first_capture(const std::vector& y_i, std::ostream* pstream__) { } struct first_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -21025,6 +21230,7 @@ last_capture(const std::vector& y_i, std::ostream* pstream__) { } struct last_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -21232,6 +21438,7 @@ prob_uncaptured(const Eigen::Matrix& p, } struct prob_uncaptured_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> @@ -21840,6 +22047,7 @@ jolly_seber_lp(const std::vector>& y, } struct jolly_seber_lp_functor__ { +const static int num_vars__ = 0; template void @@ -21927,6 +22135,7 @@ seq_cprob(const Eigen::Matrix& gamma, std::ostream* pstream__) { } struct seq_cprob_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, 1> operator()(const Eigen::Matrix& gamma, std::ostream* pstream__) const @@ -21935,6 +22144,7 @@ return seq_cprob(gamma, pstream__); } }; + class inlining_fail2_model : public model_base_crtp { private: @@ -24616,6 +24826,13 @@ stan::model::model_base& new_model( #include namespace lcm_experiment_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -24712,6 +24929,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class lcm_experiment_model : public model_base_crtp { private: @@ -24992,6 +25211,13 @@ stan::model::model_base& new_model( #include namespace lcm_experiment2_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -25089,6 +25315,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class lcm_experiment2_model : public model_base_crtp { private: @@ -25376,6 +25604,13 @@ stan::model::model_base& new_model( #include namespace lcm_fails_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -25470,6 +25705,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class lcm_fails_model : public model_base_crtp { private: @@ -25812,6 +26049,13 @@ stan::model::model_base& new_model( #include namespace lcm_fails2_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -25952,6 +26196,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'lcm-fails2.stan', line 45, column 4 to column 15)"}; + int first_capture(const std::vector& y_i, std::ostream* pstream__) { using local_scalar_t__ = double; @@ -25992,6 +26237,7 @@ first_capture(const std::vector& y_i, std::ostream* pstream__) { } struct first_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -26049,6 +26295,7 @@ last_capture(const std::vector& y_i, std::ostream* pstream__) { } struct last_capture_functor__ { +const static int num_vars__ = 0; int operator()(const std::vector& y_i, std::ostream* pstream__) const { @@ -26246,6 +26493,7 @@ prob_uncaptured(const int& nind, const int& n_occasions, } struct prob_uncaptured_functor__ { +const static int num_vars__ = 0; template Eigen::Matrix, -1, -1> @@ -26255,7 +26503,22 @@ operator()(const int& nind, const int& n_occasions, { return prob_uncaptured(nind, n_occasions, p, phi, pstream__); } -}; +template +Eigen::Matrix, -1, -1> +operator()(const int& nind, const int& n_occasions, std::ostream* pstream__, + const Eigen::Matrix& p, + const Eigen::Matrix& phi) const +{ +return prob_uncaptured(nind, n_occasions, p, phi, pstream__); +} +void set_zero_adjoints() const { } +void accumulate_adjoints(double*) const { } +void save_varis(vari**) const { } +using ValueOf__ = prob_uncaptured_functor__; +using DeepCopy__ = ValueOf__; +using captured_scalar_t__ = double;}; + class lcm_fails2_model : public model_base_crtp { @@ -27759,6 +28022,13 @@ stan::model::model_base& new_model( #include namespace off_dce_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -27890,6 +28160,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class off_dce_model : public model_base_crtp { private: @@ -28885,6 +29157,13 @@ stan::model::model_base& new_model( #include namespace off_small_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -29000,6 +29279,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class off_small_model : public model_base_crtp { private: @@ -29986,6 +30267,13 @@ stan::model::model_base& new_model( #include namespace optimizations_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -30168,6 +30456,7 @@ static const std::vector locations_array__ = {" (found before start of p " (in 'optimizations.stan', line 16, column 8 to column 18)"}; + template void @@ -30197,6 +30486,7 @@ nrfun_lp(const T0__& x, const int& y, T_lp__& lp__, T_lp_accum__& lp_accum__, } struct nrfun_lp_functor__ { +const static int num_vars__ = 0; template void @@ -30237,6 +30527,7 @@ rfun(const int& y, std::ostream* pstream__) { } struct rfun_functor__ { +const static int num_vars__ = 0; int operator()(const int& y, std::ostream* pstream__) const { @@ -30267,6 +30558,7 @@ rfun_lp(T_lp__& lp__, T_lp_accum__& lp_accum__, std::ostream* pstream__) { } struct rfun_lp_functor__ { +const static int num_vars__ = 0; template int operator()(T_lp__& lp__, T_lp_accum__& lp_accum__, std::ostream* pstream__) const @@ -30275,6 +30567,7 @@ return rfun_lp(lp__, lp_accum__, pstream__); } }; + class optimizations_model : public model_base_crtp { private: @@ -31557,6 +31850,13 @@ stan::model::model_base& new_model( #include namespace partial_eval_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -31665,6 +31965,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class partial_eval_model : public model_base_crtp { private: @@ -32470,6 +32772,13 @@ stan::model::model_base& new_model( #include namespace stalled1_failure_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -32585,6 +32894,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class stalled1_failure_model : public model_base_crtp { private: @@ -33825,6 +34136,13 @@ assignment operator <- is deprecated in the Stan language; use = instead. #include namespace unroll_limit_model_namespace { +void FnZeroAdjoint__(double) { } +void FnZeroAdjoint__(stan::math::var x) { x->set_zero_adjoint(); } +double FnGetAdjoint__(double) { return 0.0; } +double FnGetAdjoint__(stan::math::var x) { return x.adj(); } +stan::math::vari* FnGetVariPtr__(double) { throw new std::runtime_error("value is not autodiffable"); } +stan::math::vari* FnGetVariPtr__(stan::math::var x) { return x.vi_; } + template std::vector resize_to_match__(std::vector& dst, const std::vector& src) { dst.resize(src.size()); @@ -33928,6 +34246,8 @@ static const std::vector locations_array__ = {" (found before start of p + + class unroll_limit_model : public model_base_crtp { private: diff --git a/test/integration/signatures/stan_math_sigs.expected b/test/integration/signatures/stan_math_sigs.expected index 5766bcd0ca..319656270c 100644 --- a/test/integration/signatures/stan_math_sigs.expected +++ b/test/integration/signatures/stan_math_sigs.expected @@ -7729,6 +7729,14 @@ real[,] integrate_ode_adams((real, real[], real[], data real[], data int[]) => r real[,] integrate_ode_adams((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], real[], int[], real, real, real) +vector[] integrate_ode_bdf((real, vector) => vector, vector, real, real[]) +vector[] integrate_ode_bdf((real, vector) => vector, vector, real, real[], + real, real, real) +vector[] integrate_ode_bdf((real, vector, real[], data real[], data int[]) => vector, + vector, real, real[], real[], real[], int[]) +vector[] integrate_ode_bdf((real, vector, real[], data real[], data int[]) => vector, + vector, real, real[], real[], real[], int[], + real, real, real) real[,] integrate_ode_bdf((real, real[], real[], data real[], data int[]) => real[], real[], real, real[], real[], real[], int[]) real[,] integrate_ode_bdf((real, real[], real[], data real[], data int[]) => real[], diff --git a/test/unit/Optimize.ml b/test/unit/Optimize.ml index 5ea2ab280a..4d19b6b6d6 100644 --- a/test/unit/Optimize.ml +++ b/test/unit/Optimize.ml @@ -263,7 +263,8 @@ let%expect_test "list collapsing" = print_s [%sexp (mir : Middle.Program.Typed.t)] ; [%expect {| - ((functions_block + ((closures ()) + (functions_block (((fdrt ()) (fdname f) (fdargs ((AutoDiffable x UInt) (AutoDiffable y UMatrix))) (fdbody @@ -3193,7 +3194,7 @@ let%expect_test "block fixing" = print_s [%sexp (mir : Program.Typed.t)] ; [%expect {| - ((functions_block ()) (input_vars ()) (prepare_data ()) + ((closures ()) (functions_block ()) (input_vars ()) (prepare_data ()) (log_prob (((pattern (IfElse diff --git a/test/unit/Stan_math_code_gen_tests.ml b/test/unit/Stan_math_code_gen_tests.ml index a6000afd0a..493d83db9a 100644 --- a/test/unit/Stan_math_code_gen_tests.ml +++ b/test/unit/Stan_math_code_gen_tests.ml @@ -9,7 +9,7 @@ let%expect_test "udf" = Stmt.Fixed.{pattern= stmt; meta= Locations.no_span_num} in let w e = Expr.{Fixed.pattern= e; meta= Typed.Meta.empty} in - let pp_fun_def_w_rs a b = pp_fun_def a b String.Set.empty in + let pp_fun_def_w_rs a b = pp_fun_def a b false String.Set.empty in { fdrt= None ; fdname= "sars" ; fdargs= [(DataOnly, "x", UMatrix); (AutoDiffable, "y", URowVector)] @@ -44,6 +44,7 @@ let%expect_test "udf" = } struct sars_functor__ { + const static int num_vars__ = 0; template void operator()(const Eigen::Matrix& x,