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-2015 Shark Development Team
22  *
23  * <BR><HR>
24  * This file is part of Shark.
25  * <http://image.diku.dk/shark/>
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 shark{ namespace blas{
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>& 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(const vector_container<Source>& 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 ///\brief Specialization for matrix columns.
94 template<class Source>
95 detail::ADLVector<matrix_column<Source> >
96 init(const matrix_row<Source>& source){
97  return detail::ADLVector<matrix_column<Source> >(source);
98 }
99 
100 //matrices as arguments
101 
102 ///\brief Linearizes a matrix as a set of row vectors and treats them as a set of vectors for initialization.
103 template<class Matrix>
104 detail::MatrixExpression<const Matrix> toVector(const matrix_expression<Matrix>& matrix){
105  return detail::MatrixExpression<const Matrix>(matrix());
106 }
107 ///\brief Linearizes a matrix as a set of row vectors and treats them as a set of vectors for initialization.
108 template<class Matrix>
109 detail::MatrixExpression<Matrix> toVector(matrix_expression<Matrix>& matrix){
110  return detail::MatrixExpression<Matrix>(matrix());
111 }
112 
113 //parameterizable objects as arguments
114 ///\brief Uses the parameters of a parameterizable object for initialization.
115 ///
116 ///The object doesn't have to be derived from IParameterizable, but needs to offer the methods
117 template<class T>
118 detail::ParameterizableExpression<const T> parameters(const T& object){
119  return detail::ParameterizableExpression<const T>(object);
120 }
121 ///\brief Uses the parameters of a parameterizable object for initialization.
122 ///
123 ///The object doesn't have to be derived from IParameterizable, but needs to offer the methods
124 template<class T>
125 detail::ParameterizableExpression<T> parameters(T& object){
126  return detail::ParameterizableExpression<T>(object);
127 }
128 
129 
130 //ranges as arguments
131 
132 ///\brief Uses a range of vectors for initialization.
133 ///
134 ///Sometimes not a single vector but a set of vectors is needed as argument like
135 ///std::deque<RealVector> set;
136 ///in this case, vectorSet is needed
137 ///init(vec)<<vec1,vectorSet(set),vec2;
138 template<class T>
139 detail::InitializerRange<typename T::const_iterator,detail::VectorExpression<const typename T::value_type&> >
140 vectorSet(const T& range){
141  return detail::InitializerRange<
142  typename T::const_iterator,
143  detail::VectorExpression<const typename T::value_type&>
144  >(range.begin(),range.end());
145 }
146 ///\brief Uses a range of vectors for splitting and initialization.
147 ///
148 ///Sometimes not a single vector but a set of vectors is needed as argument like
149 ///std::deque<RealVector> set;
150 ///in this case, vectorSet is needed
151 ///init(vec)>>vec1,vectorSet(set),vec2;
152 template<class T>
153 detail::InitializerRange<typename T::iterator,detail::VectorExpression<typename T::value_type&> >
154 vectorSet(T& range){
155  return detail::InitializerRange<
156  typename T::iterator,
157  detail::VectorExpression<typename T::value_type&>
158  >(range.begin(),range.end());
159 }
160 
161 ///\brief Uses a range of vectors for initialization.
162 ///
163 ///Sometimes not a single matrix but a set of matrices is needed as argument like
164 ///std::deque<RealMatrix> set;
165 ///in this case, matrixSet is needed
166 ///init(vec)<<vec1,matrixSet(set),vec2;
167 template<class T>
168 detail::InitializerRange<typename T::const_iterator,detail::MatrixExpression<const typename T::value_type> >
169 matrixSet(const T& range){
170  return detail::InitializerRange<
171  typename T::const_iterator,
172  detail::MatrixExpression<const typename T::value_type>
173  >(range.begin(),range.end());
174 }
175 ///\brief Uses a range of vectors for splitting and initialization.
176 ///
177 ///Sometimes not a single matrix but a set of matrices is needed as argument like
178 ///std::deque<RealMatrix> set;
179 ///in this case, vectorSet is needed
180 ///init(vec)>>vec1,matrixSet(set),vec2;
181 template<class T>
182 detail::InitializerRange<typename T::iterator,detail::MatrixExpression<typename T::value_type> >
183 matrixSet(T& range){
184  return detail::InitializerRange<
185  typename T::iterator,
186  detail::MatrixExpression<typename T::value_type>
187  >(range.begin(),range.end());
188 }
189 
190 ///\brief Uses a range of parametrizable objects for initialization.
191 ///
192 ///The objects in the set must offer the methods described by IParameterizable. Also pointer to objects are allowed
193 template<class T>
194 detail::InitializerRange<typename T::const_iterator, detail::ParameterizableExpression<const typename T::value_type> >
195 parameterSet(const T& range){
196  return detail::InitializerRange<
197  typename T::const_iterator,
198  detail::ParameterizableExpression<const typename T::value_type>
199  >(range.begin(),range.end());
200 }
201 ///\brief Uses a range of parametrizable objects for splitting and initialization.
202 ///
203 ///The objects in the set must offer the methods described by IParameterizable. Also pointer to objects are allowed
204 template<class T>
205 detail::InitializerRange<typename T::iterator, detail::ParameterizableExpression<typename T::value_type> >
206 parameterSet(T& range){
207  return detail::InitializerRange<
208  typename T::iterator,
209  detail::ParameterizableExpression<typename T::value_type>
210  >(range.begin(),range.end());
211 }
212 
213 /** @} */
214 }}
215 
216 #endif