9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_INTERFACE_BASE_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_INTERFACE_BASE_HPP
15#include "../rcpp_include.hpp"
20#include "../../interface.hpp"
57 Variable(
double value, std::string estimation_type)
78 this->id_m = right.
id_m;
102#ifdef FIMS_HEADER_ONLY
113 if (std::isnan(x) || std::isinf(x)) {
127 out <<
"{\"id\": " << p.
id_m
163 this->storage_m = std::make_shared<std::vector<Variable>>();
164 this->storage_m->resize(1);
171 : storage_m(other.storage_m), id_m(other.id_m) {}
178 this->storage_m = std::make_shared<std::vector<Variable>>();
179 this->storage_m->resize(size);
180 for (
size_t i = 0; i < size; i++) {
191 const size_t input_size =
static_cast<size_t>(x.size());
192 if (input_size != size) {
193 throw std::invalid_argument(
194 "VariableVector::VariableVector(Rcpp::NumericVector, size_t): `x` "
196 std::to_string(input_size) +
199 std::to_string(size) +
200 "). Received length: " + std::to_string(input_size) +
".");
203 this->storage_m = std::make_shared<std::vector<Variable>>();
205 size_t n = std::min(input_size, size);
206 this->storage_m->resize(n);
207 for (
size_t i = 0; i < n; i++) {
208 storage_m->at(i).initial_value_m = x[i];
219 this->storage_m = std::make_shared<std::vector<Variable>>();
220 this->storage_m->resize(v.
size());
221 for (
size_t i = 0; i < v.
size(); i++) {
222 storage_m->at(i).initial_value_m = v[i];
235 virtual uint32_t
get_id() {
return this->id_m; }
248 SEXP
at(R_xlen_t pos) {
249 if (
static_cast<size_t>(pos) == 0 ||
250 static_cast<size_t>(pos) > this->storage_m->size()) {
251 throw std::invalid_argument(
"VariableVector: Index out of range");
253 fims::to_string(this->size()));
256 return Rcpp::wrap(this->storage_m->at(pos - 1));
267 if (pos >= this->storage_m->size()) {
268 throw std::invalid_argument(
"VariableVector: Index out of range");
270 return (this->storage_m->at(pos));
282 void set(
size_t pos,
const Variable& p) { this->storage_m->at(pos) = p; }
287 size_t size() {
return this->storage_m->size(); }
294 void resize(
size_t size) { this->storage_m->resize(size); }
300 if (values.size() != this->storage_m->size()) {
301 const size_t input_size = values.size();
302 const size_t vector_size = this->storage_m->size();
303 throw std::invalid_argument(
304 "VariableVector::set_values(): `values` length (" +
305 std::to_string(input_size) +
306 ") must equal the VariableVector "
308 std::to_string(vector_size) +
"). Received length: " +
309 std::to_string(input_size) +
". Pass a numeric vector of length " +
310 std::to_string(vector_size) +
".");
312 for (
size_t i = 0; i < this->storage_m->size(); i++) {
313 this->storage_m->at(i).initial_value_m = values[i];
322 const size_t vector_size = this->storage_m->size();
323 const size_t input_size = estimation_types.size();
325 if (input_size != 1 && input_size != vector_size) {
326 throw std::invalid_argument(
327 "VariableVector::set_estimation_types(): `estimation_types` length "
329 std::to_string(input_size) +
330 ") must be 1 (broadcast) or equal to the VariableVector size (" +
331 std::to_string(vector_size) +
333 "Received length: " +
334 std::to_string(input_size) +
336 "Pass a single estimation type to apply to all elements, or a "
337 "vector of length " +
338 std::to_string(vector_size) +
".");
341 auto validate_estimation_type = [&](
const std::string& est_type) {
342 if (est_type !=
"constant" && est_type !=
"fixed_effects" &&
343 est_type !=
"random_effects") {
344 throw std::invalid_argument(
345 "Invalid estimation_type: " + est_type +
346 ". Valid options are: constant, fixed_effects, or random_effects.");
350 for (
size_t i = 0; i < vector_size; i++) {
351 std::string est_type =
352 Rcpp::as<std::string>(estimation_types[input_size == 1 ? 0 : i]);
353 validate_estimation_type(est_type);
354 this->storage_m->at(i).estimation_type_m.set(est_type);
366 for (
size_t i = 0; i < this->storage_m->size(); i++) {
367 storage_m->at(i).initial_value_m = value;
376 Rcpp::Rcout << this->storage_m->data() <<
"\n";
378 for (
size_t i = 0; i < this->storage_m->size(); i++) {
379 Rcpp::Rcout << storage_m->at(i) <<
" ";
388 copy.
storage_m = std::make_shared<std::vector<Variable>>();
389 copy.
storage_m->reserve(this->storage_m->size());
390 for (
size_t i = 0; i < this->storage_m->size(); i++) {
392 const Variable& variable = this->storage_m->at(i);
397 copy.
storage_m->push_back(variable_copy);
403#ifdef FIMS_HEADER_ONLY
416 size_t size = v.
size();
417 for (
size_t i = 0; i < size - 1; i++) {
420 out << v[size - 1] <<
"]";
451 this->storage_m = std::make_shared<std::vector<double>>();
452 this->storage_m->resize(1);
466 this->storage_m = std::make_shared<std::vector<double>>();
467 this->storage_m->resize(
size);
477 this->storage_m = std::make_shared<std::vector<double>>();
478 const size_t input_size =
static_cast<size_t>(x.size());
479 if (input_size !=
size) {
480 throw std::invalid_argument(
481 "RealVector::RealVector(Rcpp::NumericVector, size_t): `x` length (" +
482 std::to_string(input_size) +
483 ") must equal the requested "
485 std::to_string(
size) +
486 "). Received length: " + std::to_string(input_size) +
".");
488 this->storage_m->assign(x.begin(), x.end());
497 this->storage_m = std::make_shared<std::vector<double>>();
498 this->storage_m->resize(v.
size());
499 for (
size_t i = 0; i < v.
size(); i++) {
517 this->storage_m->assign(v.begin(), v.end());
532 this->storage_m->resize(orig.size());
533 for (
size_t i = 0; i < this->storage_m->size(); i++) {
534 this->storage_m->at(i) = orig[i];
544 Rcpp::NumericVector ret(this->storage_m->size());
545 for (
size_t i = 0; i < this->
size(); i++) {
546 ret[i] = this->storage_m->at(i);
556 inline double&
operator[](
size_t pos) {
return this->storage_m->at(pos); }
563 SEXP
at(R_xlen_t pos) {
564 if (
static_cast<size_t>(pos) == 0 ||
565 static_cast<size_t>(pos) > this->storage_m->size()) {
566 throw std::invalid_argument(
"RealVector: Index out of range");
568 fims::to_string(this->
size()));
571 return Rcpp::wrap(this->storage_m->at(pos - 1));
582 if (pos >= this->storage_m->size()) {
583 throw std::invalid_argument(
"RealVector: Index out of range");
585 return (this->storage_m->at(pos));
597 void set(
size_t pos,
const double& p) { this->storage_m->at(pos) = p; }
602 size_t size() {
return this->storage_m->size(); }
619 for (
size_t i = 0; i < this->storage_m->size(); i++) {
629 Rcpp::Rcout << this->storage_m->data() <<
"\n";
631 for (
size_t i = 0; i < this->storage_m->size(); i++) {
645#ifdef FIMS_HEADER_ONLY
660 bool finalized =
false;
664 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
671 Rcpp::Rcout <<
"fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
687 return "{\"name\": \"not yet implemented\"}";
697 std::stringstream ss;
698 if (value == std::numeric_limits<double>::infinity()) {
699 ss <<
"\"Infinity\"";
700 }
else if (value == -std::numeric_limits<double>::infinity()) {
701 ss <<
"\"-Infinity\"";
702 }
else if (value != value) {
706 ss << std::fixed << std::setprecision(16) << value;
714 std::stringstream ss;
716 for (
size_t i = 0; i < rep; i++) {
717 for (
size_t j = start; j < end; j++) {
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
std::string make_dimensions(uint32_t start, uint32_t end, uint32_t rep=1)
Make a string of dimensions for the model.
Definition rcpp_interface_base.hpp:713
std::string value_to_string(double value)
Report the variable value as a string.
Definition rcpp_interface_base.hpp:696
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:665
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:670
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_interface_base.hpp:685
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_interface_base.hpp:680
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:431
double & get(size_t pos)
An internal accessor for calling a position of a RealVector from R.
Definition rcpp_interface_base.hpp:581
virtual ~RealVector()
Destroy the real Vector object.
Definition rcpp_interface_base.hpp:508
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:602
RealVector(const RealVector &other)
The constructor.
Definition rcpp_interface_base.hpp:458
Rcpp::NumericVector get_values()
Definition rcpp_interface_base.hpp:543
void set(size_t pos, const double &p)
An internal setter for setting a position of a RealVector from R.
Definition rcpp_interface_base.hpp:597
RealVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:464
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:609
std::shared_ptr< std::vector< double > > storage_m
real storage.
Definition rcpp_interface_base.hpp:440
RealVector & operator=(const Rcpp::NumericVector &v)
Definition rcpp_interface_base.hpp:516
uint32_t id_m
The local ID of the RealVector object.
Definition rcpp_interface_base.hpp:444
void fill(double value)
Sets the value of all elements in the RealVector to the provided value.
Definition rcpp_interface_base.hpp:618
static uint32_t id_g
The static ID of the RealVector object.
Definition rcpp_interface_base.hpp:436
void set_values(const Rcpp::NumericVector &orig)
Definition rcpp_interface_base.hpp:531
virtual uint32_t get_id()
Gets the ID of the RealVector object.
Definition rcpp_interface_base.hpp:524
RealVector deep_copy() const
Create a deep copy with a new RealVector ID.
Definition rcpp_interface_base.hpp:639
RealVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:475
SEXP at(R_xlen_t pos)
The accessor where the first index starts at one. This function is for calling accessing from R.
Definition rcpp_interface_base.hpp:563
double & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:556
RealVector(const fims::Vector< double > &v)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:495
void show()
The printing methods for a RealVector.
Definition rcpp_interface_base.hpp:628
RealVector()
The constructor.
Definition rcpp_interface_base.hpp:449
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1513
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
An Rcpp interface class that defines the VariableVector class.
Definition rcpp_interface_base.hpp:143
void show()
The printing methods for a VariableVector.
Definition rcpp_interface_base.hpp:375
void set_estimation_types(Rcpp::CharacterVector estimation_types)
Sets the estimation type for all Variables within a VariableVector.
Definition rcpp_interface_base.hpp:321
VariableVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a variable vector.
Definition rcpp_interface_base.hpp:190
VariableVector()
The constructor.
Definition rcpp_interface_base.hpp:161
VariableVector deep_copy() const
Create a deep copy with a new VariableVector ID.
Definition rcpp_interface_base.hpp:386
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:266
static uint32_t id_g
The static ID of the Variable object.
Definition rcpp_interface_base.hpp:148
VariableVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:176
SEXP at(R_xlen_t pos)
The accessor where the first index starts at one. This function is for calling accessing from R.
Definition rcpp_interface_base.hpp:248
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
void set(size_t pos, const Variable &p)
An internal setter for setting a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:282
VariableVector(const fims::Vector< double > &v)
The constructor for initializing a variable vector.
Definition rcpp_interface_base.hpp:217
virtual uint32_t get_id()
Gets the ID of the VariableVector object.
Definition rcpp_interface_base.hpp:235
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:294
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
Variable & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:241
std::shared_ptr< std::vector< Variable > > storage_m
Variable storage.
Definition rcpp_interface_base.hpp:152
void fill(double value)
Sets the value of all Variables in the VariableVector to the provided value.
Definition rcpp_interface_base.hpp:365
virtual ~VariableVector()
Destroy the Variable Vector object.
Definition rcpp_interface_base.hpp:230
VariableVector(const VariableVector &other)
The constructor.
Definition rcpp_interface_base.hpp:170
void set_values(Rcpp::NumericVector values)
Sets the initial values for all Variables within a VariableVector.
Definition rcpp_interface_base.hpp:299
An Rcpp interface that defines the Variable class.
Definition rcpp_interface_base.hpp:30
Variable(double value, std::string estimation_type)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:57
SharedString estimation_type_m
A string indicating the estimation type. Options are: constant, fixed_effects, or random_effects,...
Definition rcpp_interface_base.hpp:52
double initial_value_m
The initial value of the variable.
Definition rcpp_interface_base.hpp:43
Variable & operator=(const Variable &right)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:74
Variable()
The constructor for initializing a Variable.
Definition rcpp_interface_base.hpp:96
Variable(const Variable &other)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:65
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:39
double final_value_m
The final value of the variable.
Definition rcpp_interface_base.hpp:47
static uint32_t id_g
The static ID of the Variable object.
Definition rcpp_interface_base.hpp:35
Variable(double value)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:87
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:112
std::ostream & operator<<(std::ostream &out, const Variable &p)
Output for std::ostream& for a variable.
Definition rcpp_interface_base.hpp:126
Code to create shared pointers to allow for the wrapper functions in R to work correctly so the live ...