ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
factors.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Authors: Anders Nielsen <anders@nielsensweb.org>
00005  * Copyright (c) 2010-2012 ADMB Foundation
00006  */
00011 #include <fvar.hpp>
00012 #include "factors.h"
00013 
00014 int count_factor(const dvector& v, const double& eps){
00015   int from=v.indexmin();
00016   int to=v.indexmax();
00017   int counter=1;
00018   int seenBefore;
00019   for(int i=from+1; i<=to; ++i){ // can be optimized
00020     seenBefore=0;
00021     for(int j=from; j<=(i-1); ++j){
00022       if(fabs(v(j)-v(i))<eps){
00023         seenBefore=1;
00024         break;
00025       }
00026     }
00027     if(seenBefore==0){
00028       counter++;
00029     }
00030   }
00031   return counter;
00032 }
00033 
00034 ivector as_factor(const dvector& v, const double eps){
00035   int from=v.indexmin();
00036   int to=v.indexmax();
00037   ivector idxlocal(from,to);
00038   int nlevels=count_factor(v,eps);
00039   dvector whichlevel(1,nlevels);
00040     int  whichlevelindex=0;
00041     whichlevel(++whichlevelindex)=v(from);
00042     int seenBefore;
00043     for(int i=from+1; i<=to; ++i){
00044       seenBefore=0;
00045       for(int j=1; j<=whichlevelindex; ++j){
00046         if(fabs(whichlevel(j)-v(i))<eps){
00047           seenBefore=1;
00048           break;
00049         }
00050       }
00051       if(!seenBefore){
00052         whichlevel(++whichlevelindex)=v(i);
00053       }
00054     }
00055     for(int j=1; j<=nlevels; ++j){
00056       for(int i=from; i<=to; ++i){
00057         if(fabs(v(i)-whichlevel(j))<eps){
00058           idxlocal(i)=j;
00059         };
00060       }
00061     }
00062     return idxlocal;
00063 }
00064 
00065 int count_factor(const ivector& v){
00066   int from=v.indexmin();
00067   int to=v.indexmax();
00068   int counter=1;
00069   int seenBefore;
00070   for(int i=from+1; i<=to; ++i){ // can be optimized
00071     seenBefore=0;
00072     for(int j=from; j<=(i-1); ++j){
00073       if(v(j)==v(i)){
00074         seenBefore=1;
00075         break;
00076       }
00077     }
00078     if(seenBefore==0){
00079       counter++;
00080     }
00081   }
00082   return counter;
00083 }
00084 
00085 void factor::allocate(const ivector& v, dvar_vector & par){
00086   int from=v.indexmin();
00087   int to=v.indexmax();
00088 
00089   ivector idxlocal(from,to);
00090   if(par.indexmin()!=1)
00091   {
00092     cout<<"Error: par indexmin is not 1 in factor - something is wrong"<<endl;
00093     ad_exit(1);
00094   }
00095   nlevels=par.indexmax();
00096   ivector whichlevel(1,nlevels);
00097   int  whichlevelindex=0;
00098   whichlevel(++whichlevelindex)=v(from);
00099   int seenBefore;
00100   for(int i=from+1; i<=to; ++i){
00101     seenBefore=0;
00102     for(int j=1; j<=whichlevelindex; ++j){
00103       if(whichlevel(j)==v(i)){
00104         seenBefore=1;
00105         break;
00106       }
00107     }
00108     if(!seenBefore){
00109       whichlevel(++whichlevelindex)=v(i);
00110     }
00111   }
00112   for(int j=1; j<=nlevels; ++j){
00113     for(int i=from; i<=to; ++i){
00114       if(v(i)==whichlevel(j)){
00115         idxlocal(i)=j;
00116       };
00117     }
00118   }
00119   idx=idxlocal;
00120   levels=par;
00121 }
00122 
00123 dvariable factor::operator () (int i){
00124   return levels(idx(i));
00125 }
00126