Epigraph
Convex Optimization in C++
parameter.hpp
1 #pragma once
2 
3 #include <memory>
4 
5 namespace cvx
6 {
7  class Scalar;
8 
9  namespace internal
10  {
11 
12  enum class ParamOpcode
13  {
14  Add,
15  Mul,
16  Div,
17  Sqrt,
18  };
19 
20  enum class ParameterType
21  {
22  Constant,
23  Pointer,
24  Operation,
25  };
26 
27  class ParameterSource
28  {
29  public:
30  virtual double getValue() const = 0;
31  virtual ParameterType getType() const = 0;
32  };
33 
34  class ConstantSource final : public ParameterSource
35  {
36  public:
37  explicit ConstantSource(double const_value);
38  double getValue() const override;
39  ParameterType getType() const override;
40  bool operator==(const ConstantSource &other) const;
41 
42  private:
43  double value;
44  };
45 
46  class PointerSource final : public ParameterSource
47  {
48  public:
49  explicit PointerSource(const double *value_ptr);
50  double getValue() const override;
51  ParameterType getType() const override;
52  bool operator==(const PointerSource &other) const;
53 
54  private:
55  const double *ptr;
56  };
57 
58  class OperationSource final : public ParameterSource
59  {
60  public:
61  OperationSource(ParamOpcode op,
62  std::shared_ptr<ParameterSource> p1,
63  std::shared_ptr<ParameterSource> p2);
64  double getValue() const override;
65  ParameterType getType() const override;
66  bool operator==(const OperationSource &other) const;
67 
68  private:
69  ParamOpcode op;
70  std::shared_ptr<ParameterSource> p1;
71  std::shared_ptr<ParameterSource> p2;
72  };
73 
74  class Affine;
75 
76  class Parameter
77  {
78  public:
79  Parameter();
80  explicit Parameter(int const_value);
81  explicit Parameter(double const_value);
82  explicit Parameter(double *value_ptr);
83 
84  bool isZero() const;
85  bool isOne() const;
86  double getValue() const;
87 
88  bool operator==(const Parameter &other) const;
89 
90  Parameter operator+(const Parameter &other) const;
91  Parameter operator-(const Parameter &other) const;
92  Parameter operator-() const;
93  Parameter operator*(const Parameter &other) const;
94  Parameter operator/(const Parameter &other) const;
95  Parameter &operator+=(const Parameter &other);
96  Parameter &operator*=(const Parameter &other);
97  Parameter &operator/=(const Parameter &other);
98  explicit operator Affine() const;
99  explicit operator Scalar() const;
100  explicit operator double() const;
101 
102  friend Parameter sqrt(const Parameter &param);
103  friend std::ostream &operator<<(std::ostream &os, const Parameter &parameter);
104 
105  private:
106  std::shared_ptr<ParameterSource> source;
107  };
108 
109  } // namespace internal
110 } // namespace cvx