FIMS  v0.9.2
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
20#include "../../../common/def.hpp"
21#include "../../../common/information.hpp"
22#include "../../interface.hpp"
24#include <limits>
25
32class Parameter {
33 public:
37 static uint32_t id_g;
45 double initial_value_m = 0.0;
49 double final_value_m = 0.0;
55
59 Parameter(double value, std::string estimation_type)
60 : id_m(Parameter::id_g++),
61 initial_value_m(value),
63
72
77 // Check for self-assignment!
78 if (this == &right) // Same object?
79 return *this; // Yes, so skip assignment, and just return *this.
80 this->id_m = right.id_m;
81 this->initial_value_m = right.initial_value_m;
82 this->estimation_type_m = right.estimation_type_m;
83 return *this;
84 }
85
89 Parameter(double value) {
90 initial_value_m = value;
92 }
93
101 }
102};
107
114inline double sanitize_val(double x) {
115 if (std::isnan(x) || std::isinf(x)) {
116 return -999.0;
117 }
118 return x;
119}
120
128std::ostream& operator<<(std::ostream& out, const Parameter& p) {
129 out << "{\"id\": " << p.id_m
130 << ",\n\"value\": " << sanitize_val(p.initial_value_m)
131 << ",\n\"estimated_value\": " << sanitize_val(p.final_value_m);
132 out << ",\n\"estimation_type\": \"" << p.estimation_type_m << "\"\n}";
133
134 return out;
135}
136
144 public:
152 std::shared_ptr<std::vector<Parameter>> storage_m;
157
162 this->id_m = ParameterVector::id_g++;
163 this->storage_m = std::make_shared<std::vector<Parameter>>();
164 this->storage_m->resize(1); // push_back(Rcpp::wrap(p));
165 }
166
172
177 this->id_m = ParameterVector::id_g++;
178 this->storage_m = std::make_shared<std::vector<Parameter>>();
179 this->storage_m->resize(size);
180 for (size_t i = 0; i < size; i++) {
181 storage_m->at(i) = Parameter();
182 }
183 }
184
190 ParameterVector(Rcpp::NumericVector x, size_t size) {
191 if (static_cast<size_t>(x.size()) != size) {
192 throw std::invalid_argument(
193 "Error in call to ParameterVector(Rcpp::NumericVector x, size_t "
194 "size): x.size() != size argument.");
195 } else {
196 this->id_m = ParameterVector::id_g++;
197 this->storage_m = std::make_shared<std::vector<Parameter>>();
198 // Use std::min to avoid comparing signed and unsigned types
199 size_t n = std::min(static_cast<size_t>(x.size()), size);
200 this->storage_m->resize(n);
201 for (size_t i = 0; i < n; i++) {
202 storage_m->at(i).initial_value_m = x[i];
203 }
204 }
205 }
206
212 this->id_m = ParameterVector::id_g++;
213 this->storage_m = std::make_shared<std::vector<Parameter>>();
214 this->storage_m->resize(v.size());
215 for (size_t i = 0; i < v.size(); i++) {
216 storage_m->at(i).initial_value_m = v[i];
217 }
218 }
219
224 virtual ~ParameterVector() {}
225
229 virtual uint32_t get_id() { return this->id_m; }
230
235 inline Parameter& operator[](size_t pos) { return this->storage_m->at(pos); }
236
243 if (static_cast<size_t>(pos) == 0 ||
244 static_cast<size_t>(pos) > this->storage_m->size()) {
245 throw std::invalid_argument("ParameterVector: Index out of range");
246 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
247 fims::to_string(this->size()));
248 return NULL;
249 }
250 return Rcpp::wrap(this->storage_m->at(pos - 1));
251 }
252
260 Parameter& get(size_t pos) {
261 if (pos >= this->storage_m->size()) {
262 throw std::invalid_argument("ParameterVector: Index out of range");
263 }
264 return (this->storage_m->at(pos));
265 }
266
276 void set(size_t pos, const Parameter& p) { this->storage_m->at(pos) = p; }
277
281 size_t size() { return this->storage_m->size(); }
282
288 void resize(size_t size) { this->storage_m->resize(size); }
289
298 for (size_t i = 0; i < this->storage_m->size(); i++) {
299 if (estimable) {
300 this->storage_m->at(i).estimation_type_m.set("fixed_effects");
301 } else {
302 this->storage_m->at(i).estimation_type_m.set("constant");
303 }
304 }
305 }
306
315 for (size_t i = 0; i < this->storage_m->size(); i++) {
316 if (random) {
317 this->storage_m->at(i).estimation_type_m.set("random_effects");
318 } else {
319 this->storage_m->at(i).estimation_type_m.set("constant");
320 }
321 }
322 }
323
331 void fill(double value) {
332 for (size_t i = 0; i < this->storage_m->size(); i++) {
333 storage_m->at(i).initial_value_m = value;
334 }
335 }
336
341 void show() {
342 Rcpp::Rcout << this->storage_m->data() << "\n";
343
344 for (size_t i = 0; i < this->storage_m->size(); i++) {
345 Rcpp::Rcout << storage_m->at(i) << " ";
346 }
347 }
348};
350
358std::ostream& operator<<(std::ostream& out, ParameterVector& v) {
359 out << "[";
360 size_t size = v.size();
361 for (size_t i = 0; i < size - 1; i++) {
362 out << v[i] << ", ";
363 }
364 out << v[size - 1] << "]";
365 return out;
366}
367
376 public:
384 std::shared_ptr<std::vector<double>> storage_m;
389
394 this->id_m = RealVector::id_g++;
395 this->storage_m = std::make_shared<std::vector<double>>();
396 this->storage_m->resize(1);
397 }
398
404
408 RealVector(size_t size) {
409 this->id_m = RealVector::id_g++;
410 this->storage_m = std::make_shared<std::vector<double>>();
411 this->storage_m->resize(size);
412 }
413
419 RealVector(Rcpp::NumericVector x, size_t size) {
420 this->id_m = RealVector::id_g++;
421 this->storage_m = std::make_shared<std::vector<double>>();
422 this->storage_m->assign(x.begin(), x.end());
423 }
424
430 this->id_m = RealVector::id_g++;
431 this->storage_m = std::make_shared<std::vector<double>>();
432 this->storage_m->resize(v.size());
433 for (size_t i = 0; i < v.size(); i++) {
434 storage_m->at(i) = v[i];
435 }
436 }
437
442 virtual ~RealVector() {}
443
450 RealVector& operator=(const Rcpp::NumericVector& v) {
451 this->storage_m->assign(v.begin(), v.end());
452 return *this;
453 }
454
458 virtual uint32_t get_id() { return this->id_m; }
459
465 void fromRVector(const Rcpp::NumericVector& orig) {
466 this->storage_m->resize(orig.size());
467 for (size_t i = 0; i < this->storage_m->size(); i++) {
468 this->storage_m->at(i) = orig[i];
469 }
470 }
471
477 Rcpp::NumericVector toRVector() {
478 Rcpp::NumericVector ret(this->storage_m->size());
479 for (size_t i = 0; i < this->size(); i++) {
480 ret[i] = this->storage_m->at(i);
481 }
482
483 return ret;
484 }
485
490 inline double& operator[](size_t pos) { return this->storage_m->at(pos); }
491
498 if (static_cast<size_t>(pos) == 0 ||
499 static_cast<size_t>(pos) > this->storage_m->size()) {
500 throw std::invalid_argument("RealVector: Index out of range");
501 FIMS_ERROR_LOG(fims::to_string(pos) + "!<" +
502 fims::to_string(this->size()));
503 return NULL;
504 }
505 return Rcpp::wrap(this->storage_m->at(pos - 1));
506 }
507
515 double& get(size_t pos) {
516 if (pos >= this->storage_m->size()) {
517 throw std::invalid_argument("RealVector: Index out of range");
518 }
519 return (this->storage_m->at(pos));
520 }
521
531 void set(size_t pos, const double& p) { this->storage_m->at(pos) = p; }
532
536 size_t size() { return this->storage_m->size(); }
537
543 void resize(size_t size) { this->storage_m->resize(size); }
544
552 void fill(double value) {
553 for (size_t i = 0; i < this->storage_m->size(); i++) {
554 storage_m->at(i) = value;
555 }
556 }
557
562 void show() {
563 Rcpp::Rcout << this->storage_m->data() << "\n";
564
565 for (size_t i = 0; i < this->storage_m->size(); i++) {
566 Rcpp::Rcout << storage_m->at(i) << " ";
567 }
568 }
569};
571
576 public:
580 bool finalized = false;
584 static std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
586
590 virtual bool add_to_fims_tmb() {
591 Rcpp::Rcout << "fims_rcpp_interface_base::add_to_fims_tmb(): Not yet "
592 "implemented.\n";
593 return false;
594 }
595
600 virtual void finalize() {}
601
605 virtual std::string to_json() {
606 FIMS_WARNING_LOG("Method not yet defined.");
607 return "{\"name\": \"not yet implemented\"}";
608 }
609
616 std::string value_to_string(double value) {
617 std::stringstream ss;
618 if (value == std::numeric_limits<double>::infinity()) {
619 ss << "\"Infinity\"";
620 } else if (value == -std::numeric_limits<double>::infinity()) {
621 ss << "\"-Infinity\"";
622 } else if (value != value) {
623 ss << "-999";
624 } else {
625 // Set precision (R default is 16)
626 ss << std::fixed << std::setprecision(16) << value;
627 }
628 return ss.str();
629 }
634 std::stringstream ss;
635
636 for (size_t i = 0; i < rep; i++) {
637 for (size_t j = start; j < end; j++) {
638 ss << j << ", ";
639 }
640 if (i < (rep - 1)) {
641 ss << end << ", ";
642 } else {
643 ss << end;
644 }
645 }
646 return ss.str();
647 }
648};
649std::vector<std::shared_ptr<FIMSRcppInterfaceBase>>
651
652#endif
Base class for all interface objects.
Definition rcpp_interface_base.hpp:575
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:633
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:580
std::string value_to_string(double value)
Report the parameter value as a string.
Definition rcpp_interface_base.hpp:616
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:585
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:590
virtual std::string to_json()
Convert the data to json representation for the output.
Definition rcpp_interface_base.hpp:605
virtual void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_interface_base.hpp:600
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:143
ParameterVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:176
void show()
The printing methods for a ParameterVector.
Definition rcpp_interface_base.hpp:341
void set_all_random(bool random)
Sets all Parameters within a ParameterVector as random effects.
Definition rcpp_interface_base.hpp:314
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:288
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:281
ParameterVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:190
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:148
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:156
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:242
ParameterVector(const fims::Vector< double > &v)
The constructor for initializing a parameter vector.
Definition rcpp_interface_base.hpp:211
ParameterVector(const ParameterVector &other)
The constructor.
Definition rcpp_interface_base.hpp:170
virtual uint32_t get_id()
Gets the ID of the ParameterVector object.
Definition rcpp_interface_base.hpp:229
void fill(double value)
Sets the value of all Parameters in the ParameterVector to the provided value.
Definition rcpp_interface_base.hpp:331
Parameter & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:235
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:260
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:276
ParameterVector()
The constructor.
Definition rcpp_interface_base.hpp:161
std::shared_ptr< std::vector< Parameter > > storage_m
Parameter storage.
Definition rcpp_interface_base.hpp:152
virtual ~ParameterVector()
Destroy the Parameter Vector object.
Definition rcpp_interface_base.hpp:224
void set_all_estimable(bool estimable)
Sets all Parameters within a ParameterVector as estimable.
Definition rcpp_interface_base.hpp:297
An Rcpp interface that defines the Parameter class.
Definition rcpp_interface_base.hpp:32
double initial_value_m
The initial value of the parameter.
Definition rcpp_interface_base.hpp:45
Parameter(double value)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:89
Parameter(double value, std::string estimation_type)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:59
Parameter & operator=(const Parameter &right)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:76
Parameter()
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:98
double final_value_m
The final value of the parameter.
Definition rcpp_interface_base.hpp:49
SharedString estimation_type_m
A string indicating the estimation type. Options are: constant, fixed_effects, or random_effects,...
Definition rcpp_interface_base.hpp:54
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:41
static uint32_t id_g
The static ID of the Parameter object.
Definition rcpp_interface_base.hpp:37
Parameter(const Parameter &other)
The constructor for initializing a parameter.
Definition rcpp_interface_base.hpp:67
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:375
double & get(size_t pos)
An internal accessor for calling a position of a RealVector from R.
Definition rcpp_interface_base.hpp:515
virtual ~RealVector()
Destroy the real Vector object.
Definition rcpp_interface_base.hpp:442
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:536
RealVector(const RealVector &other)
The constructor.
Definition rcpp_interface_base.hpp:402
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:531
RealVector(size_t size)
The constructor.
Definition rcpp_interface_base.hpp:408
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:543
std::shared_ptr< std::vector< double > > storage_m
real storage.
Definition rcpp_interface_base.hpp:384
RealVector & operator=(const Rcpp::NumericVector &v)
Definition rcpp_interface_base.hpp:450
uint32_t id_m
The local ID of the RealVector object.
Definition rcpp_interface_base.hpp:388
void fill(double value)
Sets the value of all elements in the RealVector to the provided value.
Definition rcpp_interface_base.hpp:552
Rcpp::NumericVector toRVector()
Definition rcpp_interface_base.hpp:477
static uint32_t id_g
The static ID of the RealVector object.
Definition rcpp_interface_base.hpp:380
virtual uint32_t get_id()
Gets the ID of the RealVector object.
Definition rcpp_interface_base.hpp:458
RealVector(Rcpp::NumericVector x, size_t size)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:419
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:497
double & operator[](size_t pos)
The accessor where the first index starts is zero.
Definition rcpp_interface_base.hpp:490
RealVector(const fims::Vector< double > &v)
The constructor for initializing a real vector.
Definition rcpp_interface_base.hpp:429
void show()
The printing methods for a RealVector.
Definition rcpp_interface_base.hpp:562
void fromRVector(const Rcpp::NumericVector &orig)
Definition rcpp_interface_base.hpp:465
RealVector()
The constructor.
Definition rcpp_interface_base.hpp:393
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:639
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:655
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:128
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:114
Code to create shared pointers to allow for the wrapper functions in R to work correctly so the live ...