Vector size and declaration

Anything that doesn't fit elsewhere.
Post Reply
Vincent d
Posts: 12
Joined: Fri Oct 12, 2007 4:30 am
Location: Lausanne

Vector size and declaration

Post by Vincent d »

Hi everyone,

i've a reproduced in a simple script a problem that occured in a complex one.

Basicaly, i declare 2 vectors of the size Number_Of_Loops.
Then there is a for_loop, I fill in the vectors at every turn of the loop.

This is working generally well but, for some parameters it freaks out!

The vectors have not a size = Number_Of_Loops anymore but
size = Number_Of_Loops - 1
Furthermore, Number_Of_Loops is not modified after being initialized.

This is annoying because the script goes out of range when trying to fill the last element of the vector...

Thank you for any advice.


Here is the simple code reproducing the problem, feel free to try other parameters:

Code: Select all

////////////////////////////////////////////////////////////////

indexmax   = 0.11    //problems also with indexmax = 0.21 and indexmin = 0.2   
indexmin   = 0.1    
Increment  = 0.01       

objref h, a, gh
gh = new Graph()              

k = 0
Number_Of_Loops =  (1 + (indexmax - indexmin) / Increment)
print "Number_Of_Loops= ", Number_Of_Loops

h = new Vector(Number_Of_Loops)     //Ordinate vector for the plot                       
a = new Vector(Number_Of_Loops)     //Abciss vector for the plot                        
print "Number_Of_Loops= ", Number_Of_Loops
print "size of h= ", h.size()
print "size of a= ", a.size()

for(index = indexmin; index <= indexmax; index = index + Increment){
    print "Loop Number:", k + 1, " / ", Number_Of_Loops, ", Ordinate = ", index
    
    h.x[index] = index
    a.x[index] = k

 k = k + 1
}//end of Amp

gh.size(0, k + 1, 0, indexmax)        // appropriate X-Y-scaling
h.plot(gh, a, 3,1)
ted
Site Admin
Posts: 6299
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

You're going to have lots of problems with this code, all of them related to roundoff
error issues.

1. It is not a good idea to iterate the index of a for loop over a range of fractional
numbers. Roundoff errors are inevitable, so that code works (loops for the "correct"
number of times) for some parameter sets but fails for others.

2. Vector indices are whole numbers, i.e. 0, 1, 2, etc.
hoc is somewhat forgiving, in that it ignores the fractional part of a number. That is,
vec.x has the same meaning as vec.x[int(i)]. But the for loop in your example will only
reference the 0 (first) element of h, and it will never get to the 1 (second) element.

3. h and a start out with the "wrong" size because of roundoff error. hoc's print statement
rounds Number_Of_Loops up to 2, but if you then execute
print Number_Of_Loops-2
you will see that the result is -4.4408921e-16. Apparently
objref vec = new Vector(num)
doesn't ignore this small difference. This code

Code: Select all

indexmax   = 0.11
indexmin   = 0.1   
Increment  = 0.01       

objref vec[3]

N =  (1 + (indexmax - indexmin) / Increment)

vec[0] = new Vector(2)
vec[1] = new Vector(N)
print "N-2 is ", N-2
vec[2] = new Vector(N + 5e-16)

for i=0,2 print "vec[", i, "].size() is ", vec[i].size()
produces these results:

Code: Select all

N-2 is -4.4408921e-16 
vec[0 ].size() is 2 
vec[1 ].size() is 1 
vec[2 ].size() is 2
If there is a moral to this story, it is "beware putting the success or failure of code at the
mercy of roundoff error."
Vincent d
Posts: 12
Joined: Fri Oct 12, 2007 4:30 am
Location: Lausanne

Post by Vincent d »

Thank you Ted for answering,

I guess i supposed the cast to be automatic.
For those who would like to prevent this error, you can cast your vector size by using the simple command "int".

i.e: N = int( ... )
vec = new Vector(N)

Very best,

=)
Post Reply