function.h
00001 #ifndef _FMATVEC_FUNCTION_H_
00002 #define _FMATVEC_FUNCTION_H_
00003 
00004 #include <fmatvec/fmatvec.h>
00005 #include <fmatvec/atom.h>
00006 #include <stdexcept>
00007 #include <xercesc/util/XercesDefs.hpp>
00008 
00009 namespace XERCES_CPP_NAMESPACE {
00010   class DOMNode;
00011   class DOMElement;
00012 }
00013 
00014 namespace fmatvec {
00015 
00019 class ErrorType {};
00020 
00025 template<typename T>
00026 struct Size {
00027   typedef ErrorType type;
00028 };
00029 
00031 template<>
00032 struct Size<double> {
00033   typedef int type;
00034 };
00035 
00037 template<typename Shape>
00038 struct Size<Vector<Shape, double> > {
00039   typedef int type;
00040 };
00041 
00043 template<typename Shape>
00044 struct Size<RowVector<Shape, double> > {
00045   typedef int type;
00046 };
00047 
00049 template<typename Type, typename RowShape, typename ColShape>
00050 struct Size<Matrix<Type, RowShape, ColShape, double> > {
00051   typedef Vector<Fixed<2>, int> type;
00052 };
00053 
00059 template<typename T>
00060 struct StaticSize {
00061   enum { size1=0, size2=0 };
00062 };
00063 
00065 template<>
00066 struct StaticSize<double> {
00067   enum { size1=1, size2=1 };
00068 };
00069 
00071 template<typename Shape, typename AT>
00072 struct StaticSize<Vector<Shape, AT> > {
00073   enum { size1=0, size2=1 };
00074 };
00075 
00077 template<typename Shape, typename AT>
00078 struct StaticSize<RowVector<Shape, AT> > {
00079   enum { size1=1, size2=0 };
00080 };
00081 
00083 template<int N, typename AT>
00084 struct StaticSize<Vector<Fixed<N>, AT> > {
00085   enum { size1=N, size2=1 };
00086 };
00087 
00089 template<int N, typename AT>
00090 struct StaticSize<RowVector<Fixed<N>, AT> > {
00091   enum { size1=1, size2=N };
00092 };
00093 
00095 template<typename Storage, int N, int M, typename AT>
00096 struct StaticSize<Matrix<Storage, Fixed<N>, Fixed<M>, AT> > {
00097   enum { size1=N, size2=M };
00098 };
00099 
00101 template<typename Storage, int N, typename ColShape, typename AT>
00102 struct StaticSize<Matrix<Storage, Fixed<N>, ColShape, AT> > {
00103   enum { size1=N, size2=0 };
00104 };
00105 
00107 template<typename Storage, typename RowShape, int M, typename AT>
00108 struct StaticSize<Matrix<Storage, RowShape, Fixed<M>, AT> > {
00109   enum { size1=0, size2=M };
00110 };
00111 
00116 template<typename Dep, typename Indep>
00117 struct Der {
00118   typedef ErrorType type;
00119 };
00120 
00124 template<typename Dep>
00125 struct Der<Dep, double> {
00126   typedef Dep type;
00127 };
00128 
00134 template<typename IndepVecShape>
00135 struct Der<double, Vector<IndepVecShape, double> > {
00136   typedef RowVector<IndepVecShape, double> type;
00137 };
00138 
00144 template<typename DepVecShape, typename IndepVecShape>
00145 struct Der<Vector<DepVecShape, double>, Vector<IndepVecShape, double> > {
00146   typedef Matrix<General, DepVecShape, IndepVecShape, double> type;
00147 };
00148 
00159 template<typename DepMatShape>
00160 struct Der<Matrix<Rotation, DepMatShape, DepMatShape, double>, double> {
00161   typedef Vector<DepMatShape, double> type;
00162 };
00163 
00178 template<typename DepMatShape, typename IndepVecShape>
00179 struct Der<Matrix<Rotation, DepMatShape, DepMatShape, double>, Vector<IndepVecShape, double> > {
00180   typedef Matrix<General, DepMatShape, IndepVecShape, double> type;
00181 };
00182 
00186 class FunctionBase : public Atom {
00187   public:
00188     virtual ~FunctionBase() {}
00189     virtual void initializeUsingXML(xercesc::DOMElement *element) { }
00190     virtual xercesc::DOMElement *writeXMLFile(xercesc::DOMNode *parent) { return NULL; }
00191 };
00192 
00203 template<typename Sig>
00204 class Function;
00205 
00207 template<typename Ret, typename Arg>
00208 class Function<Ret(Arg)> : public FunctionBase {
00209 
00210   public:
00211 
00213     virtual typename Size<Arg>::type getArgSize() const {
00214       throw std::runtime_error("getArgSize must be overloaded by derived class.");
00215     }
00216 
00218     virtual Ret operator()(const Arg &arg)=0;
00219 
00221     virtual typename Der<Ret, Arg>::type parDer(const Arg &arg) {
00222       throw std::runtime_error("parDer must be overloaded by derived class.");
00223     }
00224 
00226     virtual Ret dirDer(const Arg &argDir, const Arg &arg) {
00227       throw std::runtime_error("dirDer must be overloaded by derived class.");
00228     }
00229 
00231     virtual typename Der<typename Der<Ret, Arg>::type, Arg>::type parDerParDer(const Arg &arg) {
00232       throw std::runtime_error("parDerParDer must be overloaded by derived class.");
00233     }
00234 
00236     virtual typename Der<Ret, Arg>::type parDerDirDer(const Arg &argDir, const Arg &arg) {
00237       throw std::runtime_error("parDerDirDer must be overloaded by derived class.");
00238     }
00239 
00241     //  is constant.
00242     virtual bool constParDer() const { return false; }
00243 };
00244 
00246 template<typename Ret, typename Arg1, typename Arg2>
00247 class Function<Ret(Arg1, Arg2)> : public FunctionBase {
00248 
00249   public:
00250 
00252     virtual typename Size<Arg1>::type getArg1Size() const {
00253       throw std::runtime_error("getArg1Size must be overloaded by derived class.");
00254     }
00256     virtual typename Size<Arg2>::type getArg2Size() const {
00257       throw std::runtime_error("getArg2Size must be overloaded by derived class.");
00258     }
00259 
00261     virtual Ret operator()(const Arg1 &arg1, const Arg2 &arg2)=0;
00262 
00264     virtual typename Der<Ret, Arg1>::type parDer1(const Arg1 &arg1, const Arg2 &arg2) {
00265       throw std::runtime_error("parDer1 must be overloaded by derived class.");
00266     }
00267 
00269     virtual Ret dirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00270       throw std::runtime_error("dirDer1 must be overloaded by derived class.");
00271     }
00272 
00274     virtual typename Der<Ret, Arg2>::type parDer2(const Arg1 &arg1, const Arg2 &arg2) {
00275       throw std::runtime_error("parDer2 must be overloaded by derived class.");
00276     }
00277 
00279     virtual Ret dirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00280       throw std::runtime_error("dirDer2 must be overloaded by derived class.");
00281     }
00282 
00284     virtual typename Der<typename Der<Ret, Arg1>::type, Arg1>::type parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
00285       throw std::runtime_error("parDer1ParDer1 must be overloaded by derived class.");
00286     }
00287 
00289     virtual typename Der<Ret, Arg1>::type parDer1DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00290       throw std::runtime_error("parDer1DirDer1 must be overloaded by derived class.");
00291     }
00292 
00294     virtual typename Der<typename Der<Ret, Arg2>::type, Arg2>::type parDer2ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
00295       throw std::runtime_error("parDer2ParDer2 must be overloaded by derived class.");
00296     }
00297 
00299     virtual typename Der<Ret, Arg2>::type parDer2DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00300       throw std::runtime_error("parDer2DirDer2 must be overloaded by derived class.");
00301     }
00302 
00304     virtual typename Der<typename Der<Ret, Arg1>::type, Arg2>::type parDer1ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
00305       throw std::runtime_error("parDer1ParDer2 must be overloaded by derived class.");
00306     }
00307 
00309     virtual typename Der<Ret, Arg1>::type parDer1DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00310       throw std::runtime_error("parDer1DirDer2 must be overloaded by derived class.");
00311     }
00312 
00314     virtual typename Der<typename Der<Ret, Arg2>::type, Arg1>::type parDer2ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
00315       throw std::runtime_error("parDer2ParDer1 must be overloaded by derived class.");
00316     }
00317 
00319     virtual typename Der<Ret, Arg2>::type parDer2DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00320       throw std::runtime_error("parDer2DirDer1 must be overloaded by derived class.");
00321     }
00322 
00324     //  is constant.
00325     virtual bool constParDer1() const { return false; }
00326 
00328     //  is constant.
00329     virtual bool constParDer2() const { return false; }
00330 };
00331 
00332 // A function object with 3, 4, 5, ... arguments
00333 // Not required till now!
00334 
00335 }
00336 
00337 #endif