14 #include "MEAL/ScalarVector.h"
15 #include "MEAL/Convert.h"
23 template<
class T,
class Map = ScalarMapping<
typename T::Result> >
28 typedef typename T::Result Result;
35 {
return "Vectorize<" + this->
get_model()->get_name() +
">"; }
38 unsigned size ()
const;
43 void calculate (
double& result, std::vector<double>* gradient);
50 Vectorize<T>* vectorize (T*
function) {
return new Vectorize<T>(
function); }
57 typedef DatumTraits< typename DatumTraits<T>::element_type > SubTraits;
59 static inline unsigned ndim ()
61 return DatumTraits<T>::ndim() * SubTraits::ndim();
64 static inline Element element (
const T& t,
unsigned idim)
66 const unsigned index = idim / SubTraits::ndim();
67 const unsigned sub_index = idim % SubTraits::ndim();
68 return SubTraits::element( DatumTraits<T>::element(t, index), sub_index );
76 static inline unsigned ndim () {
return 1; }
77 static inline double element (
const double& x,
unsigned idim) {
return x; }
80 template<
class T,
class M>
83 this->set_model (
function);
86 template<
class T,
class M>
88 std::vector<double>* gradient)
90 unsigned index = this->get_index();
93 throw Error (InvalidState,
94 "MEAL::Vectorize<" + std::string(T::Name) +
">::calculate",
95 "index=%u >= size=%u", index, size());
98 std::vector<Result> m_gradient;
99 std::vector<Result>* m_gradptr = &m_gradient;
103 if (this->get_verbose())
104 std::cerr << get_name() +
"::calculate call evaluate" << std::endl;
106 m_result = this->get_model()->evaluate (m_gradptr);
110 if (this->get_verbose())
111 std::cerr << get_name() +
"::calculate index=" << this->get_index() << std::endl;
113 result = map.element( m_result, index );
118 if (this->get_verbose())
119 std::cerr << get_name() +
"::calculate map gradient" << std::endl;
121 gradient->resize( m_gradient.size() );
122 for (
unsigned i=0; i<m_gradient.size(); i++)
123 (*gradient)[i] = map.element( m_gradient[i], index );
125 if (this->get_verbose())
126 std::cerr << get_name() +
"::calculate return" << std::endl;
130 template<
class T,
class M>