[ADMB Users] Implementing a convolution in the GLOBAL_SECTION
John Sibert
sibert at hawaii.edu
Fri May 25 18:30:00 PDT 2012
It seems that the vectors x and h are data and the output of the conv1(
) funtion, does not depend on the model parameters. If that is the case
they should be declared to be dvectors. A dvar_vector is a container
class for a variable for which derivative information is to be
accumulated. I'm not sure which "log" file you are looking at, but the
error "size of file cmpdiff.tmp = 0" may have been caused by
attempting to use a variable object (dvar_vector) before temporary
memory for the derivative information has been allocated.
Weihai is correct about the order of statements. If you follow the
simple cpp practice of not declaring multiple variables on a single
line, the compiler might have picket up the error. ie
dvector conv1(const dvector& x, const dvector& h)
{
dvector lowerlimit(1,2);
dvector upperlimit(1,2);
dvector y(1,n+m-1); // error since m and n are not declared yet.
int m=size_count(x);
int n=size_count(h);
Cheers,
John
John Sibert
Emeritus Researcher, SOEST
University of Hawaii at Manoa
Visit the ADMB project http://admb-project.org/
On 05/24/2012 10:41 PM, Øigård Tor Arne wrote:
> Hi all,
>
> I tried to implement a convolution in the GLOBAL_SECTION so that I can call a function conv(x,h) in the PROCEDURE_SECTION. The program compiles, but does not run, get the error message "size of file cmpdiff.tmp = 0" in the log.
>
> I read in two vectors x and h in the DATA_SECTION and want to create a vector y = conv(x,h) in the PROCEDURE_SECTION. The variable y has been declared as a vector in the PARAMETER_SECTION. In the GLOBAL_SECTION i added:
>
> #include<admodel.h>
>
> dvar_vector conv1(const dvar_vector& x, const dvar_vector& h)
> {
>
> int k,j,m,n;
> dvector lowerlimit(1,2);
> dvector upperlimit(1,2);
> dvar_vector y(1,n+m-1);
>
> m=size_count(x);
> n=size_count(h);
>
> for(k=1;k<=(int) (m+n-1);k++)
> {
> y(k) = 0;
> lowerlimit(1) = 1;
> lowerlimit(2) = k+1-n;
> upperlimit(1) = k;
> upperlimit(2) = m;
>
> for(j=max(lowerlimit);j<=(int) min(upperlimit);j++)
> {
> y(k) = y(k) + x(j)*h(k-j+1);
> }
> }
>
> return y;
>
> }
>
> So the local variable y in the function is declared as a dvar_vector, and the function is declared as a dvar_vector. I wonder if the problem has something to do with that.
>
> I tried to make another conv-function (below) which only returns one element of the output vector y, i.e., y_i, instead of the whole vector y, and then run a loop in the PROCEDURE_SECTION (the first loop in the conv1-function was moved to the PROCEDURE_SECTION). In this scenario the function conv2 is declared as a dvariable, and the local variable y (scalar) is declared as a dvariable. In this case it worked like a charm. Hence, it looks like I do something wrong when declaring the variables for the conv1-function. I also tried to replace the dvar_vector declaration for the conv1 and the local y vector with a dvector, but that did not help much.
>
>
> dvariable conv2(const dvar_vector& x, const dvar_vector& h, const double& k)
> {
>
> int j,m,n;
> dvector lowerlimit(1,2);
> dvector upperlimit(1,2);
> dvariable y;
>
> m=size_count(x);
> n=size_count(h);
>
> y = 0;
> lowerlimit(1) = 1;
> lowerlimit(2) = k+1-n;
> upperlimit(1) = k;
> upperlimit(2) = m;
>
>
> for(j=max(lowerlimit);j<=(int) min(upperlimit);j++)
> {
> y = y + x(j)*h(k-j+1);
> }
>
> return y;
>
> }
>
> Any good advice is very much appreciated.
>
> Kind regards,
> Tor Arne
> _______________________________________________
> Users mailing list
> Users at admb-project.org
> http://lists.admb-project.org/mailman/listinfo/users
>
More information about the Users
mailing list