[Developers] Template functions and documentation

dave fournier davef at otter-rsch.com
Fri Jun 27 13:43:07 PDT 2014


On 06/27/2014 01:38 PM, Mark Maunder wrote:

see the concept traits in the template book. It is the way to deal with 
this.
>
> But that would mean the user has to know how to call the function in 
> this special way, which is probably asking too much.
>
> Wouldn't simply specifying the return type as being different from the 
> argument work? And the compiler would work out t1 from the definition 
> of len?
>
> len = vonB(age,linf,k,ro);
>
> template<typename t1, typename t2, typename t3>
>
> t1 vonB(const t2 age, const t3 linf, const t3 k,const t3 to)
>
> *From:*Steve Martell [mailto:SteveM at iphc.int]
> *Sent:* Friday, June 27, 2014 1:31 PM
> *To:* Mark Maunder
> *Cc:* developers at admb-project.org; Alexandre Aires-Da-Silva; Carolina 
> Minte-Vera; Sean Patrick Cox
> *Subject:* Re: [Developers] Template functions and documentation
>
> Sorry I pulled the send trigger to quickly, the baby woke up!
>
> What I was getting at is when you have function that returns a type 
> that is not passed as one of the arguments.  You can still use a 
> template function but when you call it, you have to specify what the 
> return type is.  eg
>
> len = vonB<dvar_vector>(age,linf,k,ro);
>
> In this case you would write the template as:
>
> template<typename t1, typename t2, typename t3>
>
> t1 vonB(const t2 age, const t3 linf, const t3 k,const t3 to)
>
> Steve
>
> On Jun 27, 2014, at 12:19 PM, Mark Maunder <mmaunder at iattc.org 
> <mailto:mmaunder at iattc.org>> wrote:
>
>
>
> Steve,
>
> Re: In this case, the function returns a dvector, but assigns it to a 
> dvar_vector.
>
> Yes, I recognize this. I think this can be solved by overloading the 
> template with an explicit type for the return variable or making the 
> return variable a third type.
>
> Mark
>
> *From:*Steve Martell [mailto:SteveM at iphc.int]
> *Sent:*Friday, June 27, 2014 11:31 AM
> *To:*Mark Maunder
> *Cc:*developers at admb-project.org <mailto:developers at admb-project.org>; 
> Alexandre Aires-Da-Silva; Carolina Minte-Vera; Sean Patrick Cox
> *Subject:*Re: [Developers] Template functions and documentation
>
> Hi Mark, Alex, Carolina
>
> Attached is a pretty comprehensive book on using template classes and 
> template functions in C++.  Its a great way to fall asleep at night.
>
> There is a lot of things you can do with templates, but from my naive 
> perspective its a great way to reduce the need for function 
> overloading and only have a single copy of the function to maintain. 
>  The primary thing to remember is that the compiler will write the 
> actual code that gets implemented; therefore, the function templates 
> are usually found in the header file (.h or .htp, or .hpp).
>
> One thing to be aware of is return types that differ from the types 
> passed to the function arguments.  For example:
>
> in the vonb.hpp file:
>
> template<typename t1, typename t2>
>
> t1 vonB(const t1 age, const t2 linf, const t2, vonbk)
>
> {
>
> return(linf*(1.-exp(-vonbk*age)));
>
> }
>
> in your tpl file:
>
> dvector age(1,5);
>
> age.fill_seqadd(1,1);
>
> dvar_vector len(1,5);
>
> len = vonB(age,linf,vbk);
>
> In this case, the function returns a dvector, but assigns it to a 
> dvar_vector.  And this will be problematic.
>
> Steve
>
> On Jun 27, 2014, at 11:05 A
>
> M, Mark Maunder <mmaunder at iattc.org <mailto:mmaunder at iattc.org>> wrote:
>
> Hi developers,
>
> I am in the process or putting together a model that will be used to 
> evaluate several growth equations so I thought I better try to use the 
> ADMB project "approved" approach for writing the functions. Below is a 
> template function and documentation for the von Bertalanffy growth 
> equation. I would appreciate any advice on doing this "correctly".
>
> Thanks,
>
> Mark
>
> /**
>
> \defgroup CONTRIB Contributed libraries
>
> */
>
> /**
>
> \ingroup CONTRIB
>
> \defgroup CAPAM CAPAM created functions
>
> */
>
> #include <admodel.h>
>
> /** von Bertalanffy growth equation; constant objects.
>
> \ingroup CAPAM
>
> \brief Calculate the length from a given age based on the von 
> Bertalanffy equation. Written by Mark Maunder.
>
> \param age age of individual, \f$a\f$.
>
> \param Linf asymptotic length, \f$L_inf\f$.
>
> \param K growth rate, \f$K\f$.
>
> \param t0 age at zero length, \f$t_0\f$.
>
> \return length predicted length of individual. 
> \f$L_inf*(1-exp(-K*(a-t0)))\f$.
>
> */
>
> template <typename type1, typename type1>
>
> //can have multiple types so that they are used below in variable 
> definitions, they can differ between function calls, but remain the 
> same within a function call
>
> //type1 is probably a long
>
> //type2 is probably a dvariable (either a model parameter (Linf K t0) 
> or a derived variable (length))
>
> //may need a type3, which is a dvar_vector if age and length are 
> vectors, where type 1 will be a vector
>
> type1 vonB(const type1 &age, const type2 &Linf, const type2 &K, const 
> type2 &t0)
>
> {
>
> type2 length = Linf*(1.-mfexp(-K*(age-t0)));
>
> return (length);
>
> }
>
> //is length a bad word to use
>
> //do we use ingroup for both groups and subgroups in doxygen documentation
>
> //should we use mfexp?
>
> //what if age and length are vectors, but age is a vector and length 
> is a dvar_vector, do we need to overload the type with one with three 
> variable types
>
> //I couldn't work out how to put in the detailed description
>
> //The equations did not work on my computer
>
> _______________________________________________
> Developers mailing list
> Developers at admb-project.org <mailto:Developers at admb-project.org>
> http://lists.admb-project.org/mailman/listinfo/developers
>
> ------------------------------------------------------------------------
>
>
> This internet e-mail message, and any files transmitted with it, 
> contains confidential, privileged information that is intended only 
> for the addressee. If you have received this e-mail message in error, 
> please call us at (206) 634-1838 collect if necessary) and ask to 
> speak to the message sender. Nothing in this e-mail or the act of 
> transmitting it, is to be construed as a waiver of any rights or 
> privileges enjoyed by the sender or the International Pacific Halibut 
> Commission pursuant to the International Organizations Immunities Act, 
> 22 U.S.C. Sec. 288 et seq.
>
> ------------------------------------------------------------------------
>
>
> This internet e-mail message, and any files transmitted with it, 
> contains confidential, privileged information that is intended only 
> for the addressee. If you have received this e-mail message in error, 
> please call us at (206) 634-1838 collect if necessary) and ask to 
> speak to the message sender. Nothing in this e-mail or the act of 
> transmitting it, is to be construed as a waiver of any rights or 
> privileges enjoyed by the sender or the International Pacific Halibut 
> Commission pursuant to the International Organizations Immunities Act, 
> 22 U.S.C. Sec. 288 et seq.
>
>
>
> _______________________________________________
> Developers mailing list
> Developers at admb-project.org
> http://lists.admb-project.org/mailman/listinfo/developers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.admb-project.org/pipermail/developers/attachments/20140627/93f56388/attachment-0001.html>


More information about the Developers mailing list