FIMS  v0.10.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 <RcppCommon.h>
16#include <Rcpp.h>
17#include <map>
18#include <vector>
19
21#include "../../interface.hpp"
23#include <limits>
24
31class Variable {
32 public:
36 static uint32_t id_g;
44 double initial_value_m = 0.0;
48 double final_value_m = 0.0;
54
58 Variable(double value, std::string estimation_type)
59 : id_m(Variable::id_g++),
60 initial_value_m(value),
62
71
76 // Check for self-assignment!
77 if (this == &right) // Same object?
78 return *this; // Yes, so skip assignment, and just return *this.
79 this->id_m = right.id_m;
80 this->initial_value_m = right.initial_value_m;
81 this->estimation_type_m = right.estimation_type_m;
82 return *this;
83 }
84
88 Variable(double value) {
89 initial_value_m = value;
91 }
92
100 }
101};
102
103#ifdef FIMS_HEADER_ONLY
105#endif
106
113inline double sanitize_val(double x) {
114 if (std::isnan(x) || std::isinf(x)) {
115 return -999.0;
116 }
117 return x;
118}
119
127inline std::ostream& operator<<(std::ostream& out, const Variable& p) {
128 out << "{\"id\": " << p.id_m
129 << ",\n\"value\": " << sanitize_val(p.initial_value_m)
130 << ",\n\"estimated_value\": " << sanitize_val(p.final_value_m);
131 out << ",\n\"estimation_type\": \"" << p.estimation_type_m << "\"\n}";
132
133 return out;
134}
135
137
138
145 public:
153 std::shared_ptr<std::vector<Variable>> storage_m;
158
163 this->id_m = VariableVector::id_g++;
164 this->storage_m = std::make_shared<std::vector<Variable>>();
165 this->storage_m->resize(1); // push_back(Rcpp::wrap(p));
166 }
167
172 : storage_m(other.storage_m), id_m(other.id_m) {}
173
177 VariableVector(size_t size) {
178 this->id_m = VariableVector::id_g++;
179 this->storage_m = std::make_shared<std::vector<Variable>>();
180 this->storage_m->resize(size);
181 for (size_t i = 0; i < size; i++) {
182 storage_m->at(i) = Variable();
183 }
184 }
185
191 VariableVector(Rcpp::NumericVector x, size_t size) {
192 const size_t input_size = static_cast<size_t>(x.size());
193 if (input_size != size) {
194 throw std::invalid_argument(
195 "VariableVector::VariableVector(Rcpp::NumericVector, size_t): `x` "
196 "length (" +
197 std::to_string(input_size) +
198 ") must equal the "
199 "requested size (" +
200 std::to_string(size) +
201 "). Received length: " + std::to_string(input_size) + ".");
202 } else {
203 this->id_m = VariableVector::id_g++;
204 this->storage_m = std::make_shared<std::vector<Variable>>();
205 // Use std::min to avoid comparing signed and unsigned types
206 size_t n = std::min(input_size, size);
207 this->storage_m->resize(n);
208 for (size_t i = 0; i < n; i++) {
209 storage_m->at(i).initial_value_m = x[i];
210 }
211 }
212 }
213
219 this->id_m = VariableVector::id_g++;
220 this->storage_m = std::make_shared<std::vector<Variable>>();
221 this->storage_m->resize(v.size());
222 for (size_t i = 0; i < v.size(); i++) {
223 storage_m->at(i).initial_value_m = v[i];
224 }
225 }
226
231 virtual ~VariableVector() {}
232
236 virtual uint32_t get_id() { return this->id_m; }
237
242 inline Variable& operator[](size_t pos) { return this->storage_m->at(pos); }
243
250 if (static_cast<size_t>(pos) == 0 ||
251 static_cast<size_t>(pos) > this->storage_m->size()) {
252 throw std::invalid_argument("VariableVector: Index out of range");
253 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
254 fims::to_string(this->size()));
255 return NULL;
256 }
257 return Rcpp::wrap(this->storage_m->at(pos - 1));
258 }
259
267 Variable& get(size_t pos) {
268 if (pos >= this->storage_m->size()) {
269 throw std::invalid_argument("VariableVector: Index out of range");
270 }
271 return (this->storage_m->at(pos));
272 }
273
283 void set(size_t pos, const Variable& p) { this->storage_m->at(pos) = p; }
284
288 size_t size() { return this->storage_m->size(); }
289
295 void resize(size_t size) { this->storage_m->resize(size); }
296
300 void set_values(Rcpp::NumericVector values) {
301 if (values.size() != this->storage_m->size()) {
302 const size_t input_size = values.size();
303 const size_t vector_size = this->storage_m->size();
304 throw std::invalid_argument(
305 "VariableVector::set_values(): `values` length (" +
306 std::to_string(input_size) +
307 ") must equal the VariableVector "
308 "size (" +
309 std::to_string(vector_size) + "). Received length: " +
310 std::to_string(input_size) + ". Pass a numeric vector of length " +
311 std::to_string(vector_size) + ".");
312 }
313 for (size_t i = 0; i < this->storage_m->size(); i++) {
314 this->storage_m->at(i).initial_value_m = values[i];
315 }
316 }
317
322 void set_estimation_types(Rcpp::CharacterVector estimation_types) {
323 const size_t vector_size = this->storage_m->size();
324 const size_t input_size = estimation_types.size();
325
326 if (input_size != 1 && input_size != vector_size) {
327 throw std::invalid_argument(
328 "VariableVector::set_estimation_types(): `estimation_types` length "
329 "(" +
330 std::to_string(input_size) +
331 ") must be 1 (broadcast) or equal to the VariableVector size (" +
332 std::to_string(vector_size) +
333 ").\n"
334 "Received length: " +
335 std::to_string(input_size) +
336 ". "
337 "Pass a single estimation type to apply to all elements, or a "
338 "vector of length " +
339 std::to_string(vector_size) + ".");
340 }
341
342 auto validate_estimation_type = [&](const std::string& est_type) {
343 if (est_type != "constant" && est_type != "fixed_effects" &&
344 est_type != "random_effects") {
345 throw std::invalid_argument(
346 "Invalid estimation_type: " + est_type +
347 ". Valid options are: constant, fixed_effects, or random_effects.");
348 }
349 };
350
351 for (size_t i = 0; i < vector_size; i++) {
352 std::string est_type =
353 Rcpp::as<std::string>(estimation_types[input_size == 1 ? 0 : i]);
355 this->storage_m->at(i).estimation_type_m.set(est_type);
356 }
357 }
358
366 void fill(double value) {
367 for (size_t i = 0; i < this->storage_m->size(); i++) {
368 storage_m->at(i).initial_value_m = value;
369 }
370 }
371
376 void show() {
377 Rcpp::Rcout << this->storage_m->data() << "\n";
378
379 for (size_t i = 0; i < this->storage_m->size(); i++) {
380 Rcpp::Rcout << storage_m->at(i) << " ";
381 }
382 }
383
389 copy.storage_m = std::make_shared<std::vector<Variable>>();
390 copy.storage_m->reserve(this->storage_m->size());
391 for (size_t i = 0; i < this->storage_m->size(); i++) {
393 const Variable& variable = this->storage_m->at(i);
394 variable_copy.initial_value_m = variable.initial_value_m;
395 variable_copy.final_value_m = variable.final_value_m;
396 variable_copy.estimation_type_m =
397 SharedString(variable.estimation_type_m.get());
398 copy.storage_m->push_back(variable_copy);
399 }
400 return copy;
401 }
402};
403
404#ifdef FIMS_HEADER_ONLY
406#endif
407
415inline std::ostream& operator<<(std::ostream& out, VariableVector& v) {
416 out << "[";
417 size_t size = v.size();
418 for (size_t i = 0; i < size - 1; i++) {
419 out << v[i] << ", ";
420 }
421 out << v[size - 1] << "]";
422 return out;
423}
424
433 public:
441 std::shared_ptr<std::vector<double>> storage_m;
446
451 this->id_m = RealVector::id_g++;
452 this->storage_m = std::make_shared<std::vector<double>>();
453 this->storage_m->resize(1);
454 }
455
461
465 RealVector(size_t size) {
466 this->id_m = RealVector::id_g++;
467 this->storage_m = std::make_shared<std::vector<double>>();
468 this->storage_m->resize(size);
469 }
470
476 RealVector(Rcpp::NumericVector x, size_t size) {
477 this->id_m = RealVector::id_g++;
478 this->storage_m = std::make_shared<std::vector<double>>();
479 const size_t input_size = static_cast<size_t>(x.size());
480 if (input_size != size) {
481 throw std::invalid_argument(
482 "RealVector::RealVector(Rcpp::NumericVector, size_t): `x` length (" +
483 std::to_string(input_size) +
484 ") must equal the requested "
485 "size (" +
486 std::to_string(size) +
487 "). Received length: " + std::to_string(input_size) + ".");
488 }
489 this->storage_m->assign(x.begin(), x.end());
490 }
491
497 this->id_m = RealVector::id_g++;
498 this->storage_m = std::make_shared<std::vector<double>>();
499 this->storage_m->resize(v.size());
500 for (size_t i = 0; i < v.size(); i++) {
501 storage_m->at(i) = v[i];
502 }
503 }
504
509 virtual ~RealVector() {}
510
517 RealVector& operator=(const Rcpp::NumericVector& v) {
518 this->storage_m->assign(v.begin(), v.end());
519 return *this;
520 }
521
525 virtual uint32_t get_id() { return this->id_m; }
526
532 void set_values(const Rcpp::NumericVector& orig) {
533 this->storage_m->resize(orig.size());
534 for (size_t i = 0; i < this->storage_m->size(); i++) {
535 this->storage_m->at(i) = orig[i];
536 }
537 }
538
544 Rcpp::NumericVector get_values() {
545 Rcpp::NumericVector ret(this->storage_m->size());
546 for (size_t i = 0; i < this->size(); i++) {
547 ret[i] = this->storage_m->at(i);
548 }
549
550 return ret;
551 }
552
557 inline double& operator[](size_t pos) { return this->storage_m->at(pos); }
558
565 if (static_cast<size_t>(pos) == 0 ||
566 static_cast<size_t>(pos) > this->storage_m->size()) {
567 throw std::invalid_argument("RealVector: Index out of range");
568 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
569 fims::to_string(this->size()));
570 return NULL;
571 }
572 return Rcpp::wrap(this->storage_m->at(pos - 1));
573 }
574
582 double& get(size_t pos) {
583 if (pos >= this->storage_m->size()) {
584 throw std::invalid_argument("RealVector: Index out of range");
585 }
586 return (this->storage_m->at(pos));
587 }
588
598 void set(size_t pos, const double& p) { this->storage_m->at(pos) = p; }
599
603 size_t size() { return this->storage_m->size(); }
604
610 void resize(size_t size) { this->storage_m->resize(size); }
611
619 void fill(double value) {
620 for (size_t i = 0; i < this->storage_m->size(); i++) {
621 storage_m->at(i) = value;
622 }
623 }
624
629 void show() {
630 Rcpp::Rcout << this->storage_m->data() << "\n";
631
632 for (size_t i = 0; i < this->storage_m->size(); i++) {
633 Rcpp::Rcout << storage_m->at(i) << " ";
634 }
635 }
636
642 copy.storage_m = std::make_shared<std::vector<double>>(*this->storage_m);
643 return copy;
644 }
645};
646#ifdef FIMS_HEADER_ONLY
648#endif
649
652
653
657 public:
661 bool finalized = false;
665 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
667
671 virtual bool add_to_fims_tmb() {
672 Rcpp::Rcout << "fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
673 "implemented.\n";
674 return false;
675 }
676
681 virtual void finalize() {}
682
686 virtual std::string to_json() {
687 FIMS_WARNING_LOG("Method not yet defined.");
688 return "{\"name\": \"not yet implemented\"}";
689 }
690
697 std::string value_to_string(double value) {
698 std::stringstream ss;
699 if (value == std::numeric_limits<double>::infinity()) {
700 ss << "\"Infinity\"";
701 } else if (value == -std::numeric_limits<double>::infinity()) {
702 ss << "\"-Infinity\"";
703 } else if (value != value) {
704 ss << "-999";
705 } else {
706 // Set precision (R default is 16)
707 ss << std::fixed << std::setprecision(16) << value;
708 }
709 return ss.str();
710 }
715 std::stringstream ss;
716
717 for (size_t i = 0; i < rep; i++) {
718 for (size_t j = start; j < end; j++) {
719 ss << j << ", ";
720 }
721 if (i < (rep - 1)) {
722 ss << end << ", ";
723 } else {
724 ss << end;
725 }
726 }
727 return ss.str();
728 }
729};
730
731#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:656
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:714
std::string value_to_string(double value)
Report the variable value as a string.
Definition rcpp_interface_base.hpp:697
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:666
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:671
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_interface_base.hpp:686
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_interface_base.hpp:681
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:432
double & get(size_t pos)
An internal accessor for calling a position of a RealVector from R.
Definition rcpp_interface_base.hpp:582
virtual ~RealVector()
Destroy the real Vector object.
Definition rcpp_interface_base.hpp:509
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:603
RealVector(const RealVector &other)
The constructor.
Definition rcpp_interface_base.hpp:459
Rcpp::NumericVector get_values()
Definition rcpp_interface_base.hpp:544
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:598
RealVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:465
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:610
std::shared_ptr< std::vector< double > > storage_m
real storage.
Definition rcpp_interface_base.hpp:441
RealVector & operator=(const Rcpp::NumericVector &v)
Definition rcpp_interface_base.hpp:517
uint32_t id_m
The local ID of the RealVector object.
Definition rcpp_interface_base.hpp:445
void fill(double value)
Sets the value of all elements in the RealVector to the provided value.
Definition rcpp_interface_base.hpp:619
static uint32_t id_g
The static ID of the RealVector object.
Definition rcpp_interface_base.hpp:437
void set_values(const Rcpp::NumericVector &orig)
Definition rcpp_interface_base.hpp:532
virtual uint32_t get_id()
Gets the ID of the RealVector object.
Definition rcpp_interface_base.hpp:525
RealVector deep_copy() const
Create a deep copy with a new RealVector ID.
Definition rcpp_interface_base.hpp:640
RealVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:476
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:564
double & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:557
RealVector(const fims::Vector< double > &v)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:496
void show()
The printing methods for a RealVector.
Definition rcpp_interface_base.hpp:629
RealVector()
The constructor.
Definition rcpp_interface_base.hpp:450
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1513
An Rcpp interface class that defines the VariableVector class.
Definition rcpp_interface_base.hpp:144
void show()
The printing methods for a VariableVector.
Definition rcpp_interface_base.hpp:376
void set_estimation_types(Rcpp::CharacterVector estimation_types)
Sets the estimation type for all Variables within a VariableVector.
Definition rcpp_interface_base.hpp:322
VariableVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a variable vector.
Definition rcpp_interface_base.hpp:191
VariableVector()
The constructor.
Definition rcpp_interface_base.hpp:162
VariableVector deep_copy() const
Create a deep copy with a new VariableVector ID.
Definition rcpp_interface_base.hpp:387
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:267
static uint32_t id_g
The static ID of the Variable object.
Definition rcpp_interface_base.hpp:149
VariableVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:177
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:249
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:157
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:283
VariableVector(const fims::Vector< double > &v)
The constructor for initializing a variable vector.
Definition rcpp_interface_base.hpp:218
virtual uint32_t get_id()
Gets the ID of the VariableVector object.
Definition rcpp_interface_base.hpp:236
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:295
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:288
Variable & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:242
std::shared_ptr< std::vector< Variable > > storage_m
Variable storage.
Definition rcpp_interface_base.hpp:153
void fill(double value)
Sets the value of all Variables in the VariableVector to the provided value.
Definition rcpp_interface_base.hpp:366
virtual ~VariableVector()
Destroy the Variable Vector object.
Definition rcpp_interface_base.hpp:231
VariableVector(const VariableVector &other)
The constructor.
Definition rcpp_interface_base.hpp:171
void set_values(Rcpp::NumericVector values)
Sets the initial values for all Variables within a VariableVector.
Definition rcpp_interface_base.hpp:300
An Rcpp interface that defines the Variable class.
Definition rcpp_interface_base.hpp:31
Variable(double value, std::string estimation_type)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:58
SharedString estimation_type_m
A string indicating the estimation type. Options are: constant, fixed_effects, or random_effects,...
Definition rcpp_interface_base.hpp:53
double initial_value_m
The initial value of the variable.
Definition rcpp_interface_base.hpp:44
Variable & operator=(const Variable &right)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:75
Variable()
The constructor for initializing a Variable.
Definition rcpp_interface_base.hpp:97
Variable(const Variable &other)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:66
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:40
double final_value_m
The final value of the variable.
Definition rcpp_interface_base.hpp:48
static uint32_t id_g
The static ID of the Variable object.
Definition rcpp_interface_base.hpp:36
Variable(double value)
The constructor for initializing a variable.
Definition rcpp_interface_base.hpp:88
Definition fims_vector.hpp:27
#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....
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:236
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:113
std::ostream & operator<<(std::ostream &out, const Variable &p)
Output for std::ostream& for a variable.
Definition rcpp_interface_base.hpp:127
Code to create shared pointers to allow for the wrapper functions in R to work correctly so the live ...