File: std1.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (1579 lines) | stat: -rw-r--r-- 45,323 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
/*
    STD1.C
    More Yorick built-in functions declared in std.i

    See std.i for documentation on the functions defined here.

    $Id: std1.c,v 1.1 1993/08/27 18:32:09 munro Exp $
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#include "ydata.h"
#include "defstr.h"

extern BuiltIn Y_indgen, Y_span, Y_digitize, Y_interp, Y_integ, Y_sort,
  Y_transpose, Y_grow, Y__, Y_strlen, Y_strpart, Y_strmatch, Y_strtok,
  Y_timestamp, Y_timer, Y_random, Y_random_seed, Y_merge, Y_histogram,
  Y_poly;

/*--------------------------------------------------------------------------*/

extern void PopToD(Symbol *s);
extern DataBlock *ForceToDB(Symbol *s);

/* Intended for use by the print() and grow() functions -- dangerous
   because it zeroes the contents of the source array to avoid
   having to deal with pointers.  */
extern Array *GrowArray(Array *array, long extra);  /* ydata.c */

static long hunt(double *x, long n, double xp, long ip);

/*--------------------------------------------------------------------------*/

void Y_indgen(int nArgs)
{
  long number, origin, stride, i;
  Array *array;
  Dimension *tmp;
  Operand op;
  if (nArgs != 1) YError("indgen takes exactly one argument");

  sp->ops->FormOperand(sp, &op);
  if (op.ops==&rangeOps) {
    Range *range= op.value;
    if (range->rf || range->nilFlags)
      YError("range function and/or nil range component in indgen");
    origin= range->min;
    stride= range->inc;
    if (stride>0) number= (range->max-origin)/stride;
    else number= (origin-range->max)/(-stride);
    number++;  /* number of footprints, not number of strides */
  } else if (op.ops->promoteID<=T_LONG && !op.type.dims) {
    op.ops->ToLong(&op);
    number= *(long *)op.value;
    origin= 1L;
    stride= 1;
  } else {
    YError("indgen argument must be range or scalar integer");
    return;
  }

  if (number>0) {
    tmp= tmpDims;
    tmpDims= 0;
    FreeDimension(tmp);
    tmpDims= NewDimension(number, 1L, (Dimension *)0);
    array= PushDataBlock(NewArray(&longStruct, tmpDims));

    for (i=0 ; i<number ; i++) {
      array->value.l[i]= origin;
      origin+= stride;
    }

  } else {
    /* indgen(0) returns default origin */
    PushLongValue(1L);
  }
}

void Y_span(int nArgs)
{
  long which, nDims, number, nfast, i, j, k, kx;
  double rnumber, dp, *p0, *p1, *p;
  Array *array;
  Dimension *tmp;
  Operand op0, op1;
  if (nArgs!=3 && nArgs!=4)
    YError("span takes exactly three or four arguments");

  if (nArgs==4) { which= YGetInteger(sp)-1; Drop(1); }
  else which= 0;  /* use 0-origin which here */

  number= YGetInteger(sp);
  if (number<1) YError("3rd argument to span function must be >0");
  Drop(1);

  sp->ops->FormOperand(sp, &op1);
  (sp-1)->ops->FormOperand(sp-1, &op0);
  op1.ops->ToDouble(&op1);
  op0.ops->ToDouble(&op0);
  if (BinaryConform(&op1, &op0) & 4)
    YError("start and stop not conformable in span function");

  tmp= tmpDims;
  tmpDims= 0;
  FreeDimension(tmp);

  /* compute dimensions of result -- nfast-by-number-by-(slow),
     where nfast are the first which indices of op0 (or op1),
     and (slow) are the rest */
  nDims= CountDims(op0.type.dims);
  if (which<0) which= nDims+1+which; /* handle which<0 like array index<0 */
  if (which>8 || nDims-which>8)
    YError("the 4th argument to span is unreasonably large");
  while (which<0) {
    tmpDims= NewDimension(1L, 1L, tmpDims);
    which++;
  }
  if (which>=nDims) {
    /* can just tack new element of dimension list onto old */
    tmpDims= Ref(op0.type.dims);
    while (which>nDims) {
      tmpDims= NewDimension(1L, 1L, tmpDims);
      which--;
    }
    tmpDims= NewDimension(number, 1L, tmpDims);
    nfast= op0.type.number;
  } else {
    /* make a fresh copy of the index list, then insert new element */
    Dimension *prev;

    which= nDims-which;  /* guaranteed this is >0 */
    tmpDims= tmp= CopyDims(op0.type.dims, tmpDims, 1);
    do {
      prev= tmp;
      tmp= tmp->next;
    } while (--which);
    prev->next= NewDimension(number, 1L, tmp);
    nfast= TotalNumber(tmp);
  }

  /* create result array and fill it */
  array= PushDataBlock(NewArray(&doubleStruct, tmpDims));
  p= array->value.d;
  p0= op0.value;
  p1= op1.value;
  rnumber= 1.0/(double)(number-1);
  kx= array->type.number - nfast;
  for (i=0 ; i<op0.type.number ; i+=nfast) {
    for (j=0 ; j<nfast ; j++) {
      dp= (p1[i+j]-p0[i+j])*rnumber;
      k= j+number*i;
      p[k]= p0[i+j];
      for (k+=nfast ; k<kx ; k+=nfast) p[k]= p[k-nfast]+dp;
      p[k]= p1[i+j];  /* do after loop to assure exact equality */
    }
  }
}

static long hunt(double *x, long n, double xp, long ip)
{
  /* Based on the hunt routine given in Numerical Recipes (Press, et al.,
     Cambridge University Press, 1988), section 3.4.

     Here, x[n] is a monotone array and, if xp lies in the interval
     from x[0] to x[n-1], then
       x[h-1] <= xp < x[h]  (h is the value returned by hunt), or
       x[h-1] >= xp > x[h], as x is ascending or descending
     The value 0 or n will be returned if xp lies outside the interval.
   */
  int ascend= x[n-1]>x[0];
  long jl, ju;

  if (ip<1 || ip>n-1) {
    /* caller has declined to make an initial guess, so fall back to
       garden variety bisection method */
    if ((xp>=x[n-1]) == ascend) return n;
    if ((xp<x[0]) == ascend) return 0;
    jl= 0;
    ju= n-1;

  } else {
    /* search from initial guess ip in ever increasing steps to bracket xp */
    int inc= 1;
    jl= ip;
    if ((xp>=x[ip]) == ascend) { /* search toward larger index values */
      if (ip==n-1) return n;
      jl= ip;
      ju= ip+inc;
      while ((xp>=x[ju]) == ascend) {
	jl= ju;
	inc+= inc;
	ju+= inc;
	if (ju>=n) {
	  if ((xp>=x[n-1]) == ascend) return n;
	  ju= n;
	  break;
	}
      }
    } else {                     /* search toward smaller index values */
      if (ip==0) return 0;
      ju= ip;
      jl= ip-inc;
      while ((xp<x[jl]) == ascend) {
	ju= jl;
	inc+= inc;
	jl-= inc;
	if (jl<0) {
	  if ((xp<x[0]) == ascend) return 0;
	  jl= 0;
	  break;
	}
      }
    }
  }

  /* have x[jl]<=xp<x[ju] for ascend, x[jl]>=xp>x[ju] for !ascend */
  while (ju-jl > 1) {
    ip= (jl+ju)>>1;
    if ((xp>=x[ip]) == ascend) jl= ip;
    else ju= ip;
  }

  return ju;
}

