xref: /aosp_15_r20/external/eigen/Eigen/src/plugins/ReshapedMethods.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li 
2*bf2c3715SXin Li #ifdef EIGEN_PARSED_BY_DOXYGEN
3*bf2c3715SXin Li 
4*bf2c3715SXin Li /// \returns an expression of \c *this with reshaped sizes.
5*bf2c3715SXin Li ///
6*bf2c3715SXin Li /// \param nRows the number of rows in the reshaped expression, specified at either run-time or compile-time, or AutoSize
7*bf2c3715SXin Li /// \param nCols the number of columns in the reshaped expression, specified at either run-time or compile-time, or AutoSize
8*bf2c3715SXin Li /// \tparam Order specifies whether the coefficients should be processed in column-major-order (ColMajor), in row-major-order (RowMajor),
9*bf2c3715SXin Li ///               or follows the \em natural order of the nested expression (AutoOrder). The default is ColMajor.
10*bf2c3715SXin Li /// \tparam NRowsType the type of the value handling the number of rows, typically Index.
11*bf2c3715SXin Li /// \tparam NColsType the type of the value handling the number of columns, typically Index.
12*bf2c3715SXin Li ///
13*bf2c3715SXin Li /// Dynamic size example: \include MatrixBase_reshaped_int_int.cpp
14*bf2c3715SXin Li /// Output: \verbinclude MatrixBase_reshaped_int_int.out
15*bf2c3715SXin Li ///
16*bf2c3715SXin Li /// The number of rows \a nRows and columns \a nCols can also be specified at compile-time by passing Eigen::fix<N>,
17*bf2c3715SXin Li /// or Eigen::fix<N>(n) as arguments. In the later case, \c n plays the role of a runtime fallback value in case \c N equals Eigen::Dynamic.
18*bf2c3715SXin Li /// Here is an example with a fixed number of rows and columns:
19*bf2c3715SXin Li /// \include MatrixBase_reshaped_fixed.cpp
20*bf2c3715SXin Li /// Output: \verbinclude MatrixBase_reshaped_fixed.out
21*bf2c3715SXin Li ///
22*bf2c3715SXin Li /// Finally, one of the sizes parameter can be automatically deduced from the other one by passing AutoSize as in the following example:
23*bf2c3715SXin Li /// \include MatrixBase_reshaped_auto.cpp
24*bf2c3715SXin Li /// Output: \verbinclude MatrixBase_reshaped_auto.out
25*bf2c3715SXin Li /// AutoSize does preserve compile-time sizes when possible, i.e., when the sizes of the input are known at compile time \b and
26*bf2c3715SXin Li /// that the other size is passed at compile-time using Eigen::fix<N> as above.
27*bf2c3715SXin Li ///
28*bf2c3715SXin Li /// \sa class Reshaped, fix, fix<N>(int)
29*bf2c3715SXin Li ///
30*bf2c3715SXin Li template<int Order = ColMajor, typename NRowsType, typename NColsType>
31*bf2c3715SXin Li EIGEN_DEVICE_FUNC
32*bf2c3715SXin Li inline Reshaped<Derived,...>
33*bf2c3715SXin Li reshaped(NRowsType nRows, NColsType nCols);
34*bf2c3715SXin Li 
35*bf2c3715SXin Li /// This is the const version of reshaped(NRowsType,NColsType).
36*bf2c3715SXin Li template<int Order = ColMajor, typename NRowsType, typename NColsType>
37*bf2c3715SXin Li EIGEN_DEVICE_FUNC
38*bf2c3715SXin Li inline const Reshaped<const Derived,...>
39*bf2c3715SXin Li reshaped(NRowsType nRows, NColsType nCols) const;
40*bf2c3715SXin Li 
41*bf2c3715SXin Li /// \returns an expression of \c *this with columns (or rows) stacked to a linear column vector
42*bf2c3715SXin Li ///
43*bf2c3715SXin Li /// \tparam Order specifies whether the coefficients should be processed in column-major-order (ColMajor), in row-major-order (RowMajor),
44*bf2c3715SXin Li ///               or follows the \em natural order of the nested expression (AutoOrder). The default is ColMajor.
45*bf2c3715SXin Li ///
46*bf2c3715SXin Li /// This overloads is essentially a shortcut for `A.reshaped<Order>(AutoSize,fix<1>)`.
47*bf2c3715SXin Li ///
48*bf2c3715SXin Li /// - If `Order==ColMajor` (the default), then it returns a column-vector from the stacked columns of \c *this.
49*bf2c3715SXin Li /// - If `Order==RowMajor`, then it returns a column-vector from the stacked rows of \c *this.
50*bf2c3715SXin Li /// - If `Order==AutoOrder`, then it returns a column-vector with elements stacked following the storage order of \c *this.
51*bf2c3715SXin Li ///   This mode is the recommended one when the particular ordering of the element is not relevant.
52*bf2c3715SXin Li ///
53*bf2c3715SXin Li /// Example:
54*bf2c3715SXin Li /// \include MatrixBase_reshaped_to_vector.cpp
55*bf2c3715SXin Li /// Output: \verbinclude MatrixBase_reshaped_to_vector.out
56*bf2c3715SXin Li ///
57*bf2c3715SXin Li /// If you want more control, you can still fall back to reshaped(NRowsType,NColsType).
58*bf2c3715SXin Li ///
59*bf2c3715SXin Li /// \sa reshaped(NRowsType,NColsType), class Reshaped
60*bf2c3715SXin Li ///
61*bf2c3715SXin Li template<int Order = ColMajor>
62*bf2c3715SXin Li EIGEN_DEVICE_FUNC
63*bf2c3715SXin Li inline Reshaped<Derived,...>
64*bf2c3715SXin Li reshaped();
65*bf2c3715SXin Li 
66*bf2c3715SXin Li /// This is the const version of reshaped().
67*bf2c3715SXin Li template<int Order = ColMajor>
68*bf2c3715SXin Li EIGEN_DEVICE_FUNC
69*bf2c3715SXin Li inline const Reshaped<const Derived,...>
70*bf2c3715SXin Li reshaped() const;
71*bf2c3715SXin Li 
72*bf2c3715SXin Li #else
73*bf2c3715SXin Li 
74*bf2c3715SXin Li // This file is automatically included twice to generate const and non-const versions
75*bf2c3715SXin Li 
76*bf2c3715SXin Li #ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
77*bf2c3715SXin Li #define EIGEN_RESHAPED_METHOD_CONST const
78*bf2c3715SXin Li #else
79*bf2c3715SXin Li #define EIGEN_RESHAPED_METHOD_CONST
80*bf2c3715SXin Li #endif
81*bf2c3715SXin Li 
82*bf2c3715SXin Li #ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
83*bf2c3715SXin Li 
84*bf2c3715SXin Li // This part is included once
85*bf2c3715SXin Li 
86*bf2c3715SXin Li #endif
87*bf2c3715SXin Li 
88*bf2c3715SXin Li template<typename NRowsType, typename NColsType>
89*bf2c3715SXin Li EIGEN_DEVICE_FUNC
90*bf2c3715SXin Li inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
91*bf2c3715SXin Li                 internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
92*bf2c3715SXin Li                 internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value>
reshaped(NRowsType nRows,NColsType nCols)93*bf2c3715SXin Li reshaped(NRowsType nRows, NColsType nCols) EIGEN_RESHAPED_METHOD_CONST
94*bf2c3715SXin Li {
95*bf2c3715SXin Li   return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
96*bf2c3715SXin Li                   internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
97*bf2c3715SXin Li                   internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value>
98*bf2c3715SXin Li                 (derived(),
99*bf2c3715SXin Li                  internal::get_runtime_reshape_size(nRows,internal::get_runtime_value(nCols),size()),
100*bf2c3715SXin Li                  internal::get_runtime_reshape_size(nCols,internal::get_runtime_value(nRows),size()));
101*bf2c3715SXin Li }
102*bf2c3715SXin Li 
103*bf2c3715SXin Li template<int Order, typename NRowsType, typename NColsType>
104*bf2c3715SXin Li EIGEN_DEVICE_FUNC
105*bf2c3715SXin Li inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
106*bf2c3715SXin Li                 internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
107*bf2c3715SXin Li                 internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value,
108*bf2c3715SXin Li                 internal::get_compiletime_reshape_order<Flags,Order>::value>
reshaped(NRowsType nRows,NColsType nCols)109*bf2c3715SXin Li reshaped(NRowsType nRows, NColsType nCols) EIGEN_RESHAPED_METHOD_CONST
110*bf2c3715SXin Li {
111*bf2c3715SXin Li   return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
112*bf2c3715SXin Li                   internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
113*bf2c3715SXin Li                   internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value,
114*bf2c3715SXin Li                   internal::get_compiletime_reshape_order<Flags,Order>::value>
115*bf2c3715SXin Li                 (derived(),
116*bf2c3715SXin Li                  internal::get_runtime_reshape_size(nRows,internal::get_runtime_value(nCols),size()),
117*bf2c3715SXin Li                  internal::get_runtime_reshape_size(nCols,internal::get_runtime_value(nRows),size()));
118*bf2c3715SXin Li }
119*bf2c3715SXin Li 
120*bf2c3715SXin Li // Views as linear vectors
121*bf2c3715SXin Li 
122*bf2c3715SXin Li EIGEN_DEVICE_FUNC
123*bf2c3715SXin Li inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,SizeAtCompileTime,1>
reshaped()124*bf2c3715SXin Li reshaped() EIGEN_RESHAPED_METHOD_CONST
125*bf2c3715SXin Li {
126*bf2c3715SXin Li   return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,SizeAtCompileTime,1>(derived(),size(),1);
127*bf2c3715SXin Li }
128*bf2c3715SXin Li 
129*bf2c3715SXin Li template<int Order>
130*bf2c3715SXin Li EIGEN_DEVICE_FUNC
131*bf2c3715SXin Li inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived, SizeAtCompileTime, 1,
132*bf2c3715SXin Li                 internal::get_compiletime_reshape_order<Flags,Order>::value>
reshaped()133*bf2c3715SXin Li reshaped() EIGEN_RESHAPED_METHOD_CONST
134*bf2c3715SXin Li {
135*bf2c3715SXin Li   EIGEN_STATIC_ASSERT(Order==RowMajor || Order==ColMajor || Order==AutoOrder, INVALID_TEMPLATE_PARAMETER);
136*bf2c3715SXin Li   return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived, SizeAtCompileTime, 1,
137*bf2c3715SXin Li                   internal::get_compiletime_reshape_order<Flags,Order>::value>
138*bf2c3715SXin Li                 (derived(), size(), 1);
139*bf2c3715SXin Li }
140*bf2c3715SXin Li 
141*bf2c3715SXin Li #undef EIGEN_RESHAPED_METHOD_CONST
142*bf2c3715SXin Li 
143*bf2c3715SXin Li #ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
144*bf2c3715SXin Li #define EIGEN_RESHAPED_METHOD_2ND_PASS
145*bf2c3715SXin Li #include "ReshapedMethods.h"
146*bf2c3715SXin Li #undef EIGEN_RESHAPED_METHOD_2ND_PASS
147*bf2c3715SXin Li #endif
148*bf2c3715SXin Li 
149*bf2c3715SXin Li #endif // EIGEN_PARSED_BY_DOXYGEN
150