Stan  2.14.0
probability, sampling & optimization
has_non_param_var_vis_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_AST_FUN_HAS_NON_PARAM_VAR_VIS_DEF_HPP
2 #define STAN_LANG_AST_FUN_HAS_NON_PARAM_VAR_VIS_DEF_HPP
3 
4 #include <stan/lang/ast.hpp>
5 #include <boost/variant/apply_visitor.hpp>
6 #include <string>
7 
8 namespace stan {
9  namespace lang {
10 
11  bool is_linear_function(const std::string& name) {
12  return name == "add"
13  || name == "block"
14  || name == "append_col"
15  || name == "col"
16  || name == "cols"
17  || name == "diagonal"
18  || name == "head"
19  || name == "minus"
20  || name == "negative_infinity"
21  || name == "not_a_number"
22  || name == "append_row"
23  || name == "rep_matrix"
24  || name == "rep_row_vector"
25  || name == "rep_vector"
26  || name == "row"
27  || name == "rows"
28  || name == "positive_infinity"
29  || name == "segment"
30  || name == "subtract"
31  || name == "sum"
32  || name == "tail"
33  || name == "to_vector"
34  || name == "to_row_vector"
35  || name == "to_matrix"
36  || name == "to_array_1d"
37  || name == "to_array_2d"
38  || name == "transpose";
39  }
40 
42  : var_map_(var_map) {
43  }
44 
45  bool has_non_param_var_vis::operator()(const nil& e) const {
46  return false;
47  }
48 
50  return false;
51  }
52 
54  return false;
55  }
56 
58  for (size_t i = 0; i < e.args_.size(); ++i)
59  if (boost::apply_visitor(*this, e.args_[i].expr_))
60  return true;
61  return false;
62  }
63 
66  return vo == transformed_parameter_origin
67  || vo == local_origin;
68  }
69 
71  // if any vars, return true because integration will be nonlinear
72  return boost::apply_visitor(*this, e.y0_.expr_)
73  || boost::apply_visitor(*this, e.theta_.expr_);
74  }
75 
77  const {
78  // if any vars, return true because integration will be nonlinear
79  return boost::apply_visitor(*this, e.y0_.expr_)
80  || boost::apply_visitor(*this, e.theta_.expr_);
81  }
82 
83  bool has_non_param_var_vis::operator()(const fun& e) const {
84  // any function applied to non-linearly transformed var
85  for (size_t i = 0; i < e.args_.size(); ++i)
86  if (boost::apply_visitor(*this, e.args_[i].expr_))
87  return true;
88  // non-linear function applied to var
89  if (!is_linear_function(e.name_)) {
90  for (size_t i = 0; i < e.args_.size(); ++i)
91  if (has_var(e.args_[i], var_map_))
92  return true;
93  }
94  return false;
95  }
96 
98  return boost::apply_visitor(*this, e.expr_.expr_);
99  }
100 
102  return boost::apply_visitor(*this, e.expr_.expr_);
103  }
104 
109  return true;
110  return false;
111  }
112 
114  if (e.op == "||"
115  || e.op == "&&"
116  || e.op == "=="
117  || e.op == "!="
118  || e.op == "<"
119  || e.op == "<="
120  || e.op == ">"
121  || e.op == ">=")
122  return true;
125  return true;
126  if (e.op == "*" || e.op == "/")
127  return has_var(e.left, var_map_) && has_var(e.right, var_map_);
128  return false;
129  }
130 
132  // only negation, which is linear, so recurse
134  }
135 
136  }
137 }
138 #endif
has_non_param_var_vis(const variable_map &var_map)
Construct the visitor with the specified global variable declaration mapping.
std::string name_
Name of variable.
Definition: variable.hpp:18
std::vector< expression > args_
Sequence of expressions for array values.
Definition: array_expr.hpp:21
Node for holding a double literal.
Probability, optimization and sampling library.
expression y0_
Initial state (array of real).
Structure to hold an array expression.
Definition: array_expr.hpp:17
Structure for an indexed expression.
Definition: index_op.hpp:14
std::vector< expression > args_
Sequence of argument expressions for function.
Definition: fun.hpp:32
Structure for function application.
Definition: fun.hpp:17
const int transformed_parameter_origin
The origin of the variable is the transformed parameter block.
Definition: var_origin.hpp:37
expression expr_
Expression being indexed.
Definition: index_op.hpp:18
expression y0_
Initial state.
A map from function names to their base declarations and their origin.
Structure for integrate diff eq statement.
bool operator()(const nil &e) const
Return true if the specified expression contains a variable not declared as a parameter.
expression cond_
Condition (integer).
expression true_val_
Return value if condition is true.
std::string name_
Name of function being applied.
Definition: fun.hpp:21
expression left
First argument.
Definition: binary_op.hpp:24
var_origin get_origin(const std::string &name) const
Return the origin of the variable declaration for the variable with the specified name...
expression theta_
Parameters.
int var_origin
The type of a variable indicating where a variable was declared.
Definition: var_origin.hpp:12
bool has_non_param_var(const expression &e, const variable_map &var_map)
Returns true if the specified expression contains a variable that requires a Jacobian warning...
bool has_var(const expression &e, const variable_map &var_map)
Returns true if the specified expression contains a variable that is defined as a parameter...
Definition: has_var_def.hpp:10
bool is_linear_function(const std::string &name)
AST structure for holding an expression with a sequence of indexes.
Structure for the conditional operator.
Structure to hold a variable.
Definition: variable.hpp:14
expression right
Second argument.
Definition: binary_op.hpp:29
const variable_map & var_map_
Reference to global variable declaration map.
expression subject
Argument.
Definition: unary_op.hpp:23
const int local_origin
The origin of the variable is as a local variable.
Definition: var_origin.hpp:47
expression theta_
Parameters (array of real).
AST structure for unary operations consisting of an operation and argument.
Definition: unary_op.hpp:14
std::string op
String representation of the operation.
Definition: binary_op.hpp:19
Node for storing binary operations consisting of an operation and left and right arguments.
Definition: binary_op.hpp:15
The nil structure used as a placeholder for undefined or empty values in several structures.
Definition: nil.hpp:11
expression false_val_
Return value if condition is false.
Structure for a diff eq integration statement with control parameters for the integrator.
expression expr_
Expression being indexed.

     [ Stan Home Page ] © 2011–2016, Stan Development Team.