void Y_digitize(int nArgs)
{
  long number, origin, nbins, i, ip;
  double *x, *bins;
  Dimension *dimsx, *dimsb;
  long *ibin;
  if (nArgs!=2) YError("digitize takes exactly two arguments");

  bins= YGet_D(sp, 0, &dimsb);
  x= YGet_D(sp-1, 0, &dimsx);

  if (!dimsb || dimsb->number<2 || dimsb->next)
    YError("2nd argument to digitize must be 1D with >=2 elements");
  nbins= dimsb->number;
  origin= dimsb->origin;
  number= TotalNumber(dimsx);

  if (dimsx) {
    Array *array= PushDataBlock(NewArray(&longStruct, dimsx));
    ibin= array->value.l;
  } else {
    PushLongValue(0L);
    ibin= &sp->value.l;
  }
  ip= 0;
  for (i=0 ; i<number ; i++)
    ibin[i]= ip= origin+hunt(bins, nbins, x[i], ip);
}

void Y_interp(int nArgs)
{
  long which, nDims, number, nfast;
  long i, j, k, l, m, js, ms, ip, ipy;
  double *y, *x, *xp, *yp, c0, c1;
  Array *array;
  Dimension *tmp;
  Operand opy, opx, opxp;
  if (nArgs!=3 && nArgs!=4)
    YError("interp takes exactly three or four arguments");

  if (nArgs==4) { which= YGetInteger(sp)-1; Drop(1); }
  else which= 0;  /* use 0-origin which here */

  sp->ops->FormOperand(sp, &opxp);
  (sp-1)->ops->FormOperand(sp-1, &opx);
  (sp-2)->ops->FormOperand(sp-2, &opy);
  opxp.ops->ToDouble(&opxp);
  opx.ops->ToDouble(&opx);
  opy.ops->ToDouble(&opy);

  tmp= tmpDims;
  tmpDims= 0;
  FreeDimension(tmp);

  /* compute dimensions of y array -- nfast-by-number-by-(slow), where
     nfast are the first which dimensions of y, number is the length of
     the dimension to interpolate on, and (slow) are the rest */
  nDims= CountDims(opy.type.dims);
  if (which<0) which= nDims+which; /* handle which<0 like array index<0 */
  if (which>=nDims || which<0) YError("bad 4th argument to interp");
  i= nDims-1-which;
  tmp= opy.type.dims;
  while (i) { tmp= tmp->next; i--; }
  number= tmp? tmp->number : 1;
  if (number<2) YError("bad dimension (length 1) in interp");
  nfast= TotalNumber(tmp->next);
  if (opx.type.number!=number || opx.type.dims->next)
    YError("dimension of x does not match target dimension of y in interp");

  if (which==nDims-1) {
    /* can just tack new element(s) of dimension list onto old */
    tmpDims= Ref(opy.type.dims->next);
    tmpDims= CopyDims(opxp.type.dims, tmpDims, 1);
  } else {
    /* make a fresh copy of the index list and find where to insert new */
    Dimension *prev, *tmpprev;
    i= nDims-which;   /* guaranteed >1 AND opy is at least 2D */
    tmpDims= prev= CopyDims(opy.type.dims, tmpDims, 1);
    tmp= prev->next;  i--;
    tmp= tmp->next;  i--;   /* prev is two behind tmp */
    while (i) { prev= prev->next; tmp= tmp->next; i--; }
    tmpprev= prev->next;
    prev->next= 0;     /* cut off tail of dimension list */
    tmpprev->next= 0;  /* this pointed to tmp */
    FreeDimension(tmpprev);
    prev->next= CopyDims(opxp.type.dims, tmp, 1);
  }

  /* create result array */
  array= PushDataBlock(NewArray(&doubleStruct, tmpDims));
  yp= array->value.d;
  y= opy.value;
  x= opx.value;
  xp= opxp.value;

  /* The problem is as follows:
     For each value of the faster indices, and each value of the slower
     indices of y, and for each value of xp, find the value of yp
     corresponding to xp.  Note that for each interpolation, the y
     vector has stride nfast, while the x vector always has stride 0.
     Note also that ALL of the yp for a given xp should be computed
     once xp is found.  */
  js= nfast*number;
  ms= nfast*opxp.type.number;
  c0= c1= 0.0;
  ip= 0;  /* hunt takes this as no guess on 1st pass */
  for (i=l=0 ; i<opxp.type.number ; i++, l+=nfast) {
    ip= hunt(x, number, xp[i], ip);
    ipy= ip*nfast;
    if (ip>=1 && ip<number) {
      c0= (x[ip]-xp[i])/(x[ip]-x[ip-1]);
      c1= 1.0-c0;
    }
    for (j=m=0 ; j<opy.type.number ; j+=js, m+=ms) {
      for (k=0 ; k<nfast ; k++) {
	if (ip<1) {               /* point below minimum */
	  yp[k+l+m]= y[k+j];
	} else if (ip<number) {   /* point in range */
	  yp[k+l+m]= c0*y[k+(ipy-nfast)+j]+c1*y[k+ipy+j];
	} else {                  /* point above maximum */
	  yp[k+l+m]= y[k+(ipy-nfast)+j];
	}
      }
    }
  }

  PopToD(sp-4);
  Drop(3);
}

