codac 2.0.0
Loading...
Searching...
No Matches
codac2_flatten.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
15#include "codac2_AnalyticType.h"
16
17namespace codac2
18{
19 struct FlattenOp
20 {
21 template<typename X1>
22 static std::string str(const X1& x1)
23 {
24 return "flatten(" + x1->str() + ")";
25 }
26
27 template<typename X1>
28 static std::pair<Index,Index> output_shape(const X1& s1)
29 {
30 auto shape1 = s1->output_shape();
31 return { shape1.second*shape1.first, 1 };
32 }
33
34 static IntervalVector fwd(const IntervalMatrix& x1);
35 static VectorType fwd_natural(const MatrixType& x1);
36 static VectorType fwd_centered(const MatrixType& x1);
37 static void bwd(const IntervalVector& y, IntervalMatrix& x1);
38 };
39
40 // Analytic operator
41 // The following functions can be used to build analytic expressions.
42
43 inline VectorExpr
44 flatten(const MatrixExpr &x1)
45 {
46 return { std::make_shared<AnalyticOperationExpr<FlattenOp,VectorType,MatrixType>>(x1) };
47 }
48
49 // Inline functions
50
51 inline IntervalVector FlattenOp::fwd(const IntervalMatrix& x1)
52 {
53 return x1.reshaped();
54 }
55
56 inline VectorType FlattenOp::fwd_natural(const MatrixType& x1)
57 {
58 return {
59 fwd(x1.a),
60 x1.def_domain
61 };
62 }
63
64 inline VectorType FlattenOp::fwd_centered(const MatrixType& x1)
65 {
66 if(centered_form_not_available_for_args(x1))
67 return fwd_natural(x1);
68
69 return {
70 fwd(x1.m),
71 fwd(x1.a),
72 x1.da,
73 x1.def_domain
74 };
75 }
76
77 inline void FlattenOp::bwd(const IntervalVector& y, IntervalMatrix& x1)
78 {
79 x1 &= y.reshaped(x1.rows(),x1.cols());
80 }
81
82}