Changeset 1561


Ignore:
Timestamp:
Oct 24, 2009 11:46:25 AM (11 years ago)
Author:
bradbell
Message:

trunk: Add the assignment operator to the ADFun<Base> class.

check_doxygen.sh: add fun_construct.hpp to list of doxygen documented files.
makefile.am: add the fun_assign to list of examples.
example.cpp: add the fun_assign to list of examples.
makefile.in: changes transferred automatically from changes in makefile.am.
example_list.omh: add fun_assign to list of examples.
whats_new_09.omh: user's view of the changes.
player.hpp: add the assignment operator to this class (also).
ad_fun.hpp: add ADFun assignment operator, check for use of copy constructor.
fun_construct.hpp: implement and document ADFun copy constructor.

Location:
trunk
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/check_doxygen.sh

    r1558 r1561  
    4343        for_jac_sweep.hpp
    4444        for_sparse_jac.hpp
     45        fun_construct.hpp
    4546        hash_code.hpp
    4647        load_op.hpp
  • trunk/cppad/local/ad_fun.hpp

    r1558 r1561  
    119119// Private member functions
    120120
     121# ifdef NDEBUG
     122        // disable use of the copy constructor
     123        // private if NDEBUG is defined, and public otherwise
     124        inline ADFun(const ADFun &f)
     125        {       CPPAD_ASSERT_UNKNOWN(false); }
     126# endif
     127
    121128        /// change the operation sequence corresponding to this object
    122129        template <typename ADvector>
     
    222229// ------------------------------------------------------------
    223230public:
     231# ifndef NDEBUG
     232        /// copy constructor
     233        // priviate if NDEBUG is defined, and public otherwise
     234        ADFun(const ADFun& g)
     235        : total_num_var_(0), taylor_(CPPAD_NULL)
     236        {       CPPAD_ASSERT_KNOWN(
     237                false,
     238                "Attempting to use the ADFun<Base> copy constructor.\n"
     239                "Perhaps you are passing an ADFun<Base> object "
     240                "by value instead of by reference."
     241                );
     242         }
     243# endif
    224244        /// default constructor
    225         ADFun(void)
    226         : total_num_var_(0), taylor_(CPPAD_NULL)
    227         { }
     245        ADFun(void);
     246
     247        // assignment operator
     248        // (see doxygen in fun_construct.hpp)
     249        void operator=(const ADFun& f);
    228250
    229251        /// sequence constructor
  • trunk/cppad/local/fun_construct.hpp

    r1514 r1561  
    3333
    3434$head Syntax$$
    35 $codei%ADFun<%Base%> %f%
     35$codei%ADFun<%Base%> %f%, %g%
    3636%$$
    3737$codei%ADFun<%Base%> %f%(%x%, %y%)
     38%$$
     39$icode%g% = %f%
    3840%$$
    3941
     
    7375%$$
    7476The sequence of operations that map $icode x$$
    75 to $icode y$$ are stored in the AD function object $icode f$$.
     77to $icode y$$ are stored in the ADFun object $icode f$$.
    7678
    7779$head VectorAD$$
     
    8385
    8486$head Default Constructor$$
     87$index default, ADFun constructor$$
     88$index ADFun, default constructor$$
     89$index constructor, ADFun constructor$$
    8590The default constructor
    8691$codei%
     
    95100
    96101$head Sequence Constructor$$
     102$index sequence, ADFun constructor$$
     103$index ADFun, sequence constructor$$
     104$index constructor, ADFun sequence$$
     105The default constructor
    97106The sequence constructor
    98107$codei%
     
    109118(corresponding to the value of $icode x$$) in $icode f$$.
    110119This is equivalent to the following steps using the default constructor:
     120
    111121$list number$$
    112122Create $icode f$$ with the default constructor
     
    130140(see $xref/Forward/$$).
    131141$lend
     142
     143$head Copy Constructor$$
     144$index copy, ADFun constructor$$
     145$index ADFun, copy constructor$$
     146$index constructor, ADFun copy$$
     147It is an error to attempt to use the $code%ADFun<%Base%>%$$ copy constructor;
     148i.e., the following syntax is not allowed:
     149$codei%
     150        ADFun<%Base%> g(f)
     151%$$
     152where $icode f$$ is an $code%ADFun<%Base%>%$$ object.
     153Use its $cref/default constructor/FunConstruct/Default Constructor/$$ instead
     154and its assignment operator.
     155
     156$head Assignment Operator$$
     157$index ADFun, assignment operator$$
     158$index assignment, ADFun operator$$
     159$index operator, ADFun assignment$$
     160The $codei%ADFun<%Base%>%$$ assignment operation
     161$codei%
     162        %g% = %f%
     163%$$.
     164makes a copy of the operation sequence currently stored in $icode f$$
     165in the object $icode g$$.
     166The object $icode f$$ is not affected by this operation and
     167can be $code const$$.
     168Any operation sequence or other information in $icode g$$ is lost.
     169Directly after this operation, no Taylor coefficients are
     170stored in $icode g$$; i.e.,
     171$codei%
     172        %g%.size_taylor()
     173%$$
     174is zero (see $cref/size_taylor/$$).
     175
    132176
    133177$head OpenMP$$
     
    165209They return true if they succeed and false otherwise.
    166210
     211$children%
     212        example/fun_assign.cpp
     213%$$
     214$subhead Assignment Operator$$
     215The file
     216$cref/fun_assign.cpp/$$
     217contains an example and test of the $codei%ADFun<%Base%>%$$
     218assignment operator.
     219It returns true if it succeeds and false otherwise.
     220
    167221$end
     222----------------------------------------------------------------------------
    168223*/
    169 
    170 
    171 // BEGIN CppAD namespace
    172 namespace CppAD {
    173 
     224CPPAD_BEGIN_NAMESPACE
     225
     226/*!
     227\file fun_construct.hpp
     228ADFun function constructors and assignment operator.
     229*/
     230
     231/*!
     232ADFun default constructor
     233
     234The C++ syntax for this operation is
     235\verbatim
     236        ADFun<Base> f
     237\endverbatim
     238An empty ADFun object is created.
     239The Dependent member function,
     240or the ADFun<Base> assingment operator,
     241can then be used to put an operation sequence in this ADFun object.
     242
     243\tparam Base
     244is the base for the recording that can be stored in this ADFun object;
     245i.e., operation sequences that were recorded using the type \c AD<Base>.
     246*/
     247template <typename Base>
     248ADFun<Base>::ADFun(void)
     249: total_num_var_(0), taylor_(CPPAD_NULL)
     250{ }
     251
     252/*!
     253ADFun assignment operator
     254
     255The C++ syntax for this operation is
     256\verbatim
     257        g = f
     258\endverbatim
     259where \c g and \c f are ADFun<Base> ADFun objects.
     260A copy of the the operation sequence currently stored in \c f
     261is placed in this ADFun object (called \c g above).
     262Any information currently stored in this ADFun object is lost.
     263
     264\tparam Base
     265is the base for the recording that can be stored in this ADFun object;
     266i.e., operation sequences that were recorded using the type \c AD<Base>.
     267
     268\param f
     269ADFun object containing the operation sequence to be copied.
     270*/
     271template <typename Base>
     272void ADFun<Base>::operator=(const ADFun<Base>& f)
     273{       size_t m = f.Range();
     274        size_t n = f.Domain();
     275
     276        // go through member variables in order
     277        // (see ad_fun.hpp for meaning of each variable)
     278        compare_change_            = 0;
     279        taylor_per_var_            = 0;
     280        taylor_col_dim_            = 0;
     281        total_num_var_             = f.total_num_var_;
     282        ind_taddr_.resize(n);
     283        ind_taddr_                 = f.ind_taddr_;
     284        dep_taddr_.resize(m);
     285        dep_taddr_                 = f.dep_taddr_;
     286        dep_parameter_.resize(m);
     287        dep_parameter_             = f.dep_parameter_;
     288        play_                      = f.play_;
     289        if( taylor_ != CPPAD_NULL )
     290                CPPAD_TRACK_DEL_VEC(taylor_);
     291        taylor_                    = CPPAD_NULL;
     292        for_jac_sparse_pack_.resize(0, 0);
     293        for_jac_sparse_set_.resize(0, 0);
     294}
     295
     296/*!
     297ADFun constructor from an operation sequence.
     298
     299The C++ syntax for this operation is
     300\verbatim
     301        ADFun<Base> f(x, y)
     302\endverbatim
     303The operation sequence that started with the previous call
     304\c Independent(x), and that ends with this operation, is stored
     305in this \c ADFun<Base> object \c f.
     306
     307\tparam Base
     308is the base for the recording that will be stored in the object \c f;
     309i.e., the operations were recorded using the type \c AD<Base>.
     310
     311\tparam VectorAD
     312is a simple vector class with elements of typea \c AD<Base>.
     313
     314\param x
     315is the independent variable vector for this ADFun object.
     316The domain dimension of this object will be the size of \a x.
     317
     318\param y
     319is the dependent variable vector for this ADFun object.
     320The range dimension of this object will be the size of \a y.
     321
     322\par Taylor Coefficients
     323A zero order forward mode sweep is done,
     324and if NDEBUG is not defined the resulting values for the
     325depenedent variables are checked against the values in \a y.
     326Thus, the zero order Taylor coefficients
     327corresponding to the value of the \a x vector
     328are stored in this ADFun object.
     329*/
    174330template <typename Base>
    175331template <typename VectorAD>
     
    263419}
    264420
    265 } // END CppAD namespace
    266 
     421CPPAD_END_NAMESPACE
    267422# endif
  • trunk/cppad/local/player.hpp

    r1535 r1561  
    109109        }
    110110
    111         /*!  Creating an object that palys back an operation sequence.
     111        // ===============================================================
     112        // Begin two functions with idential code but different argument types.
     113        /*! 
     114        Moving an operation sequence from a recorder to a player
    112115 
    113116        \param rec
     
    191194        }
    192195
     196        /*! 
     197        Copying an operation sequence from one player to another
     198 
     199        \param play
     200        the object that contains the operatoion sequence to copy.
     201        */
     202        void operator=(const player& play)
     203        {       size_t i;
     204
     205                if( num_rec_op_ > 0 )
     206                        CPPAD_TRACK_DEL_VEC(rec_op_);
     207                if( num_rec_vecad_ind_ > 0 )
     208                        CPPAD_TRACK_DEL_VEC(rec_vecad_ind_);
     209                if( num_rec_op_arg_ > 0 )
     210                        CPPAD_TRACK_DEL_VEC(rec_op_arg_);
     211                if( num_rec_par_ > 0 )
     212                        CPPAD_TRACK_DEL_VEC(rec_par_);
     213                if( num_rec_text_ > 0 )
     214                        CPPAD_TRACK_DEL_VEC(rec_text_);
     215
     216                // Var
     217                num_rec_var_        = play.num_rec_var_;
     218
     219                // Op
     220                num_rec_op_         = play.num_rec_op_;
     221
     222                // VecInd
     223                num_rec_vecad_ind_  = play.num_rec_vecad_ind_;
     224
     225                // Arg
     226                num_rec_op_arg_     = play.num_rec_op_arg_;
     227
     228                // Par
     229                num_rec_par_        = play.num_rec_par_;
     230
     231                // Txt
     232                num_rec_text_       = play.num_rec_text_;
     233
     234                // Allocate the memory
     235                if( num_rec_op_ == 0 )
     236                        rec_op_ = CPPAD_NULL;
     237                else    rec_op_ =
     238                        CPPAD_TRACK_NEW_VEC(num_rec_op_,        rec_op_);
     239                if( num_rec_vecad_ind_ == 0 )
     240                        rec_vecad_ind_ = CPPAD_NULL;
     241                else    rec_vecad_ind_ =
     242                        CPPAD_TRACK_NEW_VEC(num_rec_vecad_ind_, rec_vecad_ind_);
     243                if( num_rec_op_arg_ == 0 )
     244                        rec_op_arg_ = CPPAD_NULL;
     245                else    rec_op_arg_ =
     246                        CPPAD_TRACK_NEW_VEC(num_rec_op_arg_,    rec_op_arg_);
     247                if( num_rec_par_ == 0 )
     248                        rec_par_ = CPPAD_NULL;
     249                else    rec_par_ =
     250                        CPPAD_TRACK_NEW_VEC(num_rec_par_,       rec_par_);
     251                if( num_rec_text_ == 0 )
     252                        rec_text_ = CPPAD_NULL;
     253                else    rec_text_ =
     254                        CPPAD_TRACK_NEW_VEC(num_rec_text_,      rec_text_);
     255
     256                // Copy the data
     257                i = num_rec_op_;
     258                while(i--)
     259                        rec_op_[i] = play.rec_op_[i];
     260                i = num_rec_vecad_ind_;
     261                while(i--)
     262                        rec_vecad_ind_[i] = play.rec_vecad_ind_[i];
     263                i = num_rec_op_arg_;
     264                while(i--)
     265                        rec_op_arg_[i] = play.rec_op_arg_[i];
     266                i = num_rec_par_;
     267                while(i--)
     268                        rec_par_[i] = play.rec_par_[i];
     269                i = num_rec_text_;
     270                while(i--)
     271                        rec_text_[i] = play.rec_text_[i];
     272
     273                // set the number of VecAD vectors
     274                num_rec_vecad_vec_ = 0;
     275                for(i = 0; i < num_rec_vecad_ind_; i += rec_vecad_ind_[i] + 1)
     276                        num_rec_vecad_vec_++;
     277        }
     278        // End two functions with idential code but different argument types.
     279        // ===============================================================
     280
    193281        /// Erase all information in an operation sequence player.
    194282        void Erase(void)
  • trunk/example/example.cpp

    r1553 r1561  
    7777extern bool ForSparseJac(void);
    7878extern bool Forward(void);
     79extern bool fun_assign(void);
    7980extern bool FunCheck(void);
    8081extern bool HesLagrangian(void);
     
    220221        ok &= Run( Forward,           "Forward"          );
    221222        ok &= Run( ForSparseJac,      "ForSparseJac"     );
     223        ok &= Run( fun_assign,        "fun_assign"       );
    222224        ok &= Run( FunCheck,          "FunCheck"         );
    223225        ok &= Run( HesLagrangian,     "HesLagrangian"    );
  • trunk/example/makefile.am

    r1505 r1561  
    9999        forward.cpp \
    100100        for_sparse_jac.cpp \
     101        fun_assign.cpp \
    101102        fun_check.cpp \
    102103        hes_lagrangian.cpp \
  • trunk/omh/example_list.omh

    r1505 r1561  
    117117$rref Forward.cpp$$
    118118$rref ForSparseJac.cpp$$
     119$rref fun_assign.cpp$$
    119120$rref FunCheck.cpp$$
    120121$rref get_started.cpp$$
  • trunk/omh/whats_new_09.omh

    r1558 r1561  
    5959trying to read and understand the CppAD source code.)
    6060
     61$head 10-24$$
     62Added the $cref/assignment operator/FunConstruct/Assignment Operator/$$
     63to the $code ADFun$$ object class.
     64This makes a copy of the entire operation sequence in another
     65function object.
     66The intention is that the two functions objects can do calculations in
     67parallel.
     68In addition,
     69CppAD now check for the $code ADFun$$
     70$cref/copy constructor/FunConstruct/Copy Constructor/$$
     71and generates an error message if it is used.
     72
    6173$head 10-23$$
    6274The $cref/sparse_hessian/$$ routine was extended so the user can now choose
  • trunk/test_more/makefile.in

    r1560 r1561  
    130130CYGPATH_W = @CYGPATH_W@
    131131
    132 # $Id: makefile.am 1556 2009-10-21 14:41:47Z bradbell $
     132# $Id: makefile.am 1558 2009-10-23 15:46:58Z bradbell $
    133133# -----------------------------------------------------------------------------
    134134# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell
Note: See TracChangeset for help on using the changeset viewer.