codac 2.0.0
Loading...
Searching...
No Matches
codac2_transpose.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 TransposeOp
20 {
21 template<typename X1>
22 static std::string str(const X1& x1)
23 {
24 return "transpose(" + 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 };
32 }
33
34 static IntervalMatrix fwd(const IntervalMatrix& x1);
35 static MatrixType fwd_natural(const MatrixType& x1);
36 static MatrixType fwd_centered(const MatrixType& x1);
37 static void bwd(const IntervalMatrix& y, IntervalMatrix& x1);
38 };
39
40 // Analytic operator
41 // The following functions can be used to build analytic expressions.
42
43 inline MatrixExpr
44 transpose(const MatrixExpr &x1)
45 {
46 return { std::make_shared<AnalyticOperationExpr<TransposeOp,MatrixType,MatrixType>>(x1) };
47 }
48
49 // Inline functions
50
51 inline IntervalMatrix TransposeOp::fwd(const IntervalMatrix& x1)
52 {
53 return x1.transpose();
54 }
55
56 inline MatrixType TransposeOp::fwd_natural(const MatrixType& x1)
57 {
58 return {
59 fwd(x1.a),
60 x1.def_domain
61 };
62 }
63
64 inline MatrixType TransposeOp::fwd_centered(const MatrixType& x1)
65 {
66 if(centered_form_not_available_for_args(x1))
67 return fwd_natural(x1);
68
69 IntervalMatrix d(x1.da.rows(),x1.da.cols());
70 for (Index i=0; i<x1.a.rows(); i++)
71 for (Index j=0;j<x1.a.cols(); j++) {
72 /* x2.a(j,i) = x1.a(i,j) */
73 d.row(j+i*x1.a.cols()) = x1.da.row(i+j*x1.a.rows());
74 }
75
76 return {
77 fwd(x1.m),
78 fwd(x1.a),
79 d,
80 x1.def_domain
81 };
82 }
83
84 inline void TransposeOp::bwd(const IntervalMatrix& y, IntervalMatrix& x1)
85 {
86 x1 &= y.transpose();
87 }
88
89}