[ADMB Users] Implementing a convolution in the GLOBAL_SECTION

11 views
Skip to first unread message

Øigård Tor Arne

unread,
May 25, 2012, 4:41:32 AM5/25/12
to us...@admb-project.org
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
Us...@admb-project.org
http://lists.admb-project.org/mailman/listinfo/users

Weihai Liu

unread,
May 25, 2012, 8:31:02 PM5/25/12
to Øigård Tor Arne, us...@admb-project.org
should move 
 dvar_vector y(1,n+m-1);

after following lines
  m=size_count(x);
  n=size_count(h);


weihai

John Sibert

unread,
May 25, 2012, 9:30:00 PM5/25/12
to Øigård Tor Arne, us...@admb-project.org
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/

John Sibert

unread,
May 26, 2012, 4:11:20 PM5/26/12
to Øigård Tor Arne, us...@admb-project.org
Glad you were able to get it working.

Regarding the message in the .log file (ie simple.log), it can be safely
ignored. It seems to contain the same information under all conditions.

John Sibert
Emeritus Researcher, SOEST
University of Hawaii at Manoa

Visit the ADMB project http://admb-project.org/


On 05/26/2012 06:21 AM, Øigård Tor Arne wrote:
> Thanks to both John and Weihai,
>
> Yes it works now. I really appreciate it. Thanks for the extra information
> regarding dvar_vector and dvectors!
>
> Kind regards,
> Tor Arne
>
> On 5/26/12 02:30 , "John Sibert"<sib...@hawaii.edu> wrote:
>
>> 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
>>
>

Øigård Tor Arne

unread,
May 26, 2012, 12:21:34 PM5/26/12
to John Sibert, us...@admb-project.org
Thanks to both John and Weihai,

Yes it works now. I really appreciate it. Thanks for the extra information
regarding dvar_vector and dvectors!

Kind regards,
Tor Arne

On 5/26/12 02:30 , "John Sibert" <sib...@hawaii.edu> wrote:

>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
>

Reply all
Reply to author
Forward
0 new messages