TTTT Analysis  0.1
example1.cpp
Go to the documentation of this file.
1 
36 #include <iostream>
37 #include <utility>
38 
39 #include "filval/filval.hpp"
40 
41 int main(int argc, const char* argv[]){
42  // Initialize logging. Let's not worry about what is being logged right now
43  // and just redirect it to /dev/null so it doesn't appear on the screen.
44  fv::util::Log::init_logger("/dev/null", fv::util::LogPriority::kLogDebug);
45 
46  // declare a helper function to print out a std::pair object
47  auto print_pair = [](fv::Value<std::pair<double, double>>* dp){
48  std::pair<double, double> p = dp->get_value();
49  std::cout << "(" << p.first << ", " << p.second << ")\n";
50  };
51 
52  // These variables are the "Values" that will be observed. Think of these
53  // as sources of data that will be updated as new observations are loaded.
54  double x = 12;
55  double y = 13;
56 
57  // This is where the fun begins. Here we declare a couple ObservedValue
58  // objects. these are basically just fancy wrappers around the x, and y
59  // variables declared above. They have the job of supplying the value
60  // stored by x and y when requested.
61  fv::ObservedValue<double> x_val("x", &x);
62  fv::ObservedValue<double> y_val("y", &y);
63 
64  // Now that we have a few source values, let's compose them together into a
65  // pair. The fv api defines a function to do this: fv::pair. This function
66  // takes pointers to two Value objects and creates a new value that is a
67  // std::pair of these objects.
68  fv::Value<std::pair<double, double>>* dp = fv::pair(&x_val, &y_val);
69 
70  // If we call the print_pair function that we declared earlier with the new
71  // value object, we can see that, indeed, we see the correct output
72  // (12, 13)
73  print_pair(dp);
74 
75  // Now let's update the values of x and y to both be 2. Normally this job
76  // will be handled by a DataSet object which manages these variables, but
77  // we can do it here for now.
78  x = 2;
79  y = 2;
80 
81  // Before we can access these new values through our value chain, we need
82  // to tell the values to "reset". One of the main features of FV is that
83  // each value is only calculated at most once per observation, regardless
84  // of how many times it is accessed. However, this means that we have to
85  // tell the objects when a new object has been loaded so it will actually
86  // do a re-calculation. In the case of dp, this means that it will
87  // re-access the values of x and y and create a new pair with the updated
88  // values.
89  fv::GenValue::reset();
90 
91  // Call print_pair again just to verify that it gives the updated values,
92  // and indeed it does.
93  // (2, 2)
94  print_pair(dp);
95 
96  return 0;
97 }
A value supplied by the dataset, not derived.
Definition: value.hpp:450
A templated value.
Definition: value.hpp:265