ADMB Documentation  11.1.2246
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_dif.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_dif.cpp 1919 2014-04-22 22:02:01Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 void DF_first_diference(void);
00014 
00019 dvar_vector first_difference(const dvar_vector& x)
00020  {
00021    if (x.size()<=1)
00022    {
00023      cerr << "Error -- vector size too small"
00024              " in first_difference(const dvar_vector&)" << endl;
00025      ad_exit(1);
00026    }
00027 
00028    RETURN_ARRAYS_INCREMENT();
00029    int mmin=x.indexmin();
00030    int mmax=x.indexmax()-1;
00031    dvar_vector tmp(mmin,mmax);
00032    for (int i=mmin; i<=mmax; i++)
00033    {
00034      tmp.elem_value(i)=x.elem_value(i+1)-x.elem_value(i);
00035    }
00036   save_identifier_string("CE4");
00037   x.save_dvar_vector_position();
00038   tmp.save_dvar_vector_position();
00039   save_identifier_string("CE1");
00040   gradient_structure::GRAD_STACK1->
00041       set_gradient_stack(DF_first_diference);
00042    RETURN_ARRAYS_DECREMENT();
00043    return(tmp);
00044  }
00045 
00050 void DF_first_diference(void)
00051 {
00052   verify_identifier_string("CE1");
00053   dvar_vector_position tmp_pos=restore_dvar_vector_position();
00054   dvar_vector_position x_pos=restore_dvar_vector_position();
00055   verify_identifier_string("CE4");
00056   dvector dftmp=restore_dvar_vector_derivatives(tmp_pos);
00057   dvector dfx(x_pos.indexmin(),x_pos.indexmax());
00058   dfx.initialize();
00059   for (int i=dfx.indexmax()-1; i>=dfx.indexmin(); i--)
00060   {
00061     // tmp.elem_value(i)=x.elem_value(i+1)-x.elem_value(i);
00062        dfx.elem(i+1)+=dftmp.elem(i);
00063        dfx.elem(i)-=dftmp.elem(i);
00064   }
00065   dfx.save_dvector_derivatives(x_pos);
00066 }