1 #ifndef __STAN__IO__DUMP_HPP__
2 #define __STAN__IO__DUMP_HPP__
10 #include <boost/throw_exception.hpp>
19 size_t product(std::vector<size_t> dims) {
21 for (
size_t i = 0; i < dims.size(); ++i)
40 void write_name_equals(
const std::string& name) {
41 for (
size_t i = 0; i < name.size(); ++i)
42 if (name.at(i) ==
'"')
43 BOOST_THROW_EXCEPTION(
44 std::invalid_argument (
"name can not contain quote char"));
45 out_ <<
'"' << name <<
'"' <<
" <- " <<
'\n';
50 void write_val(
const double& x) {
55 for (std::string::iterator it = s.begin();
58 if (*it ==
'.' || *it ==
'e' || *it ==
'E') {
66 void write_val(
const unsigned long long int& n) {
70 void write_val(
const unsigned long int& n) {
74 void write_val(
const unsigned int& n) {
78 void write_val(
const unsigned short& n) {
82 void write_val(
const long long& n) {
86 void write_val(
const long& n) {
90 void write_val(
const int& n) {
94 void write_val(
const short& n) {
98 void write_val(
const char& n) {
103 template <
typename T>
104 void write_list(T xs) {
107 if (i > 0) out_ <<
", ";
113 template <
typename T>
114 void write_structure(std::vector<T> xs,
115 std::vector<size_t> dims) {
116 out_ <<
"structure(";
125 void dims(
double x, std::vector<size_t> ds) {
129 void dims(
int x, std::vector<size_t> ds) {
133 template <
typename T>
134 void dims(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> m,
135 std::vector<size_t> ds) {
136 ds.push_back(m.rows());
137 ds.push_back(m.cols());
140 template <
typename T>
141 void dims(Eigen::Matrix<T,Eigen::Dynamic,1> v,
142 std::vector<size_t> ds) {
143 ds.push_back(v.size());
146 template <
typename T>
147 void dims(Eigen::Matrix<T,1,Eigen::Dynamic> rv,
148 std::vector<size_t> ds) {
149 ds.push_back(rv.size());
152 template <
typename T>
153 void dims(std::vector<T> x, std::vector<size_t> ds) {
154 ds.push_back(x.size());
159 template <
typename T>
160 std::vector<size_t> dims(T x) {
161 std::vector<size_t> ds;
166 bool increment(
const std::vector<size_t>& dims,
167 std::vector<size_t>& idx) {
168 for (
size_t i = 0; i < dims.size(); ++i) {
170 if (idx[i] < dims[i])
return true;
176 template <
typename T>
177 void write_stan_val(
const std::vector<T>& x,
178 const std::vector<size_t>& idx,
180 size_t next_pos = pos + 1;
181 write_stan_val(x[idx[pos]],idx,next_pos);
183 void write_stan_val(
const std::vector<double>& x,
184 const std::vector<size_t>& idx,
186 write_val(x[idx[pos]]);
188 void write_stan_val(
const std::vector<int>& x,
189 const std::vector<size_t>& idx,
191 write_val(x[idx[pos]]);
193 void write_stan_val(
const Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>& x,
194 const std::vector<size_t>& idx,
196 size_t next_pos = pos + 1;
197 write_val(x(idx[pos],idx[next_pos]));
199 void write_stan_val(
const Eigen::Matrix<double,1,Eigen::Dynamic>& x,
200 const std::vector<size_t>& idx,
202 write_val(x[idx[pos]]);
204 void write_stan_val(
const Eigen::Matrix<double,Eigen::Dynamic,1>& x,
205 const std::vector<size_t>& idx,
207 write_val(x[idx[pos]]);
211 template <
typename T>
212 void write_stan(
const std::vector<T>& x) {
213 std::vector<size_t> dims = dims(x);
214 out_ <<
"structure(c(";
215 std::vector<size_t> idx(dims.size(),0U);
216 for (
size_t count = 0;
true; ++count) {
217 if (count > 0) out_ <<
", ";
218 write_stan_val(x,idx);
219 if (!increment(dims,idx))
break;
221 out_ <<
"), .Dim = ";
225 void write_stan(
const std::vector<double>& x) {
228 void write_stan(
const std::vector<int>& x) {
231 void write_stan(
double x) {
234 void write_stan(
int x) {
237 void write_stan(
const Eigen::Matrix<double,1,Eigen::Dynamic>& x) {
240 void write_stan(
const Eigen::Matrix<double,Eigen::Dynamic,1>& x) {
243 void write_stan(
const Eigen::Matrix<
double,
244 Eigen::Dynamic,Eigen::Dynamic>& x) {
245 typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>::Index Index;
246 out_ <<
"structure(c(";
247 std::vector<double> vals;
248 for (Index m = 0; m < x.cols(); ++m) {
249 for (Index n = 0; n < x.rows(); ++n) {
250 if (m > 0 || n > 0) out_ <<
", ";
254 out_ <<
"), .Dim = c(";
255 out_ << x.rows() <<
", " << x.cols();
297 template <
typename T>
300 write_name_equals(name);
313 template <
typename T>
315 std::vector<size_t> dims,
317 if (xs.size() != product(dims))
318 BOOST_THROW_EXCEPTION(
319 std::invalid_argument(
"xs.size() != product(dims)"));
320 write_structure(xs,dims);
331 template <
typename T>
334 write_name_equals(name);
346 template <
typename T>
349 write_name_equals(name);
429 std::vector<int> stack_i_;
430 std::vector<double> stack_r_;
431 std::vector<size_t> dims_;
435 bool scan_single_char(
char c_expected) {
444 bool scan_optional_long() {
445 if (scan_single_char(
'l'))
447 else if (scan_single_char(
'L'))
453 bool scan_char(
char c_expected) {
456 if (c != c_expected) {
463 bool scan_name_unquoted() {
466 if (!std::isalpha(c))
return false;
469 if (std::isalpha(c) || std::isdigit(c) || c ==
'_' || c ==
'.') {
480 if (scan_char(
'"')) {
481 if (!scan_name_unquoted())
return false;
482 if (!scan_char(
'"'))
return false;
483 }
else if (scan_char(
'\'')) {
484 if (!scan_name_unquoted())
return false;
485 if (!scan_char(
'\''))
return false;
487 if (!scan_name_unquoted())
return false;
493 bool scan_chars(std::string s,
bool case_sensitive =
true) {
494 for (
size_t i = 0; i < s.size(); ++i) {
497 for (
size_t j = 1; j < i; ++j)
502 if ((case_sensitive && c != s[i])
503 || (!case_sensitive && ::toupper(c) != ::toupper(s[i]))) {
505 for (
size_t j = 1; j < i; ++j)
513 bool scan_number(
bool negate_val) {
516 if (scan_chars(
"Inf")) {
518 stack_r_.push_back(negate_val
519 ? -std::numeric_limits<double>::infinity()
520 : std::numeric_limits<double>::infinity());
523 if (scan_chars(
"NaN",
false)) {
524 stack_r_.push_back(std::numeric_limits<double>::quiet_NaN());
529 bool is_double =
false;
532 if (std::isdigit(c)) {
546 if (!is_double && stack_r_.size() == 0) {
548 if (!(std::stringstream(buf) >> n))
550 stack_i_.push_back(negate_val ? -n : n);
551 scan_optional_long();
553 for (
size_t j = 0; j < stack_i_.size(); ++j)
554 stack_r_.push_back(negate_val
555 ? -
static_cast<double>(stack_i_[j])
556 :
static_cast<double>(stack_i_[j]));
559 if (!(std::stringstream(buf) >> x))
561 stack_r_.push_back(negate_val ? -x : x);
569 if (std::isspace(c))
continue;
573 bool negate_val = scan_char(
'-');
574 if (!negate_val) scan_char(
'+');
575 return scan_number(negate_val);
579 bool scan_seq_value() {
580 if (!scan_char(
'('))
return false;
581 if (scan_char(
')')) {
585 if (!scan_number())
return false;;
586 while (scan_char(
',')) {
587 if (!scan_number())
return false;
589 dims_.push_back(stack_r_.size() + stack_i_.size());
590 return scan_char(
')');
593 bool scan_struct_value() {
594 if (!scan_char(
'('))
return false;
595 if (scan_char(
'c')) {
605 for (
size_t i = start; i <= end; ++i)
606 stack_i_.push_back(i);
608 for (
size_t i = start; i >= end; --i)
609 stack_i_.push_back(i);
613 if (!scan_char(
','))
return false;
614 if (!scan_char(
'.'))
return false;
615 if (!scan_chars(
"Dim"))
return false;
616 if (!scan_char(
'='))
return false;
617 if (scan_char(
'c')) {
618 if (!scan_char(
'('))
return false;
621 scan_optional_long();
622 dims_.push_back(dim);
623 while (scan_char(
',')) {
625 scan_optional_long();
626 dims_.push_back(dim);
628 if (!scan_char(
')'))
return false;
637 for (
size_t i = start; i <= end; ++i)
640 for (
size_t i = start; i >= end; --i)
644 if (!scan_char(
')'))
return false;
652 return scan_seq_value();
653 if (scan_chars(
"structure"))
654 return scan_struct_value();
659 if (stack_i_.size() != 1)
663 if (stack_i_.size() != 2)
665 int start = stack_i_[0];
666 int end = stack_i_[1];
669 for (
int i = start; i <= end; ++i)
670 stack_i_.push_back(i);
672 for (
int i = start; i >= end; --i)
673 stack_i_.push_back(i);
675 dims_.push_back(stack_i_.size());
683 std::cout <<
"var name=|" << name_ <<
"|" << std::endl;
684 std:: cout <<
"dims=(";
685 for (
size_t i = 0; i < dims_.size(); ++i) {
688 std::cout << dims_[i];
690 std::cout <<
")" << std::endl;
691 std::cout <<
"float stack:" << std::endl;
692 for (
size_t i = 0; i < stack_r_.size(); ++i)
693 std::cout <<
" [" << i <<
"] " << stack_r_[i] << std::endl;
694 std::cout <<
"int stack" << std::endl;
695 for (
size_t i = 0; i < stack_i_.size(); ++i)
696 std::cout <<
" [" << i <<
"] " << stack_i_[i] << std::endl;
746 return stack_i_.size() > 0;
814 std::map<std::string,
815 std::pair<std::vector<double>,
816 std::vector<size_t> > > vars_r_;
817 std::map<std::string,
818 std::pair<std::vector<int>,
819 std::vector<size_t> > > vars_i_;
820 std::vector<double>
const empty_vec_r_;
821 std::vector<int>
const empty_vec_i_;
822 std::vector<size_t>
const empty_vec_ui_;
832 bool contains_r_only(
const std::string& name)
const {
833 return vars_r_.find(name) != vars_r_.end();
849 = std::pair<std::vector<int>,
850 std::vector<size_t> >(
reader.int_values(),
855 = std::pair<std::vector<double>,
856 std::vector<size_t> >(
reader.double_values(),
871 return contains_r_only(name) ||
contains_i(name);
883 return vars_i_.find(name) != vars_i_.end();
893 std::vector<double>
vals_r(
const std::string& name)
const {
894 if (contains_r_only(name)) {
895 return (vars_r_.find(name)->second).first;
897 std::vector<int> vec_int = (vars_i_.find(name)->second).first;
898 std::vector<double> vec_r(vec_int.size());
899 for (
size_t ii = 0; ii < vec_int.size(); ii++) {
900 vec_r[ii] = vec_int[ii];
914 std::vector<size_t>
dims_r(
const std::string& name)
const {
915 if (contains_r_only(name)) {
916 return (vars_r_.find(name)->second).second;
918 return (vars_i_.find(name)->second).second;
920 return empty_vec_ui_;
930 std::vector<int>
vals_i(
const std::string& name)
const {
932 return (vars_i_.find(name)->second).first;
944 std::vector<size_t>
dims_i(
const std::string& name)
const {
946 return (vars_i_.find(name)->second).second;
948 return empty_vec_ui_;
957 virtual void names_r(std::vector<std::string>& names)
const {
959 for (std::map<std::string,
960 std::pair<std::vector<double>,
961 std::vector<size_t> > >
962 ::const_iterator it = vars_r_.begin();
963 it != vars_r_.end(); ++it)
964 names.push_back((*it).first);
973 virtual void names_i(std::vector<std::string>& names)
const {
975 for (std::map<std::string,
976 std::pair<std::vector<int>,
977 std::vector<size_t> > >
978 ::const_iterator it = vars_i_.begin();
979 it != vars_i_.end(); ++it)
980 names.push_back((*it).first);
991 return (vars_i_.erase(name) > 0)
992 || (vars_r_.erase(name) > 0);
internal::traits< Derived >::Index size_type
Reads data from S-plus dump format.
bool is_int()
Checks if the last item read is integer.
std::vector< int > int_values()
Returns the integer values from the last item if the last item read was an integer and the empty vect...
dump_reader(std::istream &in)
Construct a reader for the specified input stream.
dump_reader()
Construct a reader for standard input.
std::vector< size_t > dims()
Return the dimensions of the most recently read variable.
std::vector< double > double_values()
Returns the floating point values from the last item if the last item read contained floating point v...
bool next()
Read the next value from the input stream, returning true if successful and false if no further input...
std::string name()
Return the name of the most recently read variable.
~dump_reader()
Destroy this reader.
Writes data into the S-plus dump format.
~dump_writer()
Destroy this writer.
void write(const std::string &name, const T &x)
Write a variable with the specified name and value.
dump_writer(std::ostream &out)
Construct a dump writer writing to the specified output stream.
void dump_var(std::string name, T x)
Write a list variable with the specified name and integer or double values.
void dump_structure(std::string name, std::vector< size_t > dims, std::vector< T > xs)
Write a structure variable with the specified name, dimensions, and integer or double values.
dump_writer()
Construct a dump writer writing to standard output.
void dump_list(std::string name, std::vector< T > xs)
Write a list variable with the specified name and integer or double values.
Represents named arrays with dimensions.
dump(std::istream &in)
Construct a dump object from the specified input stream.
bool contains_i(const std::string &name) const
Return true if this dump contains an integer valued array with the specified name.
virtual void names_i(std::vector< std::string > &names) const
Return a list of the names of the integer variables in the dump.
std::vector< size_t > dims_r(const std::string &name) const
Return the dimensions for the double variable with the specified name.
bool contains_r(const std::string &name) const
Return true if this dump contains the specified variable name is defined.
std::vector< int > vals_i(const std::string &name) const
Return the integer values for the variable with the specified name.
std::vector< double > vals_r(const std::string &name) const
Return the double values for the variable with the specified name or null.
virtual void names_r(std::vector< std::string > &names) const
Return a list of the names of the floating point variables in the dump.
bool remove(const std::string &name)
Remove variable from the object.
std::vector< size_t > dims_i(const std::string &name) const
Return the dimensions for the integer variable with the specified name.
A stream-based reader for integer, scalar, vector, matrix and array data types, with Jacobian calcula...
A var_reader reads array variables of integer and floating point type by name and dimension.
Probability, optimization and sampling library.
Template specification of functions in std for Stan.