Stan  1.0
probability, sampling & optimization
ast_def.cpp
Go to the documentation of this file.
1 #ifndef __STAN__GM__AST_DEF_HPP__
2 #define __STAN__GM__AST_DEF_HPP__
3 
4 #include <boost/variant/apply_visitor.hpp>
5 #include <boost/variant/recursive_variant.hpp>
6 
7 #include <cstddef>
8 #include <limits>
9 #include <climits>
10 #include <iostream>
11 #include <map>
12 #include <stdexcept>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include <stan/gm/ast.hpp>
18 
19 namespace stan {
20 
21  namespace gm {
22 
23  std::ostream& write_base_expr_type(std::ostream& o, base_expr_type type) {
24  switch (type) {
25  case INT_T :
26  o << "int";
27  break;
28  case DOUBLE_T :
29  o << "real";
30  break;
31  case VECTOR_T :
32  o << "vector";
33  break;
34  case ROW_VECTOR_T :
35  o << "row vector";
36  break;
37  case MATRIX_T :
38  o << "matrix";
39  break;
40  case ILL_FORMED_T :
41  o << "ill formed";
42  break;
43  default:
44  o << "UNKNOWN";
45  }
46  return o;
47  }
48 
49  // expr_type ctors and methods
51  : base_type_(ILL_FORMED_T),
52  num_dims_(0) {
53  }
55  : base_type_(base_type),
56  num_dims_(0) {
57  }
59  size_t num_dims)
60  : base_type_(base_type),
61  num_dims_(num_dims) {
62  }
63  bool expr_type::operator==(const expr_type& et) const {
64  return base_type_ == et.base_type_
65  && num_dims_ == et.num_dims_;
66  }
67  bool expr_type::operator!=(const expr_type& et) const {
68  return !(*this == et);
69  }
70  bool expr_type::is_primitive() const {
71  return is_primitive_int()
73  }
75  return base_type_ == INT_T
76  && num_dims_ == 0U;
77  }
79  return base_type_ == DOUBLE_T
80  && num_dims_ == 0U;
81  }
82  bool expr_type::is_ill_formed() const {
83  return base_type_ == ILL_FORMED_T;
84  }
86  return base_type_;
87  }
88  size_t expr_type::num_dims() const {
89  return num_dims_;
90  }
91 
92  std::ostream& operator<<(std::ostream& o, const expr_type& et) {
93  write_base_expr_type(o,et.type());
94  if (et.num_dims() > 0)
95  o << '[' << et.num_dims() << ']';
96  return o;
97  }
98 
100  if (!et.is_primitive())
101  return expr_type();
102  return et;
103  }
104 
106  const expr_type& et2) {
107  if (!et1.is_primitive() || !et2.is_primitive())
108  return expr_type();
109  return et1.type() == DOUBLE_T ? et1 : et2;
110  }
111 
113  // FIXME: for threaded models, requires double-check lock
114  if (!sigs_)
115  sigs_ = new function_signatures;
116  return *sigs_;
117  }
118  void function_signatures::add(const std::string& name,
119  const expr_type& result_type,
120  const std::vector<expr_type>& arg_types) {
121  sigs_map_[name].push_back(function_signature_t(result_type,arg_types));
122 
123  }
124  void function_signatures::add(const std::string& name,
125  const expr_type& result_type) {
126  std::vector<expr_type> arg_types;
127  add(name,result_type,arg_types);
128  }
129  void function_signatures::add(const std::string& name,
130  const expr_type& result_type,
131  const expr_type& arg_type) {
132  std::vector<expr_type> arg_types;
133  arg_types.push_back(arg_type);
134  add(name,result_type,arg_types);
135  }
136  void function_signatures::add(const std::string& name,
137  const expr_type& result_type,
138  const expr_type& arg_type1,
139  const expr_type& arg_type2) {
140  std::vector<expr_type> arg_types;
141  arg_types.push_back(arg_type1);
142  arg_types.push_back(arg_type2);
143  add(name,result_type,arg_types);
144  }
145  void function_signatures::add(const std::string& name,
146  const expr_type& result_type,
147  const expr_type& arg_type1,
148  const expr_type& arg_type2,
149  const expr_type& arg_type3) {
150  std::vector<expr_type> arg_types;
151  arg_types.push_back(arg_type1);
152  arg_types.push_back(arg_type2);
153  arg_types.push_back(arg_type3);
154  add(name,result_type,arg_types);
155  }
156  void function_signatures::add(const std::string& name,
157  const expr_type& result_type,
158  const expr_type& arg_type1,
159  const expr_type& arg_type2,
160  const expr_type& arg_type3,
161  const expr_type& arg_type4) {
162  std::vector<expr_type> arg_types;
163  arg_types.push_back(arg_type1);
164  arg_types.push_back(arg_type2);
165  arg_types.push_back(arg_type3);
166  arg_types.push_back(arg_type4);
167  add(name,result_type,arg_types);
168  }
169  void function_signatures::add(const std::string& name,
170  const expr_type& result_type,
171  const expr_type& arg_type1,
172  const expr_type& arg_type2,
173  const expr_type& arg_type3,
174  const expr_type& arg_type4,
175  const expr_type& arg_type5) {
176  std::vector<expr_type> arg_types;
177  arg_types.push_back(arg_type1);
178  arg_types.push_back(arg_type2);
179  arg_types.push_back(arg_type3);
180  arg_types.push_back(arg_type4);
181  arg_types.push_back(arg_type5);
182  add(name,result_type,arg_types);
183  }
184  void function_signatures::add_nullary(const::std::string& name) {
185  add(name,DOUBLE_T);
186  }
187  void function_signatures::add_unary(const::std::string& name) {
188  add(name,DOUBLE_T,DOUBLE_T);
189  }
190  void function_signatures::add_binary(const::std::string& name) {
192  }
193  void function_signatures::add_ternary(const::std::string& name) {
195  }
196  void function_signatures::add_quaternary(const::std::string& name) {
198  }
200  const std::vector<expr_type>& call_args,
201  const std::vector<expr_type>& sig_args) {
202  if (call_args.size() != sig_args.size()) {
203  return -1; // failure
204  }
205  int num_promotions = 0;
206  for (size_t i = 0; i < call_args.size(); ++i) {
207  if (call_args[i] == sig_args[i]) {
208  continue;
209  } else if (call_args[i].is_primitive_int()
210  && sig_args[i].is_primitive_double()) {
211  ++num_promotions;
212  } else {
213  return -1; // failed match
214  }
215  }
216  return num_promotions;
217  }
219  const std::string& name,
220  const std::vector<expr_type>& args,
221  std::ostream& error_msgs) {
222  std::vector<function_signature_t> signatures = sigs_map_[name];
223  size_t match_index = 0;
224  size_t min_promotions = std::numeric_limits<size_t>::max();
225  size_t num_matches = 0;
226 
227  for (size_t i = 0; i < signatures.size(); ++i) {
228  int promotions = num_promotions(args,signatures[i].second);
229  if (promotions < 0) continue; // no match
230  size_t promotions_ui = static_cast<size_t>(promotions);
231  if (promotions_ui < min_promotions) {
232  min_promotions = promotions_ui;
233  match_index = i;
234  num_matches = 1;
235  } else if (promotions_ui == min_promotions) {
236  ++num_matches;
237  }
238  }
239 
240  if (num_matches == 1) {
241  return signatures[match_index].first;
242  } else if (num_matches == 0) {
243  error_msgs << "no matches for function name=\"" << name << "\""
244  << std::endl;
245  } else {
246  error_msgs << num_matches << " matches with "
247  << min_promotions << " integer promotions "
248  << "for function name=\"" << name << "\"" << std::endl;
249  }
250  for (size_t i = 0; i < args.size(); ++i)
251  error_msgs << " arg " << i << " type=" << args[i] << std::endl;
252  return expr_type(); // ill-formed dummy
253  }
254  function_signatures::function_signatures() {
256  }
257  function_signatures* function_signatures::sigs_ = 0;
258 
259 
260 
262  statements::statements(const std::vector<var_decl>& local_decl,
263  const std::vector<statement>& stmts)
264  : local_decl_(local_decl),
265  statements_(stmts) {
266  }
267 
269  return expr_type();
270  }
271  // template <typename T>
272  // expr_type expression_type_vis::operator()(const T& e) const {
273  // return e.type_;
274  // }
276  return e.type_;
277  }
279  return e.type_;
280  }
282  return e.type_;
283  }
285  return e.type_;
286  }
288  return e.type_;
289  }
291  return e.type_;
292  }
294  return e.type_;
295  }
297  return e.type_;
298  }
299 
301  : expr_(nil()) {
302  }
304  : expr_(e.expr_) {
305  }
308  return boost::apply_visitor(vis,expr_);
309  }
310  // template <typename Expr>
311  // expression::expression(const Expr& expr) : expr_(expr) { }
312 
313  expression::expression(const expression_t& expr) : expr_(expr) { }
314  expression::expression(const nil& expr) : expr_(expr) { }
315  expression::expression(const int_literal& expr) : expr_(expr) { }
316  expression::expression(const double_literal& expr) : expr_(expr) { }
317  expression::expression(const array_literal& expr) : expr_(expr) { }
318  expression::expression(const variable& expr) : expr_(expr) { }
319  expression::expression(const fun& expr) : expr_(expr) { }
320  expression::expression(const index_op& expr) : expr_(expr) { }
321  expression::expression(const binary_op& expr) : expr_(expr) { }
322  expression::expression(const unary_op& expr) : expr_(expr) { }
323 
324  printable::printable() : printable_("") { }
325  printable::printable(const expression& expr) : printable_(expr) { }
326  printable::printable(const std::string& msg) : printable_(msg) { }
328  : printable_(printable) { }
330  : printable_(printable.printable_) { }
331 
332  // contains_var::contains_var(const variable_map& var_map)
333  // : var_map_(var_map) {
334  // }
335  // bool contains_var::operator()(const nil& e) const {
336  // return false;
337  // }
338  // bool contains_var::operator()(const int_literal& e) const {
339  // return false;
340  // }
341  // bool contains_var::operator()(const double_literal& e) const {
342  // return false;
343  // }
344  // bool contains_var::operator()(const array_literal& e) const {
345  // for (size_t i = 0; i < e.args_.size(); ++i)
346  // if (boost::apply_visitor(*this,e.args_[i]);
347  // return true;
348  // return false;
349  // }
350  // bool contains_var::operator()(const variable& e) const {
351  // var_origin vo = variable_map_.get_origin(e.name_);
352  // return vo == parameter_origin
353  // || vo == transformed_paramter_origin
354  // || vo == local_origin;
355  // }
356  // bool contains_var::operator()(const fun& e) const {
357  // for (size_t i = 0; i < e.args_.size(); ++i)
358  // if (boost::apply_visitor(*this,e.args_[i]))
359  // return true;
360  // return false;
361  // }
362  // bool contains_var::operator()(const index_op& e) const {
363  // if (boost::apply_visitor(*this,e.expr_))
364  // return true;
365  // for (size_t i = 0; i < e.dimss_.size(); ++i)
366  // for (size_t j = 0; j < e.dimss_[i].size(); ++j)
367  // if (boost::apply_visitor(*this,e.dims_[i][j]))
368  // return true;
369  // return false;
370  // }
371  // bool contains_var::operator()(const binary_op& e) const {
372  // return boost::apply_visitor(*this,e.left)
373  // || apply_visitor(*this,e.right);
374  // }
375  // bool contains_var::operator()(const unary_op& e) const {
376  // return boost::apply_visitor(*this,e.subject);
377  // }
378 
379  bool is_nil_op::operator()(const nil& x) const { return true; }
380  bool is_nil_op::operator()(const int_literal& x) const { return false; }
381  bool is_nil_op::operator()(const double_literal& x) const { return false; }
382  bool is_nil_op::operator()(const array_literal& x) const { return false; }
383  bool is_nil_op::operator()(const variable& x) const { return false; }
384  bool is_nil_op::operator()(const fun& x) const { return false; }
385  bool is_nil_op::operator()(const index_op& x) const { return false; }
386  bool is_nil_op::operator()(const binary_op& x) const { return false; }
387  bool is_nil_op::operator()(const unary_op& x) const { return false; }
388 
389  // template <typename T>
390  // bool is_nil_op::operator()(const T& x) const { return false; }
391 
392  bool is_nil(const expression& e) {
393  is_nil_op ino;
394  return boost::apply_visitor(ino,e.expr_);
395  }
396 
397  variable_dims::variable_dims() { } // req for FUSION_ADAPT
398  variable_dims::variable_dims(std::string const& name,
399  std::vector<expression> const& dims)
400  : name_(name),
401  dims_(dims) {
402  }
403 
404 
406  : type_(INT_T) {
407  }
409  : val_(val),
410  type_(INT_T) {
411  }
413  : val_(il.val_),
414  type_(il.type_) {
415  }
417  val_ = il.val_;
418  type_ = il.type_;
419  return *this;
420  }
421 
422 
424  : type_(DOUBLE_T,0U) {
425  }
427  : val_(val),
428  type_(DOUBLE_T,0U) {
429  }
431  val_ = dl.val_;
432  type_ = dl.type_;
433  return *this;
434  }
435 
436 
438  : args_(),
439  type_(DOUBLE_T,1U) {
440  }
441  array_literal::array_literal(const std::vector<expression>& args)
442  : args_(args),
443  type_() { // ill-formed w/o help
444  }
446  args_ = al.args_;
447  type_ = al.type_;
448  return *this;
449  }
450 
452  variable::variable(std::string name) : name_(name) { }
453  void variable::set_type(const base_expr_type& base_type,
454  size_t num_dims) {
455  type_ = expr_type(base_type, num_dims);
456  }
457 
458 
459  fun::fun() { }
460  fun::fun(std::string const& name,
461  std::vector<expression> const& args)
462  : name_(name),
463  args_(args) {
464  infer_type();
465  }
467  // FIXME: remove this useless function and any calls to it
468  }
469 
470 
471  size_t total_dims(const std::vector<std::vector<expression> >& dimss) {
472  size_t total = 0U;
473  for (size_t i = 0; i < dimss.size(); ++i)
474  total += dimss[i].size();
475  return total;
476  }
477 
478 
480  size_t num_expr_dims,
481  size_t num_index_dims) {
482  if (num_index_dims <= num_expr_dims)
483  return expr_type(expr_base_type,num_expr_dims - num_index_dims);
484  if (num_index_dims == (num_expr_dims + 1)) {
485  if (expr_base_type == VECTOR_T || expr_base_type == ROW_VECTOR_T)
486  return expr_type(DOUBLE_T,0U);
487  if (expr_base_type == MATRIX_T)
488  return expr_type(ROW_VECTOR_T,0U);
489  }
490  if (num_index_dims == (num_expr_dims + 2))
491  if (expr_base_type == MATRIX_T)
492  return expr_type(DOUBLE_T,0U);
493 
494  // error condition, result expr_type has is_ill_formed() = true
495  return expr_type();
496  }
497 
499  size_t num_index_dims) {
501  expr.expression_type().num_dims(),
502  num_index_dims);
503  }
504 
505 
508  const std::vector<std::vector<expression> >& dimss)
509  : expr_(expr),
510  dimss_(dimss) {
511  infer_type();
512  }
515  }
516 
519  const std::string& op,
520  const expression& right)
521  : op(op),
522  left(left),
523  right(right),
524  type_(promote_primitive(left.expression_type(),
525  right.expression_type())) {
526  }
527 
528 
530  expression const& subject)
531  : op(op),
532  subject(subject),
533  type_(promote_primitive(subject.expression_type())) {
534  }
535 
536 
539  expression const& high)
540  : low_(low),
541  high_(high) {
542  }
543  bool range::has_low() const {
544  return !is_nil(low_.expr_);
545  }
546  bool range::has_high() const {
547  return !is_nil(high_.expr_);
548  }
549 
550  void print_var_origin(std::ostream& o, const var_origin& vo) {
551  if (vo == data_origin)
552  o << "data";
553  else if (vo == transformed_data_origin)
554  o << "transformed data";
555  else if (vo == parameter_origin)
556  o << "parameter";
557  else if (vo == transformed_parameter_origin)
558  o << "transformed parameter";
559  else if (vo == derived_origin)
560  o << "derived";
561  else if (vo == local_origin)
562  o << "local";
563  else
564  o << "UNKNOWN ORIGIN";
565  }
566 
567 
570  : base_type_(base_type) {
571  }
572  base_var_decl::base_var_decl(const std::string& name,
573  const std::vector<expression>& dims,
574  const base_expr_type& base_type)
575  : name_(name),
576  dims_(dims),
577  base_type_(base_type) {
578  }
579 
580  bool variable_map::exists(const std::string& name) const {
581  return map_.find(name) != map_.end();
582  }
583  base_var_decl variable_map::get(const std::string& name) const {
584  if (!exists(name))
585  throw std::invalid_argument("variable does not exist");
586  return map_.find(name)->second.first;
587  }
588  base_expr_type variable_map::get_base_type(const std::string& name) const {
589  return get(name).base_type_;
590  }
591  size_t variable_map::get_num_dims(const std::string& name) const {
592  return get(name).dims_.size();
593  }
594  var_origin variable_map::get_origin(const std::string& name) const {
595  if (!exists(name))
596  throw std::invalid_argument("variable does not exist");
597  return map_.find(name)->second.second;
598  }
599  void variable_map::add(const std::string& name,
600  const base_var_decl& base_decl,
601  const var_origin& vo) {
602  map_[name] = range_t(base_decl,vo);
603  }
604  void variable_map::remove(const std::string& name) {
605  map_.erase(name);
606  }
607 
609  : base_var_decl(INT_T)
610  { }
611 
613  std::string const& name,
614  std::vector<expression> const& dims)
615  : base_var_decl(name,dims,INT_T),
616  range_(range)
617  { }
618 
619 
620 
623  { }
624 
626  std::string const& name,
627  std::vector<expression> const& dims)
628  : base_var_decl(name,dims,DOUBLE_T),
629  range_(range)
630  { }
631 
634  { }
635 
637  std::string const& name,
638  std::vector<expression> const& dims)
639  : base_var_decl(name,dims,VECTOR_T),
640  K_(K)
641  { }
642 
645  { }
646 
648  std::string const& name,
649  std::vector<expression> const& dims)
650  : base_var_decl(name,dims,VECTOR_T),
651  K_(K) {
652  }
653 
656  { }
657 
659  std::string const& name,
660  std::vector<expression> const& dims)
661  : base_var_decl(name,dims,VECTOR_T),
662  K_(K) {
663  }
664 
666 
668  expression const& M,
669  std::string const& name,
670  std::vector<expression> const& dims)
671  : base_var_decl(name,dims,VECTOR_T),
672  range_(range),
673  M_(M) {
674  }
675 
678  expression const& N,
679  std::string const& name,
680  std::vector<expression> const& dims)
681  : base_var_decl(name,dims,ROW_VECTOR_T),
682  range_(range),
683  N_(N) {
684  }
685 
688  expression const& M,
689  expression const& N,
690  std::string const& name,
691  std::vector<expression> const& dims)
692  : base_var_decl(name,dims,MATRIX_T),
693  range_(range),
694  M_(M),
695  N_(N) {
696  }
697 
698 
700  }
702  std::string const& name,
703  std::vector<expression> const& dims)
704  : base_var_decl(name,dims,MATRIX_T),
705  K_(K) {
706  }
707 
710  std::string const& name,
711  std::vector<expression> const& dims)
712  : base_var_decl(name,dims,MATRIX_T),
713  K_(K) {
714  }
715 
716 
717 
718 
720  std::string name_vis::operator()(const nil& x) const {
721  return ""; // fail if arises
722  }
723  std::string name_vis::operator()(const int_var_decl& x) const {
724  return x.name_;
725  }
726  std::string name_vis::operator()(const double_var_decl& x) const {
727  return x.name_;
728  }
729  std::string name_vis::operator()(const vector_var_decl& x) const {
730  return x.name_;
731  }
732  std::string name_vis::operator()(const row_vector_var_decl& x) const {
733  return x.name_;
734  }
735  std::string name_vis::operator()(const matrix_var_decl& x) const {
736  return x.name_;
737  }
738  std::string name_vis::operator()(const simplex_var_decl& x) const {
739  return x.name_;
740  }
741  std::string name_vis::operator()(const ordered_var_decl& x) const {
742  return x.name_;
743  }
744  std::string name_vis::operator()(const positive_ordered_var_decl& x) const {
745  return x.name_;
746  }
747  std::string name_vis::operator()(const cov_matrix_var_decl& x) const {
748  return x.name_;
749  }
750  std::string name_vis::operator()(const corr_matrix_var_decl& x) const {
751  return x.name_;
752  }
753 
754 
755 
756 
757 
758 
759  var_decl::var_decl(const var_decl_t& decl) : decl_(decl) { }
760  var_decl::var_decl() : decl_(nil()) { }
761  var_decl::var_decl(const nil& decl) : decl_(decl) { }
762  var_decl::var_decl(const int_var_decl& decl) : decl_(decl) { }
763  var_decl::var_decl(const double_var_decl& decl) : decl_(decl) { }
764  var_decl::var_decl(const vector_var_decl& decl) : decl_(decl) { }
765  var_decl::var_decl(const row_vector_var_decl& decl) : decl_(decl) { }
766  var_decl::var_decl(const matrix_var_decl& decl) : decl_(decl) { }
767  var_decl::var_decl(const simplex_var_decl& decl) : decl_(decl) { }
768  var_decl::var_decl(const ordered_var_decl& decl) : decl_(decl) { }
769  var_decl::var_decl(const positive_ordered_var_decl& decl) : decl_(decl) { }
770  var_decl::var_decl(const cov_matrix_var_decl& decl) : decl_(decl) { }
771  var_decl::var_decl(const corr_matrix_var_decl& decl) : decl_(decl) { }
772 
773  // template <typename Decl>
774  // var_decl::var_decl(Decl const& decl) : decl_(decl) { }
775 
776 
777 
778  std::string var_decl::name() const {
779  return boost::apply_visitor(name_vis(),decl_);
780  }
781 
782  statement::statement() : statement_(nil()) { }
783 
784  // FIXME: template these out
785  statement::statement(const statement_t& st) : statement_(st) { }
786  statement::statement(const nil& st) : statement_(st) { }
787  statement::statement(const assignment& st) : statement_(st) { }
788  statement::statement(const sample& st) : statement_(st) { }
789  statement::statement(const statements& st) : statement_(st) { }
790  statement::statement(const for_statement& st) : statement_(st) { }
791  statement::statement(const while_statement& st) : statement_(st) { }
792  statement::statement(const conditional_statement& st) : statement_(st) { }
793  statement::statement(const print_statement& st) : statement_(st) { }
794  statement::statement(const no_op_statement& st) : statement_(st) { }
795 
796  // template <typename Statement>
797  // statement::statement(const Statement& statement)
798  // : statement_(statement) {
799  // }
800 
801 
802 
803 
805  }
807  range& range,
808  statement& stmt)
809  : variable_(variable),
810  range_(range),
811  statement_(stmt) {
812  }
813 
815  }
817  const statement& body)
818  : condition_(condition),
819  body_(body) {
820  }
821 
823  }
825  ::conditional_statement(const std::vector<expression>& conditions,
826  const std::vector<statement>& bodies)
827  : conditions_(conditions),
828  bodies_(bodies) {
829  }
830 
832 
833  print_statement::print_statement(const std::vector<printable>& printables)
834  : printables_(printables) {
835  }
836 
838  program::program(const std::vector<var_decl>& data_decl,
839  const std::pair<std::vector<var_decl>,
840  std::vector<statement> >& derived_data_decl,
841  const std::vector<var_decl>& parameter_decl,
842  const std::pair<std::vector<var_decl>,
843  std::vector<statement> >& derived_decl,
844  const statement& st,
845  const std::pair<std::vector<var_decl>,
846  std::vector<statement> >& generated_decl)
847  : data_decl_(data_decl),
848  derived_data_decl_(derived_data_decl),
849  parameter_decl_(parameter_decl),
850  derived_decl_(derived_decl),
851  statement_(st),
852  generated_decl_(generated_decl) {
853  }
854 
856  }
858  distribution& dist)
859  : expr_(e),
860  dist_(dist) {
861  }
862  bool sample::is_ill_formed() const {
864  || ( truncation_.has_low()
865  && expr_.expression_type()
867  || ( truncation_.has_high()
868  && expr_.expression_type()
870  }
871 
873  }
875  expression& expr)
876  : var_dims_(var_dims),
877  expr_(expr) {
878  }
879 
881  expr_ = binary_op(expr_, "+", rhs);
882  return *this;
883  }
884 
886  expr_ = binary_op(expr_, "-", rhs);
887  return *this;
888  }
889 
891  expr_ = binary_op(expr_, "*", rhs);
892  return *this;
893  }
894 
896  expr_ = binary_op(expr_, "/", rhs);
897  return *this;
898  }
899 
900 
901  }
902 }
903 
904 
905 #endif
void add_ternary(const ::std::string &name)
Definition: ast_def.cpp:193
void add(const std::string &name, const expr_type &result_type, const std::vector< expr_type > &arg_types)
Definition: ast_def.cpp:118
void add_binary(const ::std::string &name)
Definition: ast_def.cpp:190
void add_nullary(const ::std::string &name)
Definition: ast_def.cpp:184
void add_quaternary(const ::std::string &name)
Definition: ast_def.cpp:196
static function_signatures & instance()
Definition: ast_def.cpp:112
int num_promotions(const std::vector< expr_type > &call_args, const std::vector< expr_type > &sig_args)
Definition: ast_def.cpp:199
void add_unary(const ::std::string &name)
Definition: ast_def.cpp:187
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs)
Definition: ast_def.cpp:218
size_t total_dims(const std::vector< std::vector< expression > > &dimss)
Definition: ast_def.cpp:471
const int parameter_origin
Definition: ast.hpp:361
expr_type infer_type_indexing(const base_expr_type &expr_base_type, size_t num_expr_dims, size_t num_index_dims)
Definition: ast_def.cpp:479
const int ROW_VECTOR_T
Definition: ast.hpp:57
std::ostream & write_base_expr_type(std::ostream &o, base_expr_type type)
Definition: ast_def.cpp:23
const int INT_T
Definition: ast.hpp:54
int var_origin
Definition: ast.hpp:358
const int ILL_FORMED_T
Definition: ast.hpp:59
expr_type promote_primitive(const expr_type &et)
Definition: ast_def.cpp:99
const int transformed_data_origin
Definition: ast.hpp:360
const int DOUBLE_T
Definition: ast.hpp:55
bool is_nil(const expression &e)
Definition: ast_def.cpp:392
const int VECTOR_T
Definition: ast.hpp:56
const int data_origin
Definition: ast.hpp:359
std::pair< expr_type, std::vector< expr_type > > function_signature_t
Definition: ast.hpp:87
const int transformed_parameter_origin
Definition: ast.hpp:362
int base_expr_type
Definition: ast.hpp:50
const int local_origin
Definition: ast.hpp:364
std::ostream & operator<<(std::ostream &o, const expr_type &et)
Definition: ast_def.cpp:92
const int derived_origin
Definition: ast.hpp:363
const int MATRIX_T
Definition: ast.hpp:58
void print_var_origin(std::ostream &o, const var_origin &vo)
Definition: ast_def.cpp:550
double e()
Return the base of the natural logarithm.
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: matrix.hpp:968
double dist(const std::vector< double > &x, const std::vector< double > &y)
Definition: util.hpp:61
Probability, optimization and sampling library.
Definition: agrad.cpp:6
std::vector< expression > args_
Definition: ast.hpp:281
array_literal & operator=(const array_literal &al)
Definition: ast_def.cpp:445
std::vector< expression > dims_
Definition: ast.hpp:372
base_expr_type base_type_
Definition: ast.hpp:373
std::string name_
Definition: ast.hpp:371
double_literal & operator=(const double_literal &dl)
Definition: ast_def.cpp:430
bool is_primitive() const
Definition: ast_def.cpp:70
bool operator==(const expr_type &et) const
Definition: ast_def.cpp:63
bool operator!=(const expr_type &et) const
Definition: ast_def.cpp:67
bool is_primitive_int() const
Definition: ast_def.cpp:74
base_expr_type type() const
Definition: ast_def.cpp:85
bool is_primitive_double() const
Definition: ast_def.cpp:78
bool is_ill_formed() const
Definition: ast_def.cpp:82
size_t num_dims_
Definition: ast.hpp:65
size_t num_dims() const
Definition: ast_def.cpp:88
base_expr_type base_type_
Definition: ast.hpp:64
expr_type operator()(const nil &e) const
Definition: ast_def.cpp:268
expression & operator*=(const expression &rhs)
Definition: ast_def.cpp:890
expr_type expression_type() const
Definition: ast_def.cpp:306
expression & operator+=(const expression &rhs)
Definition: ast_def.cpp:880
expression & operator/=(const expression &rhs)
Definition: ast_def.cpp:895
expression & operator-=(const expression &rhs)
Definition: ast_def.cpp:885
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< int_literal >, boost::recursive_wrapper< double_literal >, boost::recursive_wrapper< array_literal >, boost::recursive_wrapper< variable >, boost::recursive_wrapper< fun >, boost::recursive_wrapper< index_op >, boost::recursive_wrapper< binary_op >, boost::recursive_wrapper< unary_op > > expression_t
Definition: ast.hpp:181
expression_t expr_
Definition: ast.hpp:205
void infer_type()
Definition: ast_def.cpp:466
std::vector< std::vector< expression > > dimss_
Definition: ast.hpp:319
expression expr_
Definition: ast.hpp:318
expr_type type_
Definition: ast.hpp:320
int_literal & operator=(const int_literal &il)
Definition: ast_def.cpp:416
expr_type type_
Definition: ast.hpp:264
bool operator()(const nil &x) const
Definition: ast_def.cpp:379
std::string operator()(const nil &x) const
Definition: ast_def.cpp:720
Placeholder struct for boost::variant default ctors.
Definition: ast.hpp:15
boost::variant< boost::recursive_wrapper< std::string >, boost::recursive_wrapper< expression > > printable_t
Definition: ast.hpp:225
expression high_
Definition: ast.hpp:350
bool has_low() const
Definition: ast_def.cpp:543
bool has_high() const
Definition: ast_def.cpp:546
expression low_
Definition: ast.hpp:349
bool is_ill_formed() const
Definition: ast_def.cpp:862
expression expr_
Definition: ast.hpp:641
range truncation_
Definition: ast.hpp:643
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< assignment >, boost::recursive_wrapper< sample >, boost::recursive_wrapper< statements >, boost::recursive_wrapper< for_statement >, boost::recursive_wrapper< conditional_statement >, boost::recursive_wrapper< while_statement >, boost::recursive_wrapper< print_statement >, boost::recursive_wrapper< no_op_statement > > statement_t
Definition: ast.hpp:556
unary_op(char op, expression const &subject)
Definition: ast_def.cpp:529
var_decl_t decl_
Definition: ast.hpp:524
boost::variant< boost::recursive_wrapper< nil >, boost::recursive_wrapper< int_var_decl >, boost::recursive_wrapper< double_var_decl >, boost::recursive_wrapper< vector_var_decl >, boost::recursive_wrapper< row_vector_var_decl >, boost::recursive_wrapper< matrix_var_decl >, boost::recursive_wrapper< simplex_var_decl >, boost::recursive_wrapper< ordered_var_decl >, boost::recursive_wrapper< positive_ordered_var_decl >, boost::recursive_wrapper< cov_matrix_var_decl >, boost::recursive_wrapper< corr_matrix_var_decl > > var_decl_t
Definition: ast.hpp:522
std::string name() const
Definition: ast_def.cpp:778
base_expr_type get_base_type(const std::string &name) const
Definition: ast_def.cpp:588
std::map< std::string, range_t > map_
Definition: ast.hpp:383
void add(const std::string &name, const base_var_decl &base_decl, const var_origin &vo)
Definition: ast_def.cpp:599
std::pair< base_var_decl, var_origin > range_t
Definition: ast.hpp:382
base_var_decl get(const std::string &name) const
Definition: ast_def.cpp:583
var_origin get_origin(const std::string &name) const
Definition: ast_def.cpp:594
void remove(const std::string &name)
Definition: ast_def.cpp:604
bool exists(const std::string &name) const
Definition: ast_def.cpp:580
size_t get_num_dims(const std::string &name) const
Definition: ast_def.cpp:591
expr_type type_
Definition: ast.hpp:290
void set_type(const base_expr_type &base_type, size_t num_dims)
Definition: ast_def.cpp:453

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