Stan  2.14.0
probability, sampling & optimization
indexed_type_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_AST_FUN_INDEXED_TYPE_DEF_HPP
2 #define STAN_LANG_AST_FUN_INDEXED_TYPE_DEF_HPP
3 
4 #include <stan/lang/ast.hpp>
5 #include <vector>
6 
7 namespace stan {
8  namespace lang {
9 
10  expr_type indexed_type(const expression& e, const std::vector<idx>& idxs) {
11  expr_type e_type = e.expression_type();
12 
13  base_expr_type base_type = e_type.base_type_;
14  size_t base_dims = e_type.num_dims_;
15  size_t unindexed_dims = base_dims;
16  size_t out_dims = 0U;
17  size_t i = 0;
18  for ( ; unindexed_dims > 0 && i < idxs.size(); ++i, --unindexed_dims)
19  if (is_multi_index(idxs[i]))
20  ++out_dims;
21  if (idxs.size() - i == 0) {
22  return expr_type(base_type, out_dims + unindexed_dims);
23  } else if (idxs.size() - i == 1) {
24  if (base_type == MATRIX_T) {
25  if (is_multi_index(idxs[i]))
26  return expr_type(MATRIX_T, out_dims);
27  else
28  return expr_type(ROW_VECTOR_T, out_dims);
29  } else if (base_type == VECTOR_T) {
30  if (is_multi_index(idxs[i]))
31  return expr_type(VECTOR_T, out_dims);
32  else
33  return expr_type(DOUBLE_T, out_dims);
34  } else if (base_type == ROW_VECTOR_T) {
35  if (is_multi_index(idxs[i]))
36  return expr_type(ROW_VECTOR_T, out_dims);
37  else
38  return expr_type(DOUBLE_T, out_dims);
39  } else {
40  return expr_type(ILL_FORMED_T, 0U);
41  }
42  } else if (idxs.size() - i == 2) {
43  if (base_type == MATRIX_T) {
44  if (is_multi_index(idxs[i]) && is_multi_index(idxs[i + 1]))
45  return expr_type(MATRIX_T, out_dims);
46  else if (is_multi_index(idxs[i]))
47  return expr_type(VECTOR_T, out_dims);
48  else if (is_multi_index(idxs[i + 1]))
49  return expr_type(ROW_VECTOR_T, out_dims);
50  else
51  return expr_type(DOUBLE_T, out_dims);
52  } else {
53  return expr_type(ILL_FORMED_T, 0U);
54  }
55  } else {
56  return expr_type(ILL_FORMED_T, 0U);
57  }
58  }
59 
60  }
61 }
62 #endif
const int ROW_VECTOR_T
Row vector type; scalar type is real.
std::size_t num_dims_
The number of array dimensions.
Definition: expr_type.hpp:23
Probability, optimization and sampling library.
bool is_multi_index(const idx &idx)
Return true if the specified index potentially takes more than one value.
const int DOUBLE_T
Real scalar type.
int base_expr_type
The type of a base expression.
Structure of the type of an expression, which consists of a base type and a number of dimensions...
Definition: expr_type.hpp:14
expr_type expression_type() const
expr_type indexed_type(const expression &e, const std::vector< idx > &idxs)
Return the type of the expression indexed by the generalized index sequence.
base_expr_type base_type_
The base expression type.
Definition: expr_type.hpp:18
const int VECTOR_T
Column vector type; scalar type is real.
const int ILL_FORMED_T
Type denoting an ill-formed expression.
const int MATRIX_T
Matrix type; scalar type is real.

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