codac 2.0.0
Loading...
Searching...
No Matches
codac2_analytic_variables.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <iostream>
13#include "codac2_AnalyticExpr.h"
14#include "codac2_VarBase.h"
15#include "codac2_component.h"
16
17namespace codac2
18{
19 template<typename T>
20 struct AnalyticExprWrapper;
21
22 template<typename T>
23 class AnalyticVarExpr : public AnalyticExpr<T>, public VarBase
24 {
25 public:
26
27 explicit AnalyticVarExpr(const std::string& name)
28 : VarBase(name)
29 { }
30
31 virtual const ExprID& unique_id() const
32 {
34 }
35
36 T fwd_eval(ValuesMap& v, [[maybe_unused]] Index total_input_size, [[maybe_unused]] bool natural_eval) const
37 {
38 return AnalyticExpr<T>::value(v);
39 }
40
41 void bwd_eval([[maybe_unused]] ValuesMap& v) const
42 { }
43
44 void replace_arg([[maybe_unused]] const ExprID& old_arg_id, [[maybe_unused]] const std::shared_ptr<ExprBase>& new_expr)
45 { }
46
47 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
48 {
49 for(const auto& xi : args)
50 if(xi->unique_id() == this->unique_id())
51 return true;
52 return false;
53 }
54
55 virtual std::string str(bool in_parentheses = false) const
56 {
57 return in_parentheses ? "(" + _name + ")" : _name;
58 }
59
60 virtual bool is_str_leaf() const
61 {
62 return true;
63 }
64 };
65
66 class ScalarVar : public AnalyticVarExpr<ScalarType>
67 {
68 public:
69
70 explicit ScalarVar(const std::string& name = "?");
71
72 std::shared_ptr<VarBase> arg_copy() const;
73 std::shared_ptr<ExprBase> copy() const;
74 Index size() const;
75 std::pair<Index,Index> output_shape() const;
76
77 AnalyticExprWrapper<ScalarType> operator-() const;
78 };
79
80 class VectorVar : public AnalyticVarExpr<VectorType>
81 {
82 public:
83
84 explicit VectorVar(Index n, const std::string& name = "?");
85
86 std::shared_ptr<VarBase> arg_copy() const;
87 std::shared_ptr<ExprBase> copy() const;
88 Index size() const;
89 std::pair<Index,Index> output_shape() const;
90
91 AnalyticExprWrapper<ScalarType> operator[](Index i) const;
92 AnalyticExprWrapper<VectorType> subvector(Index i, Index j) const;
93
94 protected:
95
96 Index _n;
97 };
98
99 class MatrixVar : public AnalyticVarExpr<MatrixType>
100 {
101 public:
102
103 explicit MatrixVar(Index r, Index c, const std::string& name = "?");
104
105 std::shared_ptr<VarBase> arg_copy() const;
106 std::shared_ptr<ExprBase> copy() const;
107 Index size() const;
108 Index rows() const;
109 Index cols() const;
110 std::pair<Index,Index> output_shape() const;
111
112 AnalyticExprWrapper<ScalarType> operator()(Index i, Index j) const;
113 //AnalyticExprWrapper<VectorType> col(Index i) const;
114
115 protected:
116
117 Index _r, _c;
118 };
119}
const ExprID & unique_id() const
Returns the unique identifier of the expression.
Abstract base class for representing variables in analytic or set functions.
Definition codac2_VarBase.h:24