ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dmat20.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00012 // file fvar.cpp
00013 // constructors, destructors and misc functions involving class dvariable
00014 
00015 #include "fvar.hpp"
00016 
00017 #ifdef __TURBOC__
00018   #pragma hdrstop
00019   #include <iostream.h>
00020 #endif
00021 
00022 #ifdef __ZTC__
00023   #include <iostream.hpp>
00024 #endif
00025 
00026 
00027 #include <stdio.h>
00028 #ifndef __SUN__
00029 #endif
00030 #include <math.h>
00031 
00032 // dmatrix mathematical functions
00033 dmatrix exp(const dmatrix& m);
00034 dmatrix log(const dmatrix& m);
00035 dmatrix sin(const dmatrix& m);
00036 dmatrix cos(const dmatrix& m);
00037 dmatrix tan(const dmatrix& m);
00038 dmatrix pow(const dmatrix& m, const double e);
00039 dmatrix pow(const dmatrix& m,int e);
00040 // end of dvar_vector mathematical functions
00041 
00042 /*  "template form for editor
00043 dmatrix XXX(const dmatrix& m)
00044 {
00045   ivector cmin(m.rowmin(),m.rowmax());
00046   ivector cmax(m.rowmin(),m.rowmax());
00047   for (int i=m.rowmin();i<=m.rowmax();i++)
00048   {
00049     cmin(i)=m(i).indexmin();
00050     cmax(i)=m(i).indexmax();
00051   }
00052   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00053   for (i=m.rowmin();i<=m.rowmax();i++)
00054   {
00055     tmp(i)=XXX(m(i));
00056   }
00057   return tmp;
00058 }
00059 
00060 */
00061 
00066 dmatrix pow(const dmatrix& m,int e)
00067 {
00068   ivector cmin(m.rowmin(),m.rowmax());
00069   ivector cmax(m.rowmin(),m.rowmax());
00070   int i;
00071   for (i=m.rowmin();i<=m.rowmax();i++)
00072   {
00073     cmin(i)=m(i).indexmin();
00074     cmax(i)=m(i).indexmax();
00075   }
00076   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00077   for (i=m.rowmin();i<=m.rowmax();i++)
00078   {
00079     tmp(i)=pow(m(i),e);
00080   }
00081   return tmp;
00082 }
00083 
00088 dmatrix pow(const dmatrix& m, const double e)
00089 {
00090   ivector cmin(m.rowmin(),m.rowmax());
00091   ivector cmax(m.rowmin(),m.rowmax());
00092   int i;
00093   for (i=m.rowmin();i<=m.rowmax();i++)
00094   {
00095     cmin(i)=m(i).indexmin();
00096     cmax(i)=m(i).indexmax();
00097   }
00098   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00099   for (i=m.rowmin();i<=m.rowmax();i++)
00100   {
00101     tmp(i)=pow(m(i),e);
00102   }
00103   return tmp;
00104 }
00105 
00110 dmatrix tan(const dmatrix& m)
00111 {
00112   ivector cmin(m.rowmin(),m.rowmax());
00113   ivector cmax(m.rowmin(),m.rowmax());
00114   int i;
00115   for (i=m.rowmin();i<=m.rowmax();i++)
00116   {
00117     cmin(i)=m(i).indexmin();
00118     cmax(i)=m(i).indexmax();
00119   }
00120   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00121   for (i=m.rowmin();i<=m.rowmax();i++)
00122   {
00123     tmp(i)=tan(m(i));
00124   }
00125   return tmp;
00126 }
00127 
00132 dmatrix cos(const dmatrix& m)
00133 {
00134   ivector cmin(m.rowmin(),m.rowmax());
00135   ivector cmax(m.rowmin(),m.rowmax());
00136   int i;
00137   for (i=m.rowmin();i<=m.rowmax();i++)
00138   {
00139     cmin(i)=m(i).indexmin();
00140     cmax(i)=m(i).indexmax();
00141   }
00142   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00143   for (i=m.rowmin();i<=m.rowmax();i++)
00144   {
00145     tmp(i)=cos(m(i));
00146   }
00147   return tmp;
00148 }
00149 
00154 dmatrix sin(const dmatrix& m)
00155 {
00156   ivector cmin(m.rowmin(),m.rowmax());
00157   ivector cmax(m.rowmin(),m.rowmax());
00158   int i;
00159   for (i=m.rowmin();i<=m.rowmax();i++)
00160   {
00161     cmin(i)=m(i).indexmin();
00162     cmax(i)=m(i).indexmax();
00163   }
00164   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00165   for (i=m.rowmin();i<=m.rowmax();i++)
00166   {
00167     tmp(i)=sin(m(i));
00168   }
00169   return tmp;
00170 }
00171 
00176 dmatrix elem_prod(const dmatrix& m, const dmatrix& m2)
00177 {
00178   ivector cmin(m.rowmin(),m.rowmax());
00179   ivector cmax(m.rowmin(),m.rowmax());
00180   int i;
00181   for (i=m.rowmin();i<=m.rowmax();i++)
00182   {
00183     cmin(i)=m(i).indexmin();
00184     cmax(i)=m(i).indexmax();
00185   }
00186   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00187   for (i=m.rowmin();i<=m.rowmax();i++)
00188   {
00189     tmp(i)=elem_prod(m(i),m2(i));
00190   }
00191   return tmp;
00192 }
00193 
00198 dmatrix log(const dmatrix& m)
00199 {
00200   ivector cmin(m.rowmin(),m.rowmax());
00201   ivector cmax(m.rowmin(),m.rowmax());
00202   int i;
00203   for (i=m.rowmin();i<=m.rowmax();i++)
00204   {
00205     cmin(i)=m(i).indexmin();
00206     cmax(i)=m(i).indexmax();
00207   }
00208   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00209   for (i=m.rowmin();i<=m.rowmax();i++)
00210   {
00211     tmp(i)=log(m(i));
00212   }
00213   return tmp;
00214 }
00215 
00220 dmatrix sqrt(const dmatrix& m)
00221 {
00222   ivector cmin(m.rowmin(),m.rowmax());
00223   ivector cmax(m.rowmin(),m.rowmax());
00224   int i;
00225   for (i=m.rowmin();i<=m.rowmax();i++)
00226   {
00227     cmin(i)=m(i).indexmin();
00228     cmax(i)=m(i).indexmax();
00229   }
00230   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00231   for (i=m.rowmin();i<=m.rowmax();i++)
00232   {
00233     tmp(i)=sqrt(m(i));
00234   }
00235   return tmp;
00236 }
00237 
00242 dmatrix sqr(const dmatrix& m)
00243 {
00244   ivector cmin(m.rowmin(),m.rowmax());
00245   ivector cmax(m.rowmin(),m.rowmax());
00246   int i;
00247   for (i=m.rowmin();i<=m.rowmax();i++)
00248   {
00249     cmin(i)=m(i).indexmin();
00250     cmax(i)=m(i).indexmax();
00251   }
00252   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00253   for (i=m.rowmin();i<=m.rowmax();i++)
00254   {
00255     tmp(i)=sqr(m(i));
00256   }
00257   return tmp;
00258 }
00259 
00264 dmatrix exp(const dmatrix& m)
00265 {
00266   ivector cmin(m.rowmin(),m.rowmax());
00267   ivector cmax(m.rowmin(),m.rowmax());
00268   int i;
00269   for (i=m.rowmin();i<=m.rowmax();i++)
00270   {
00271     cmin(i)=m(i).indexmin();
00272     cmax(i)=m(i).indexmax();
00273   }
00274   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00275   for (i=m.rowmin();i<=m.rowmax();i++)
00276   {
00277     tmp(i)=exp(m(i));
00278   }
00279   return tmp;
00280 }
00281 
00286 dmatrix elem_div(const dmatrix& m, const dmatrix& m2)
00287 {
00288   ivector cmin(m.rowmin(),m.rowmax());
00289   ivector cmax(m.rowmin(),m.rowmax());
00290   int i;
00291   for (i=m.rowmin();i<=m.rowmax();i++)
00292   {
00293     cmin(i)=m(i).indexmin();
00294     cmax(i)=m(i).indexmax();
00295   }
00296   dmatrix tmp(m.rowmin(),m.rowmax(),cmin,cmax);
00297   for (i=m.rowmin();i<=m.rowmax();i++)
00298   {
00299     tmp(i)=elem_div(m(i),m2(i));
00300   }
00301   return tmp;
00302 }