## Arrays, Vectors and Doubles?

The basics of how to develop, test, and use models.
Bill Connelly
Posts: 86
Joined: Thu May 22, 2008 11:54 pm
Location: Australian National University

### Arrays, Vectors and Doubles?

Hi,

I'm working through the HOC exercises from the neuron course, and I'm deeply confused by a couple of things. I do the following:

Code: Select all

``````double x[1]
x[0]=10``````
So I'm making an array with one element which is a double, and then setting it's value to 10. I'm fine with that. But if I try:

Code: Select all

``````objref y[1]
y[0]=10``````
...and I get an error. I though all variables were by default doubles? What's going on here? Is it because the idea behind objref is that is should be used only for declaring variables which will be used to point to objects?

And is that why there are both arrays and vectors... an can potentially be used holding anything, and whether an array is defined with 'double' or 'objref' decides whether it is happy holding a number or an object pointer?

Finally, how do I declare an array without stating how many elements it has and/or adding elements on the fly?
ted
Posts: 5810
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

### Re: Arrays, Vectors and Doubles?

Doubles are not objrefs
double x[n]
declares an array of doubles.
objref y[n]
declares n objrefs whose names are y[0]...y[n-1].
I though all variables were by default doubles?
False. To paraphrase Orwell, "the slovenliness of our language makes it easier tor us to
make programming errors."

The type of a user-defined symbol is determined by its first appearance in a program.

Code: Select all

``````oc>y = sin(0.1)  // first appearance of y
oc>y
0.099833417
oc>y[0]
/usr/local/nrn/i686/bin/nrniv: y not an array variable
near line 9
y[0]
^``````
So y is a scalar with double precision. The declaration
double y[n]
would make y be an array of n double precision values. Although variables that are
explicitly declared with
double varname
syntax are actually arrays, they are generally called "doubles".

objrefs are objrefs, not scalars or arrays of scalars.

Code: Select all

``````oc>objref z
oc>z = x[0]
bad stack access: expecting (Object **); really (double)
/usr/local/nrn/i686/bin/nrniv: interpreter stack type error
near line 14
z = x[0]
^
oc>z = y
bad stack access: expecting (Object **); really (double)
/usr/local/nrn/i686/bin/nrniv: interpreter stack type error
near line 15
z = y
^``````
hoc has arrays and Vectors as an accident of history. It started as a toy programming
language with a c-like syntax in the mid 1970s, at which time it had scalars and arrays of
scalars. Objects (which include Vectors) were added later.
Finally, how do I declare an array without stating how many elements it has and/or adding elements on the fly?
To the extent possible, avoid using "doubles" (variables declared with
double varname
syntax). Stick with Vectors. The Vector class has several methods for changing the