[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Recursion / global variables trouble
From: 
John W. Eaton 
Subject: 
Recursion / global variables trouble 
Date: 
Fri, 25 Jan 2002 03:56:32 0600 
On 25Jan2002, Pieter Thysebaert <address@hidden> wrote:
 Hello,

 I'm new to this mailing list, but I've already got some questions ;)

 The version of octave I'm using is
 GNU Octave, version 2.0.16 (i386pclinuxgnu)
 on Debian GNU/Linux 2.2

 I needed to plot a function which is defined as N1 nested sums (over the
 variables x1,...,x(N1) say).
 These values do not range over some interval independently of one another,
 but their sum must equal a certain fixed number, say Q (so there are N
 variables, but the last one can be calculated from the first N1 vars)

 So x1 ranges from 0 to Q, for every x1 value, x2 ranges from 0 to Qx1, ....

 However, N is a parameter and I would like to be able to plot this function
 for different values of N.

 The approach I came up with is to use a global vector of elements, to be used
 to store the actual values of the iteration variables in some iteration.

 Specifically, what I wrote looks like:

 function res=calc_term_in_function_of_iteration_vars(z)
 global iterationvars;
 % calculate useful things and store into res
 endfunction

 function r=forloop(z,depth)
 global iterationvars; %vector that holds the variable values
 global N;
 global Q;

 if (depth==N)
 r = calc_term_in_function_of_iteration_vars(z);
 else
 for iterationvars(depth)=0:Qsum(iterationvars(1:depth1))
 %for x1, where depth = 1, sum(iterationvars(1:0)) will return 0=OK
 %DEBUG printf
 printf("Value of x%d is %d\n\r",depth,iterationvars(depth));
 r = r+forloop(z,depth+1);
 endfor
 endif
 endfunction

 To evaluate the function in a point z, I call forloop(z,1) (after having
 initialized the needed global variables as global)

 As you can guess from where I inserted the printf, things don't work like I
 expected them to do:
 I have tried it with N=2 and Q=2, and then for each iteration at depth 1
 (there are three terms : x1 = 0,1,2=Q) I get
 Value of x1 = 0

 So somehow iterationvars(depth) is not adjusted properly (in the for...line)
 Can anybody help me with this parameter passing /global variable issue ?
I'm not sure I follow what you're trying to do, but it seems to me
that there is a bug in Octave 2.0.x that prevents code like
for x(i) = 1:n
from working correctly, though it seems to work in the current
development version (2.1.35):
2.0.16:
octave:4> x = ones (1,3); i = 3; n = 5; for x(i) = 1:5 x(i) end
ans = 1
ans = 1
ans = 1
ans = 1
ans = 1
octave:5> x
x =
1 1 1
2.1.35 (this is the behavior I would expect):
octave2.1:1> x = ones (1,3); i = 3; n = 5; for x(i) = 1:5 x(i) end
ans = 1
ans = 2
ans = 3
ans = 4
ans = 5
octave2.1:2> x
x =
1 1 5
I suppose a workaround would be to write
for tmp = 1:n
x(i) = tmp;
...
If this isn't the problem, then can you please post a complete working
example that demonstrates the problem?
Thanks,
jwe

Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
