ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
factors.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Authors: Anders Nielsen <anders@nielsensweb.org>
5  * Copyright (c) 2010-2012 ADMB Foundation
6  */
11 #include <fvar.hpp>
12 #include "factors.h"
13 
14 int count_factor(const dvector& v, const double& eps){
15  int from=v.indexmin();
16  int to=v.indexmax();
17  int counter=1;
18  int seenBefore;
19  for(int i=from+1; i<=to; ++i){ // can be optimized
20  seenBefore=0;
21  for(int j=from; j<=(i-1); ++j){
22  if(fabs(v(j)-v(i))<eps){
23  seenBefore=1;
24  break;
25  }
26  }
27  if(seenBefore==0){
28  counter++;
29  }
30  }
31  return counter;
32 }
33 
34 ivector as_factor(const dvector& v, const double eps){
35  int from=v.indexmin();
36  int to=v.indexmax();
37  ivector idxlocal(from,to);
38  int nlevels=count_factor(v,eps);
39  dvector whichlevel(1,nlevels);
40  int whichlevelindex=0;
41  whichlevel(++whichlevelindex)=v(from);
42  int seenBefore;
43  for(int i=from+1; i<=to; ++i){
44  seenBefore=0;
45  for(int j=1; j<=whichlevelindex; ++j){
46  if(fabs(whichlevel(j)-v(i))<eps){
47  seenBefore=1;
48  break;
49  }
50  }
51  if(!seenBefore){
52  whichlevel(++whichlevelindex)=v(i);
53  }
54  }
55  for(int j=1; j<=nlevels; ++j){
56  for(int i=from; i<=to; ++i){
57  if(fabs(v(i)-whichlevel(j))<eps){
58  idxlocal(i)=j;
59  };
60  }
61  }
62  return idxlocal;
63 }
64 
65 int count_factor(const ivector& v){
66  int from=v.indexmin();
67  int to=v.indexmax();
68  int counter=1;
69  int seenBefore;
70  for(int i=from+1; i<=to; ++i){ // can be optimized
71  seenBefore=0;
72  for(int j=from; j<=(i-1); ++j){
73  if(v(j)==v(i)){
74  seenBefore=1;
75  break;
76  }
77  }
78  if(seenBefore==0){
79  counter++;
80  }
81  }
82  return counter;
83 }
84 
85 void factor::allocate(const ivector& v, dvar_vector & par){
86  int from=v.indexmin();
87  int to=v.indexmax();
88 
89  ivector idxlocal(from,to);
90  if(par.indexmin()!=1)
91  {
92  cout<<"Error: par indexmin is not 1 in factor - something is wrong"<<endl;
93  ad_exit(1);
94  }
95  nlevels=par.indexmax();
96  ivector whichlevel(1,nlevels);
97  int whichlevelindex=0;
98  whichlevel(++whichlevelindex)=v(from);
99  int seenBefore;
100  for(int i=from+1; i<=to; ++i){
101  seenBefore=0;
102  for(int j=1; j<=whichlevelindex; ++j){
103  if(whichlevel(j)==v(i)){
104  seenBefore=1;
105  break;
106  }
107  }
108  if(!seenBefore){
109  whichlevel(++whichlevelindex)=v(i);
110  }
111  }
112  for(int j=1; j<=nlevels; ++j){
113  for(int i=from; i<=to; ++i){
114  if(v(i)==whichlevel(j)){
115  idxlocal(i)=j;
116  };
117  }
118  }
119  idx=idxlocal;
120  levels=par;
121 }
122 
124  return levels(idx(i));
125 }
126 
int count_factor(const dvector &v, const double &eps)
Definition: factors.cpp:14
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
ivector idx
Definition: factors.h:16
void allocate(const ivector &v, dvar_vector &par)
Definition: factors.cpp:85
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
ADMB variable vector.
Definition: fvar.hpp:2172
dvar_vector levels
Definition: factors.h:17
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
int indexmin() const
Definition: fvar.hpp:2287
Support functions for factor.
double eps
Definition: ftweak.cpp:13
ivector as_factor(const dvector &v, const double eps)
Definition: factors.cpp:34
dvariable operator()(int i)
Definition: factors.cpp:123
int nlevels
Definition: factors.h:15
int indexmax() const
Definition: fvar.hpp:2292
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518