codac 1.5.6
Loading...
Searching...
No Matches
codac2_analytic_constants.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Index.h"
13#include "codac2_ExprType.h"
14
15namespace codac2
16{
17 template<typename T>
18 class ConstValueExpr : public AnalyticExpr<T>
19 {
20 public:
21
22 ConstValueExpr(const typename T::Domain& x)
23 : _x(x)
24 { }
25
26 const typename T::Domain& value() const
27 {
28 return _x;
29 }
30
31 std::shared_ptr<ExprBase> copy() const
32 {
33 return std::make_shared<ConstValueExpr<T>>(*this);
34 }
35
36 T fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
37 {
38 if(natural_eval)
39 return AnalyticExpr<T>::init_value(v, T(
40 // the mid is not considered for const values in centered form expression:
41 _x,
42 // the definition domain is necesarily met at this point:
43 true
44 ));
45
46 else
47 return AnalyticExpr<T>::init_value(v, T(
48 // the mid is not considered for const values in centered form expression:
49 _x,
50 _x,
51 // the derivative of a const value is zero:
52 IntervalMatrix::zero(_x.size(),total_input_size),
53 // the definition domain is necesarily met at this point:
54 true
55 ));
56 }
57
58 void bwd_eval(ValuesMap& v) const
59 {
60 AnalyticExpr<T>::value(v).a &= _x;
61 }
62
63 std::pair<Index,Index> output_shape() const
64 {
65 if constexpr(std::is_same_v<T,ScalarType>)
66 return {1,1};
67
68 if constexpr(std::is_same_v<T,VectorType>)
69 return {_x.size(),1};
70
71 if constexpr(std::is_same_v<T,MatrixType>)
72 return {_x.rows(),_x.cols()};
73
74 assert_release_constexpr(false && "unknow output shape for constant");
75 }
76
77 void replace_arg([[maybe_unused]] const ExprID& old_arg_id, [[maybe_unused]] const std::shared_ptr<ExprBase>& new_expr)
78 { }
79
80 virtual bool belongs_to_args_list([[maybe_unused]] const FunctionArgsList& args) const
81 {
82 return true;
83 }
84
85 virtual std::string str(bool in_parentheses = false) const
86 {
87 std::ostringstream s;
88 if(_x.is_degenerated()) s << _x.mid();
89 else s << _x;
90 return in_parentheses ? "(" + s.str() + ")" : s.str();
91 }
92
93 virtual bool is_str_leaf() const
94 {
95 return true;
96 }
97
98 protected:
99
100 const typename T::Domain _x;
101 };
102
103 template<typename T>
104 inline AnalyticExprWrapper<typename ExprType<T>::Type> const_value(const T& x)
105 {
106 return { std::make_shared<ConstValueExpr<typename ExprType<T>::Type>>(x) };
107 }
108}