void Y_integ(int nArgs)
{
  long which, nDims, number, nfast;
  long i, j, k, l, m, js, ms, ip, ipy;
  double *y, *x, *xp, *yi, *psum, c0, c1, dx;
  Array *array;
  Dimension *tmp;
  Operand opy, opx, opxp;
  if (nArgs!=3 && nArgs!=4)
    YError("integ takes exactly three or four arguments");

  if (nArgs==4) {
    which= YGetInteger(sp)-1;  /* use 0-origin which here */
    Drop(1);
  } else {
    which= 0;  /* use 0-origin which here */
    ClearTmpArray();  /* need a temporary for this calculation */
  }

  sp->ops->FormOperand(sp, &opxp);
  (sp-1)->ops->FormOperand(sp-1, &opx);
  (sp-2)->ops->FormOperand(sp-2, &opy);
  opxp.ops->ToDouble(&opxp);
  opx.ops->ToDouble(&opx);
  opy.ops->ToDouble(&opy);

  tmp= tmpDims;
  tmpDims= 0;
  FreeDimension(tmp);

  /* compute dimensions of y array -- nfast-by-number-by-(slow), where
     nfast are the first which dimensions of y, number is the length of
     the dimension to interpolate on, and (slow) are the rest */
  nDims= CountDims(opy.type.dims);
  if (which<0) which= nDims+which; /* handle which<0 like array index<0 */
  if (which>=nDims || which<0) YError("bad 4th argument to integ");
  i= nDims-1-which;
  tmp= opy.type.dims;
  while (i) { tmp= tmp->next; i--; }
  number= tmp? tmp->number : 1;
  if (number<2) YError("bad dimension (length 1) in integ");
  nfast= TotalNumber(tmp->next);
  if (opx.type.number!=number || opx.type.dims->next)
    YError("dimension of x does not match target dimension of y in integ");

  if (which==nDims-1) {
    /* can just tack new element(s) of dimension list onto old */
    tmpDims= Ref(opy.type.dims->next);
    tmpDims= CopyDims(opxp.type.dims, tmpDims, 1);
  } else {
    /* make a fresh copy of the index list and find where to insert new */
    Dimension *prev, *tmpprev;
    i= nDims-which;   /* guaranteed >1 AND opy is at least 2D */
    tmpDims= prev= CopyDims(opy.type.dims, tmpDims, 1);
    tmp= prev->next;  i--;
    tmp= tmp->next;  i--;   /* prev is two behind tmp */
    while (i) { prev= prev->next; tmp= tmp->next; i--; }
    tmpprev= prev->next;
    prev->next= 0;     /* cut off tail of dimension list */
    tmpprev->next= 0;  /* this pointed to tmp */
    FreeDimension(tmpprev);
    prev->next= CopyDims(opxp.type.dims, tmp, 1);
  }

  /* create partial sum array (integrals up to each xp) */
  array= NewTmpArray(&doubleStruct, opy.type.dims);
  psum= array->value.d;

  /* create result array */
  array= PushDataBlock(NewArray(&doubleStruct, tmpDims));
  yi= array->value.d;
  y= opy.value;
  x= opx.value;
  xp= opxp.value;

  /* The problem is as follows (same as interp):
     For each value of the faster indices, and each value of the slower
     indices of y, and for each value of xp, find the value of yi
     corresponding to xp.  Note that for each interpolation, the y
     vector has stride nfast, while the x vector always has stride 0.
     Note also that ALL of the yi for a given xp should be computed
     once xp is found.  */

  /* first compute partial sums */
  js= nfast*number;
  for (j=0 ; j<opy.type.number ; j+=js) {
    for (k=0 ; k<nfast ; k++) {
      psum[k+j]= 0.0;
      for (i=1, l=nfast ; i<number ; i++, l+=nfast)
	psum[k+l+j]= psum[k+l-nfast+j]+
	  0.5*(y[k+l-nfast+j]+y[k+l+j])*(x[i]-x[i-1]);
    }
  }

  /* use that to compute interpolated integrals */
  ms= nfast*opxp.type.number;
  c0= c1= 0.0;
  ip= 0;  /* hunt takes this as no guess on 1st pass */
  for (i=l=0 ; i<opxp.type.number ; i++, l+=nfast) {
    ip= hunt(x, number, xp[i], ip);
    ipy= ip*nfast;
    if (ip>=1 && ip<number) {
      dx= xp[i]-x[ip-1];
      c1= 0.5*dx*dx/(x[ip]-x[ip-1]);
      c0= dx-c1;
    }
    for (j=m=0 ; j<opy.type.number ; j+=js, m+=ms) {
      for (k=0 ; k<nfast ; k++) {
	if (ip<1) {               /* point below minimum */
	  yi[k+l+m]= 0.0;
	} else if (ip<number) {   /* point in range */
	  yi[k+l+m]= c0*y[k+(ipy-nfast)+j]+c1*y[k+ipy+j]
	    +psum[k+(ipy-nfast)+j];
	} else {                  /* point above maximum */
	  yi[k+l+m]= psum[k+(ipy-nfast)+j];
	}
      }
    }
  }

  ClearTmpArray();
  PopToD(sp-4);
  Drop(3);
}

/*--------------------------------------------------------------------------*/

/* The sorting algorithm implemented here is a hybrid of the algorithms
   in section 4.10 of 2nd ed Kernighan and Ritchie and section 8.4 of
   Numerical Recipes in C (Press et. al.).
   The C library qsort routine does not allow for strides in an obvious
   way, which is why Yorick needs these sorting routines.  */

static long sortSize, sortStride, sortLimit;
static long *longData;
static double *doubleData;
static char **stringData;

static long Random(long range);
static void ysortQ(long *list, long n);
static void ysortD(long *list, long n);
static void ysortL(long *list, long n);

static long sortSeed= 0;     /* for linear congruential random number
				between 0 and 7874 inclusive,
				(sortSeed*211+1663)%7875 next time */

static long Random(long range)
{
  sortSeed= (sortSeed*211+1663) % 7875;
  return (range*sortSeed)/7875;
}

static void ysortQ(long *list, long n)
{
  long j;
  if (n < sortLimit) {
    /* straight insertion fastest for short sorts */
    long i, listel;
    char *partition;
    for (i=sortStride ; i<n ; i+=sortStride) {
      listel= list[i];
      partition= stringData[listel];
      for (j=i-sortStride ;
	   j>=0 && strcmp(stringData[list[j]], partition)>0 ;
	   j-=sortStride) list[j+sortStride]= list[j];
      list[j+sortStride]= listel;
    }
    return;  /* halt recursion */

  } else {
    /* generate random partition element, remember it (listel), and
       replace it by the first element */
    long i;
    long partel= Random(n/sortStride)*sortStride;
    long listel= list[partel];
    char *partition= stringData[listel];
    list[partel]= list[0];
    /* partition the remainder of the list into elements which precede
       the partel then elements which follow it */
    for (j=sortStride ; j<n ; j+=sortStride)
      if (strcmp(stringData[list[j]], partition) >= 0) break;
    for (i=j+sortStride ; i<n ; i+=sortStride)
      if (strcmp(stringData[list[i]], partition) < 0) {
	register long tmp= list[j];
	list[j]= list[i];
	list[i]= tmp;
	j+= sortStride;  /* known to be >= partel (or == n) */
      }
    /* re-insert partition element at beginning of 2nd part of list
       -- this will be its final resting place */
    list[0]= list[j-sortStride];
    list[j-sortStride]= listel;
  }

  /* recurse to sort the < and >= partitions themselves
     This is outside previous if block to minimize stack space required
     for the recursion.  */
  if (j>sortStride) ysortQ(list, j-sortStride);
  if (j<n) ysortQ(&list[j], n-j);
}

