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 
00008 namespace fmatvec {
00009 
00013 class ErrorType {};
00014 
00019 template<typename T>
00020 struct Size {
00021   typedef ErrorType type;
00022 };
00023 
00025 template<>
00026 struct Size<double> {
00027   typedef int type;
00028 };
00029 
00031 template<typename Shape>
00032 struct Size<Vector<Shape, double> > {
00033   typedef int type;
00034 };
00035 
00037 template<typename Shape>
00038 struct Size<RowVector<Shape, double> > {
00039   typedef int type;
00040 };
00041 
00043 template<typename Type, typename RowShape, typename ColShape>
00044 struct Size<Matrix<Type, RowShape, ColShape, double> > {
00045   typedef Vector<Fixed<2>, int> type;
00046 };
00047 
00053 template<typename T>
00054 struct StaticSize {
00055   enum { size1=0, size2=0 };
00056 };
00057 
00059 template<>
00060 struct StaticSize<double> {
00061   enum { size1=1, size2=1 };
00062 };
00063 
00065 template<typename Shape, typename AT>
00066 struct StaticSize<Vector<Shape, AT> > {
00067   enum { size1=0, size2=1 };
00068 };
00069 
00071 template<typename Shape, typename AT>
00072 struct StaticSize<RowVector<Shape, AT> > {
00073   enum { size1=1, size2=0 };
00074 };
00075 
00077 template<int N, typename AT>
00078 struct StaticSize<Vector<Fixed<N>, AT> > {
00079   enum { size1=N, size2=1 };
00080 };
00081 
00083 template<int N, typename AT>
00084 struct StaticSize<RowVector<Fixed<N>, AT> > {
00085   enum { size1=1, size2=N };
00086 };
00087 
00089 template<typename Storage, int N, int M, typename AT>
00090 struct StaticSize<Matrix<Storage, Fixed<N>, Fixed<M>, AT> > {
00091   enum { size1=N, size2=M };
00092 };
00093 
00095 template<typename Storage, int N, typename ColShape, typename AT>
00096 struct StaticSize<Matrix<Storage, Fixed<N>, ColShape, AT> > {
00097   enum { size1=N, size2=0 };
00098 };
00099 
00101 template<typename Storage, typename RowShape, int M, typename AT>
00102 struct StaticSize<Matrix<Storage, RowShape, Fixed<M>, AT> > {
00103   enum { size1=0, size2=M };
00104 };
00105 
00110 template<typename Dep, typename Indep>
00111 struct Der {
00112   typedef ErrorType type;
00113 };
00114 
00118 template<typename Dep>
00119 struct Der<Dep, double> {
00120   typedef Dep type;
00121 };
00122 
00128 template<typename IndepVecShape>
00129 struct Der<double, Vector<IndepVecShape, double> > {
00130   typedef RowVector<IndepVecShape, double> type;
00131 };
00132 
00138 template<typename DepVecShape, typename IndepVecShape>
00139 struct Der<Vector<DepVecShape, double>, Vector<IndepVecShape, double> > {
00140   typedef Matrix<General, DepVecShape, IndepVecShape, double> type;
00141 };
00142 
00153 template<typename DepMatShape>
00154 struct Der<Matrix<Rotation, DepMatShape, DepMatShape, double>, double> {
00155   typedef Vector<DepMatShape, double> type;
00156 };
00157 
00172 template<typename DepMatShape, typename IndepVecShape>
00173 struct Der<Matrix<Rotation, DepMatShape, DepMatShape, double>, Vector<IndepVecShape, double> > {
00174   typedef Matrix<General, DepMatShape, IndepVecShape, double> type;
00175 };
00176 
00187 template<typename Sig>
00188 class Function;
00189 
00191 template<typename Ret, typename Arg>
00192 class Function<Ret(Arg)> : virtual public Atom {
00193 
00194   public:
00195 
00197     virtual typename Size<Arg>::type getArgSize() const {
00198       throw std::runtime_error("getArgSize must be overloaded by derived class.");
00199     }
00200 
00202     virtual Ret operator()(const Arg &arg)=0;
00203 
00205     virtual typename Der<Ret, Arg>::type parDer(const Arg &arg) {
00206       throw std::runtime_error("parDer must be overloaded by derived class.");
00207     }
00208 
00210     virtual Ret dirDer(const Arg &argDir, const Arg &arg) {
00211       throw std::runtime_error("dirDer must be overloaded by derived class.");
00212     }
00213 
00215     virtual typename Der<typename Der<Ret, Arg>::type, Arg>::type parDerParDer(const Arg &arg) {
00216       throw std::runtime_error("parDerParDer must be overloaded by derived class.");
00217     }
00218 
00220     virtual typename Der<Ret, Arg>::type parDerDirDer(const Arg &argDir, const Arg &arg) {
00221       throw std::runtime_error("parDerDirDer must be overloaded by derived class.");
00222     }
00223 
00225     //  is constant.
00226     virtual bool constParDer() const { return false; }
00227 };
00228 
00230 template<typename Ret, typename Arg1, typename Arg2>
00231 class Function<Ret(Arg1, Arg2)> : virtual public Atom {
00232 
00233   public:
00234 
00236     virtual typename Size<Arg1>::type getArg1Size() const {
00237       throw std::runtime_error("getArg1Size must be overloaded by derived class.");
00238     }
00240     virtual typename Size<Arg2>::type getArg2Size() const {
00241       throw std::runtime_error("getArg2Size must be overloaded by derived class.");
00242     }
00243 
00245     virtual Ret operator()(const Arg1 &arg1, const Arg2 &arg2)=0;
00246 
00248     virtual typename Der<Ret, Arg1>::type parDer1(const Arg1 &arg1, const Arg2 &arg2) {
00249       throw std::runtime_error("parDer1 must be overloaded by derived class.");
00250     }
00251 
00253     virtual Ret dirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00254       throw std::runtime_error("dirDer1 must be overloaded by derived class.");
00255     }
00256 
00258     virtual typename Der<Ret, Arg2>::type parDer2(const Arg1 &arg1, const Arg2 &arg2) {
00259       throw std::runtime_error("parDer2 must be overloaded by derived class.");
00260     }
00261 
00263     virtual Ret dirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00264       throw std::runtime_error("dirDer2 must be overloaded by derived class.");
00265     }
00266 
00268     virtual typename Der<typename Der<Ret, Arg1>::type, Arg1>::type parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
00269       throw std::runtime_error("parDer1ParDer1 must be overloaded by derived class.");
00270     }
00271 
00273     virtual typename Der<Ret, Arg1>::type parDer1DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00274       throw std::runtime_error("parDer1DirDer1 must be overloaded by derived class.");
00275     }
00276 
00278     virtual typename Der<typename Der<Ret, Arg2>::type, Arg2>::type parDer2ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
00279       throw std::runtime_error("parDer2ParDer2 must be overloaded by derived class.");
00280     }
00281 
00283     virtual typename Der<Ret, Arg2>::type parDer2DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00284       throw std::runtime_error("parDer2DirDer2 must be overloaded by derived class.");
00285     }
00286 
00288     virtual typename Der<typename Der<Ret, Arg1>::type, Arg2>::type parDer1ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
00289       throw std::runtime_error("parDer1ParDer2 must be overloaded by derived class.");
00290     }
00291 
00293     virtual typename Der<Ret, Arg1>::type parDer1DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
00294       throw std::runtime_error("parDer1DirDer2 must be overloaded by derived class.");
00295     }
00296 
00298     virtual typename Der<typename Der<Ret, Arg2>::type, Arg1>::type parDer2ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
00299       throw std::runtime_error("parDer2ParDer1 must be overloaded by derived class.");
00300     }
00301 
00303     virtual typename Der<Ret, Arg2>::type parDer2DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
00304       throw std::runtime_error("parDer2DirDer1 must be overloaded by derived class.");
00305     }
00306 
00308     //  is constant.
00309     virtual bool constParDer1() const { return false; }
00310 
00312     //  is constant.
00313     virtual bool constParDer2() const { return false; }
00314 };
00315 
00316 // A function object with 3, 4, 5, ... arguments
00317 // Not required till now!
00318 
00319 }
00320 
00321 #endif