Page 1 of 1

Non-uniform parameter with colon notation?

Posted: Wed Jul 31, 2024 10:31 am
by patoorio
Hello,

In HOC, I was used to write something like

Code: Select all

section diam(0:1) = 2.0:1.0
which would yield an interpolated diameter between 2 and 1 accross the section's segments.
But I am not able to find the way to do this in Python! Any similar syntax that I tried gives an invalid syntax error. I have looked a lot in the documentation and in the forums but I cannot get to this 'colon notation' equivalent.

I know I can do something like

Code: Select all

for seg in section:
    seg.diam = <some interpolating formula>
but I wish I was able to use the more compact and straightforward colon thing (so I don't have to figure out the correct interpolation expression!).
Is there a way to do that in Python?

Thank you in advance.

Re: Non-uniform parameter with colon notation?

Posted: Wed Jul 31, 2024 3:32 pm
by ted
I wish I was able to use the more compact and straightforward colon thing (so I don't have to figure out the correct interpolation expression!).
Is there a way to do that in Python?
Might be compact and straightforward to you, but to many it would seem obscure (there's no accounting for taste, is there?). But the problem isn't just esthetic--it's that most people won't understand or remember the colon syntax. Not to say that Python is immune from readability problems--that language has a ton of obscure notations and terminology, just ask any C/C++ or FORTRAN programmer. (ooh, did he say that?)

The only place one should see the colon notation for specifying position-dependent values of range variables is in legacy code. Noone should be writing new code that uses it, because both hoc and Python have better ways to specify variation of range variables along a section. The algorithm is

Code: Select all

for each segment in section foo
  set range variable to f(range of this segment)
For your particular case (linear variation along the length of a section), f would return

Code: Select all

  val0 * (1 - x) + val1 * x
where val0 and val1 are the assumed values of the range value at the section's 0 and 1 ends, and x is range of the center of the segment (i.e. normalized distance of the segment center from the section's 0 end). After rearrangement, this is

Code: Select all

val0 + (val1 - val0)*x
How to implement this in hoc? Given a section called foo,

Code: Select all

foo for (x,0) diam(x) = val0 + (val1 - val0) * x
The Python equivalent is
for seg in foo: seg.diam = val0 + (val1 - val0) * seg.x
where foo is the Python alias for a section that was created by a statement such as
foo = h.Section('foo')

If I wanted to do this in many sections, I'd probably factor out the linear function into an actual function.

Re: Non-uniform parameter with colon notation?

Posted: Wed Jul 31, 2024 3:46 pm
by patoorio
Thanks for you answer!
In summary, there is no equivalent to the 'colon notation' and its use should be discouraged in hoc as well. Message received and appreciated!

BTW, I was being a little ironic with the difficulty of figuring out the 'interpolation function', but your help is (and will always be) useful.