Initialize.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Easy initialization of vectors.
5  *
6  * Often, you want to initialize a vector using already available data or you know a fixed initialization. In this case, this header helps, since
7  * it is possible to initialize a vector using
8  * init(vec) << a,b,c,...;
9  * also if you want to split a vector into several smaller parts, you can write
10  * init(vec) >> a,b,c,...;
11  * a,b,c are allowed to be vectors, vector expressions or single values. However, all vectors needs to be initialized to the correct size. It is checked
12  * in debug mode, that size(vec) equals size(a,b,c,...). The usage is not restricted to initialization, but can be used for any assignment where a
13  * vector is constructed from several parts. For example the construction of parameter vectors in AbstractModel::parameterVector.
14  *
15  *
16  *
17  * \author O.Krause, T.Glasmachers
18  * \date 2010-2011
19  *
20  *
21  * \par Copyright 1995-2017 Shark Development Team
22  *
23  * <BR><HR>
24  * This file is part of Shark.
25  * <http://shark-ml.org/>
26  *
27  * Shark is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU Lesser General Public License as published
29  * by the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * Shark is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35  * GNU Lesser General Public License for more details.
36  *
37  * You should have received a copy of the GNU Lesser General Public License
38  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
39  *
40  */
41 //===========================================================================
42 #ifndef SHARK_LINALG_INITIALIZE_H
43 #define SHARK_LINALG_INITIALIZE_H
44 
45 #include "Impl/Initialize.h"
46 namespace remora{
47 
48 /**
49  * \ingroup shark_globals
50  *
51  * @{
52  */
53 
54 ///\brief Starting-point for the initialization sequence.
55 ///
56 ///Usage: init(vector)<<a,b,c where vector is a ublas vector or sub-vector and a,b,c are either scalars or vectors.
57 ///In debug mode, it is checked that size(vector) == size(a,b,c)
58 template<class Source>
59 detail::ADLVector<Source&> init(vector_container<Source, cpu_tag>& source){
60  return detail::ADLVector<Source&>(source());
61 }
62 ///\brief Starting-point for the initialization sequence.
63 ///
64 ///Usage: init(vector)<<a,b,c where vector is a ublas vector or sub-vector and a,b,c are either scalars or vectors.
65 ///In debug mode, it is checked that size(vector) == size(a,b,c)
66 template<class Source>
67 detail::ADLVector<const Source&> init(vector_container<Source, cpu_tag> const& source){
68  return detail::ADLVector<const Source&>(source());
69 }
70 ///\brief Starting-point for the initialization sequence when used for splitting the vector.
71 ///
72 ///Usage: init(vector)>>a,b,c where vector is a ublas vector or sub-vector and a,b,c are mutable scalars or vectors.
73 ///In debug mode, it is checked that size(vector) == size(a,b,c)
74 //~ template<class Source>
75 //~ detail::ADLVector<const Source&> init(const vector_expression<Source>& source){
76  //~ return detail::ADLVector<const Source&>(source());
77 //~ }
78 
79 
80 ///\brief Specialization for ublas vector_range.
81 template<class Source>
82 detail::ADLVector<vector_range<Source> >
83 init(const vector_range<Source>& source){
84  return detail::ADLVector<vector_range<Source> >(source);
85 }
86 
87 ///\brief Specialization for matrix rows.
88 template<class Source>
89 detail::ADLVector<matrix_row<Source> >
90 init(const matrix_row<Source>& source){
91  return detail::ADLVector<matrix_row<Source> >(source);
92 }
93 
94 //matrices as arguments
95 
96 ///\brief Linearizes a matrix as a set of row vectors and treats them as a set of vectors for initialization.
97 template<class Matrix>
98 detail::MatrixExpression<const Matrix> toVector(const matrix_expression<Matrix, cpu_tag>& matrix){
99  return detail::MatrixExpression<const Matrix>(matrix());
100 }
101 ///\brief Linearizes a matrix as a set of row vectors and treats them as a set of vectors for initialization.
102 template<class Matrix>
103 detail::MatrixExpression<Matrix> toVector(matrix_expression<Matrix, cpu_tag>& matrix){
104  return detail::MatrixExpression<Matrix>(matrix());
105 }
106 
107 //parameterizable objects as arguments
108 ///\brief Uses the parameters of a parameterizable object for initialization.
109 ///
110 ///The object doesn't have to be derived from IParameterizable, but needs to offer the methods
111 template<class T>
112 detail::ParameterizableExpression<const T> parameters(const T& object){
113  return detail::ParameterizableExpression<const T>(object);
114 }
115 ///\brief Uses the parameters of a parameterizable object for initialization.
116 ///
117 ///The object doesn't have to be derived from IParameterizable, but needs to offer the methods
118 template<class T>
119 detail::ParameterizableExpression<T> parameters(T& object){
120  return detail::ParameterizableExpression<T>(object);
121 }
122 
123 
124 //ranges as arguments
125 
126 ///\brief Uses a range of vectors for initialization.
127 ///
128 ///Sometimes not a single vector but a set of vectors is needed as argument like
129 ///std::deque<RealVector> set;
130 ///in this case, vectorSet is needed
131 ///init(vec)<<vec1,vectorSet(set),vec2;
132 template<class T>
133 detail::InitializerRange<typename T::const_iterator,detail::VectorExpression<const typename T::value_type&> >
134 vectorSet(const T& range){
135  return detail::InitializerRange<
136  typename T::const_iterator,
137  detail::VectorExpression<const typename T::value_type&>
138  >(range.begin(),range.end());
139 }
140 ///\brief Uses a range of vectors for splitting and initialization.
141 ///
142 ///Sometimes not a single vector but a set of vectors is needed as argument like
143 ///std::deque<RealVector> set;
144 ///in this case, vectorSet is needed
145 ///init(vec)>>vec1,vectorSet(set),vec2;
146 template<class T>
147 detail::InitializerRange<typename T::iterator,detail::VectorExpression<typename T::value_type&> >
148 vectorSet(T& range){
149  return detail::InitializerRange<
150  typename T::iterator,
151  detail::VectorExpression<typename T::value_type&>
152  >(range.begin(),range.end());
153 }
154 
155 ///\brief Uses a range of vectors for initialization.
156 ///
157 ///Sometimes not a single matrix but a set of matrices is needed as argument like
158 ///std::deque<RealMatrix> set;
159 ///in this case, matrixSet is needed
160 ///init(vec)<<vec1,matrixSet(set),vec2;
161 template<class T>
162 detail::InitializerRange<typename T::const_iterator,detail::MatrixExpression<const typename T::value_type> >
163 matrixSet(const T& range){
164  return detail::InitializerRange<
165  typename T::const_iterator,
166  detail::MatrixExpression<const typename T::value_type>
167  >(range.begin(),range.end());
168 }
169 ///\brief Uses a range of vectors for splitting and initialization.
170 ///
171 ///Sometimes not a single matrix but a set of matrices is needed as argument like
172 ///std::deque<RealMatrix> set;
173 ///in this case, vectorSet is needed
174 ///init(vec)>>vec1,matrixSet(set),vec2;
175 template<class T>
176 detail::InitializerRange<typename T::iterator,detail::MatrixExpression<typename T::value_type> >
177 matrixSet(T& range){
178  return detail::InitializerRange<
179  typename T::iterator,
180  detail::MatrixExpression<typename T::value_type>
181  >(range.begin(),range.end());
182 }
183 
184 ///\brief Uses a range of parametrizable objects for initialization.
185 ///
186 ///The objects in the set must offer the methods described by IParameterizable. Also pointer to objects are allowed
187 template<class T>
188 detail::InitializerRange<typename T::const_iterator, detail::ParameterizableExpression<const typename T::value_type> >
189 parameterSet(const T& range){
190  return detail::InitializerRange<
191  typename T::const_iterator,
192  detail::ParameterizableExpression<const typename T::value_type>
193  >(range.begin(),range.end());
194 }
195 ///\brief Uses a range of parametrizable objects for splitting and initialization.
196 ///
197 ///The objects in the set must offer the methods described by IParameterizable. Also pointer to objects are allowed
198 template<class T>
199 detail::InitializerRange<typename T::iterator, detail::ParameterizableExpression<typename T::value_type> >
200 parameterSet(T& range){
201  return detail::InitializerRange<
202  typename T::iterator,
203  detail::ParameterizableExpression<typename T::value_type>
204  >(range.begin(),range.end());
205 }
206 
207 /** @} */
208 }
209 
210 #endif