static void ysortD(long *list, long n)
{
  long j;
  if (n < sortLimit) {
    /* straight insertion fastest for short sorts */
    long i, listel;
    double partition;
    for (i=sortStride ; i<n ; i+=sortStride) {
      listel= list[i];
      partition= doubleData[listel];
      for (j=i-sortStride ;
	   j>=0 && doubleData[list[j]]>partition ;
	   j-=sortStride) list[j+sortStride]= list[j];
      list[j+sortStride]= listel;
    }
    return;  /* halt recursion */

  } else {
    /* generate random partition element, remember it (listel), and
       replace it by the first element */
    long i;
    long partel= Random(n/sortStride)*sortStride;
    long listel= list[partel];
    double partition= doubleData[listel];
    list[partel]= list[0];
    /* partition the remainder of the list into elements which precede
       the partel then elements which follow it */
    for (j=sortStride ; j<n ; j+=sortStride)
      if (doubleData[list[j]] >= partition) break;
    for (i=j+sortStride ; i<n ; i+=sortStride)
      if (doubleData[list[i]] < partition) {
	register long tmp= list[j];
	list[j]= list[i];
	list[i]= tmp;
	j+= sortStride;  /* known to be >= partel (or == n) */
      }
    /* re-insert partition element at beginning of 2nd part of list
       -- this will be its final resting place */
    list[0]= list[j-sortStride];
    list[j-sortStride]= listel;
  }

  /* recurse to sort the < and >= partitions themselves
     This is outside previous if block to minimize stack space required
     for the recursion.  */
  if (j>sortStride) ysortD(list, j-sortStride);
  if (j<n) ysortD(&list[j], n-j);
}

static void ysortL(long *list, long n)
{
  long j;
  if (n < sortLimit) {
    /* straight insertion fastest for short sorts */
    long i, listel;
    long partition;
    for (i=sortStride ; i<n ; i+=sortStride) {
      listel= list[i];
      partition= longData[listel];
      for (j=i-sortStride ;
	   j>=0 && longData[list[j]]>partition ;
	   j-=sortStride) list[j+sortStride]= list[j];
      list[j+sortStride]= listel;
    }
    return;  /* halt recursion */

  } else {
    /* generate random partition element, remember it (listel), and
       replace it by the first element */
    long i;
    long partel= Random(n/sortStride)*sortStride;
    long listel= list[partel];
    long partition= longData[listel];
    list[partel]= list[0];
    /* partition the remainder of the list into elements which precede
       the partel then elements which follow it */
    for (j=sortStride ; j<n ; j+=sortStride)
      if (longData[list[j]] >= partition) break;
    for (i=j+sortStride ; i<n ; i+=sortStride)
      if (longData[list[i]] < partition) {
	register long tmp= list[j];
	list[j]= list[i];
	list[i]= tmp;
	j+= sortStride;  /* known to be >= partel (or == n) */
      }
    /* re-insert partition element at beginning of 2nd part of list
       -- this will be its final resting place */
    list[0]= list[j-sortStride];
    list[j-sortStride]= listel;
  }

  /* recurse to sort the < and >= partitions themselves
     This is outside previous if block to minimize stack space required
     for the recursion.  */
  if (j>sortStride) ysortL(list, j-sortStride);
  if (j<n) ysortL(&list[j], n-j);
}

void Y_sort(int nArgs)
{
  Operand op;
  Array *result;
  long *ilist, i, j, which, nDims, origin;
  Dimension *tmp;
  void (*ysort)(long *list, long n);
  if (nArgs!=1 && nArgs!=2)
    YError("sort takes exactly one or two arguments");

  if (nArgs==2) { which= YGetInteger(sp)-1; Drop(1); }
  else which= 0;  /* use 0-origin which here */

  /* get array to be sorted */
  sp->ops->FormOperand(sp, &op);
  if (op.ops->typeID <= T_LONG) {
    op.ops->ToLong(&op);
    ysort= &ysortL;
    longData= op.value;
  } else if (op.ops->typeID <= T_DOUBLE) {
    op.ops->ToDouble(&op);
    ysort= &ysortD;
    doubleData= op.value;
  } else if (op.ops==&stringOps) {
    ysort= &ysortQ;
    stringData= op.value;
  } else {
    YError("sort function requires integer, real, or string operand");
    ysort= 0;
  }

  /* figure out stride for the sort */
  nDims= CountDims(op.type.dims);
  if (nDims==0) {
    PushIntValue(0);
    sp->ops= &longScalar;
    sp->value.l= 0;
    return;
  }
  if (which<0) which+= nDims;
  if (which<0 || which>=nDims)
    YError("2nd argument to sort function out of range");
  if (nDims<2) {
    sortStride= 1;
    sortSize= op.type.number;
  } else {
    which= nDims-1-which;
    tmp= op.type.dims;
    while (which--) tmp= tmp->next;
    sortStride= TotalNumber(tmp->next);
    sortSize= sortStride*tmp->number;
  }
  sortLimit= 7*sortStride;  /* use straight insertion for <7 elements */

  /* push result Array, then fill it with index to be sorted */
  result= PushDataBlock(NewArray(&longStruct, op.type.dims));
  ilist= result->value.l;
  for (i=0 ; i<op.type.number ; i++) ilist[i]= i;

  for (i=0 ; i<sortStride ; i++)
    for (j=0 ; j<op.type.number ; j+=sortSize)
      ysort(&ilist[i+j], sortSize);

  if ((origin= op.type.dims->origin))
    for (i=0 ; i<op.type.number ; i++) ilist[i]+= origin;
}

/*--------------------------------------------------------------------------*/

