FIMS  v0.9.3
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 Parameter {
32 public:
36 static uint32_t id_g;
44 double initial_value_m = 0.0;
48 double final_value_m = 0.0;
54
58 Parameter(double value, std::string estimation_type)
59 : id_m(Parameter::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 Parameter(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 Parameter& 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<Parameter>> storage_m;
158
163 this->id_m = ParameterVector::id_g++;
164 this->storage_m = std::make_shared<std::vector<Parameter>>();
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 ParameterVector(size_t size) {
178 this->id_m = ParameterVector::id_g++;
179 this->storage_m = std::make_shared<std::vector<Parameter>>();
180 this->storage_m->resize(size);
181 for (size_t i = 0; i < size; i++) {
182 storage_m->at(i) = Parameter();
183 }
184 }
185
191 ParameterVector(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 "ParameterVector::ParameterVector(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 = ParameterVector::id_g++;
204 this->storage_m = std::make_shared<std::vector<Parameter>>();
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 = ParameterVector::id_g++;
220 this->storage_m = std::make_shared<std::vector<Parameter>>();
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 ~ParameterVector() {}
232
236 virtual uint32_t get_id() { return this->id_m; }
237
242 inline Parameter& 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("ParameterVector: 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 Parameter& get(size_t pos) {
268 if (pos >= this->storage_m->size()) {
269 throw std::invalid_argument("ParameterVector: Index out of range");
270 }
271 return (this->storage_m->at(pos));
272 }
273
283 void set(size_t pos, const Parameter& 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 "ParameterVector::set_values(): `values` length (" +
306 std::to_string(input_size) +
307 ") must equal the ParameterVector "
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 "ParameterVector::set_estimation_types(): `estimation_types` length "
329 "(" +
330 std::to_string(input_size) +
331 ") must be 1 (broadcast) or equal to the ParameterVector 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};
384
385#ifdef FIMS_HEADER_ONLY
387#endif
388
396inline std::ostream& operator<<(std::ostream& out, ParameterVector& v) {
397 out << "[";
398 size_t size = v.size();
399 for (size_t i = 0; i < size - 1; i++) {
400 out << v[i] << ", ";
401 }
402 out << v[size - 1] << "]";
403 return out;
404}
405
414 public:
422 std::shared_ptr<std::vector<double>> storage_m;
427
432 this->id_m = RealVector::id_g++;
433 this->storage_m = std::make_shared<std::vector<double>>();
434 this->storage_m->resize(1);
435 }
436
442
446 RealVector(size_t size) {
447 this->id_m = RealVector::id_g++;
448 this->storage_m = std::make_shared<std::vector<double>>();
449 this->storage_m->resize(size);
450 }
451
457 RealVector(Rcpp::NumericVector x, size_t size) {
458 this->id_m = RealVector::id_g++;
459 this->storage_m = std::make_shared<std::vector<double>>();
460 const size_t input_size = static_cast<size_t>(x.size());
461 if (input_size != size) {
462 throw std::invalid_argument(
463 "RealVector::RealVector(Rcpp::NumericVector, size_t): `x` length (" +
464 std::to_string(input_size) +
465 ") must equal the requested "
466 "size (" +
467 std::to_string(size) +
468 "). Received length: " + std::to_string(input_size) + ".");
469 }
470 this->storage_m->assign(x.begin(), x.end());
471 }
472
478 this->id_m = RealVector::id_g++;
479 this->storage_m = std::make_shared<std::vector<double>>();
480 this->storage_m->resize(v.size());
481 for (size_t i = 0; i < v.size(); i++) {
482 storage_m->at(i) = v[i];
483 }
484 }
485
490 virtual ~RealVector() {}
491
498 RealVector& operator=(const Rcpp::NumericVector& v) {
499 this->storage_m->assign(v.begin(), v.end());
500 return *this;
501 }
502
506 virtual uint32_t get_id() { return this->id_m; }
507
513 void set_values(const Rcpp::NumericVector& orig) {
514 this->storage_m->resize(orig.size());
515 for (size_t i = 0; i < this->storage_m->size(); i++) {
516 this->storage_m->at(i) = orig[i];
517 }
518 }
519
525 Rcpp::NumericVector get_values() {
526 Rcpp::NumericVector ret(this->storage_m->size());
527 for (size_t i = 0; i < this->size(); i++) {
528 ret[i] = this->storage_m->at(i);
529 }
530
531 return ret;
532 }
533
538 inline double& operator[](size_t pos) { return this->storage_m->at(pos); }
539
546 if (static_cast<size_t>(pos) == 0 ||
547 static_cast<size_t>(pos) > this->storage_m->size()) {
548 throw std::invalid_argument("RealVector: Index out of range");
549 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
550 fims::to_string(this->size()));
551 return NULL;
552 }
553 return Rcpp::wrap(this->storage_m->at(pos - 1));
554 }
555
563 double& get(size_t pos) {
564 if (pos >= this->storage_m->size()) {
565 throw std::invalid_argument("RealVector: Index out of range");
566 }
567 return (this->storage_m->at(pos));
568 }
569
579 void set(size_t pos, const double& p) { this->storage_m->at(pos) = p; }
580
584 size_t size() { return this->storage_m->size(); }
585
591 void resize(size_t size) { this->storage_m->resize(size); }
592
600 void fill(double value) {
601 for (size_t i = 0; i < this->storage_m->size(); i++) {
602 storage_m->at(i) = value;
603 }
604 }
605
610 void show() {
611 Rcpp::Rcout << this->storage_m->data() << "\n";
612
613 for (size_t i = 0; i < this->storage_m->size(); i++) {
614 Rcpp::Rcout << storage_m->at(i) << " ";
615 }
616 }
617};
618#ifdef FIMS_HEADER_ONLY
620#endif
621
624
625
629 public:
633 bool finalized = false;
637 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
639
643 virtual bool add_to_fims_tmb() {
644 Rcpp::Rcout << "fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
645 "implemented.\n";
646 return false;
647 }
648
653 virtual void finalize() {}
654
658 virtual std::string to_json() {
659 FIMS_WARNING_LOG("Method not yet defined.");
660 return "{\"name\": \"not yet implemented\"}";
661 }
662
669 std::string value_to_string(double value) {
670 std::stringstream ss;
671 if (value == std::numeric_limits<double>::infinity()) {
672 ss << "\"Infinity\"";
673 } else if (value == -std::numeric_limits<double>::infinity()) {
674 ss << "\"-Infinity\"";
675 } else if (value != value) {
676 ss << "-999";
677 } else {
678 // Set precision (R default is 16)
679 ss << std::fixed << std::setprecision(16) << value;
680 }
681 return ss.str();
682 }
687 std::stringstream ss;
688
689 for (size_t i = 0; i < rep; i++) {
690 for (size_t j = start; j < end; j++) {
691 ss << j << ", ";
692 }
693 if (i < (rep - 1)) {
694 ss << end << ", ";
695 } else {
696 ss << end;
697 }
698 }
699 return ss.str();
700 }
701};
702
703#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:628
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:686
std::string value_to_string(double value)
Report the parameter value as a string.
Definition rcpp_interface_base.hpp:669
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:638
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:643
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_interface_base.hpp:658
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_interface_base.hpp:653
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:144
ParameterVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:177
void show()
The printing methods for a ParameterVector.
Definition rcpp_interface_base.hpp:376
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:295
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:288
ParameterVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:191
void set_estimation_types(Rcpp::CharacterVector estimation_types)
Sets the estimation type for all Parameters within a ParameterVector.
Definition rcpp_interface_base.hpp:322
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:149
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:157
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
ParameterVector(const fims::Vector< double > &v)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:218
ParameterVector(const ParameterVector &other)
The constructor.
Definition rcpp_interface_base.hpp:171
virtual uint32_t get_id()
Gets the ID of the ParameterVector object.
Definition rcpp_interface_base.hpp:236
void fill(double value)
Sets the value of all Parameters in the ParameterVector to the provided value.
Definition rcpp_interface_base.hpp:366
void set_values(Rcpp::NumericVector values)
Sets the initial values for all Parameters within a ParameterVector.
Definition rcpp_interface_base.hpp:300
Parameter & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:242
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:267
void set(size_t pos, const Parameter &p)
An internal setter for setting a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:283
ParameterVector()
The constructor.
Definition rcpp_interface_base.hpp:162
std::shared_ptr< std::vector< Parameter > > storage_m
Parameter storage.
Definition rcpp_interface_base.hpp:153
virtual ~ParameterVector()
Destroy the Parameter Vector object.
Definition rcpp_interface_base.hpp:231
An Rcpp interface that defines the Parameter class.
Definition rcpp_interface_base.hpp:31
double initial_value_m
The initial value of the parameter.
Definition rcpp_interface_base.hpp:44
Parameter(double value)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:88
Parameter(double value, std::string estimation_type)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:58
Parameter & operator=(const Parameter &right)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:75
Parameter()
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:97
double final_value_m
The final value of the parameter.
Definition rcpp_interface_base.hpp:48
SharedString estimation_type_m
A string indicating the estimation type. Options are: constant, fixed_effects, or random_effects,...
Definition rcpp_interface_base.hpp:53
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:40
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:36
Parameter(const Parameter &other)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:66
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:413
double & get(size_t pos)
An internal accessor for calling a position of a RealVector from R.
Definition rcpp_interface_base.hpp:563
virtual ~RealVector()
Destroy the real Vector object.
Definition rcpp_interface_base.hpp:490
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:584
RealVector(const RealVector &other)
The constructor.
Definition rcpp_interface_base.hpp:440
Rcpp::NumericVector get_values()
Definition rcpp_interface_base.hpp:525
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:579
RealVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:446
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:591
std::shared_ptr< std::vector< double > > storage_m
real storage.
Definition rcpp_interface_base.hpp:422
RealVector & operator=(const Rcpp::NumericVector &v)
Definition rcpp_interface_base.hpp:498
uint32_t id_m
The local ID of the RealVector object.
Definition rcpp_interface_base.hpp:426
void fill(double value)
Sets the value of all elements in the RealVector to the provided value.
Definition rcpp_interface_base.hpp:600
static uint32_t id_g
The static ID of the RealVector object.
Definition rcpp_interface_base.hpp:418
void set_values(const Rcpp::NumericVector &orig)
Definition rcpp_interface_base.hpp:513
virtual uint32_t get_id()
Gets the ID of the RealVector object.
Definition rcpp_interface_base.hpp:506
RealVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:457
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:545
double & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:538
RealVector(const fims::Vector< double > &v)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:477
void show()
The printing methods for a RealVector.
Definition rcpp_interface_base.hpp:610
RealVector()
The constructor.
Definition rcpp_interface_base.hpp:431
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1513
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:235
std::ostream & operator<<(std::ostream &out, const Parameter &p)
Output for std::ostream& for a parameter.
Definition rcpp_interface_base.hpp:127
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:113
Code to create shared pointers to allow for the wrapper functions in R to work correctly so the live ...