FIMS  v0.8.1
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#include <RcppCommon.h>
13#include <map>
14#include <vector>
15
16#include "../../../common/def.hpp"
17#include "../../../common/information.hpp"
18#include "../../interface.hpp"
20#include <limits>
21
22#define RCPP_NO_SUGAR
23#include <Rcpp.h>
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};
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
127std::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
143 public:
151 std::shared_ptr<std::vector<Parameter>> storage_m;
156
161 this->id_m = ParameterVector::id_g++;
162 this->storage_m = std::make_shared<std::vector<Parameter>>();
163 this->storage_m->resize(1); // push_back(Rcpp::wrap(p));
164 }
165
171
176 this->id_m = ParameterVector::id_g++;
177 this->storage_m = std::make_shared<std::vector<Parameter>>();
178 this->storage_m->resize(size);
179 for (size_t i = 0; i < size; i++) {
180 storage_m->at(i) = Parameter();
181 }
182 }
183
189 ParameterVector(Rcpp::NumericVector x, size_t size) {
190 if (static_cast<size_t>(x.size()) != size) {
191 throw std::invalid_argument(
192 "Error in call to ParameterVector(Rcpp::NumericVector x, size_t "
193 "size): x.size() != size argument.");
194 } else {
195 this->id_m = ParameterVector::id_g++;
196 this->storage_m = std::make_shared<std::vector<Parameter>>();
197 // Use std::min to avoid comparing signed and unsigned types
198 size_t n = std::min(static_cast<size_t>(x.size()), size);
199 this->storage_m->resize(n);
200 for (size_t i = 0; i < n; i++) {
201 storage_m->at(i).initial_value_m = x[i];
202 }
203 }
204 }
205
211 this->id_m = ParameterVector::id_g++;
212 this->storage_m = std::make_shared<std::vector<Parameter>>();
213 this->storage_m->resize(v.size());
214 for (size_t i = 0; i < v.size(); i++) {
215 storage_m->at(i).initial_value_m = v[i];
216 }
217 }
218
223 virtual ~ParameterVector() {}
224
228 virtual uint32_t get_id() { return this->id_m; }
229
234 inline Parameter& operator[](size_t pos) { return this->storage_m->at(pos); }
235
242 if (static_cast<size_t>(pos) == 0 ||
243 static_cast<size_t>(pos) > this->storage_m->size()) {
244 throw std::invalid_argument("ParameterVector: Index out of range");
245 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
246 fims::to_string(this->size()));
247 return NULL;
248 }
249 return Rcpp::wrap(this->storage_m->at(pos - 1));
250 }
251
259 Parameter& get(size_t pos) {
260 if (pos >= this->storage_m->size()) {
261 throw std::invalid_argument("ParameterVector: Index out of range");
262 }
263 return (this->storage_m->at(pos));
264 }
265
275 void set(size_t pos, const Parameter& p) { this->storage_m->at(pos) = p; }
276
280 size_t size() { return this->storage_m->size(); }
281
287 void resize(size_t size) { this->storage_m->resize(size); }
288
297 for (size_t i = 0; i < this->storage_m->size(); i++) {
298 if (estimable) {
299 this->storage_m->at(i).estimation_type_m.set("fixed_effects");
300 } else {
301 this->storage_m->at(i).estimation_type_m.set("constant");
302 }
303 }
304 }
305
314 for (size_t i = 0; i < this->storage_m->size(); i++) {
315 if (random) {
316 this->storage_m->at(i).estimation_type_m.set("random_effects");
317 } else {
318 this->storage_m->at(i).estimation_type_m.set("constant");
319 }
320 }
321 }
322
330 void fill(double value) {
331 for (size_t i = 0; i < this->storage_m->size(); i++) {
332 storage_m->at(i).initial_value_m = value;
333 }
334 }
335
340 void show() {
341 Rcpp::Rcout << this->storage_m->data() << "\n";
342
343 for (size_t i = 0; i < this->storage_m->size(); i++) {
344 Rcpp::Rcout << storage_m->at(i) << " ";
345 }
346 }
347};
349
357std::ostream& operator<<(std::ostream& out, ParameterVector& v) {
358 out << "[";
359 size_t size = v.size();
360 for (size_t i = 0; i < size - 1; i++) {
361 out << v[i] << ", ";
362 }
363 out << v[size - 1] << "]";
364 return out;
365}
366
375 public:
383 std::shared_ptr<std::vector<double>> storage_m;
388
393 this->id_m = RealVector::id_g++;
394 this->storage_m = std::make_shared<std::vector<double>>();
395 this->storage_m->resize(1);
396 }
397
403
407 RealVector(size_t size) {
408 this->id_m = RealVector::id_g++;
409 this->storage_m = std::make_shared<std::vector<double>>();
410 this->storage_m->resize(size);
411 }
412
418 RealVector(Rcpp::NumericVector x, size_t size) {
419 this->id_m = RealVector::id_g++;
420 this->storage_m = std::make_shared<std::vector<double>>();
421 this->storage_m->assign(x.begin(), x.end());
422 }
423
429 this->id_m = RealVector::id_g++;
430 this->storage_m = std::make_shared<std::vector<double>>();
431 this->storage_m->resize(v.size());
432 for (size_t i = 0; i < v.size(); i++) {
433 storage_m->at(i) = v[i];
434 }
435 }
436
441 virtual ~RealVector() {}
442
449 RealVector& operator=(const Rcpp::NumericVector& v) {
450 this->storage_m->assign(v.begin(), v.end());
451 return *this;
452 }
453
457 virtual uint32_t get_id() { return this->id_m; }
458
464 void fromRVector(const Rcpp::NumericVector& orig) {
465 this->storage_m->resize(orig.size());
466 for (size_t i = 0; i < this->storage_m->size(); i++) {
467 this->storage_m->at(i) = orig[i];
468 }
469 }
470
476 Rcpp::NumericVector toRVector() {
477 Rcpp::NumericVector ret(this->storage_m->size());
478 for (size_t i = 0; i < this->size(); i++) {
479 ret[i] = this->storage_m->at(i);
480 }
481
482 return ret;
483 }
484
489 inline double& operator[](size_t pos) { return this->storage_m->at(pos); }
490
497 if (static_cast<size_t>(pos) == 0 ||
498 static_cast<size_t>(pos) > this->storage_m->size()) {
499 throw std::invalid_argument("RealVector: Index out of range");
500 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
501 fims::to_string(this->size()));
502 return NULL;
503 }
504 return Rcpp::wrap(this->storage_m->at(pos - 1));
505 }
506
514 double& get(size_t pos) {
515 if (pos >= this->storage_m->size()) {
516 throw std::invalid_argument("RealVector: Index out of range");
517 }
518 return (this->storage_m->at(pos));
519 }
520
530 void set(size_t pos, const double& p) { this->storage_m->at(pos) = p; }
531
535 size_t size() { return this->storage_m->size(); }
536
542 void resize(size_t size) { this->storage_m->resize(size); }
543
551 void fill(double value) {
552 for (size_t i = 0; i < this->storage_m->size(); i++) {
553 storage_m->at(i) = value;
554 }
555 }
556
561 void show() {
562 Rcpp::Rcout << this->storage_m->data() << "\n";
563
564 for (size_t i = 0; i < this->storage_m->size(); i++) {
565 Rcpp::Rcout << storage_m->at(i) << " ";
566 }
567 }
568};
570
575 public:
579 bool finalized = false;
583 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
585
589 virtual bool add_to_fims_tmb() {
590 Rcpp::Rcout << "fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
591 "implemented.\n";
592 return false;
593 }
594
599 virtual void finalize() {}
600
604 virtual std::string to_json() {
605 FIMS_WARNING_LOG("Method not yet defined.");
606 return "{\"name\": \"not yet implemented\"}";
607 }
608
615 std::string value_to_string(double value) {
616 std::stringstream ss;
617 if (value == std::numeric_limits<double>::infinity()) {
618 ss << "\"Infinity\"";
619 } else if (value == -std::numeric_limits<double>::infinity()) {
620 ss << "\"-Infinity\"";
621 } else if (value != value) {
622 ss << "-999";
623 } else {
624 // Set precision (R default is 16)
625 ss << std::fixed << std::setprecision(16) << value;
626 }
627 return ss.str();
628 }
633 std::stringstream ss;
634
635 for (size_t i = 0; i < rep; i++) {
636 for (size_t j = start; j < end; j++) {
637 ss << j << ", ";
638 }
639 if (i < (rep - 1)) {
640 ss << end << ", ";
641 } else {
642 ss << end;
643 }
644 }
645 return ss.str();
646 }
647};
648std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
650
651#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:574
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:632
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:579
std::string value_to_string(double value)
Report the parameter value as a string.
Definition rcpp_interface_base.hpp:615
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:584
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:589
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_interface_base.hpp:604
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_interface_base.hpp:599
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:142
ParameterVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:175
void show()
The printing methods for a ParameterVector.
Definition rcpp_interface_base.hpp:340
void set_all_random(bool random)
Sets all Parameters within a ParameterVector as random effects.
Definition rcpp_interface_base.hpp:313
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:287
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:280
ParameterVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:189
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:147
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:155
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:241
ParameterVector(const fims::Vector< double > &v)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:210
ParameterVector(const ParameterVector &other)
The constructor.
Definition rcpp_interface_base.hpp:169
virtual uint32_t get_id()
Gets the ID of the ParameterVector object.
Definition rcpp_interface_base.hpp:228
void fill(double value)
Sets the value of all Parameters in the ParameterVector to the provided value.
Definition rcpp_interface_base.hpp:330
Parameter & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:234
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:259
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:275
ParameterVector()
The constructor.
Definition rcpp_interface_base.hpp:160
std::shared_ptr< std::vector< Parameter > > storage_m
Parameter storage.
Definition rcpp_interface_base.hpp:151
virtual ~ParameterVector()
Destroy the Parameter Vector object.
Definition rcpp_interface_base.hpp:223
void set_all_estimable(bool estimable)
Sets all Parameters within a ParameterVector as estimable.
Definition rcpp_interface_base.hpp:296
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:374
double & get(size_t pos)
An internal accessor for calling a position of a RealVector from R.
Definition rcpp_interface_base.hpp:514
virtual ~RealVector()
Destroy the real Vector object.
Definition rcpp_interface_base.hpp:441
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:535
RealVector(const RealVector &other)
The constructor.
Definition rcpp_interface_base.hpp:401
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:530
RealVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:407
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:542
std::shared_ptr< std::vector< double > > storage_m
real storage.
Definition rcpp_interface_base.hpp:383
RealVector & operator=(const Rcpp::NumericVector &v)
Definition rcpp_interface_base.hpp:449
uint32_t id_m
The local ID of the RealVector object.
Definition rcpp_interface_base.hpp:387
void fill(double value)
Sets the value of all elements in the RealVector to the provided value.
Definition rcpp_interface_base.hpp:551
Rcpp::NumericVector toRVector()
Definition rcpp_interface_base.hpp:476
static uint32_t id_g
The static ID of the RealVector object.
Definition rcpp_interface_base.hpp:379
virtual uint32_t get_id()
Gets the ID of the RealVector object.
Definition rcpp_interface_base.hpp:457
RealVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:418
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:496
double & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:489
RealVector(const fims::Vector< double > &v)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:428
void show()
The printing methods for a RealVector.
Definition rcpp_interface_base.hpp:561
void fromRVector(const Rcpp::NumericVector &orig)
Definition rcpp_interface_base.hpp:464
RealVector()
The constructor.
Definition rcpp_interface_base.hpp:392
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1508
Definition fims_vector.hpp:27
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:545
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:553
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:239
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 ...