void Y_transpose(int nArgs)
{
  Symbol *stack= sp-nArgs+1;
  Operand op, opp;
  int i, nDims, order[10];
  long numbers[10], origins[10], strides[10];
  long stride, *cycle, index, prev, next, last;
  Dimension *dims;
  LValue *lvalue;
  Strider *strider;
  if (nArgs < 1) YError("transpose needs at least argument");

  stack->ops->FormOperand(stack, &op);
  if (!op.ops->isArray) YError("1st argument to transpose must be array");
  nDims= CountDims(op.type.dims);
  if (nDims>10) YError("transpose fails for arrays with >10 dimensions");
  if (nDims<1) {
    if (nArgs>1) Drop(nArgs-1);
    return;
  }

  /* collect dimension lengths and strides into arrays to be permuted */
  dims= op.type.dims;
  stride= 1;
  for (i=0 ; i<nDims ; i++) {
    numbers[nDims-1-i]= dims->number;
    origins[nDims-1-i]= dims->origin;
    dims= dims->next;
  }
  stride= op.type.base->size;
  for (i=0 ; i<nDims ; i++) {
    strides[i]= stride;
    stride*= numbers[i];
  }

  /* compute the permutation from the remaining arguments */
  for (i=0 ; i<nDims ; i++) order[i]= i;
  if (nArgs<2) {
    /* default is to swap first and last indices */
    if (nDims) {
      prev= order[0];
      order[0]= order[nDims-1];
      order[nDims-1]= prev;
    }
  } else {
    /* read permutation list */
    while (stack<sp) {
      stack++;
      stack->ops->FormOperand(stack, &opp);
      if (opp.ops->promoteID>T_LONG ||
	  (opp.type.dims && opp.type.dims->next))
	YError("bad permutation list in transpose");
      opp.ops->ToLong(&opp);
      cycle= opp.value;
      if (opp.type.dims) {
	/* this is a cycle list */
	last= cycle[0]-1;
	if (last<0) last+= nDims;
	if (last<0 || last>=nDims)
	  YError("permutation list references non-existent dimension "
		 "in transpose");
	prev= order[last];
	for (i=1 ; i<opp.type.number ; i++) {
	  index= cycle[i]-1;
	  if (index<0) index+= nDims;
	  if (index<0 || index>=nDims)
	    YError("permutation list references non-existent dimension "
		   "in transpose");
	  next= order[index];
	  order[index]= prev;
	  prev= next;
	}
	order[last]= prev;
      } else {
	/* this is a cyclic permutation of all nDims indices */
	long inc= cycle[0]-1;		/* index which 0 should go to */
	long now;
	if (inc<0) inc= nDims - (-inc)%nDims;
	if (inc>=nDims) inc%= nDims;
	prev= order[0];
	now= inc;
	last= now;
	for (i=0 ; i<nDims ; i++) {
	  next= order[now];
	  order[now]= prev;
	  prev= next;
	  now+= inc;
	  if (now>=nDims) now-= nDims;
	  if (last==now) {
	    /* handle case of several independent cycles when nDims is
	       evenly divisible by inc */
	    prev= order[++now];
	    now+= inc;
	    if (now>=nDims) now-= nDims;
	    last= now;
	  }
	}
      }
    }
    Drop(nArgs-1);
  }

  /* build re-ordered dimension list */
  dims= tmpDims;
  tmpDims= 0;
  FreeDimension(dims);
  for (i=0 ; i<nDims ; i++)
    tmpDims= NewDimension(numbers[order[i]], origins[order[i]], tmpDims);

  /* push LValue describing result onto stack */
  lvalue= PushDataBlock(NewLValueM((Array *)sp->value.db, op.value,
				   op.type.base, tmpDims));

  /* build strider list describing re-ordering */
  for (i=0 ; i<nDims ; i++) {
    strider= NewStrider(strides[order[i]], numbers[order[i]]);
    strider->next= lvalue->strider;
    lvalue->strider= strider;
  }

  PopTo(sp-2);
  Drop(1);
  FetchLValue(lvalue, sp);
}

static Dimension *growDims= 0;

void Y_grow(int nArgs)
{
  Symbol *s0, *s= sp-nArgs+1;
  long index= s->index;
  Array *array;
  Dimension *dims;
  StructDef *base;
  Operand op;
  long extra, number;
  int nDims;
  DataBlock *db;
  int amSubroutine= CalledAsSubroutine();

  if (nArgs < 2) YError("grow function needs at least two arguments");
  if (amSubroutine && s->ops!=&referenceSym)
    YError("1st argument to grow must be a variable reference");
  if (!s->ops) YError("unxepected keyword argument in grow");

  dims= growDims;
  growDims= 0;
  FreeDimension(dims);

  /* scan argument list to find first non-nil argument */
  base= 0;
  s0= 0;
  for (;;) {
    if (!s0 && amSubroutine) array= (Array *)ForceToDB(&globTab[index]);
    else array= (Array *)ForceToDB(s);  /* does ReplaceRef if required */
    s0= s;
    if (array->ops==&lvalueOps) array= FetchLValue(array, s);
    if (array->ops->isArray) {
      base= array->type.base;
      if (array->references) {
	/* the grow operation is destructive, must copy 1st arg */
	Array *copy= PushDataBlock(NewArray(base, array->type.dims));
	base->Copy(base, copy->value.c, array->value.c, array->type.number);
	PopTo(s);
	array= copy;
      }
      if (array->type.dims) {
	growDims= NewDimension(1L, 1L, Ref(array->type.dims->next));
      } else {
	growDims= NewDimension(1L, 1L, (Dimension *)0);
	array->type.dims= NewDimension(1L, 1L, (Dimension *)0);
      }
      break;
    } else if (array->ops!=&voidOps) {
      YError("bad data type in function grow");
    }
    if (++s > sp) {  /* all arguments void, will return nil */
      Drop(nArgs-1);
      PopTo(sp-1);
      return;
    }
  }
  nDims= CountDims(growDims);

  /* scan through remaining arguments to force right-conformability with
     growDims and count the number of extra dimensions */
  extra= 0;
  while (s<sp) {
    s++;
    if (!s->ops) YError("unxepected keyword argument in grow");
    s->ops->FormOperand(s, &op);
    if (op.ops->isArray) {
      if (nDims==CountDims(op.type.dims))
	growDims->number= op.type.dims->number;
      else
	growDims->number= 1;
      if (RightConform(growDims, &op))
	YError("later arguments not conformable with 1st in grow");
      extra+= growDims->number;
    } else if (op.ops!=&voidOps) {
      YError("illegal data type in function grow");
    }
  }

  if (extra) {
    LValue lvalue;
    long size;
    BinaryOp *Assign= base->dataOps->Assign;

    /* phony LValue necessary for Assign virtual function */
    lvalue.references= nArgs;    /* NEVER want to free this */
    lvalue.ops= &lvalueOps;
    lvalue.owner= 0;             /* not true, but safer */
    lvalue.type.base= base;      /* NOT Ref(base) -- won't be freed */
    lvalue.address.m= 0;
    lvalue.strider= 0;

    size= base->size;
    /* copy 1st non-nil argument */
    number= array->type.number;
    array= PushDataBlock(GrowArray(array, extra));
    lvalue.address.m= array->value.c + size*number;

    /* second pass through argument list copies 2nd-Nth arguments
       into result array using the Assign virtual function */
    s= s0;
    while (++s<sp) {  /* note that sp is bigger than for previous loop */
      s->ops->FormOperand(s, &op);
      if (op.ops->isArray) {
	lvalue.type.dims= op.type.dims; /* NOT Ref(dims) -- won't be freed */
	lvalue.type.number= op.type.number;
	/* Assign virtual functions assume their first parameter is an
	   LValue* rather than an Operation* (like all other BinaryOps).  */
	(*Assign)((Operand *)&lvalue, &op);
	lvalue.address.m+= size*lvalue.type.number;
      }
    }
  }

  /* store result back to first reference -- will also be left on stack
     by EvalBI */
  if (amSubroutine) {
    s= &globTab[index];  /* guaranteed this is a dataBlockSym by ForceToDB */
    db= s->value.db;
    s->value.db= (DataBlock *)Ref(array);
    Unref(db);
    if (extra) Drop(nArgs);
    else Drop(nArgs-1);
    ReplaceRef(sp);  /* result is 1st argument */
    PopTo(sp-1);
  } else {
    if (extra) {   /* result is on top of stack */
      PopTo(sp-nArgs-1);
      Drop(nArgs);
    } else {       /* result is unchanged s0 argument */
      int nAbove= sp-s0;
      Drop(nAbove);
      nArgs-= nAbove;
      PopTo(sp-nArgs);
      Drop(nArgs-1);
    }
  }
}

