Epigraph
Convex Optimization in C++
variable.hpp
1 #pragma once
2 
3 #include <utility>
4 #include <ostream>
5 #include <vector>
6 #include <string>
7 #include <memory>
8 
9 namespace cvx
10 {
11  class Scalar;
12 
13  namespace internal
14  {
15 
16  class Term;
17 
18  struct VariableSource
19  {
20  enum class Type
21  {
22  Scalar,
23  Vector,
24  Matrix,
25  };
26 
27  // The pointer gets deleted with the problem.
28  std::shared_ptr<std::vector<double>> solution_ptr = nullptr;
29  size_t solution_idx = 0;
30  std::string name;
31  std::pair<size_t, size_t> index = {0, 0};
32  Type type;
33  };
34 
35  class Variable
36  {
37  public:
38  Variable() = default;
39  explicit Variable(const std::string &name);
40  Variable(const std::string &name, size_t row);
41  Variable(const std::string &name, size_t row, size_t col);
42 
43  bool operator==(const Variable &other) const;
44 
45  bool isLinkedToSolver() const;
46  bool linkToSolver(std::shared_ptr<std::vector<double>> solution_ptr, size_t solution_idx);
47  double getSolution() const;
48  size_t getProblemIndex() const;
49  void unlink();
50 
51  friend std::ostream &operator<<(std::ostream &os,
52  const Variable &variable);
53  operator Term() const;
54  operator Scalar() const;
55 
56  private:
57  std::shared_ptr<VariableSource> source;
58  };
59 
60  } // namespace internal
61 
62 } // namespace cvx