[Developers] Template functions and documentation

Mark Maunder mmaunder at iattc.org
Tue Jul 1 10:08:32 PDT 2014


Thanks Steve. For a growth function (for example) library what you suggest would probably be easy.

From: Steve Martell [mailto:SteveM at iphc.int]
Sent: Tuesday, July 1, 2014 9:51 AM
To: Mark Maunder
Cc: dave fournier; Sean Patrick Cox; developers at admb-project.org; Alexandre Aires-Da-Silva; Carolina Minte-Vera
Subject: Re: [Developers] Template functions and documentation

Hi Mark,

It's a bit complicated I suppose, but you can recycle the traits for other functions as well, so you don't have to write a bunch of classes for each of the functions.  However, functions that require specific specializations will have to have their own traits attributed to them.

For example, you should be able to add the Richards growth function template, and use the same vonBtraits to return the appropriate dvar_vector or dvector.

Steve
On Jun 30, 2014, at 2:10 PM, Mark Maunder <mmaunder at iattc.org<mailto:mmaunder at iattc.org>> wrote:


Thanks Steve,

I was able to work out what the code is doing, but this is complicated. I would hate to do this for every function, but I guess that is what you should do for ADMB libraries.

Mark

From: developers-bounces at admb-project.org<mailto:developers-bounces at admb-project.org> [mailto:developers-bounces at admb-project.org] On Behalf Of Steve Martell
Sent: Friday, June 27, 2014 11:55 PM
To: dave fournier
Cc: Sean Patrick Cox; developers at admb-project.org<mailto:developers at admb-project.org>; Alexandre Aires-Da-Silva; Carolina Minte-Vera
Subject: Re: [Developers] Template functions and documentation

Here is an example using the vonb model.

You should just be able to compile the tpl file and be sure the hpp file is saved in the same directory.  Note that I've commented out the standard template and the implemented two traits where if the user passes doubles for the growth parameters, the function returns a dvector, and if the user passes dvariables, then the function returns a dvar_vector.

Steve


Steve
On Jun 27, 2014, at 2:19 PM, Steve Martell <SteveM at iphc.int<mailto:SteveM at iphc.int>> wrote:

The Soln Dave points to is the most robust. You would have to overload the template function to accommodate all of the possible combinations otherwise.  I have an example of traits I can send you when I get back to my mac.

Sent from my iPad

On Jun 27, 2014, at 1:43 PM, "dave fournier" <davef at otter-rsch.com<mailto:davef at otter-rsch.com>> wrote:
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<mailto: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<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.
_______________________________________________
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.admb-project.org/pipermail/developers/attachments/20140701/456fcd3c/attachment-0001.html>


More information about the Developers mailing list