void Y__(int nArgs)
{
  Y_grow(nArgs);
}

/*--------------------------------------------------------------------------*/

void Y_strlen(int nArgs)
{
  long i, n, *lens;
  Dimension *dims;
  char **input;

  if (nArgs != 1) YError("strlen takes exactly one argument");

  input= YGet_Q(sp, 0, &dims);
  n= TotalNumber(dims);

  if (dims) {
    Array *result= PushDataBlock(NewArray(&longStruct, dims));
    lens= result->value.l;
  } else {
    PushLongValue(0L);
    lens= &sp->value.l;
  }

  for (i=0 ; i<n ; i++) {
    if (input[i]) lens[i]= strlen(input[i]);
    else lens[i]= 0;
  }
}

void Y_strpart(int nArgs)
{
  Operand op;
  Range *range;
  long min, max, i, n, len, mn, mx;
  int maxNil;
  Dimension *dims;
  char **input, **output;
  Array *result;

  if (nArgs != 2) YError("strpart takes exactly two arguments");

  sp->ops->FormOperand(sp, &op);
  range= op.value;
  if (op.ops!=&rangeOps || range->rf || range->inc!=1 ||
      range->nilFlags&(R_PSEUDO|R_RUBBER|R_NULLER))
    YError("bad 2nd argument to strpart");
  min= range->nilFlags&R_MINNIL? 0 : range->min-1L;
  maxNil= range->nilFlags&R_MAXNIL;
  max= range->max-1L;

  input= YGet_Q(sp-1, 0, &dims);
  n= TotalNumber(dims);

  result= PushDataBlock(NewArray(&stringStruct, dims));
  output= result->value.q;

  for (i=0 ; i<n ; i++) {
    if (input[i]) {
      len= strlen(input[i]);
      mn= min<0? len+min : min;
      if (maxNil) mx= len-1;
      else mx= max<0? len+max : max;
      if (mn<0) mn= 0;
      if (mx<0) mx= -1;
      if (mx>=len) mx= len-1;
      if (mn<len) output[i]= StrNCpy(input[i]+mn, mx-mn+1);
      else output[i]= StrAlloc(0);
    } else {
      output[i]= 0;
    }
  }
}

void Y_strmatch(int nArgs)
{
  long i, n;
  long caseFold;
  char *pattern, *s, *p;
  Dimension *dims;
  char **input;
  int *output, uorl, a, z;

  if (nArgs!=2 && nArgs!=3)
    YError("strmatch takes exactly two or three arguments");

  if (nArgs==3) { caseFold= YGetInteger(sp); Drop(1); }
  else caseFold= 0;
  uorl= ('A'^'a');   /* probably no safer than '\040' */
  a= ('a'|uorl);     /* probably no safer than 'a' */
  z= ('z'|uorl);     /* probably no safer than 'z' */

  pattern= YGetString(sp);

  input= YGet_Q(sp-1, 0, &dims);
  n= TotalNumber(dims);

  if (dims) {
    Array *result= PushDataBlock(NewArray(&intStruct, dims));
    output= result->value.i;
  } else {
    PushIntValue(0);
    output= &sp->value.i;
  }

  /* note that pattern=="" always matches, pattern==0 never matches */
  for (i=0 ; i<n ; i++) {
    if (pattern && input[i]) {
      s= input[i]; p= pattern;
      if (caseFold) {
	while (*s) {
	  while (*s && !(*s==*p || ((*s|uorl)==(*p|uorl) &&
				    (*s|uorl)>=a && (*s|uorl)<=z))) s++;
	  if (!*s) break;
	  do { s++; p++; } while (*s &&
				  (*s==*p || ((*s|uorl)==(*p|uorl) &&
					      (*s|uorl)>=a && (*s|uorl)<=z)));
	  if (!*p) break;
	  p= pattern;
	}
      } else {
	while (*s) {
	  while (*s && *s!=*p) s++;
	  if (!*s) break;
	  do { s++; p++; } while (*s && *s==*p);
	  if (!*p) break;
	  p= pattern;
	}
      }
      output[i]= (*p=='\0');
    } else {
      output[i]= 0;
    }
  }
}

static char *current;

void Y_strtok(int nArgs)
{
  Symbol *s= sp-nArgs+1;
  long i, n;
  char **input, **output, *delimit;
  Array *result;
  Dimension *dims;

  if (nArgs!=1 && nArgs!=2)
    YError("strtok takes exactly one or two arguments");

  if (nArgs==2) {
    delimit= YGetString(sp);
    if (!delimit) delimit= " \t\n";
  } else {
    delimit= " \t\n";
  }

  dims= tmpDims;
  tmpDims= 0;
  FreeDimension(dims);

  input= YGet_Q(s, 0, &dims);
  n= TotalNumber(dims);

  tmpDims= NewDimension(2L, 1L, (Dimension *)0);
  tmpDims= CopyDims(dims, tmpDims, 1);

  result= PushDataBlock(NewArray(&stringStruct, tmpDims));
  output= result->value.q;

  for (i=0 ; i<n ; i++) {
    if (input[i]) {
      StrFree(current);  current= 0;
      current= StrCpy(input[i]);
      output[2*i]= StrCpy(strtok(current, delimit));
      output[2*i+1]= StrCpy(strtok((char *)0, ""));
    } else {
      output[2*i]= output[2*i+1]= 0;
    }
  }
  StrFree(current);  current= 0;
}

/*--------------------------------------------------------------------------*/

extern char *Ytimestamp(void);  /* 25 character return */
extern void Ytimer(double *cpu, double *sys, double *wall);

