codac 2.0.0
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_ValueType.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 std::shared_ptr<ExprBase> copy() const
27 {
28 return std::make_shared<ConstValueExpr<T>>(*this);
29 }
30
31 T fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
32 {
33 if(natural_eval)
34 return AnalyticExpr<T>::init_value(v, T(
35 // the mid is not considered for const values in centered form expression:
36 _x,
37 // the definition domain is necesarily met at this point:
38 true
39 ));
40
41 else
42 return AnalyticExpr<T>::init_value(v, T(
43 // the mid is not considered for const values in centered form expression:
44 _x,
45 _x,
46 // the derivative of a const value is zero:
47 IntervalMatrix::zero(_x.size(),total_input_size),
48 // the definition domain is necesarily met at this point:
49 true
50 ));
51 }
52
53 void bwd_eval(ValuesMap& v) const
54 {
55 AnalyticExpr<T>::value(v).a &= _x;
56 }
57
58 std::pair<Index,Index> output_shape() const
59 {
60 if constexpr(std::is_same_v<T,ScalarType>)
61 return {1,1};
62
63 if constexpr(std::is_same_v<T,VectorType>)
64 return {_x.size(),1};
65
66 if constexpr(std::is_same_v<T,MatrixType>)
67 return {_x.rows(),_x.cols()};
68
69 assert_release(false && "unknow output shape for constant");
70 }
71
72 void replace_arg([[maybe_unused]] const ExprID& old_arg_id, [[maybe_unused]] const std::shared_ptr<ExprBase>& new_expr)
73 { }
74
75 virtual bool belongs_to_args_list([[maybe_unused]] const FunctionArgsList& args) const
76 {
77 return true;
78 }
79
80 virtual std::string str(bool in_parentheses = false) const
81 {
82 std::ostringstream s;
83 if(_x.is_degenerated()) s << _x.mid();
84 else s << _x;
85 return in_parentheses ? "(" + s.str() + ")" : s.str();
86 }
87
88 virtual bool is_str_leaf() const
89 {
90 return true;
91 }
92
93 protected:
94
95 const typename T::Domain _x;
96 };
97
98 template<typename T>
99 inline AnalyticExprWrapper<typename ValueType<T>::Type> const_value(const T& x)
100 {
101 return { std::make_shared<ConstValueExpr<typename ValueType<T>::Type>>(x) };
102 }
103}