-
Notifications
You must be signed in to change notification settings - Fork 0
/
decvec.hoc
1752 lines (1626 loc) · 52.8 KB
/
decvec.hoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: decvec.hoc,v 1.426 2011/12/21 17:12:04 billl Exp $
proc decvec() {}
//* Declarations
objref ind, tvec, vec, vec0, vec1, tmpvec, vrtmp, veclist, veccollect, pwm
objref tmpobj, XO, YO, rdm, dir
strdef filename,dblform,tabform,xtmp
{dblform="%.4g" tabform=" "}
dir = new List()
tmpfile = new File()
if (! name_declared("datestr")) load_file("setup.hoc")
load_file("declist.hoc") // declare list iterators
print "Loading decvec"
fnum=verbose=0
{symnum = 7 colnum = 10}
func cg () { return $1%(colnum-1)+1 } // skip white color
objref clrsym[colnum+1]
for ii=0,colnum { clrsym[ii]=new Union() clrsym[ii].x=ii%colnum }
// black->red->blue->green->orange->brown->violet->yellow->grey
{clrsym[0].s="white" clrsym[1].s="black" clrsym[2].s="red" clrsym[3].s="blue"
clrsym[4].s="green" clrsym[5].s="orange" clrsym[6].s="brown" clrsym[7].s="violet"
clrsym[8].s="yellow" clrsym[9].s="grey"}
clrsym[0].o=new List()
{symmax=20 symmin=2}
obfunc sg () { local ii localobj o
ii=$1
o=clrsym[ii%(colnum-1)+1]
o.x=ii%(colnum-1)+1
o.t=clrsym[0].o.o(ii%symnum).s
o.x[1]=(3-ii)%4+1 // descending line types
o.x[2]=(symmax-symmin-2*ii)%(symmax-symmin+1)+symmin
o.x[3]=(4-ii)%(symmax-symmin+1)+symmin
o.x[4]=(int((ii+1)/5)%2+1)*4-(ii+1)%4+1 // another line type sequence
return o
}
{ MSONUM=100 MSOSIZ=100 msomax=0 msoptr=0 objref mso[MSONUM] }
double x[4],y[4]
xx=0 // declare a scalar
ind = new Vector(100)
tvec = new Vector(100)
vec = new Vector(100)
vec0 = new Vector(10)
vec1 = new Vector(10)
vrtmp = new Vector(10)
veclist = new List()
veccollect = new List()
rdm = new Random()
rdm.MCellRan4()
if (!(xwindows && name_declared("xwindows"))) {
xwindows=0
objref graphItem
strdef temp_string_, temp_string2_
}
//* stuff that doesn't belong here
//** dired([list,]file) - put together list of files matching 'file', calls 'ls -1 file'
// dired([list,]file,1) file name to read for list of files
// dired([list,]file,2) clear dir first; if list isn't present assume 'dir'
// dired([list,]file,3) do recursive search
func dired () { local f,i,f1 localobj st,o
f1=f=0
st=new String2()
if (numarg()==0) { print "dired([list,]filename[,flag])\t\
adds the filename to list (use wildcards) (flag:1 read file;flag:2 clear list)"
return 0 }
if (argtype(1)==2) {o=dir st.s=$s1 i=2} else {o=$o1 st.s=$s2 i=3}
while (i<=numarg()) {
if (argtype(i)==2) st.t=$si else f=$i
i+=1
}
if (f==2 || f==3) o.remove_all
if (f==1) {
tmpfile.ropen(st.s)
} else { // f!=1
rmxtmp()
if (f==3) {
printf("Search starting in ") system("pwd")
sprint(st.t,"sh -fc \"find . -name %s > %s\"",st.s,xtmp)
} else {
if (strm(st.s,"[*?]")) f1=0 else f1=1 // is this a wildcard or a directory
if (f1) {
if (sfunc.len(st.t)>0) {
sprint(st.t,"find %s -name %s >> %s",st.s,st.t,xtmp)
} else sprint(st.t,"find %s >> %s",st.s,xtmp)
} else sprint(st.t,"ls -1R %s >> %s",st.s,xtmp)
}
system(st.t)
tmpfile.ropen(xtmp)
}
while (tmpfile.scanstr(st.t) != -1) {
if (f1) { // a directory
if ((x=ftype(st.t))!=2) {print "Ignoring ",st.t,x continue}
}
o.append(new String(st.t))
tmpfile.gets(st.t) // get rid of the rest of the line
}
printf("%d files in dir\n",o.count)
return o.count
}
// lsdir([dir])
proc lsdir () {
if (numarg()==1) {
for ltr($o1) {sprint(tstr,"ls -l %s",XO.s) system(tstr)}
} else for ltr(dir) {sprint(tstr,"ls -l %s",XO.s) system(tstr)}
}
//** lbrw(list,action) is used to put up a browser
// note action given without '()'
proc lbrw () {
$o1.browser($s2,"s")
sprint($s2,"%s()",$s2)
$o1.accept_action($s2)
}
//** l2v(S1,S2) makes a list(S1) and puts all the XO.S2 into vec
// eg l2v("IClamp","amp")
proc l2v () {
tmpobj=new List($s1)
if (numarg()==3) YO=$o3 else YO=vec
YO.resize(tmpobj.count) YO.resize(0)
for ltr(tmpobj) {
sprint(tstr,"YO.append(%s.%s)",XO,$s2)
execute(tstr)
}
}
//* vector iterator vtr
// for vtr(vec) { print x }
// for vtr(&x, vec) { print x }
// for vtr(&x, vec, &y) { print x,y }
// for vtr(&x, vec, max_ind) { print x }
// for vtr(&x, vec, min_ind, max_ind) { print x }
// for vtr(&x, vec, &y, min_ind, max_ind) { print x,y }
iterator vtr () { local i,j,pf,cf,b,e localobj o
cf=pf=0
if (argtype(1)==1) { // 1st arg a vector or the pointer
o=$o1 i=2
} else if (argtype(1)==3) {
pf=1 o=$o2 i=3 // pointer alway in position 1
}
b=0 e=o.size-1 // default: do whole vec
// now can take counter or limits
if (argtype(i)==3) {cf=i i+=1} // cf gives counter location
if (argtype(i)==0) {
if (argtype(i+1)==0) {b=$i i+=1 e=$i} else e=$i
}
if (!cf) i1=0 else {i=cf $&i=0} // default counter
for j=b,e {
if (pf) $&1=o.x[j] else x=o.x[j]
iterator_statement
if (cf) $&i+=1 else i1+=1
}
}
//* vector iterator vtr2, treat two vectors as pairs
// usage 'for vtr2(&x, &y, vec1, vec2) { print x,y }'
iterator vtr2 () { local i,pairwise,noi1
noi1=pairwise=0
if (numarg()==3) { pairwise=1 i1=0 }
if (numarg()==4) if (argtype(4)==3) { pairwise=1 $&4=0 noi1=1}
if (pairwise) if ($o3.size%2!=0) { print "vtr2 ERROR: vec not even sized." return }
if (! pairwise) {
if ($o3.size != $o4.size) { print "vtr2 ERROR: sizes differ." return }
if (numarg()==5) {$&5=0 noi1=1} else {i1 = 0}
}
for i = 0,$o3.size()-1 {
$&1 = $o3.x[i]
if (pairwise) $&2=$o3.x[i+=1] else $&2=$o4.x[i]
iterator_statement
if (noi1) { if (pairwise) $&4+=1 else $&5+=1 } else i1+=1
}
}
//** viconv(TARG,OLD_INDS,NEW_INDS)
proc viconv () { local a,b
if (numarg()==0) { printf("viconv(TARG,OLD_INDS,NEW_INDS)\n") return }
a=b=allocvecs(2) b+=1
if ($o2.size!=$o3.size) {printf("OLD_INDS %d != NEW_INDS %d\n",$o2.size,$o3.size) return}
mso[b].resize($o1.size)
for vtr2(&x,&y,$o2,$o3) { // x -> y
mso[a].indvwhere($o1,"==",x)
mso[b].indset(mso[a],y)
}
$o1.copy(mso[b])
dealloc(a)
}
//* iterator lvtr, step through a list and a vector together
// usage 'for lvtr(XO, &x, list, vec) { print XO,x }'
iterator lvtr () { local i
if ($o3.count < $o4.size) { printf("lvtr ERROR: vecsize > listsize: list %d,vec %d.\n",$o3.count,$o4.size) return }
if ($o3.count != $o4.size) { printf("lvtr WARNING: sizes differ: list %d,vec %d.\n",$o3.count,$o4.size) }
if (numarg()==5) {$&5=0} else {i1 = 0}
for i = 0, $o4.size()-1 {
$o1 = $o3.object(i)
$&2 = $o4.x[i]
iterator_statement
if (numarg()==5) { $&5+=1 } else { i1+=1 }
}
}
//* other iterators: case, scase, ocase
iterator case () { local i,j,max,flag
if (argtype(numarg())==3) {flag=1 max=numarg()-1} else {flag=0 max=numarg()}
if (flag) {i=max+1 $&i=0} else i1 = 0
for i = 2, max {
$&1 = $i
iterator_statement
if (flag) {j=i i=max+1 $&i+=1 i=j} else i1+=1
}
}
iterator scase () { local i,j,min,max,flag
if (argtype(numarg())==3) {flag=1 max=numarg()-1} else {flag=0 max=numarg()}
if (flag) {i=max+1 $&i=0} else i1=0
if (argtype(1)==1) {
if (! isobj($o1,"String")) $o1=new String() // will accept String or String2
min=2
} else min=1
for i = min,max {
if (min==1) temp_string_=$si else $o1.s=$si
iterator_statement
if (flag) {j=i i=max+1 $&i+=1 i=j} else i1+=1
}
}
// eg for scase2("a","b","c","d","e","f") print tmpobj.s,tmpobj.t
iterator scase2 () { local i,min,flag,na,newstr localobj o
flag=i1=0 newstr=min=1
na=numarg()
if (argtype(na)==0) {i=na newstr=$i na-=1}
if (argtype(1)==1) {flag=1 min=2}
for i=min,na {
if (i==min || newstr) o=new String2()
o.s=$si i+=1 o.t=$si
if (flag) $o1=o else tmpobj=o
iterator_statement
i1+=1
}
}
iterator ocase () { local i
i1 = 0
if (isassigned($o1)) {
for i = 1, numarg() {
XO = $oi
iterator_statement
i1+=1
}
XO=nil
} else {
for i = 2, numarg() {
$o1 = $oi
iterator_statement
i1+=1
}
}
XO=nil
}
//* strm(STR,REGEXP) == regexp string match
func strm () { return sfunc.head($s1,$s2,"")!=-1 }
func strc () { return strcmp($s1,$s2)==0 }
//** count_substr(str,sub): count occurences of substring in str
func count_substr () { local cnt localobj st
st=new String($s1)
cnt = 0
while (sfunc.tail(st.s,$s2,st.s) != -1) { cnt += 1}
return cnt
}
//* nind(targ,data,ind) fill the target vector with data NOT index by ind (opposite of v.index)
proc nind () {
if (! eqobj($o1,$o2)) $o1.copy($o2)
$o1.indset($o3,-1e20)
$o1.where($o1,">",-1e20)
}
//* vlk(vec)
// vlk(vec,max)
// vlk(vec,min,max)
// vlk(vec,min,max,"INDEX") -- give index of each entry
// prints out a segment of a vector
vlk_width=80
proc vlkomitoff () { execute1("func vlkomit () {return NOP}") }
proc vlkomit0(){execute1("func vlkomit(){if(vcnt($o1,0)==$o1.size) return 1 else return 0}")}
vlkomitoff()
proc vlk () { local ii,i,j,ami,min,max,wdh,nl,na,width,tablen,omfl,ixfl localobj st,v1,vi
st=new String2() v1=new Vector(numarg()) vi=new Vector()
nl=1 wdh=vlk_width j=0 omfl=ixfl=0 ami=2
na=numarg() min=0 max=$o1.size-1
if (vlkomit(v1)!=NOP) omfl=1 // omfl to omit printing some
if (argtype(na)==2) {i=na
if (strm($si,"I")) {ixfl=1 vrsz($o1,vi) vi.indgen ami=1}
na-=1
}
if (argtype(na)==0) {i=na
if ($i<0) min=$i else max=$i
na-=1
}
if (argtype(na)==0) {i=na min=$i na-=1}
if (max<0) max+=$o1.size
if (min<0) min+=$o1.size
if (max>$o1.size-1) { max=$o1.size-1 printf("vlk: max beyond $o1 size\n") }
sprint(st.t,"%%s:%s",dblform)
width=0
if (strm(tabform,"\t")) tablen=6 else if (strm(tabform,"\n")) tablen=-1e5 else {
tablen=sfunc.len(tabform) }
for ii=min,max {
if (omfl) { v1.resize(0)
for i=1,na v1.append($oi.x[ii])
if (vlkomit(v1)) continue
}
if (ixfl) sprint(st.s,dblform,vi.x[ii]) else sprint(st.s,dblform,$o1.x[ii])
for i=ami,na sprint(st.s,st.t,st.s,$oi.x[ii])
width+=(sfunc.len(st.s)+tablen)
if (width>vlk_width && nl) {printf("\n") width=0}
printf("%s%s",st.s,tabform)
}
if (nl) print ""
}
// vpr2(v1,v2) to print out 2 vecs in parallel
proc vpr2 () { local i,fl2,max,min,newline localobj v1,v2
newline=80
v1=$o1 v2=$o2 min=0 max=v1.size if (v2.size!=max) {print "vpr2 diff szs" return}
for ({i=min fl2=0}; i<max; i+=1) {
if (v1.x[i]==BVBASE || v2.x[i]==BVBASE) {
if (!fl2) printf(" ")
fl2=1
} else {
fl2=0
printf("\n%5.1g %5.1g [%d]",v1.x[i],v2.x[i],i)
}
}
print ""
}
//** vlkp(SRC,PVEC) uses indices in PVEC to print out values in SRC
proc vlkp () { local i,j,wdh
j=0 nl=1 wdh=vlk_width
if (numarg()==2) {
for vtr(&x,$o1) {
printf("%g%s",$o2.x[x],tabform)
if ((j=j+1)%vlk_width==0 && nl && strcmp(tabform," ")==0) { print "" }
}
} else {
for vtr(&x,$o1) { for i=2,numarg() printf("%g%s",$oi.x[x],tabform)
print "" }
}
if (nl) print ""
}
//* vprf() prints 1,2 or 3 vectors in parallel to output file
proc vprf () { local x2
if (! tmpfile.isopen()) {
print "Writing to temp file 'temp'"
tmpfile.wopen("temp")
}
if (numarg()==1) {
for vtr(&x,$o1) { tmpfile.printf("%g\n",x) }
} else if (numarg()==2) {
for vtr2(&x,&y,$o1,$o2) { tmpfile.printf("%g %g\n",x,y) }
} else if (numarg()==3) {
for vtr2(&x,&y,$o1,$o2,&ii) { x2=$o3.x[ii] tmpfile.printf("%g %g %g\n",x,y,x2) }
}
tmpfile.close
}
//* vpr() prints 1,2 or 3 vectors in parallel to STDOUT
proc vpr () { local x2
if (numarg()==1) {
for vtr(&x,$o1) { printf("%g",x) }
} else if (numarg()==2) {
for vtr2(&x,&y,$o1,$o2) { printf("%g:%g ",x,y) }
} else if (numarg()==3) {
for vtr2(&x,&y,$o1,$o2,&ii) { x2=$o3.x[ii] printf("%g:%g:%g ",x,y,x2) }
}
print ""
}
//* readvec(vec) read from line
proc readvec () {
$o1.resize(0)
while (read(xx)) $o1.append(xx)
vlk($o1)
}
//* popvec(), savenums, readnums, vecsprint, savevec, savestr
// vrsz(), vcp(), zvec(), resize, copy, empty
proc pushvec () { local i // same as .append, retained for compatability
for i=2, numarg() $o1.append($i)
}
//** insvec(VEC,IND,VAL1[,VAL2,...]) insert values into the vector
proc insvec () { local ix,i,a // insert values into a vector
a=allocvecs(1) ix=$2
for i=3, numarg() mso[a].append($i)
$o1.insrt(ix,mso[a])
dealloc(a)
}
//** revec() clear vector then append
proc revec () { local i,x localobj o,st // clear vector then append
if (! isobj($o1,"Vector")) $o1=new Vector(100)
if (argtype(2)==2) {
o=$o1 o.resize(0)
st=new String2($s2)
if (strm(st.s,",")) { // use split
split(st.s,o)
} else if (strm(st.s," ")) {
split(st.s,o," ")
} else while (sfunc.len(st.s)>0) { // assume binary vector, could generalize for hex and base64
sscanf(st.s,"%1d",&x)
o.append(x)
sfunc.tail(st.s, ".", st.t)
st.s=st.t
}
} else for (i=1;i<=numarg();i+=1) {
ty=argtype(i)
if (ty==1) { o=$oi o.resize(0)
} else if (ty==0) { o.append($i)
} else if (ty==3) { o.append($&i) }
}
}
//** unvec(VEC,&a,&b,...) put values from vector back into doubles (via pointers)
proc unvec () { local i
if ($o1.size!=numarg()-1) { printf("unvec WARNING resizing %s to %d\n",$o1,numarg()-1)
$o1.resize(numarg()-1) }
for i=2,numarg() $&i=$o1.x[i-2]
}
//** wevec(VEC,wt0,wt1,...) returned weighted sum
func wevec () { local i,sum
if ($o1.size!=numarg()-1) { printf("wevec SIZE ERR %d %d\n",$o1.size,numarg()-1) return }
sum=0
for i=2,numarg() sum+=$o1.x[i-2]*$i
return sum
}
//** vrsz(VEC or NUM,VEC1,VEC2...,VECn or NUM) -- vector resize -- to size of first arg
// vrsz(VEC or NUM,VEC1,NUM,VEC2,etc) -- vector resize -- to size of first arg (vec or num)
// or prior NUM
// optional final number is fill
func vrsz () { local i,sz,max,fill,flag,rsz0
max=numarg()
flag=rsz0=0
if (argtype(1)==1) {
if (isobj($o1,"Vector")) sz=$o1.size else if (isobj($o1,"List")) sz=$o1.count
if (argtype(2)==0) {printf("vrsz(vec,num) backwards ERR\n") return -1}
} else sz=$1
if (argtype(max)==0) {i=max max-=1 fill=$i flag=1}
if (argtype(max)==2) {max-=1 rsz0=1} // string means resize(0)
if (sz<0) sz+=$o2.size // reduce size
if (sz<0) {printf("vrsz ERR: can't resize %s to %d\n",$o2,sz) return sz}
for i=2, max {
if (argtype(i)==0) sz=$i else {
$oi.resize(sz)
if (rsz0) $oi.resize(0) else if (flag) $oi.fill(fill)
}
}
return sz
}
//** vcp() -- copy vector segment with resizing
proc vcp () { local i,sz
$o1.resize($4-$3+1) $o1.copy($o2,$3,$4)
}
//** veccut(VEC,min,max) just keep a piece of the vector
// veccut(VEC,min,max,tstep) generate indices from times using tstep
proc veccut () { local a localobj v1
a=allocvecs(v1)
if (numarg()==4) { min=round($2/$4) max=round($3/$4)
} else { min=$2 max=$3 }
v1.copy($o1,min,max)
$o1.copy(v1)
dealloc(a)
}
//** zvec()
proc zvec () { local i // make vectors zero size
for i=1, numarg() $oi.resize(0)
}
//* save and read series
//** savenums(x[,y,...]) save numbers to tmpfile via a vector
proc savenums () { local i,vv
vv=allocvecs(1)
if (argtype(1)==3) {
mso[vv].from_double($2,&$&1)
} else for i=1, numarg() mso[vv].append($i)
mso[vv].vwrite(tmpfile)
dealloc(vv)
}
//** readnums(&x[,&y...]) recover nums from tmpfile via a vector
func readnums () { local a,i,cnt localobj v1
if (tmpfile.eof()) return 0
a=allocvecs(v1)
v1.vread(tmpfile)
cnt=0
if (numarg()==1 && v1.size>1) {
cnt=v1.size
if (verbose) printf("readnums WARNING: reading %d vals into presumed double array\n",cnt)
v1.v2d(&$&1)
} else {
if (numarg()>v1.size && verbose) {
printf("readnums() WARNING: too many args %d>%d\n",numarg(),v1.size) }
for (i=1; i<=numarg() && i<=v1.size; i+=1) $&i = v1.x[i-1]
cnt=i-1
}
dealloc(a)
return cnt
}
//** wrvstr(str) save string to a file by converting to ascii
proc wrvstr () { local vv,i
vv=allocvecs(1)
str2v($s1,mso[vv])
mso[vv].vwrite(tmpfile,1)
dealloc(vv)
}
//** rdvstr(str) read string from a file via vread and conversion
func rdvstr () { local vv,i,flag
flag=1
vv=allocvecs(1)
if (mso[vv].vread(tmpfile)) {
if (numarg()==1) v2str(mso[vv],$s1) else v2str(mso[vv],tstr)
} else flag=0
dealloc(vv)
return flag
}
//** str2v()
proc str2v () { localobj lo
lo=new String()
$o2.resize(0)
lo.s=$s1
while (sfunc.len(lo.s)>0) {
sscanf(lo.s,"%c%*s",&x)
sfunc.right(lo.s,1)
$o2.append(x)
}
}
//** v2str() translates from vector to string
proc v2str () { local ii,x
$s2=""
round($o1)
for ii=0,$o1.size-1 { x=$o1.x[ii] sprint($s2,"%s%c",$s2,x) }
}
//* popvec() remove last entry
func popvec () { local sz, ret
sz = $o1.size-1
if (sz<0) return 1e9
ret = $o1.x[sz]
$o1.resize[sz]
return ret
}
//* chkvec (look at last entry)
func chkvec () { if ($o1.size>0) return $o1.x[$o1.size-1] else return -1e10 }
// vecsprint(strdef,vec)
proc vecsprint () { local ii
if ($o2.size>100) { return }
for ii=0,$o2.size-1 { sprint($s1,"%s %g ",$s1,$o2.x[ii]) }
}
// savevec([list,]vec1[,vec2,...]) add vector onto veclist or other list if given as 1st arg
// don't throw out vectors
func savevec () { local i,flag,beg localobj v1
if (numarg()==0) { savevec(hoc_obj_[0],hoc_obj_[1]) return }
if (isobj($o1,"List")) beg=2 else beg=1
for i=beg, numarg() {
if (veccollect.count>0) { // grab a vector from garbage collection
v1=veccollect.object(veccollect.count-1)
veccollect.remove(veccollect.count-1)
} else v1 = new Vector($oi.size)
v1.copy($oi)
v1.label($oi.label)
if (beg==2) $o1.append(v1) else veclist.append(v1)
}
if (beg==2) return $o1.count-1 else return veclist.count-1
}
// prveclist(filename[,list])
proc prveclist () { localobj xo
if (!batch_flag && tmpfile.ropen($s1)) {
printf("%s exists; save anyway? (y/n) ",$s1)
getstr(temp_string_) chop(temp_string_)
if (strcmp(temp_string_,"y")!=0) return
}
if (! tmpfile.wopen($s1)) { print "Can't open ",$s1 return }
if (numarg()==2) {
for ltr(xo,$o2) xo.vwrite(tmpfile)
} else {
for ltr(xo,veclist) xo.vwrite(tmpfile)
}
tmpfile.close()
}
// prvl2(filename[,list]) --- save using a more standard fwrite
proc prvl2 () { localobj xo,o
if (!batch_flag && tmpfile.ropen($s1)) {
printf("%s exists; save anyway? (y/n) ",$s1)
getstr(temp_string_) chop(temp_string_)
if (strcmp(temp_string_,"y")!=0) return
}
if (! tmpfile.wopen($s1)) { print "Can't open ",$s1 return }
if (numarg()==2) o=$o2 else o=veclist
for ltr(xo,o) {tmpfile.printf("%d\n",xo.size) xo.fwrite(tmpfile)}
tmpfile.close()
}
// rdveclist("FILENAME"[,list])
// rdveclist("FILENAME"[,NOERASE])
proc rdveclist () { local flag,a
flag=0
a=allocvecs(1)
if (numarg()==1) { flag=1 clrveclist() } else if (argtype(2)==1) $o2.remove_all else flag=1
if (! tmpfile.ropen($s1)) { print "Can't open ",$s1 return }
while (mso[a].vread(tmpfile)) {
if (flag) savevec(mso[a]) else savevec($o2,mso[a])
}
tmpfile.close()
tmpobj=veclist
dealloc(a)
}
// rdvecs("FILENAME",vec1,vec2,...)
proc rdvecs () { local i
if (! tmpfile.ropen($s1)) { print "Can't open ",$s1 return }
for i=2,numarg() {
if ($oi==nil) $oi=new Vector()
if ($oi.vread(tmpfile)==0) printf("WARNING nothing to read into %s\n",$oi)
}
tmpfile.close()
}
// svvecs("FILENAME",vec1,vec2,...)
proc svvecs () { local i
clrveclist()
for i=2,numarg() savevec($oi)
prveclist($s1)
clrveclist()
}
// vpad(vec,howmany,val[,right])
proc vpad () { local a
a=allocvecs(1)
mso[a].resize($2) mso[a].fill($3)
if (numarg()==4) $o1.append(mso[a]) else {
mso[a].append($o1) $o1.copy(mso[a]) }
dealloc(a)
}
// vtrunc(vec,howmany[,right])
proc vtrunc () { local a
if (numarg()==3) $o1.resize($o1.size-$2) else {
$o1.reverse $o1.resize($o1.size-$2) $o1.reverse
}
}
proc rdxy () { local a,flag
a = allocvecs(1)
revec(ind,vec)
if (numarg()>=1) tmpfile.ropen($s1) else tmpfile.ropen("aa")
if (numarg()>=2) flag=$2 else flag=2
mso[a].scanf(tmpfile)
if (flag==2) {
for vtr2(&x,&y,mso[a]) {ind.append(x) vec.append(y)}
} else {
ind.copy(mso[a])
}
print ind.size," points read"
dealloc(a)
}
// closest(vec,num) -- return ind for vec member closest to num
func closest () { local a,ret
a=allocvecs(1)
mso[a].copy($o1) mso[a].sub($2) mso[a].abs
ret=mso[a].min_ind
dealloc(a)
return ret
}
// memb(TEST#,#1,#2,...) -- true if the TEST# is in the list
func memb () { local na,i
for i=2,numarg() if ($1==$i) return 1
return 0
}
proc clrveclist () { localobj o,xo
if (numarg()==1) o=$o1 else o=veclist
for ltr(xo,o) { xo.resize(0) veccollect.append(xo) }
o.remove_all()
}
// savestr(str1...) add string obj onto tmplist
proc savestr () { local i
if (argtype(1)==1) for i=2, numarg() $o1.append(new String($si)) else {
for i=1, numarg() tmplist.append(new String($si))
}
}
// redund with v.count in vecst.mod
func vcount () { local val,sum
val=$2 sum=0
for vtr(&x,$o1) if (x==val) sum+=1
return sum
}
// tvecl(inlist[,outlist]) -- transpose veclist
obfunc tvecl () { local x,cnt,sz,err,ii,p localobj xo,il,ol
il=$o1
if (numarg()>1) ol=$o2
if (!isassigned(ol)) {ol=veclist clrveclist()} else ol.remove_all
err=0 cnt=il.count sz=il.o(0).size
for ltr(xo,il,&x) if (xo.size!=sz) err=x
if (err) { print "Wrong size vector is #",x return ol }
p = allocvecs(1,cnt) mso[p].resize(cnt)
for ii=0,sz-1 {
for jj=0,cnt-1 mso[p].x[jj] = il.o(jj).x[ii]
savevec(ol,mso[p])
}
dealloc(p)
return ol
}
//* vinsect(v1,v2,v3) -- v1 gets intersection (common members) of v2,v3
// replaced by v.insct() in vecst.mod
proc vinsect () {
$o1.resize(0)
for vtr(&x,$o2) for vtr(&y,$o3) if (x==y) $o1.append(x)
}
//* vecsplit(vec,vec1,vec2[,vec3,...])
// splits vec into other vecs given
proc vecsplit () { local num,ii,i
num = numarg()-1 // how many
for i=2,numarg() $oi.resize(0)
for (ii=0;ii<$o1.size;ii+=num) {
for i=2,numarg() if (ii+i-2<$o1.size) $oi.append($o1.x[ii+i-2])
}
}
//* vecsort(vec,vec1,vec2[,vec3,...])
// sorts n vecs including first vec by first one
proc vecsort () { local i,inv,scr,narg
narg=numarg()
if (narg<2 || narg>10) {print "Wrong #args in decvec.hoc:vecsort" return}
scr=inv=allocvecs(2) scr+=1
$o1.sortindex(mso[inv])
mso[scr].resize(mso[inv].size)
sprint(temp_string_,"%s.fewind(%s,%s,%s",mso[scr],mso[inv],$o1,$o2)
for i=3,narg sprint(temp_string_,"%s,%s",temp_string_,$oi)
sprint(temp_string_,"%s)",temp_string_)
execute(temp_string_)
dealloc(inv)
}
//** order(&x,&y,...) put values in order
proc order () { local a,i,na
na=numarg()
a=allocvecs(1)
for i=1,na mso[a].append($&i)
mso[a].sort
for i=1,na $&i=mso[a].x[i-1]
dealloc(a)
}
//** vdelind() -- delete a single index
proc vdelind () { local i,iin
iin = $2
if (iin<0) iin=$o1.size+iin
if (iin>$o1.size-1 || iin<0) {
printf("vdelind Error: index %d doesn't exist.\n",iin) return }
if (iin<$o1.size-1) $o1.copy($o1,iin,iin+1,$o1.size-1)
$o1.resize($o1.size-1)
}
//* mkveclist(num[,sz]) recreate veclist to have NUM vecs each of size SZ (or MSOSIZ)
proc mkveclist () { local ii,num,sz,diff localobj xo
num=$1
diff = num-veclist.count
if (numarg()==2) { sz=$2 } else { sz = MSOSIZ }
if (diff>0) {
for ii=0,diff-1 {
tmpvec = new Vector(sz)
veclist.append(tmpvec)
}
} else if (diff<0) {
for (ii=veclist.count-1;ii>=num;ii=ii-1) { veclist.remove(ii) }
}
for ltr(xo,veclist) { xo.resize(sz) }
}
//* allocvecs
// create temp set of vectors on mso
// returns starting point on mso
// eg p = allocvecs(3)
// p = allocvecs(v1,v2,v3) // where v1..v3 are localobj or objref
// p = allocvecs(v1,v2,v3,...,size) // set all to size
// p = allocvecs(num,list) // append num vecs to list
// p = allocvecs(num,size) // num vecs of size size
// p = allocvecs(num,size,list) // append num vecs of size to list
// p = allocvecs(num,list,size) // allow args to be given in reverse order
// access these vectors by mso[p+0] ... [p+2]
func allocvecs () { local i,ii,llen,sz,newv,aflg,lflg,na localobj o
if (numarg()==0) {
print "p=allocvecs(#) or p=allocvecs(v1,v2,...), access with mso[p], mso[p+1]..." return 0 }
sz=MSOSIZ na=numarg()
lflg=0
if (argtype(1)==0) {
aflg=0 newv=$1
if (na>=2) if (argtype(2)==0) sz=$2 else {lflg=1 o=$o2} // append to list in arg2
if (na>=3) if (argtype(3)==0) sz=$3 else {lflg=1 o=$o3}
if (lflg) o.remove_all
} else {
aflg=1
if (argtype(na)==0) {
i=na sz=$i newv=i-1
} else newv=na
}
llen = msoptr
for ii=msomax,msoptr+newv-1 { // may need new vectors
if (ii>=MSONUM) { print "alloc ERROR: MSONUM exceeded." return 0 }
mso[ii] = new Vector(sz)
}
for ii=0,newv-1 {
mso[msoptr].resize(sz)
mso[msoptr].resize(0)
msoptr = msoptr+1
}
if (msomax<msoptr) msomax = msoptr
if (aflg) for i=1,newv $oi=mso[i-1+llen]
if (lflg) for i=0,newv-1 o.append(mso[i+llen])
return llen
}
//** dealloc(start)
// remove temp set of vectors from mso
proc dealloc () { local ii,min
if (numarg()==0) { min = 0 } else { min = $1 }
msoptr = min
}
//* indvwhere family
//** vwh(VEC,VAL) returns index where VEC.x[i]==VAL
func vwh () {
if (argtype(2)==0) return $o1.indwhere("==",$2)
if (numarg()==3) return $o1.indwhere($s2,$3)
return $o1.indwhere($s2,$3,$4)
}
//** vval(VEC,STR,NUM) uses indwhere to return first value that qualifies
func vval () {
if (argtype(2)==0) return $o1.x[$o1.indwhere("==",$2)]
if (numarg()==3) return $o1.x[$o1.indwhere($s2,$3)]
return $o1.x[$o1.indwhere($s2,$3,$4)]
}
//** vcnt(VEC,STR,x[,y]) uses indvwhere and returns # of values that qualify
func vcnt () { local a,ret
a=allocvecs(1)
if (numarg()==2) mso[a].indvwhere($o1,"==",$2)
if (numarg()==3) mso[a].indvwhere($o1,$s2,$3)
if (numarg()==4) mso[a].indvwhere($o1,$s2,$3,$4)
ret = mso[a].size
// if ($o1.size>0) printf("%d/%d (%g)\n",ret,$o1.size,ret/$o1.size*100)
dealloc(a)
return ret
}
//** civw(DEST,SRC1,STR1,x1[,y1]...) does compound indvwhere
// overwrites tstr; DEST should be size 0 unless to be compounded
// civw(DEST,0,...) will resize DEST to 0
func civw () { local i,a,b,c,f2,x,y,sz,min
a=b=c=allocvecs(3) b+=1 c+=2
min=2
// if ($o1.size>0) print "Starting with previously set index vector"
if (argtype(2)==0) {
if ($2==0) { $o1.resize(0) min=3
if (argtype(3)==1) sz=$o3.size else {
printf("ERR0: arg 3 should be obj when $2==0\n",i) return -1 }
} else {
printf("ERR0a: arg 2 should be 0 if a number -- zero sizes ind vector\n")
return -1
}
} else if (argtype(2)==1) sz=$o2.size else {
printf("ERR0b: arg 2 should be obj\n",i) return -1 }
for (i=min;i<=numarg();) {
mso[c].copy($o1)
if (argtype(i)!=1) { printf("ERR1: arg %d should be obj\n",i) return -1}
if ($oi.size!=sz) { printf("ERR1a: all vecs should be size %d\n",sz) return -1}
mso[a].copy($oi) i+=1 // look in a
if (argtype(i)!=2) { printf("ERR2: arg %d should be str\n",i) return -1}
tstr=$si i+=1
if (strm(tstr,"[[(]")) f2=1 else f2=0 // opstring2 needs 2 args
if (argtype(i)!=0) { printf("ERR3: arg %d should be dbl\n",i) return -1}
x=$i i+=1
if (f2) {
if (argtype(i)!=0) { printf("ERR4: arg %d should be dbl\n",i) return -1}
y=$i i+=1
}
if (f2) mso[b].indvwhere(mso[a],tstr,x,y) else { // the engine
mso[b].indvwhere(mso[a],tstr,x) }
$o1.resize(sz) // make sure it's big enough for insct -- shouldn't need
if (mso[c].size>0) $o1.insct(mso[b],mso[c]) else $o1.copy(mso[b])
if ($o1.size==0) break
}
dealloc(a)
return $o1.size
}
//* vecconcat(vec1,vec2,...)
// vecconcat(vec1,list) puts concat all vecs from list
// destructive: concatenates all vecs onto vec1
// performs a list2vec() functionality
proc vecconcat () { local i,max localobj xo
max=numarg()
if (numarg()<2) {
print "vecconcat(v1,v2,...) puts all into v1" return
}
if (argtype(max)==0) {max-=1 $o1.resize(0)}
if (isobj($o2,"List")) {
$o1.resize(0)
for ltr(xo,$o2) $o1.append(xo)
} else for i=2,max $o1.append($oi)
}
//** vecelim(v1,v2) eliminates items in v1 given by index vec v2
proc vecelim () {
for vtr(&x,$o2) { $o1.x[x]= -1e20 }
$o1.where($o1,"!=",-1e20)
}
//** redundout(vec) eliminates sequential redundent entries
// destructive
func redundout () { local x,ii,p1
p1=allocvecs(1)
$o1.sort
mso[p1].resize($o1.size)
mso[p1].redundout($o1)
$o1.copy(mso[p1])
dealloc(p1)
return $o1.size
}
//** uniq(src,dest[,cnt]) uses redundout to return random values of a vector
// like redundout except nondestructive
obfunc uniq () { local a localobj v1,vret
a=allocvecs(v1)
v1.copy($o1) v1.sort
if (numarg()==3) { $o2.redundout(v1,0,$o3)
} else if (numarg()==2) { $o2.redundout(v1)
} else {
vret=new Vector(v1.size) vret.redundout(v1)
}
dealloc(a)
if (numarg()==1) return vret else return $o2
}
//** complement(ind,max) for indices -- return values from 0..max that are not in ind
proc complement () { local a,b,max
a=b=allocvecs(2) b+=1
max=$2
mso[a].indgen(0,max,1)
mso[b].resize(mso[a].size)
mso[b].cull(mso[a],$o1)
$o1.copy(mso[b])
dealloc(a)
}
//** albetname() generate sequential 3 char alphabetical file names (make filenames)
obfunc albetname () { local na,sret localobj st
na=numarg() sret=0
st=new String2()
if (na==0) { fnum+=1 st.t=".id"
} else if (na>=1) {
if (argtype(1)==2) {st.t=$s1 fnum+=1} else fnum=$1
}
if (na==2) st.t=$s2 // partially back compatible, doesn't handle albetname(tstr,".id")
if (na==3) {st.t=$s3 sret=1}
if (fnum>17575) printf("albetname WARN: out of names: %d > %d\n",fnum,26^3-1)
sprint(st.s,"%c%c%c", fnum/26/26%26+97,fnum/26%26+97,fnum%26+97)
if (sfunc.len(st.t)>0) sprint(st.s,"%s%s",st.s,st.t)
if (sret) $s2=st.s