FIMS  v0.11.0
Loading...
Searching...
No Matches
rcpp_interface_base.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_INTERFACE_BASE_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_INTERFACE_BASE_HPP
11
12#ifndef RCPP_NO_SUGAR
13#define RCPP_NO_SUGAR
14#endif
15#include "../rcpp_include.hpp"
16#include <map>
17#include <vector>
18
20#include "../../interface.hpp"
22#include <limits>
23
30class Variable {
31 public:
35 static uint32_t id_g;
39 uint32_t id_m;
43 double initial_value_m = 0.0;
47 double final_value_m = 0.0;
53
57 Variable(double value, std::string estimation_type)
58 : id_m(Variable::id_g++),
59 initial_value_m(value),
60 estimation_type_m(estimation_type) {}
61
70
74 Variable& operator=(const Variable& right) {
75 // Check for self-assignment!
76 if (this == &right) // Same object?
77 return *this; // Yes, so skip assignment, and just return *this.
78 this->id_m = right.id_m;
79 this->initial_value_m = right.initial_value_m;
80 this->estimation_type_m = right.estimation_type_m;
81 return *this;
82 }
83
87 Variable(double value) {
88 initial_value_m = value;
90 }
91
99 }
100};
101
102#ifdef FIMS_HEADER_ONLY
103uint32_t Variable::id_g = 0;
104#endif
105
112inline double sanitize_val(double x) {
113 if (std::isnan(x) || std::isinf(x)) {
114 return -999.0;
115 }
116 return x;
117}
118
126inline std::ostream& operator<<(std::ostream& out, const Variable& p) {
127 out << "{\"id\": " << p.id_m
128 << ",\n\"value\": " << sanitize_val(p.initial_value_m)
129 << ",\n\"estimated_value\": " << sanitize_val(p.final_value_m);
130 out << ",\n\"estimation_type\": \"" << p.estimation_type_m << "\"\n}";
131
132 return out;
133}
134
135RCPP_EXPOSED_CLASS(Variable)
136
137
144 public:
148 static uint32_t id_g;
152 std::shared_ptr<std::vector<Variable>> storage_m;
156 uint32_t id_m;
157
162 this->id_m = VariableVector::id_g++;
163 this->storage_m = std::make_shared<std::vector<Variable>>();
164 this->storage_m->resize(1); // push_back(Rcpp::wrap(p));
165 }
166
171 : storage_m(other.storage_m), id_m(other.id_m) {}
172
176 VariableVector(size_t size) {
177 this->id_m = VariableVector::id_g++;
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++) {
181 storage_m->at(i) = Variable();
182 }
183 }
184
190 VariableVector(Rcpp::NumericVector x, size_t size) {
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` "
195 "length (" +
196 std::to_string(input_size) +
197 ") must equal the "
198 "requested size (" +
199 std::to_string(size) +
200 "). Received length: " + std::to_string(input_size) + ".");
201 } else {
202 this->id_m = VariableVector::id_g++;
203 this->storage_m = std::make_shared<std::vector<Variable>>();
204 // Use std::min to avoid comparing signed and unsigned types
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];
209 }
210 }
211 }
212
218 this->id_m = VariableVector::id_g++;
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];
223 }
224 }
225
230 virtual ~VariableVector() {}
231
235 virtual uint32_t get_id() { return this->id_m; }
236
241 inline Variable& operator[](size_t pos) { return this->storage_m->at(pos); }
242
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");
252 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
253 fims::to_string(this->size()));
254 return NULL;
255 }
256 return Rcpp::wrap(this->storage_m->at(pos - 1));
257 }
258
266 Variable& get(size_t pos) {
267 if (pos >= this->storage_m->size()) {
268 throw std::invalid_argument("VariableVector: Index out of range");
269 }
270 return (this->storage_m->at(pos));
271 }
272
282 void set(size_t pos, const Variable& p) { this->storage_m->at(pos) = p; }
283
287 size_t size() { return this->storage_m->size(); }
288
294 void resize(size_t size) { this->storage_m->resize(size); }
295
299 void set_values(Rcpp::NumericVector values) {
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 "
307 "size (" +
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) + ".");
311 }
312 for (size_t i = 0; i < this->storage_m->size(); i++) {
313 this->storage_m->at(i).initial_value_m = values[i];
314 }
315 }
316
321 void set_estimation_types(Rcpp::CharacterVector estimation_types) {
322 const size_t vector_size = this->storage_m->size();
323 const size_t input_size = estimation_types.size();
324
325 if (input_size != 1 && input_size != vector_size) {
326 throw std::invalid_argument(
327 "VariableVector::set_estimation_types(): `estimation_types` length "
328 "(" +
329 std::to_string(input_size) +
330 ") must be 1 (broadcast) or equal to the VariableVector size (" +
331 std::to_string(vector_size) +
332 ").\n"
333 "Received length: " +
334 std::to_string(input_size) +
335 ". "
336 "Pass a single estimation type to apply to all elements, or a "
337 "vector of length " +
338 std::to_string(vector_size) + ".");
339 }
340
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.");
347 }
348 };
349
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);
355 }
356 }
357
365 void fill(double value) {
366 for (size_t i = 0; i < this->storage_m->size(); i++) {
367 storage_m->at(i).initial_value_m = value;
368 }
369 }
370
375 void show() {
376 Rcpp::Rcout << this->storage_m->data() << "\n";
377
378 for (size_t i = 0; i < this->storage_m->size(); i++) {
379 Rcpp::Rcout << storage_m->at(i) << " ";
380 }
381 }
382
387 VariableVector copy;
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++) {
391 Variable variable_copy;
392 const Variable& variable = this->storage_m->at(i);
393 variable_copy.initial_value_m = variable.initial_value_m;
394 variable_copy.final_value_m = variable.final_value_m;
395 variable_copy.estimation_type_m =
397 copy.storage_m->push_back(variable_copy);
398 }
399 return copy;
400 }
401};
402
403#ifdef FIMS_HEADER_ONLY
404uint32_t VariableVector::id_g = 0;
405#endif
406
414inline std::ostream& operator<<(std::ostream& out, VariableVector& v) {
415 out << "[";
416 size_t size = v.size();
417 for (size_t i = 0; i < size - 1; i++) {
418 out << v[i] << ", ";
419 }
420 out << v[size - 1] << "]";
421 return out;
422}
423
432 public:
436 static uint32_t id_g;
440 std::shared_ptr<std::vector<double>> storage_m;
444 uint32_t id_m;
445
450 this->id_m = RealVector::id_g++;
451 this->storage_m = std::make_shared<std::vector<double>>();
452 this->storage_m->resize(1);
453 }
454
458 RealVector(const RealVector& other)
459 : storage_m(other.storage_m), id_m(other.id_m) {}
460
464 RealVector(size_t size) {
465 this->id_m = RealVector::id_g++;
466 this->storage_m = std::make_shared<std::vector<double>>();
467 this->storage_m->resize(size);
468 }
469
475 RealVector(Rcpp::NumericVector x, size_t size) {
476 this->id_m = RealVector::id_g++;
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 "
484 "size (" +
485 std::to_string(size) +
486 "). Received length: " + std::to_string(input_size) + ".");
487 }
488 this->storage_m->assign(x.begin(), x.end());
489 }
490
496 this->id_m = RealVector::id_g++;
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++) {
500 storage_m->at(i) = v[i];
501 }
502 }
503
508 virtual ~RealVector() {}
509
516 RealVector& operator=(const Rcpp::NumericVector& v) {
517 this->storage_m->assign(v.begin(), v.end());
518 return *this;
519 }
520
524 virtual uint32_t get_id() { return this->id_m; }
525
531 void set_values(const Rcpp::NumericVector& orig) {
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];
535 }
536 }
537
543 Rcpp::NumericVector get_values() {
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);
547 }
548
549 return ret;
550 }
551
556 inline double& operator[](size_t pos) { return this->storage_m->at(pos); }
557
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");
567 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
568 fims::to_string(this->size()));
569 return NULL;
570 }
571 return Rcpp::wrap(this->storage_m->at(pos - 1));
572 }
573
581 double& get(size_t pos) {
582 if (pos >= this->storage_m->size()) {
583 throw std::invalid_argument("RealVector: Index out of range");
584 }
585 return (this->storage_m->at(pos));
586 }
587
597 void set(size_t pos, const double& p) { this->storage_m->at(pos) = p; }
598
602 size_t size() { return this->storage_m->size(); }
603
609 void resize(size_t size) { this->storage_m->resize(size); }
610
618 void fill(double value) {
619 for (size_t i = 0; i < this->storage_m->size(); i++) {
620 storage_m->at(i) = value;
621 }
622 }
623
628 void show() {
629 Rcpp::Rcout << this->storage_m->data() << "\n";
630
631 for (size_t i = 0; i < this->storage_m->size(); i++) {
632 Rcpp::Rcout << storage_m->at(i) << " ";
633 }
634 }
635
640 RealVector copy;
641 copy.storage_m = std::make_shared<std::vector<double>>(*this->storage_m);
642 return copy;
643 }
644};
645#ifdef FIMS_HEADER_ONLY
646uint32_t RealVector::id_g = 0;
647#endif
648
649RCPP_EXPOSED_CLASS(VariableVector)
650RCPP_EXPOSED_CLASS(RealVector)
651
652
656 public:
660 bool finalized = false;
664 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
666
670 virtual bool add_to_fims_tmb() {
671 Rcpp::Rcout << "fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
672 "implemented.\n";
673 return false;
674 }
675
680 virtual void finalize() {}
681
685 virtual std::string to_json() {
686 FIMS_WARNING_LOG("Method not yet defined.");
687 return "{\"name\": \"not yet implemented\"}";
688 }
689
696 std::string value_to_string(double value) {
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) {
703 ss << "-999";
704 } else {
705 // Set precision (R default is 16)
706 ss << std::fixed << std::setprecision(16) << value;
707 }
708 return ss.str();
709 }
713 std::string make_dimensions(uint32_t start, uint32_t end, uint32_t rep = 1) {
714 std::stringstream ss;
715
716 for (size_t i = 0; i < rep; i++) {
717 for (size_t j = start; j < end; j++) {
718 ss << j << ", ";
719 }
720 if (i < (rep - 1)) {
721 ss << end << ", ";
722 } else {
723 ss << end;
724 }
725 }
726 return ss.str();
727 }
728};
729
730#endif
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
Code to store all objects that are created in FIMS because FIMS uses integer representation....
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 ...