STK++ 0.9.13
STK_ReshapeAccessors.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Arrays
27 * created on: 17 oct. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
37#ifndef STK_RESHAPEACCESSORS_H
38#define STK_RESHAPEACCESSORS_H
39
40
41#define EGAL(arg1, arg2) ((arg1::structure_ == int(Arrays::arg2)))
42
43namespace STK
44{
45// forward declaration
46template< typename Lhs> class DiagonalizeAccessor;
47template< typename Lhs> class DiagonalGetterAccessor;
48template< typename Lhs> class UpperTriangularizeAccessor;
49template< typename Lhs> class LowerTriangularizeAccessor;
50template< typename Lhs> class SymmetrizeAccessor;
51template< typename Lhs> class UpperSymmetrizeAccessor;
52template< typename Lhs> class LowerSymmetrizeAccessor;
53
54namespace hidden
55{
59template<typename Lhs>
61{
62 enum
63 {
64 structure_ = Arrays::diagonal_,
65 orient_ = Lhs::orient_,
66 sizeRows_ = (Lhs::structure_== int(Arrays::diagonal_) )
67 ? Lhs::sizeRows_ != UnknownSize ? Lhs::sizeRows_ : Lhs::sizeCols_
68 : Lhs::structure_== int(Arrays::point_) ? int(Lhs::sizeCols_) : int(Lhs::sizeRows_),
69 sizeCols_ = (Lhs::structure_== int(Arrays::diagonal_) )
70 ? Lhs::sizeRows_ != UnknownSize ? Lhs::sizeRows_ : Lhs::sizeCols_
71 : Lhs::structure_== int(Arrays::point_) ? Lhs::sizeCols_ : Lhs::sizeRows_,
72 storage_ = Lhs::storage_
73 };
76 typedef typename Lhs::Type Type;
77 typedef typename Lhs::TypeConst TypeConst;
78};
79
80} // namespace hidden
81
82
99template< typename Lhs>
100class DiagonalizeAccessor: public ArrayBase< DiagonalizeAccessor< Lhs> >, public TRef<1>
101{
102 public:
106 enum
107 {
113 // this is safe as we can use DiagonalizeAccesor only on 1D container
114 size_ = (sizeRows_ != UnknownSize) ? sizeRows_ : sizeCols_
115 };
121 inline DiagonalizeAccessor(Lhs& lhs): Base(), lhs_(lhs)
122 {
124 }
126 inline RowRange const& rowsImpl() const { return lhs().range();}
128 inline ColRange const& colsImpl() const { return lhs().range();}
130 inline Lhs const& lhs() const { return lhs_; }
131
135 inline TypeConst elt2Impl(int i, int j) const { return (lhs_.elt(i, j));}
139 inline TypeConst elt1Impl(int i) const { return (lhs_.elt(i));}
141 inline TypeConst elt0Impl() const { return (lhs_.elt());}
142
146 inline Type& elt2Impl(int i, int j)
147 {
148#ifdef STK_BOUNDS_CHECK
149 if (i != j) { STKOUT_OF_RANGE_2ARG(DiagonalizeAccessor::elt, i, j, i != j);}
150#endif
151 return (lhs_.elt(i));
152 }
156 inline Type& elt1Impl(int i) { return (lhs_.elt(i));}
158 inline Type& elt0Impl() { return (lhs_.elt());}
159
160 protected:
162};
163
164
165namespace hidden
166{
170template<typename Lhs>
172{
173 enum
174 {
175 structure_ = Arrays::diagonal_,
176 orient_ = Lhs::orient_,
177 sizeRows_ = ((Lhs::sizeRows_ < Lhs::sizeCols_)) ? Lhs::sizeRows_ : Lhs::sizeCols_,
178 sizeCols_ = sizeRows_,
179 storage_ = Lhs::storage_,
180 isValid_ = ( hidden::Traits<Lhs>::structure_==(int)Arrays::array2D_)
181 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::square_)
182 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::diagonal_)
183 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::lower_triangular_)
184 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::upper_triangular_)
185 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::symmetric_)
186 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::lower_symmetric_)
187 ||(hidden::Traits<Lhs>::structure_==(int)Arrays::upper_symmetric_)
189 use_ = (sizeRows_ != UnknownSize) ? Arrays::useLhsSize_ : Arrays::useLhsOtherSize_,
190 size_ = (sizeRows_ != UnknownSize) ? sizeRows_ : sizeCols_
191 };
194 typedef typename Lhs::Type Type;
195 typedef typename Lhs::TypeConst TypeConst;
196};
197
198
199} // namespace hidden
200
201
217template< typename Lhs>
218class DiagonalGetterAccessor: public ArrayBase< DiagonalGetterAccessor< Lhs> >, public TRef<1>
219{
220 public:
224
225 enum
226 {
235 };
241
243 inline DiagonalGetterAccessor( Lhs& lhs): Base(), lhs_(lhs)
244 {
245 STK_STATIC_ASSERT(isValid_,YOU_TRIED_CALLING_A_MATRIX_METHOD_ON_A_VECTOR);
246#ifdef STK_BOUNDS_CHECK
247 if (lhs.rows()!=lhs.cols())
248 STKRUNTIME_ERROR_NO_ARG(DiagonalGetterAccessor,lhs.rows()!=lhs.cols());
249#endif
250 }
252 inline Lhs const& lhs() const { return lhs_; }
254 inline RowRange const& rowsImpl() const { return RangeImpl::rangeImpl(lhs_);}
256 inline ColRange const& colsImpl() const { return RangeImpl::rangeImpl(lhs_);}
257
261 inline TypeConst elt2Impl(int i, int j) const { return (lhs_.elt(i, j));}
265 inline TypeConst elt1Impl(int i) const { return (lhs_.elt(i,i));}
267 inline TypeConst elt0Impl() const { return (lhs_.elt());}
268
272 inline Type& elt2Impl(int i, int j) { return (lhs_.elt(i, j));}
276 inline Type& elt1Impl(int i) { return (lhs_.elt(i,i));}
278 inline Type& elt0Impl() { return (lhs_.elt());}
279
280 protected:
282};
283
284namespace hidden
285{
289template<typename Lhs>
291{
292 enum
293 {
294 structure_ = Arrays::upper_triangular_,
295 orient_ = Lhs::orient_,
296 sizeRows_ = Lhs::sizeRows_,
297 sizeCols_ = Lhs::sizeCols_,
298 storage_ = Lhs::storage_
299 };
302 typedef typename Lhs::Type Type;
303 typedef typename Lhs::TypeConst TypeConst;
304};
305
306} // namespace hidden
307
324template< typename Lhs>
325class UpperTriangularizeAccessor: public ArrayBase< UpperTriangularizeAccessor< Lhs> >, public TRef<1>
326{
327 public:
331
332 enum
333 {
339 };
344
346 inline UpperTriangularizeAccessor( Lhs& lhs): Base(), lhs_(lhs) {}
348 inline Lhs const& lhs() const { return lhs_; }
350 inline RowRange const& rowsImpl() const { return lhs().rows();}
352 inline ColRange const& colsImpl() const { return lhs().cols();}
353
357 inline TypeConst elt2Impl(int i, int j) const
358 {
359#ifdef STK_BOUNDS_CHECK
360 if (j<i)
361 STKRUNTIME_ERROR_2ARG(UpperTriangularizeAccessor::elt2Impl,i,j,use of the lower part);
362#endif
363 return (lhs_.elt(i, j));
364 }
365
369 inline Type& elt2Impl(int i, int j) {
370#ifdef STK_BOUNDS_CHECK
371 if (i>j)
372 STKRUNTIME_ERROR_2ARG(UpperTriangularizeAccessor::elt2Impl,i,j,use of the lower part);
373#endif
374 return (lhs_.elt(i, j));
375 }
376
377 protected:
379};
380
381namespace hidden
382{
386template<typename Lhs>
388{
389 enum
390 {
391 structure_ = Arrays::lower_triangular_,
392 orient_ = Lhs::orient_,
393 sizeRows_ = Lhs::sizeRows_,
394 sizeCols_ = Lhs::sizeCols_,
395 storage_ = Lhs::storage_
396 };
399 typedef typename Lhs::Type Type;
400 typedef typename Lhs::TypeConst TypeConst;
401};
402
403} // namespace hidden
404
421template< typename Lhs>
422class LowerTriangularizeAccessor: public ArrayBase< LowerTriangularizeAccessor< Lhs> >, public TRef<1>
423{
424 public:
428
429 enum
430 {
436 };
441
443 inline LowerTriangularizeAccessor( Lhs& lhs): Base(), lhs_(lhs) {}
445 inline Lhs const& lhs() const { return lhs_; }
447 inline RowRange const& rowsImpl() const { return lhs().rows();}
449 inline ColRange const& colsImpl() const { return lhs().cols();}
450
454 inline TypeConst elt2Impl(int i, int j) const
455 {
456#ifdef STK_BOUNDS_CHECK
457 if (j>i)
458 STKRUNTIME_ERROR_2ARG(LowerTriangularizeAccessor::elt2Impl,i,j,use of the upper part);
459#endif
460 return (lhs_.elt(i, j));
461 }
462
466 inline Type& elt2Impl(int i, int j)
467 {
468#ifdef STK_BOUNDS_CHECK
469 if (j>i)
470 STKRUNTIME_ERROR_2ARG(LowerTriangularizeAccessor::elt2Impl,i,j,use of the upper part);
471#endif
472 return (lhs_.elt(i, j));
473 }
474
475 protected:
477};
478
479namespace hidden
480{
484template<typename Lhs>
486{
487 enum
488 {
489 structure_ = Arrays::symmetric_,
490 orient_ = Lhs::orient_,
491 sizeRows_ = Lhs::sizeRows_,
492 sizeCols_ = Lhs::sizeCols_,
493 storage_ = Lhs::storage_
494 };
497 typedef typename Lhs::Type Type;
498 typedef typename Lhs::TypeConst TypeConst;
499};
500
501} // namespace hidden
502
519template< typename Lhs>
520class SymmetrizeAccessor: public ArrayBase< SymmetrizeAccessor< Lhs> >, public TRef<1>
521{
522 public:
526
527 enum
528 {
529 structure_ = hidden::Traits< SymmetrizeAccessor<Lhs> >::structure_,
533 storage_ = hidden::Traits< SymmetrizeAccessor<Lhs> >::storage_
534 };
539
544 inline Lhs const& lhs() const { return lhs_; }
546 inline RowRange const& rowsImpl() const { return lhs().rows();}
548 inline ColRange const& colsImpl() const { return lhs().cols();}
549
553 inline TypeConst elt2Impl(int i, int j) const
554 { return (lhs_.elt(i, j));}
555
559 inline Type& elt2Impl(int i, int j) { return (lhs_.elt(i, j));}
560
561 protected:
563};
564
565namespace hidden
566{
570template<typename Lhs>
572{
573 enum
574 {
575 structure_ = Arrays::upper_symmetric_,
576 orient_ = Lhs::orient_,
577 sizeRows_ = Lhs::sizeRows_,
578 sizeCols_ = Lhs::sizeCols_,
579 storage_ = Lhs::storage_
580 };
583 typedef typename Lhs::Type Type;
584 typedef typename Lhs::TypeConst TypeConst;
585};
586
587} // namespace hidden
588
605template< typename Lhs>
606class UpperSymmetrizeAccessor: public ArrayBase< UpperSymmetrizeAccessor< Lhs> >, public TRef<1>
607{
608 public:
612
613 enum
614 {
620 };
625
630 inline Lhs const& lhs() const { return lhs_; }
632 inline RowRange const& rowsImpl() const { return lhs().rows();}
634 inline ColRange const& colsImpl() const { return lhs().cols();}
635
639 inline TypeConst elt2Impl(int i, int j) const
640 { return ((j<i) ? lhs_.elt(j, i) : lhs_.elt(i, j));}
641
648 inline Type& elt2Impl(int i, int j)
649 { return ((j<i) ? lhs_.elt(j, i) : lhs_.elt(i, j));}
650
651 protected:
653};
654
655namespace hidden
656{
660template<typename Lhs>
662{
663 enum
664 {
665 structure_ = Arrays::lower_symmetric_,
666 orient_ = Lhs::orient_,
667 sizeRows_ = Lhs::sizeRows_,
668 sizeCols_ = Lhs::sizeCols_,
669 storage_ = Lhs::storage_
670 };
673 typedef typename Lhs::Type Type;
674 typedef typename Lhs::TypeConst TypeConst;
675};
676
677} // end namespace hidden
678
679
696template< typename Lhs>
697class LowerSymmetrizeAccessor: public ArrayBase< LowerSymmetrizeAccessor< Lhs> >, public TRef<1>
698{
699 public:
703
704 enum
705 {
711 };
716
721 inline Lhs const& lhs() const { return lhs_; }
723 inline RowRange const& rowsImpl() const { return lhs().rows();}
725 inline ColRange const& colsImpl() const { return lhs().cols();}
726
730 inline TypeConst elt2Impl(int i, int j) const
731 { return ((j>i) ? lhs_.elt(j, i) : lhs_.elt(i, j));}
732
736 inline Type& elt2Impl(int i, int j)
737 { return ((j>i) ? lhs_.elt(j, i) : lhs_.elt(i, j));}
738
739 protected:
741};
742
743} // namespace STK
744
745
746#undef EGAL
747
748#endif /* STK_RESHAPEACCESSORS_H */
#define STKOUT_OF_RANGE_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:102
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
#define STK_STATIC_ASSERT(COND, MSG)
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
#define STK_STATIC_ASSERT_TWO_DIMENSIONS_ONLY(EXPR)
base class for template arrays.
Generic expression when we want to get the diagonal of a two-dimensional square expression.
hidden::Traits< DiagonalGetterAccessor< Lhs > >::Type Type
ColRange const & colsImpl() const
TRange< size_ > RowRange
Type of the Range for the rows.
ArrayBase< DiagonalGetterAccessor< Lhs > > Base
TRange< size_ > ColRange
Type of the Range for the columns.
RowRange const & rowsImpl() const
hidden::Traits< DiagonalGetterAccessor< Lhs > >::TypeConst TypeConst
hidden::DiagonalRangeImpl< Lhs, size_, use_ > RangeImpl
TypeConst elt1Impl(int i) const
TypeConst elt2Impl(int i, int j) const
DiagonalGetterAccessor(Lhs &lhs)
Constructor.
Generic expression when a one dimensional vector/point/idagonal expression is "diagonalized".
ArrayBase< DiagonalizeAccessor< Lhs > > Base
hidden::Traits< DiagonalizeAccessor< Lhs > >::TypeConst TypeConst
TRange< size_ > RowRange
Type of the Range for the rows.
TypeConst elt2Impl(int i, int j) const
TRange< size_ > ColRange
Type of the Range for the columns.
TypeConst elt1Impl(int i) const
RowRange const & rowsImpl() const
ColRange const & colsImpl() const
Type & elt2Impl(int i, int j)
hidden::Traits< DiagonalizeAccessor< Lhs > >::Type Type
DiagonalizeAccessor(Lhs &lhs)
Constructor.
Generic expression when we want to get the lower-part of a two-dimensional symmetric expression.
TypeConst elt2Impl(int i, int j) const
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
ArrayBase< LowerSymmetrizeAccessor< Lhs > > Base
RowRange const & rowsImpl() const
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
hidden::Traits< LowerSymmetrizeAccessor< Lhs > >::TypeConst TypeConst
hidden::Traits< LowerSymmetrizeAccessor< Lhs > >::Type Type
LowerSymmetrizeAccessor(Lhs &lhs)
Constructor.
ColRange const & colsImpl() const
Generic expression when we want to get the lower-part of a two-dimensional expression.
hidden::Traits< LowerTriangularizeAccessor< Lhs > >::Type Type
LowerTriangularizeAccessor(Lhs &lhs)
Constructor.
TypeConst elt2Impl(int i, int j) const
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
ArrayBase< LowerTriangularizeAccessor< Lhs > > Base
hidden::Traits< LowerTriangularizeAccessor< Lhs > >::TypeConst TypeConst
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Generic expression when we want to get the upper-part of a two-dimensional symmetric expression.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
ColRange const & colsImpl() const
hidden::Traits< SymmetrizeAccessor< Lhs > >::TypeConst TypeConst
ArrayBase< SymmetrizeAccessor< Lhs > > Base
RowRange const & rowsImpl() const
hidden::Traits< SymmetrizeAccessor< Lhs > >::Type Type
SymmetrizeAccessor(Lhs &lhs)
Constructor.
Type & elt2Impl(int i, int j)
TypeConst elt2Impl(int i, int j) const
Generic expression when we want to get the upper-part of a two-dimensional symmetric expression.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
hidden::Traits< UpperSymmetrizeAccessor< Lhs > >::TypeConst TypeConst
RowRange const & rowsImpl() const
ColRange const & colsImpl() const
ArrayBase< UpperSymmetrizeAccessor< Lhs > > Base
hidden::Traits< UpperSymmetrizeAccessor< Lhs > >::Type Type
TypeConst elt2Impl(int i, int j) const
UpperSymmetrizeAccessor(Lhs &lhs)
Constructor.
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
Generic expression when we want to get the upper-part of a two-dimensional expression.
ArrayBase< UpperTriangularizeAccessor< Lhs > > Base
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
TypeConst elt2Impl(int i, int j) const
hidden::Traits< UpperTriangularizeAccessor< Lhs > >::TypeConst TypeConst
UpperTriangularizeAccessor(Lhs &lhs)
Constructor.
hidden::Traits< UpperTriangularizeAccessor< Lhs > >::Type Type
@ diagonal_
diagonal matrix/array/expression
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
The namespace STK is the main domain space of the Statistical ToolKit project.
Base class for all referencing containers.
Allow to disambiguate the call to range to use.
ColOperator< DiagonalGetterAccessor< Lhs > > Col
RowOperator< DiagonalGetterAccessor< Lhs > > Row
RowOperator< DiagonalizeAccessor< Lhs > > Row
ColOperator< DiagonalizeAccessor< Lhs > > Col
RowOperator< LowerSymmetrizeAccessor< Lhs > > Row
ColOperator< LowerSymmetrizeAccessor< Lhs > > Col
ColOperator< LowerTriangularizeAccessor< Lhs > > Col
RowOperator< LowerTriangularizeAccessor< Lhs > > Row
RowOperator< SymmetrizeAccessor< Lhs > > Row
ColOperator< SymmetrizeAccessor< Lhs > > Col
ColOperator< UpperSymmetrizeAccessor< Lhs > > Col
RowOperator< UpperSymmetrizeAccessor< Lhs > > Row
ColOperator< UpperTriangularizeAccessor< Lhs > > Col
RowOperator< UpperTriangularizeAccessor< Lhs > > Row