void Y_timestamp(int nArgs)
{
  Array *array;
  if (nArgs!=1 || YNotNil(sp))
    YError("timestamp takes exactly one nil argument");
  array= PushDataBlock(NewArray(&stringStruct, (Dimension *)0));
  array->value.q[0]= Ytimestamp();
}

void Y_timer(int nArgs)
{
  Operand op;
  double *absTime, *incTime, cpu, sys, wall;

  if (nArgs<1 || nArgs>2 || !sp->ops)
    YError("timer takes exactly one or two arguments");
  sp->ops->FormOperand(sp, &op);
  if (op.ops!=&doubleOps || op.type.number!=3)
    YError("timer arguments must be array(double,3)");
  if (nArgs==1) {
    absTime= op.value;
    incTime= 0;
  } else {
    incTime= op.value;
    (sp-1)->ops->FormOperand(sp-1, &op);
    if (op.ops!=&doubleOps || op.type.number!=3)
      YError("timer arguments must be array(double,3)");
    absTime= op.value;
  }

  Ytimer(&cpu, &sys, &wall);
  if (incTime) {
    incTime[0]+= cpu-absTime[0];
    incTime[1]+= sys-absTime[1];
    incTime[2]+= wall-absTime[2];
  }
  absTime[0]= cpu;
  absTime[1]= sys;
  absTime[2]= wall;

  Drop(nArgs);
}

/*--------------------------------------------------------------------------*/

extern void BuildDimList(Symbol *stack, int nArgs);  /* ops3.c */
static void NextRandom(double *random, long n);
static void InitRandom(double seed);

void Y_random(int nArgs)
{
  Symbol *stack= sp-nArgs+1;
  double *random;
  long n;
  if (nArgs==1 && !YNotNil(stack)) {
    /* return scalar result */
    PushDoubleValue(0.0);
    random= &sp->value.d;
    n= 1;
  } else {
    /* return array result */
    Array *array;
    BuildDimList(stack, nArgs);
    array= PushDataBlock(NewArray(&doubleStruct, tmpDims));
    random= array->value.d;
    n= array->type.number;
  }
  NextRandom(random, n);
}

void Y_random_seed(int nArgs)
{
  double seed= 0.0;
  if (nArgs==1) {
    if (YNotNil(sp)) seed= YGetReal(sp);
  } else if (nArgs>0) {
    YError("random_seed takes exactly zero or one arguments");
  }
  InitRandom(seed);
  Drop(nArgs);
}

/* Algorithm from Press and Teukolsky, Computers in Physics, 6, #5,
   Sep/Oct 1992, pp. 522-524.  They offer a $1000 reward to anyone
   who finds a statistical test this generator fails non-trivially.
   Based on a generator of L'Ecuyer with a shuffle algorithm of
   Bays-Durham and other improvements.
   The period of the generator is 2.3e18.  */

/* The long period is achieved by combining two sequences with
   nearly incomensurate periods.  */
static long idum= 0;       /* this is the primary seed */
static long idum2= 0;      /* this is the secondary seed */
#undef NTAB
#define NTAB 32
static long iv[NTAB], iy;  /* shuffle table (of idum) and previous result */

/* Choose two sets of linear congruential parameters-- the
   multiplier is IA, and the modulus is IM.  The IR and IQ are
   cunningly chosen so that (IA*x)%IM can be computed as
   IA*(x%IQ) - IR*(x/IQ), the latter expression having the
   advantage that the product will not overflow.  The IM values
   are near 2^31-1, the largest guaranteed positive long.
   The clever calculation of (IA*x)%IM is due to Schrage,
   ACM Trans. Mathem. Software 5, 132-138;
   IM= IA*IQ+IR with IR<IQ is the required relation.  */
#undef IM1
#define IM1 2147483563
#undef IA1
#define IA1 40014
#undef IQ1
#define IQ1 53668
#undef IR1
#define IR1 12211

#undef IM2
#define IM2 2147483399
#undef IA2
#define IA2 40692
#undef IQ2
#define IQ2 52774
#undef IR2
#define IR2 3791

#undef AM
#define AM (1.0/(double)IM1)
#undef IMM1
#define IMM1 (IM1-1)
#undef NDIV
#define NDIV (1+IMM1/NTAB)

static void NextRandom(double *random, long n)
{
  long j;
  if (idum<=0) InitRandom(0.0);

  while (n--) {
    /* compute idum= (IA1*idum)%IM1 without overflow */
    idum= IA1*(idum%IQ1) - IR1*(idum/IQ1);
    if (idum<0) idum+= IM1;

    /* compute idum2= (IA2*idum2)%IM2 without overflow */
    idum2= IA2*(idum2%IQ2) - IR2*(idum2/IQ2);
    if (idum2<0) idum2+= IM2;

    /* previous result is used to determine which element of the shuffle
       table to use for this result */
    j= iy/NDIV;            /* in range 0..NTAB-1 */
    iy= iv[j]-idum2;
    iv[j]= idum;
    if (iy<1) iy+= IMM1;

    /* Really only IMM1 possible values can be returned, 1<=iy<=IMM1.
       Algorithm given by Press and Teukolsky has a slight bug.
       Here, the return values are centered in IMM1 equal bins.
       If 2.e9 distinct values are not enough, could use, say, idum2
       to scoot the points around randomly within bins...  */
    *random++= AM*(iy-0.5);
  }
}

static void InitRandom(double seed)
{
  long j;

  /* translate seed to integer between 1 and IM2-1 inclusive */
  if (seed<=0.0 || seed>=1.0) seed= 0.6180339885;  /* default seed */
  idum= (long)(seed*(double)IM2);
  if (idum<1 || idum>=IM2) idum= 1;
  idum2= idum;

  /* do 8 warm-ups, then load shuffle table */
  for (j=NTAB+7 ; j>=0 ; j--) {
    idum= IA1*(idum%IQ1) - IR1*(idum/IQ1);
    if (idum<0) idum+= IM1;
    if (j<NTAB) iv[j]= idum;
  }
  iy= iv[0];
}

/*--------------------------------------------------------------------------*/

