Page 1 of 1
pass Vector as argument to a function
Posted: Sat Mar 07, 2009 3:23 pm
by pfortier
Is it possible to pass a Vector() as an argument to a function?
For example:
Code: Select all
objref src
src=new Vector(5,99)
func readvector() {
return $&1.x[3] // I want to have access to all other methods and properties of Vector()
}
print readvector(&src)
If not possible for a function, then is it possible for a template?
Thanks,
Pierre
Re: pass Vector as argument to a function
Posted: Sat Mar 07, 2009 5:19 pm
by ted
Your example is almost correct;
return $o1.x[3]
would fix it. Note that & is not needed, since objrefs are automatically passed by reference.
Arguments and other aspects hoc syntax are discussed fairly extensively in the Programmer's Reference--see
http://www.neuron.yale.edu/neuron/stati ... yntax.html
Be sure to look at some of the other stuff (especially iterator, obfunc) while you're there.
Re: pass Vector as argument to a function
Posted: Mon Mar 09, 2009 10:23 am
by pfortier
Thank you, that worked well.
Here is a related question. Can I pass an array of objects, as shown below for example?
Code: Select all
objref ab[2][3]
ab[0][0]=new Vector(10,0)
ab[0][1]=new Vector(10,01)
ab[0][2]=new Vector(10,02)
ab[1][0]=new Vector(10,10)
ab[1][1]=new Vector(10,11)
ab[1][2]=new Vector(10,12)
proc showvectors() { local i
for i=0,1 for j=0,2 print "$o1[",i,"][",j,"].x[5]=",$o1[i][j].x[5]
}
showvectors(ab)
The actual array I'm working with has 3 dimensions.
Thanks,
Pierre
Re: pass Vector as argument to a function
Posted: Mon Mar 09, 2009 10:35 am
by ted
Why not?
Re: pass Vector as argument to a function
Posted: Mon Mar 09, 2009 10:38 am
by pfortier
Because it doesn't work. Could you please indicate what is required to make it work?
Thanks,
Pierre
Re: pass Vector as argument to a function
Posted: Mon Mar 09, 2009 1:02 pm
by ted
Of course, clear in retrospect. Declaring objref ab[N1][N2] creates multiple objrefs, and a subsequent statement makes ab[0][0] point to a Vector. However, procname(ab) does NOT tell the code inside procname that there is any object other than the Vector that ab points to, let alone that those objects are also Vectors.
So you need to write a template that defines an "N dimensional array of Vectors" class. The first step might be to define a "1 dimensional array of Vectors" class, then the second step would be to define an "array of 1 dimensional arrays of Vectors" class that exploits your first class; both of these would use Lists. Or skip the first step and just use the Matrix class for your 1D array of Vectors, and make an "array of Matrices" class (again, the List class will be useful).
I have the feeling that in Python none of this would even be an issue--that it would be trivial to write a couple of lines of code that create and use arrays of arrays, without having to create any new class definitions.
Re: pass Vector as argument to a function
Posted: Tue Mar 10, 2009 11:14 am
by ted
The solution turned out to be much simpler than I thought--unlike the grandiose scheme that I had envisioned.
First consider the two dimensional case.
Define a class O2d:
Code: Select all
begintemplate O2d
public o
objref o[1][1]
proc init() {
objref o[$1][$2]
}
endtemplate O2d
which I will presume is saved to the file o2d.hoc.
Here is a working example of how the O2d class might be used:
Code: Select all
// small is good for initial tests
XSIZE = 2
YSIZE = 2
VECSIZE = 2
load_file("o2d.hoc")
objref ab
ab = new O2d(XSIZE, YSIZE)
for i = 0, XSIZE-1 {
for j = 0, YSIZE-1 {
ab.o[i][j] = new Vector(VECSIZE, i*10+j)
print i, j
ab.o[i][j].printf
print " "
}
}
print " "
print "//////// array of objrefs complete ////////"
print " "
print "//////// testing proc showvecs() ////////"
print " "
proc showvecs() { local ii, jj
for ii = 0, XSIZE-1 {
for jj = 0, YSIZE-1 {
print ii, jj
$o1.o[ii][jj].printf
print " "
}
}
}
showvecs(ab)
It is easy to extend this approach to 3 dimensional arrays of Vectors--or of any object type, for that matter.
The following caveats may be regarded as strengths or weaknesses, depending on one's perspective.
1. This particular approach requires a new class definition for each number of dimensions.
2. It does not compel one to initialize any of the objrefs to point to an instance of anything in particular, or anything at all.
3. Given two instances of N-dimensional class ONd (where N is a positive integer) there is no guarantee that they have the same number of elements along any of their dimensions.
Re: pass Vector as argument to a function
Posted: Tue Mar 10, 2009 10:05 pm
by pfortier
This is nice.
Thanks,
Pierre