function.h
00001 #ifndef _FMATVEC_FUNCTION_H_
00002 #define _FMATVEC_FUNCTION_H_
00003 
00004 #include <fmatvec/fmatvec.h>
00005 #include <stdexcept>
00006 
00007 namespace MBXMLUtils {
00008   class TiXmlElement;
00009   class TiXmlNode;
00010 }
00011 
00012 namespace fmatvec {
00013 
00017 class ErrorType {};
00018 
00023 template<typename T>
00024 struct Size {
00025   typedef ErrorType type;
00026 };
00027 
00029 template<>
00030 struct Size<double> {
00031   typedef int type;
00032 };
00033 
00035 template<typename Shape>
00036 struct Size<Vector<Shape, double> > {
00037   typedef int type;
00038 };
00039 
00041 template<typename Shape>
00042 struct Size<RowVector<Shape, double> > {
00043   typedef int type;
00044 };
00045 
00047 template<typename Type, typename RowShape, typename ColShape>
00048 struct Size<Matrix<Type, RowShape, ColShape, double> > {
00049   typedef Vector<Fixed<2>, int> type;
00050 };
00051 
00057 template<typename T>
00058 struct StaticSize {
00059   enum { size1=0, size2=0 };
00060 };
00061 
00063 template<>
00064 struct StaticSize<double> {
00065   enum { size1=1, size2=1 };
00066 };
00067 
00069 template<typename Shape, typename AT>
00070 struct StaticSize<Vector<Shape, AT> > {
00071   enum { size1=0, size2=1 };
00072 };
00073 
00075 template<typename Shape, typename AT>
00076 struct StaticSize<RowVector<Shape, AT> > {
00077   enum { size1=1, size2=0 };
00078 };
00079 
00081 template<int N, typename AT>
00082 struct StaticSize<Vector<Fixed<N>, AT> > {
00083   enum { size1=N, size2=1 };
00084 };
00085 
00087 template<int N, typename AT>
00088 struct StaticSize<RowVector<Fixed<N>, AT> > {
00089   enum { size1=1, size2=N };
00090 };
00091 
00093 template<typename Storage, int N, int M, typename AT>
00094 struct StaticSize<Matrix<Storage, Fixed<N>, Fixed<M>, AT> > {
00095   enum { size1=N, size2=M };
00096 };
00097 
00099 template<typename Storage, int N, typename ColShape, typename AT>
00100 struct StaticSize<Matrix<Storage, Fixed<N>, ColShape, AT> > {
00101   enum { size1=N, size2=0 };
00102 };
00103 
00105 template<typename Storage, typename RowShape, int M, typename AT>
00106 struct StaticSize<Matrix<Storage, RowShape, Fixed<M>, AT> > {
00107   enum { size1=0, size2=M };
00108 };
00109 
00114 template<typename Dep, typename Indep>
00115 struct Der {
00116   typedef ErrorType type;
00117 };
00118 
00122 template<typename Dep>
00123 struct Der<Dep, double> {
00124   typedef Dep type;
00125 };
00126 
00132 template<typename IndepVecShape>
00133 struct Der<double, Vector<IndepVecShape, double> > {
00134   typedef RowVector<IndepVecShape, double> type;
00135 };
00136 
00142 template<typename DepVecShape, typename IndepVecShape>
00143 struct Der<Vector<DepVecShape, double>, Vector<IndepVecShape, double> > {
00144   typedef Matrix<General, DepVecShape, IndepVecShape, double> type;
00145 };
00146 
00157 template<typename DepMatShape>
00158 struct Der<Matrix<Rotation, DepMatShape, DepMatShape, double>, double> {
00159   typedef Vector<DepMatShape, double> type;
00160 };
00161 
00176 template<typename DepMatShape, typename IndepVecShape>
00177 struct Der<Matrix<Rotation, DepMatShape, DepMatShape, double>, Vector<IndepVecShape, double> > {
00178   typedef Matrix<General, DepMatShape, IndepVecShape, double> type;
00179 };
00180 
00184 class FunctionBase {
00185   public:
00186     virtual ~FunctionBase() {}
00187     virtual void initializeUsingXML(MBXMLUtils::TiXmlElement *element) { }
00188     virtual MBXMLUtils::TiXmlElement *writeXMLFile(MBXMLUtils::TiXmlNode *parent) { return NULL; }
00189 };
00190 
00201 template<typename Sig>
00202 class Function;
00203 
00205 template<typename Ret, typename Arg>
00206 class Function<Ret(Arg)> : public FunctionBase {
00207 
00208   public:
00209 
00211     virtual typename Size<Arg>::type getArgSize() const {
00212       throw std::runtime_error("getArgSize must be overloaded by derived class.");
00213     }
00214 
00216     virtual Ret operator()(const Arg &arg)=0;
00217 
00219     virtual typename Der<Ret, Arg>::type parDer(const Arg &arg) {
00220       throw std::runtime_error("parDer must be overloaded by derived class.");
00221     }
00222 
00224     virtual Ret dirDer(const Arg &argDir, const Arg &arg) {
00225       throw std::runtime_error("dirDer must be overloaded by derived class.");
00226     }
00227 
00229     virtual typename Der<typename Der<Ret, Arg>::type, Arg>::type parDerParDer(const Arg &arg) {
00230       throw std::runtime_error("parDerParDer must be overloaded by derived class.");
00231     }
00232 
00234     virtual typename Der<Ret, Arg>::type parDerDirDer(const Arg &argDir, const Arg &arg) {
00235       throw std::runtime_error("parDerDirDer must be overloaded by derived class.");
00236     }
00237 
00239     //  is constant.
00240     virtual bool constParDer() const { return false; }
00241 };
00242 
00244 template<typename Ret, typename Arg1, typename Arg2>
00245 class Function<Ret(Arg1, Arg2)> : public FunctionBase {
00246 
00247   public:
00248 
00250     virtual typename Size<Arg1>::type getArg1Size() const {
00251       throw std::runtime_error("getArg1Size must be overloaded by derived class.");
00252     }
00254     virtual typename Size<Arg2>::type getArg2Size() const {
00255       throw std::runtime_error("getArg2Size must be overloaded by derived class.");
00256     }
00257 
00259     virtual Ret operator()(const Arg1 &arg1, const Arg2 &arg2)=0;
00260 
00262     virtual typename Der<Ret, Arg1>::type parDer1(const Arg1 &arg1, const Arg2 &arg2) {
00263       throw std::runtime_error("parDer1 must be overloaded by derived class.");
00264     }
00265 
00267     virtual Ret dirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00268       throw std::runtime_error("dirDer1 must be overloaded by derived class.");
00269     }
00270 
00272     virtual typename Der<Ret, Arg2>::type parDer2(const Arg1 &arg1, const Arg2 &arg2) {
00273       throw std::runtime_error("parDer2 must be overloaded by derived class.");
00274     }
00275 
00277     virtual Ret dirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00278       throw std::runtime_error("dirDer2 must be overloaded by derived class.");
00279     }
00280 
00282     virtual typename Der<typename Der<Ret, Arg1>::type, Arg1>::type parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
00283       throw std::runtime_error("parDer1ParDer1 must be overloaded by derived class.");
00284     }
00285 
00287     virtual typename Der<Ret, Arg1>::type parDer1DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00288       throw std::runtime_error("parDer1DirDer1 must be overloaded by derived class.");
00289     }
00290 
00292     virtual typename Der<typename Der<Ret, Arg2>::type, Arg2>::type parDer2ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
00293       throw std::runtime_error("parDer2ParDer2 must be overloaded by derived class.");
00294     }
00295 
00297     virtual typename Der<Ret, Arg2>::type parDer2DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00298       throw std::runtime_error("parDer2DirDer2 must be overloaded by derived class.");
00299     }
00300 
00302     virtual typename Der<typename Der<Ret, Arg1>::type, Arg2>::type parDer1ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
00303       throw std::runtime_error("parDer1ParDer2 must be overloaded by derived class.");
00304     }
00305 
00307     virtual typename Der<Ret, Arg1>::type parDer1DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00308       throw std::runtime_error("parDer1DirDer2 must be overloaded by derived class.");
00309     }
00310 
00312     virtual typename Der<typename Der<Ret, Arg2>::type, Arg1>::type parDer2ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
00313       throw std::runtime_error("parDer2ParDer1 must be overloaded by derived class.");
00314     }
00315 
00317     virtual typename Der<Ret, Arg2>::type parDer2DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00318       throw std::runtime_error("parDer2DirDer1 must be overloaded by derived class.");
00319     }
00320 
00322     //  is constant.
00323     virtual bool constParDer1() const { return false; }
00324 
00326     //  is constant.
00327     virtual bool constParDer2() const { return false; }
00328 };
00329 
00330 // A function object with 3, 4, 5, ... arguments
00331 // Not required till now!
00332 
00333 }
00334 
00335 #endif