static void MrgCpyC(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyS(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyI(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyL(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyF(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyD(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyZ(StructDef *, void *, void *, void *, int *, long);
static void MrgCpyX(StructDef *, void *, void *, void *, int *, long);

static void (*MrgCpy[10])(StructDef*, void*, void*, void*, int*, long)= {
  &MrgCpyC, &MrgCpyS, &MrgCpyI, &MrgCpyL, &MrgCpyF, &MrgCpyD, &MrgCpyZ,
  &MrgCpyX, &MrgCpyX, &MrgCpyX };

extern VMaction True;

void Y_merge(int nArgs)
{
  Operand t, f;
  Operations *ops;
  Dimension *dims;
  int *cond;
  long i, n;
  void *rslt= 0;
  StructDef *base;

  if (nArgs!=3) YError("merge function takes exactly three arguments");
  if (sp->ops==&referenceSym) ReplaceRef(sp);
  True();    /* convert condition to type int, values 0 or 1 */
  sp->ops->FormOperand(sp, &t);
  dims= t.type.dims;
  n= t.type.number;
  cond= t.value;
  if (!(sp-2)->ops)
    YError("merge function recognizes no keyword arguments");
  (sp-2)->ops->FormOperand(sp-2, &t);
  (sp-1)->ops->FormOperand(sp-1, &f);
  if (t.ops==&voidOps) {
    t.type.number= 0;
    if (f.ops==&voidOps) f.type.number= 0;
    ops= f.ops;
    base= f.type.base;
  } else if (f.ops==&voidOps) {
    f.type.number= 0;
    ops= t.ops;
    base= t.type.base;
  } else {
    ops= t.ops->Promote[f.ops->promoteID](&t, &f);
    base= t.type.base;
    if (ops==&structOps && !StructEqual(base, f.type.base))
      YError("two different struct instance types cannot be merged");
  }
  if (!ops || !ops->isArray)
    YError("merge requires array or nil arguments");
  if (t.type.number+f.type.number != n)
    YError("number of trues + number of falses not number of conditions");

  if (!dims) {
    if (base==&doubleStruct) {
      PushDoubleValue(0.0);
      rslt= &sp->value.d;
    } else if (base==&longStruct) {
      PushLongValue(0L);
      rslt= &sp->value.l;
    } else if (base==&intStruct) {
      PushIntValue(0);
      rslt= &sp->value.i;
    }
  }
  if (!rslt) {
    Array *result= PushDataBlock(NewArray(base, dims));
    rslt= result->value.c;
  }

  for (i=0 ; i<n ; i++) if (cond[i]) t.type.number--;
  if (t.type.number)
    YError("number of falses does not match number of 0 conditions");
  MrgCpy[ops->typeID](base, rslt, t.value, f.value, cond, n);
}

#undef OPERATION
#define OPERATION(op, typd) \
static void op(StructDef *base, void*rr, void*tt, void*ff, int*c, long n) \
{ typd *r= rr, *t= tt, *f= ff;   long i; \
  for (i=0 ; i<n ; i++) r[i]= c[i]? *t++ : *f++; }

/* ARGSUSED */
OPERATION(MrgCpyC, char)
/* ARGSUSED */
OPERATION(MrgCpyS, short)
/* ARGSUSED */
OPERATION(MrgCpyI, int)
/* ARGSUSED */
OPERATION(MrgCpyL, long)
/* ARGSUSED */
OPERATION(MrgCpyF, float)
/* ARGSUSED */
OPERATION(MrgCpyD, double)

/* ARGSUSED */
static void MrgCpyZ(StructDef *base, void*rr, void*tt, void*ff, int*c, long n)
{
  double *r= rr, *t= tt, *f= ff;
  long i;
  n*= 2;
  for (i=0 ; i<n ; i+=2) {
    if (c[i>>1]) { r[i]= *t++; r[i+1]= *t++; }
    else         { r[i]= *f++; r[i+1]= *f++; }
  }
}

static void MrgCpyX(StructDef *base, void*rr, void*tt, void*ff, int*c, long n)
{
  char *r= rr, *t= tt, *f= ff;
  long size= base->size;
  Copier *Copy= base->Copy;
  int cc= c[0];
  long nn, i= 0;
  do {
    i++;
    if (cc) {
      for (nn=1 ; i<n && c[i] ; i++) nn++;
      Copy(base, r, t, nn);
      nn*= size;
      r+= nn;
      t+= nn;
      cc= 0;
    } else {
      for (nn=1 ; i<n && !c[i] ; i++) nn++;
      Copy(base, r, f, nn);
      nn*= size;
      r+= nn;
      f+= nn;
      cc= 1;
    }
  } while (i<n);
}

/*--------------------------------------------------------------------------*/

#undef N_KEYWORDS
#define N_KEYWORDS 1
static char *histKeys[N_KEYWORDS+1]= { "top", 0 };

void Y_histogram(int nArgs)
{
  Symbol *keySymbols[N_KEYWORDS];
  Symbol *stack= YGetKeywords(sp-nArgs+1, nArgs, histKeys, keySymbols);
  int iPass= 0;
  long *list= 0;
  double *weight= 0;
  Dimension *dimsl, *dimsw;
  long number, i, top;

  while (stack<=sp) {
    if (!stack->ops) { stack+= 2; continue; }
    if (iPass==0) list= YGet_L(stack, 0, &dimsl);
    else if (iPass==1) weight= YGet_D(stack, 0, &dimsw);
    else list= 0;  /* error */
    iPass++;
    stack++;
  }
  if (!list) YError("histogram takes one or two non-keyword arguments");
  number= TotalNumber(dimsl);
  if (weight && TotalNumber(dimsw)!=number)
    YError("histogram weight array must be same length as list");

  top= 1;
  for (i=0 ; i<number ; i++) {
    if (list[i]>top) top= list[i];
    else if (list[i]<1) YError("histogram list element < 1 illegal");
  }

  if (YNotNil(keySymbols[0])) {
    i= YGetInteger(keySymbols[0]);
    if (i<top) YError("histogram list element > top illegal");
    top= i;
  }

  dimsl= tmpDims;
  tmpDims= 0;
  FreeDimension(dimsl);
  tmpDims= NewDimension(top, 1L, (Dimension *)0);

  if (!weight) {
    Array *array= PushDataBlock(NewArray(&longStruct, tmpDims));
    long *hist= array->value.l;
    for (i=0 ; i<top ; i++) hist[i]= 0;
    for (i=0 ; i<number ; i++) hist[list[i]-1]++;
  } else {
    Array *array= PushDataBlock(NewArray(&doubleStruct, tmpDims));
    double *hist= array->value.d;
    for (i=0 ; i<top ; i++) hist[i]= 0.0;
    for (i=0 ; i<number ; i++) hist[list[i]-1]+= weight[i];
  }
}

/*--------------------------------------------------------------------------*/

void Y_poly(int nArgs)
{
  Symbol *stack, *xsp= sp - nArgs + 1;
  extern void Multiply(void);
  extern void Add(void);

  for (stack=xsp ; stack<=sp ; stack++) {
    if (!stack->ops) YError("poly accepts no keyword arguments");
    if (stack->ops==&referenceSym) ReplaceRef(stack);
    if (stack->ops==&dataBlockSym && stack->value.db->ops==&lvalueOps)
      FetchLValue(stack->value.db, stack);
  }

  while (sp>xsp+1) {
    PushCopy(xsp);
    Multiply();
    Add();
  }
}

/*--------------------------------------------------------------------------*/