File: apop_fexact.c

package info (click to toggle)
apophenia 1.0%2Bds-7
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,904 kB
  • sloc: ansic: 19,479; makefile: 374; awk: 124; sh: 105; sed: 32
file content (1926 lines) | stat: -rw-r--r-- 55,911 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
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
/** \file apop_fexact.c

   Fisher's exact test for contingency tables 

   This file primarily consists of an algorithm from the ACM, fully
   documented below. The C code below was cut and pasted from the
   R project. Thanks, guys.

Un-R-ifying modifications Copyright (c) 2006--2009 by Ben Klemens.
Licensed under the GPLv2; see COPYING.

R version credits:
fexact.f -- translated by f2c (version 19971204).\\
Run through a slightly modified version of MM's f2c-clean.\\
Heavily hand-edited by KH and MM.
 */

#include "apop_internal.h"
#include <gsl/gsl_sf.h>
#include <gsl/gsl_math.h>
#include <stdbool.h>

/* These are the R-specific items. */
typedef enum { FALSE = 0, TRUE /*, MAYBE */ } Rboolean;
int imax2(int a, int b){return (a>b) ? a : b;}
int imin2(int a, int b){return (a<b) ? a : b;}
float fmax2(float a, float b){return (a>b) ? a : b;}
float fmin2(float a, float b){return (a<b) ? a : b;}
/* end R-to-Apophenia additions */

static void f2xact(int nrow, int ncol, int *table, int ldtabl,
		   double *expect, double *percnt, double *emin,
		   double *prt, double *pre, double *fact, int *ico, int *iro,
		   int *kyy, int *idif, int *irn, int *key,
		   int *ldkey, int *ipoin, double *stp, int *ldstp,
		   int *ifrq, double *LP, double *SP, double *tm,
		   int *key2, int *iwk, double *rwk);
static double f3xact(int nrow, int *irow, int ncol, int *icol, int ntot,
		     double *fact, int *ico, int *iro,
		     int *it, int *lb, int *nr, int *nt, int *nu,
		     int *itc, int *ist, double *stv, double *alen,
		     const double *tol);
static double f4xact(int nrow, int *irow, int ncol, int *icol, double dspt,
		     double *fact, int *icstk, int *ncstk,
		     int *lstk, int *mstk, int *nstk, int *nrstk, int *irstk,
		     double *ystk, const double *tol);
static void f5xact(double *pastp, const double *tol, int *kval, int *key,
		   int *ldkey, int *ipoin, double *stp, int *ldstp,
		   int *ifrq, int *npoin, int *nr, int *nl, int *ifreq,
		   int *itop, Rboolean psh);
static Rboolean f6xact(int nrow, int *irow, int *kyy,
		       int *key, int *ldkey, int *last, int *ipn);
static void f7xact(int nrow, int *imax, int *idif, int *k, int *ks,
		   int *iflag);
static void f8xact(int *irow, int is, int i1, int izero, int *new);
static double f9xact(int n, int ntot, int *ir, double *fact);
static Rboolean f10act(int nrow, int *irow, int ncol, int *icol, double *val,
		       double *fact, int *nd, int *ne, int *m);
static void f11act(int *irow, int i1, int i2, int *new);
static int iwork(int iwkmax, int *iwkpt, int number, int itype);

static void isort(int *n, int *ix);
static double gammds(double *y, double *p, int *ifault);

threadlocal bool has_error;
void prterr(int icode, const char *mes) {
    has_error++;
    Apop_notify(1, "FEXACT error %d.\n%s", icode, mes);
}

/* The interface to the original code, which apop_test_fisher_exact calls: */
static void fexact(int *nrow, int *ncol, int *table, int *ldtabl,
       double *expect, double *percnt, double *emin, double *prt,
       double *pre, /* new in C : */ int *workspace,
       /* new arg, was const = 30*/int *mult) {

/*
  ALGORITHM 643, COLLECTED ALGORITHMS FROM ACM.
  THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
  VOL. 19, NO. 4, DECEMBER, 1993, PP. 484-488.
  -----------------------------------------------------------------------
  Name:	      FEXACT
  Purpose:    Computes Fisher's exact test probabilities and a hybrid
	      approximation to Fisher exact test probabilities for a
	      contingency table using the network algorithm.

  Arguments:
    NROW    - The number of rows in the table.			(Input)
    NCOL    - The number of columns in the table.		(Input)
    TABLE   - NROW by NCOL matrix containing the contingency
              table.						(Input)
    LDTABL  - Leading dimension of TABLE exactly as specified
              in the dimension statement in the calling
	      program.						(Input)
    EXPECT  - Expected value used in the hybrid algorithm for
	      deciding when to use asymptotic theory
	      probabilities.					(Input)
	      If EXPECT <= 0.0 then asymptotic theory probabilities
	      are not used and Fisher exact test probabilities are
	      computed.	 Otherwise, if PERCNT or more of the cells in
	      the remaining table have estimated expected values of
	      EXPECT or more, with no remaining cell having expected
	      value less than EMIN, then asymptotic chi-squared
	      probabilities are used.  See the algorithm section of the
	      manual document for details.
	      Use EXPECT = 5.0 to obtain the 'Cochran' condition.
    PERCNT  - Percentage of remaining cells that must have
              estimated expected values greater than EXPECT
	      before asymptotic probabilities can be used.	(Input)
	      See argument EXPECT for details.
	      Use PERCNT = 80.0 to obtain the 'Cochran' condition.
    EMIN    - Minimum cell estimated expected value allowed for
	      asymptotic chi-squared probabilities to be used.	(Input)
	      See argument EXPECT for details.
	      Use EMIN = 1.0 to obtain the 'Cochran' condition.
    PRT     - Probability of the observed table for fixed
              marginal totals.					(Output)
    PRE     - Table p-value.					(Output)
	      PRE is the probability of a more extreme table,
	      where `extreme' is in a probabilistic sense.
	      If EXPECT < 0 then the Fisher exact probability
	      is returned.  Otherwise, an approximation to the
	      Fisher exact probability is computed based upon
	      asymptotic chi-squared probabilities for ``large''
	      table expected values.  The user defines ``large''
	      through the arguments EXPECT, PERCNT, and EMIN.

  Remarks:
  1. For many problems one megabyte or more of workspace can be
     required.	If the environment supports it, the user should begin
     by increasing the workspace used to 200,000 units.
  2. In FEXACT, LDSTP = MULT*LDKEY.  The proportion of table space used
     by STP may be changed by changing the line MULT = 30 below to
     another value. --> MULT is now an __argument__ of the function
  3. FEXACT may be converted to single precision by setting IREAL = 3,
     and converting all DOUBLE PRECISION specifications (except the
     specifications for RWRK, IWRK, and DWRK) to REAL.	This will
     require changing the names and specifications of the intrinsic
     functions ALOG, AMAX1, AMIN1, EXP, and REAL.  In addition, the
     machine specific constants will need to be changed, and the name
     DWRK will need to be changed to RWRK in the call to F2XACT.
  4. Machine specific constants are specified and documented in F2XACT.
     A missing value code is specified in both FEXACT and F2XACT.
  5. Although not a restriction, is is not generally practical to call
     this routine with large tables which are not sparse and in
     which the 'hybrid' algorithm has little effect.  For example,
     although it is feasible to compute exact probabilities for the
     table
	    1 8 5 4 4 2 2
	    5 3 3 4 3 1 0
	   10 1 4 0 0 0 0,
     computing exact probabilities for a similar table which has been
     enlarged by the addition of an extra row (or column) may not be
     feasible.
  -----------------------------------------------------------------------
  */

    /* To increase the length of the table of past path lengths relative
       to the length of the hash table, increase MULT.
    */

    /* AMISS is a missing value indicator which is returned when the
       probability is not defined.
    */
    const double amiss = GSL_NAN;
    /*
      Set IREAL = 4 for DOUBLE PRECISION
      Set IREAL = 3 for SINGLE PRECISION
    */
#define i_real 4
#define i_int  2

    /* System generated locals */
    int ikh;
    /* Local variables */
    int nco, nro, ntot, numb, iiwk, irwk;
    int i, j, k, kk, ldkey, ldstp, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10;
    int i3a, i3b, i3c, i9a, iwkmax, iwkpt;

    /* Workspace Allocation  */
    double *equiv;
    iwkmax = 2 * (int) (*workspace / 2);
    equiv = (double *) calloc(iwkmax / 2, sizeof(double));

#define dwrk (equiv)
#define iwrk ((int *)equiv)
#define rwrk ((float *)equiv)

    iwkpt = 0;

    Apop_stopif(*nrow > *ldtabl, has_error=true; return, 0, "NROW must be less than or equal to LDTABL.");

    ntot = 0;
    for (i = 0; i < *nrow; ++i) {
        for (j = 0; j < *ncol; ++j) {
            if (table[i + j * *ldtabl] < 0)
            prterr(2, "All elements of TABLE may not be negative.");
            ntot += table[i + j * *ldtabl];
        }
    }
    if (ntot == 0) {
        prterr(3, "All elements of TABLE are zero.\n"
               "PRT and PRE are set to missing values.");
        *pre = *prt = amiss;
        free(equiv);
        return;
    }

    /* nco := max(*nrow, *ncol)
     * nro := min(*nrow, *ncol) */
    if(*ncol > *nrow) {
        nco = *ncol;
        nro = *nrow;
    } else {
        nco = *nrow;
        nro = *ncol;
    }
    k = *nrow + *ncol + 1;
    kk = k * nco;

    ikh = ntot + 1;
    i1  = iwork(iwkmax, &iwkpt, ikh, i_real);
    i2  = iwork(iwkmax, &iwkpt, nco, i_int);
    i3  = iwork(iwkmax, &iwkpt, nco, i_int);
    i3a = iwork(iwkmax, &iwkpt, nco, i_int);
    i3b = iwork(iwkmax, &iwkpt, nro, i_int);
    i3c = iwork(iwkmax, &iwkpt, nro, i_int);
    ikh = imax2(k * 5 + (kk << 1), nco * 7 + 800);
    iiwk= iwork(iwkmax, &iwkpt, ikh, i_int);
    ikh = imax2(nco + 401, k);
    irwk= iwork(iwkmax, &iwkpt, ikh, i_real);

    /* NOTE:
       What follows below splits the remaining amount iwkmax - iwkpt of
       (int) workspace into hash tables as follows.
           type  size       index
	   INT   2 * ldkey  i4 i5 i11
	   REAL  2 * ldkey  i8 i9 i10
	   REAL  2 * ldstp  i6
	   INT   6 * ldstp  i7
       Hence, we need ldkey times
           3 * 2 + 3 * 2 * s + 2 * mult * s + 6 * mult
       chunks of integer memory, where s = sizeof(REAL) / sizeof(INT).
       If doubles are used and are twice as long as ints, this gives
           18 + 10 * mult
       so that the value of ldkey can be obtained by dividing available
       (int) workspace by this number.

       In fact, because iwork() can actually s * n + s - 1 int chunks
       when allocating a REAL, we use ldkey = available / numb - 1.

       FIXME:
       Can we always assume that sizeof(double) / sizeof(int) is 2?
       */

    if (i_real == 4) 		/* Double precision reals */
        numb = 18 + 10 * *mult;
    else			/* Single precision reals */
        numb = (*mult << 3) + 12;
    ldkey = (iwkmax - iwkpt) / numb - 1;
    ldstp = *mult * ldkey;
    ikh = ldkey << 1;	i4  = iwork(iwkmax, &iwkpt, ikh, i_int);
    ikh = ldkey << 1;	i5  = iwork(iwkmax, &iwkpt, ikh, i_int);
    ikh = ldstp << 1;	i6  = iwork(iwkmax, &iwkpt, ikh, i_real);
    ikh = ldstp * 6;	i7  = iwork(iwkmax, &iwkpt, ikh, i_int);
    ikh = ldkey << 1;	i8  = iwork(iwkmax, &iwkpt, ikh, i_real);
    ikh = ldkey << 1;	i9  = iwork(iwkmax, &iwkpt, ikh, i_real);
    ikh = ldkey << 1;	i9a = iwork(iwkmax, &iwkpt, ikh, i_real);
    ikh = ldkey << 1;	i10 = iwork(iwkmax, &iwkpt, ikh, i_int);

    /* To convert to double precision, change RWRK to DWRK in the next CALL.
     */
    f2xact(*nrow,
	   *ncol,
	   table,
	   *ldtabl,
	   expect,
	   percnt,
	   emin,
	   prt,
	   pre,
	   dwrk + i1,
	   iwrk + i2,
	   iwrk + i3,
	   iwrk + i3a,
	   iwrk + i3b,
	   iwrk + i3c,
	   iwrk + i4,
	   &ldkey,
	   iwrk + i5,
	   dwrk + i6,
	   &ldstp,
	   iwrk + i7,
	   dwrk + i8,
	   dwrk + i9,
	   dwrk + i9a,
	   iwrk + i10,
	   iwrk + iiwk,
	   dwrk + irwk);

    free(equiv);
}

#undef rwrk
#undef iwrk
#undef dwrk

static void f2xact(int nrow, int ncol, int *table, int ldtabl,
       double *expect, double *percnt, double *emin, double *prt,
       double *pre, double *fact, int *ico, int *iro, int *kyy,
       int *idif, int *irn, int *key, int *ldkey, int *ipoin,
       double *stp, int *ldstp, int *ifrq, double *LP, double *SP,
       double *tm, int *key2, int *iwk, double *rwk) {
/* -----------------------------------------------------------------------
  Name:		F2XACT
  Purpose:	Computes Fisher's exact test for a contingency table,
		routine with workspace variables specified.
  -----------------------------------------------------------------------
  */
    const int imax = INT_MAX;/* the largest representable int on the machine.*/

    /* AMISS is a missing value indicator which is returned when the
       probability is not defined. */
    const double amiss = GSL_NAN;

    /* TOL is chosen as the square root of the smallest relative spacing. */
    const static double tol = 3.45254e-7;

    const char* ch_err_5 =
	"The hash table key cannot be computed because the largest key\n"
	"is larger than the largest representable int.\n"
	"The algorithm cannot proceed.\n"
	"Reduce the workspace size or use another algorithm.";

    /* Local variables -- changed from "static"
     *  (*does* change results very slightly on i386 linux) */
    int i, ii, j, k, n,
	iflag,ifreq, ikkey, ikstp, ikstp2, ipn, ipo, itop, itp = 0,
	jkey, jstp, jstp2, jstp3, jstp4, k1, kb, kd, ks, kval = 0, kmax, last,
	ncell, ntot, nco, nro, nro2, nrb,
	i31, i32, i33, i34, i35, i36, i37, i38, i39,
	i41, i42, i43, i44, i45, i46, i47, i48, i310, i311;

    double dspt, d1,dd,df,ddf, drn,dro, obs, obs2, obs3, pastp,pv, tmp=0.;

#ifndef USING_R
    double d2;
    int ifault;
#endif
    Rboolean nr_gt_nc, maybe_chisq, chisq = FALSE/* -Wall */, psh;

    /* Parameter adjustments */
    table -= ldtabl + 1;
    --ico;
    --iro;
    --kyy;
    --idif;
    --irn;

    --key;
    --ipoin;
    --stp;
    --ifrq;
    --LP;
    --SP;
    --tm;
    --key2;
    --iwk;
    --rwk;

    /* Check table dimensions */
    Apop_stopif(nrow > ldtabl, has_error=true; return, 0, "NROW must be less than or equal to LDTABL.");
    Apop_stopif(ncol <= 1, has_error=true; return, 0, "NCOL must be at least 2");

    /* Initialize KEY array */
    for (i = 1; i <= *ldkey << 1; ++i) {
        key[i] = -9999;
        key2[i] = -9999;
    }

    nr_gt_nc =  nrow > ncol;
    /* nco := max(nrow, ncol) : */
    if (nr_gt_nc) nco = nrow;
    else          nco = ncol;
    /* Compute row marginals and total */
    ntot = 0;
    for (i = 1; i <= nrow; ++i) {
        iro[i] = 0;
        for (j = 1; j <= ncol; ++j) {
            Apop_stopif(table[i + j * ldtabl] < 0., has_error=true; return,
                    0, "All elements of TABLE must be non-negative.");
            iro[i] += table[i + j * ldtabl];
        }
        ntot += iro[i];
    }

    if (ntot == 0) {
        prterr(3, "All elements of TABLE are zero.\n"
               "PRT and PRE are set to missing values.");
        *pre = *prt = amiss;
        return;
    }

    /* Column marginals */
    for (i = 1; i <= ncol; ++i) {
        ico[i] = 0;
        for (j = 1; j <= nrow; ++j)
            ico[i] += table[j + i * ldtabl];
    }

    /* sort marginals */
    isort(&nrow, &iro[1]);
    isort(&ncol, &ico[1]);

    /*	Determine row and column marginals.
	Define max(nrow,ncol) =: nco >= nro := min(nrow,ncol)
	nco is defined above

	Swap marginals if necessary to	ico[1:nco] & iro[1:nro]
     */
    if (nr_gt_nc) {
        nro = ncol;
        /* Swap marginals */
        for (i = 1; i <= nco; ++i) {
            ii = iro[i];
            if (i <= nro) iro[i] = ico[i];
            ico[i] = ii;
        }
    } else
        nro = nrow;

    /* Get multiplers for stack */
    kyy[1] = 1;
    for (i = 1; i < nro; ++i) {
        /* Hash table multipliers */
        if (iro[i] + 1 <= imax / kyy[i]) {
            kyy[i + 1] = kyy[i] * (iro[i] + 1);
            j /= kyy[i];
        }
        else {
            prterr(5, ch_err_5);
            return;
        }
    }

    /* Check for Maximum product : */
    /* original code: if (iro[nro - 1] + 1 > imax / kyy[nro - 1]) */
    if (iro[nro] + 1 > imax / kyy[nro]) {
        prterr(501, ch_err_5);
        return;
    }

    /* Compute log factorials */
    fact[0] = 0.;
    fact[1] = 0.;
    if(ntot >= 2) fact[2] = log(2.);
    /* MM: old code assuming log() to be SLOW */
    for (i = 3; i <= ntot; i += 2) {
        fact[i] = fact[i - 1] + log((double) i);
        j = i + 1;
        if (j <= ntot)
            fact[j] = fact[i] + fact[2] + fact[j / 2] - fact[j / 2 - 1];
    }
    /* Compute obs := observed path length */
    obs = tol;
    ntot = 0;
    for (j = 1; j <= nco; ++j) {
        dd = 0.;
        if (nr_gt_nc) {
            for (i = 1; i <= nro; ++i) {
            dd += fact[table[j + i * ldtabl]];
            ntot +=    table[j + i * ldtabl];
            }
        } else {
            for (i = 1, ii = j * ldtabl + 1; i <= nro; i++, ii++) {
            dd += fact[table[ii]];
            ntot +=    table[ii];
            }
        }
        obs += fact[ico[j]] - dd;
    }

    /* Denominator of observed table: DRO */
    dro = f9xact(nro, ntot, &iro[1], fact);
    /* improve: the following "easily" underflows to zero -- return "log()" */
    *prt = exp(obs - dro);
    *pre = 0.;
    itop = 0;
    maybe_chisq = (*expect > 0.);

    /* Initialize pointers for workspace */
    /* f3xact */
    i31 = 1;
    i32 = i31 + nco;
    i33 = i32 + nco;
    i34 = i33 + nco;
    i35 = i34 + nco;
    i36 = i35 + nco;
    i37 = i36 + nco;
    i38 = i37 + nco;
    i39 = i38 + 400;
    i310 = 1;
    i311 = 1 + 400;
    /* f4xact */
    i = nrow + ncol + 1;
    i41 = 1;
    i42 = i41 + i;
    i43 = i42 + i;
    i44 = i43 + i;
    i45 = i44 + i;
    i46 = i45 + i;
    i47 = i46 + i * nco;
    i48 = 1;

    /* Initialize pointers */
    k = nco;
    last = *ldkey + 1;
    jkey = *ldkey + 1;
    jstp = *ldstp + 1;
    jstp2 = *ldstp * 3 + 1;
    jstp3 = (*ldstp << 2) + 1;
    jstp4 = *ldstp * 5 + 1;
    ikkey = 0;
    ikstp = 0;
    ikstp2 = *ldstp << 1;
    ipo = 1;
    ipoin[1] = 1;
    stp[1] = 0.;
    ifrq[1] = 1;
    ifrq[ikstp2 + 1] = -1;

Outer_Loop:
    kb = nco - k + 1;
    ks = 0;
    n = ico[kb];
    kd = nro + 1;
    kmax = nro;
    /* IDIF is the difference in going to the daughter */
    for (i = 1; i <= nro; ++i)
        idif[i] = 0;

    /* Generate the first daughter */
    do {
        --kd;
        ntot = imin2(n, iro[kd]);
        idif[kd] = ntot;
        if (idif[kmax] == 0)
            --kmax;
        n -= ntot;
    } while (n > 0 && kd != 1);

    if (n != 0) /* i.e. kd == 1 */
	goto L310;

    k1 = k - 1;
    n = ico[kb];
    ntot = 0;
    for (i = kb + 1; i <= nco; ++i)
        ntot += ico[i];

L150:
    /* Arc to daughter length=ICO[KB] */
    for (i = 1; i <= nro; ++i)
        irn[i] = iro[i] - idif[i];

    if (k1 > 1) {
        /* Sort irn */
        if (nro == 2) {
            if (irn[1] > irn[2]) {
            ii = irn[1]; irn[1] = irn[2]; irn[2] = ii;
            }
        } else
            isort(&nro, &irn[1]);

        /* Adjust start for zero */
        for (i = 1; i <= nro; ++i) {
            if (irn[i] != 0)
            break;
        }
        nrb = i;
    } else 
        nrb = 1;
    nro2 = nro - nrb + 1;

    /* Some table values */
    ddf = f9xact(nro,  n,    &idif[1],  fact);
    drn = f9xact(nro2, ntot, &irn[nrb], fact) - dro + ddf;
    /* Get hash value */
    if (k1 > 1) {
        kval = irn[1];
        /* Note that with the corrected check at error "502",
         * we won't have overflow in  kval  below : */
        for (i = 2; i <= nro; ++i)
            kval += irn[i] * kyy[i];

        /* Get hash table entry */
        i = kval % (*ldkey << 1) + 1;
        /* Search for unused location */
        for (itp = i; itp <= *ldkey << 1; ++itp) {
            ii = key2[itp];
            if (ii == kval) {
                goto L240;
            } else if (ii < 0) {
                key2[itp] = kval;
                LP[itp] = 1.;
                SP[itp] = 1.;
                goto L240;
            }
        }

        for (itp = 1; itp <= i - 1; ++itp) {
            ii = key2[itp];
            if (ii == kval) 
                goto L240;
            else if (ii < 0) {
                key2[itp] = kval;
                LP[itp] = 1.;
                goto L240;
            }
        }

        /* KH
           prterr(6, "LDKEY is too small.\n"
           "It is not possible to give the value of LDKEY required,\n"
           "but you could try doubling LDKEY (and possibly LDSTP).");
           */
        prterr(6, "LDKEY is too small for this problem.\n"
               "Try increasing the size of the workspace.");
    }

L240:
    psh = TRUE;
    /* Recover pastp */
    ipn = ipoin[ipo + ikkey];
    pastp = stp[ipn + ikstp];
    ifreq = ifrq[ipn + ikstp];
    /* Compute shortest and longest path */
    if (k1 > 1) {
        obs2 = obs - fact[ico[kb + 1]] - fact[ico[kb + 2]] - ddf;
        for (i = 3; i <= k1; ++i)
            obs2 -= fact[ico[kb + i]];

        if (LP[itp] > 0.) {
            dspt = obs - obs2 - ddf;
            /* Compute longest path */
            LP[itp] = f3xact(nro2, &irn[nrb], k1, &ico[kb + 1], ntot, fact,
                      &iwk[i31], &iwk[i32], &iwk[i33], &iwk[i34],
                      &iwk[i35], &iwk[i36], &iwk[i37], &iwk[i38],
                      &iwk[i39], &rwk[i310], &rwk[i311], &tol);
            if(LP[itp] > 0.) {/* can this happen? */
                printf("___ LP[itp=%d] = %g > 0\n", itp, LP[itp]);
                LP[itp] = 0.;
            }

            /* Compute shortest path -- using  dspt  as offset */
            SP[itp] = f4xact(nro2, &irn[nrb], k1, &ico[kb + 1], dspt, fact,
                      &iwk[i47], &iwk[i41], &iwk[i42], &iwk[i43],
                      &iwk[i44], &iwk[i45], &iwk[i46], &rwk[i48], &tol);
            /* SP[itp] = fmin2(0., SP[itp] - dspt);*/
            if(SP[itp] > 0.) { /* can this happen? */
                printf("___ SP[itp=%d] = %g > 0\n", itp, SP[itp]);
                SP[itp] = 0.;
            }

            /* Use chi-squared approximation? */
            if (maybe_chisq && (irn[nrb] * ico[kb + 1]) > ntot * *emin) {
                ncell = 0.;
                for (i = 0; i < nro2; ++i)
                    for (j = 1; j <= k1; ++j)
                        if (irn[nrb + i] * ico[kb + j] >= ntot * *expect)
                            ncell++;

                if (ncell * 100 >= k1 * nro2 * *percnt) {
                    tmp = 0.;
                    for (i = 0; i < nro2; ++i)
                        tmp += (fact[irn[nrb + i]] -
                            fact[irn[nrb + i] - 1]);
                    tmp *= k1 - 1;
                    for (j = 1; j <= k1; ++j)
                    tmp += (nro2 - 1) * (fact[ico[kb + j]] -
                                 fact[ico[kb + j] - 1]);
                    df = (double) ((nro2 - 1) * (k1 - 1));
                    tmp += df * 1.83787706640934548356065947281;
                    tmp -= (nro2 * k1 - 1) * (fact[ntot] - fact[ntot - 1]);
                    tm[itp] = (obs - dro) * -2. - tmp;
                } else {
                    /* tm[itp] set to a flag value */
                    tm[itp] = -9876.;
                }
            } else
                tm[itp] = -9876.;
        }
        obs3 = obs2 - LP[itp];
        obs2 -= SP[itp];
        if (tm[itp] == -9876.) 
            chisq = FALSE;
        else {
            chisq = TRUE;
            tmp = tm[itp];
        }
    } else {
        obs2 = obs - drn - dro;
        obs3 = obs2;
    }

L300:
    /* Process node with new PASTP */
    if (pastp <= obs3)  /* Update pre */
        *pre += (double) ifreq * exp(pastp + drn);
    else if (pastp < obs2) {
        if (chisq) {
            df = (double) ((nro2 - 1) * (k1 - 1));
            d1 = fmax2(0., tmp + (pastp + drn) * 2.) / 2.;
            d2 = df / 2.;
            pv = 1. - gammds(&d1, &d2, &ifault);
            *pre += (double) ifreq * exp(pastp + drn) * pv;
        } else {
            /* Put daughter on queue */
            d1 = pastp + ddf;
            f5xact(&d1, &tol, &kval, &key[jkey], ldkey, &ipoin[jkey],
               &stp[jstp], ldstp, &ifrq[jstp], &ifrq[jstp2],
               &ifrq[jstp3], &ifrq[jstp4], &ifreq, &itop, psh);
            psh = FALSE;
        }
    }
    /* Get next PASTP on chain */
    ipn = ifrq[ipn + ikstp2];
    if (ipn > 0) {
        pastp = stp[ipn + ikstp];
        ifreq = ifrq[ipn + ikstp];
        goto L300;
    }
    /* Generate a new daughter node */
    f7xact(kmax, &iro[1], &idif[1], &kd, &ks, &iflag);
    if (iflag != 1)
	goto L150;


L310:
    /* Go get a new mother from stage K */
    do {
        if(!f6xact(nro, &iro[1], &kyy[1], &key[ikkey + 1], ldkey, &last, &ipo))
            /* Update pointers */
            goto Outer_Loop;

        /* else : no additional nodes to process */
        --k;
        itop = 0;
        ikkey = jkey - 1;
        ikstp = jstp - 1;
        ikstp2 = jstp2 - 1;
        jkey = *ldkey - jkey + 2;
        jstp = *ldstp - jstp + 2;
        jstp2 = (*ldstp << 1) + jstp;
        for (i = 1; i <= *ldkey << 1; ++i)
            key2[i] = -9999;
    } while (k >= 2);
}/* f2xact() */

static double f3xact(int nrow, int *irow, int ncol, int *icol,
       int ntot, double *fact, int *ico, int *iro, int *it,
       int *lb, int *nr, int *nt, int *nu, int *itc, int *ist,
       double *stv, double *alen, const double *tol) {
/* -----------------------------------------------------------------------
  Name:	      F3XACT
  Purpose:    Computes the longest path length for a given table.

  Arguments:
    NROW    - The number of rows in the table.			(Input)
    IROW    - Vector of length NROW containing the row sums
              for the table.					(Input)
    NCOL    - The number of columns in the table.		(Input)
    ICOL    - Vector of length K containing the column sums
              for the table.					(Input)
    NTOT    - The total count in the table.			(Input)
    FACT    - Vector containing the logarithms of factorials.	(Input)
    ICO     - Work vector of length MAX(NROW,NCOL).
    IRO     - Work vector of length MAX(NROW,NCOL).
    IT	    - Work vector of length MAX(NROW,NCOL).
    LB	    - Work vector of length MAX(NROW,NCOL).
    NR	    - Work vector of length MAX(NROW,NCOL).
    NT	    - Work vector of length MAX(NROW,NCOL).
    NU	    - Work vector of length MAX(NROW,NCOL).
    ITC     - Work vector of length 400.
    IST     - Work vector of length 400.
    STV     - Work vector of length 400.
    ALEN    - Work vector of length MAX(NROW,NCOL).
    TOL     - Tolerance.					(Input)

  Return Value :
    LP     - The longest path for the table.			(Output)
  -----------------------------------------------------------------------
  */

    const int ldst = 200;/* half stack size */
    /* Initialized data */
    static int nst = 0;
    static int nitc = 0;

    int i, k;
    int n11, n12, ii, nn, ks, ic1, ic2, nc1, nn1;
    int nr1, nco, nct, ipn, irl, key, lev, itp, nro, nrt, kyy, nc1s;
    double LP, v, val, vmn;
    Rboolean xmin;

    --stv;
    --ist;
    --itc;
    --nu;
    --nt;
    --nr;
    --lb;
    --it;
    --iro;
    --ico;
    --icol;
    --irow;

    if (nrow <= 1) {	/* nrow is 1 */
        LP = 0.;
        if (nrow > 0) 
            for (i = 1; i <= ncol; ++i)
                LP -= fact[icol[i]];
        return LP;
    }

    if (ncol <= 1) {	/* ncol is 1 */
        LP = 0.;
        if (ncol > 0) {
            for (i = 1; i <= nrow; ++i)
            LP -= fact[irow[i]];
        }
        return LP;
    }

    /* 2 by 2 table */
    if (nrow * ncol == 4) {
        n11 = (irow[1] + 1) * (icol[1] + 1) / (ntot + 2);
        n12 = irow[1] - n11;
        return -(fact[n11] + fact[n12] +
             fact[icol[1] - n11] + fact[icol[2] - n12]);
    }

    /* ELSE:  larger than 2 x 2 : */

    /* Test for optimal table */
    val = 0.;
    if (irow[nrow] <= irow[1] + ncol) 
        xmin = f10act(nrow, &irow[1], ncol, &icol[1], &val, fact,
		      &lb[1], &nu[1], &nr[1]);
    else xmin = FALSE;
    if (! xmin &&  icol[ncol] <= icol[1] + nrow) 
        xmin = f10act(ncol, &icol[1], nrow, &irow[1], &val, fact,
		      &lb[1], &nu[1], &nr[1]);
    if (xmin)
        return  - val;

    /* Setup for dynamic programming */

    for (i = 0; i <= ncol; ++i)
        alen[i] = 0.;
    for (i = 1; i <= 2*ldst; ++i)
        ist[i] = -1;

    nn = ntot;
    /* Minimize ncol */
    if (nrow >= ncol) {
        nro = nrow;
        nco = ncol;
        ico[1] = icol[1];
        nt[1] = nn - ico[1];
        for (i = 2; i <= ncol; ++i) {
            ico[i] = icol[i];
            nt[i] = nt[i - 1] - ico[i];
        }
        for (i = 1; i <= nrow; ++i)
            iro[i] = irow[i];
    } else {
        nro = ncol;
        nco = nrow;
        ico[1] = irow[1];
        nt[1] = nn - ico[1];
        for (i = 2; i <= nrow; ++i) {
            ico[i] = irow[i];
            nt[i] = nt[i - 1] - ico[i];
        }
        for (i = 1; i <= ncol; ++i)
            iro[i] = icol[i];
    }

    nc1s = nco - 1;
    kyy = ico[nco] + 1;
    /* Initialize pointers */
    vmn = 1e100;/* to contain min(v..) */
    irl = 1;
    ks = 0;
    k = ldst;

LnewNode: /* Setup to generate new node */

    lev = 1;
    nr1 = nro - 1;
    nrt = iro[irl];
    nct = ico[1];
    lb[1] = (int) ((((double) nrt + 1) * (nct + 1)) /
		    (double) (nn + nr1 * nc1s + 1) - *tol) - 1;
    nu[1] = (int) ((((double) nrt + nc1s) * (nct + nr1)) /
		    (double) (nn + nr1 + nc1s)) - lb[1] + 1;
    nr[1] = nrt - lb[1];

LoopNode: /* Generate a node */
    --nu[lev];
    if (nu[lev] == 0) {
	if (lev == 1)
	    goto L200;

	--lev;
	goto LoopNode;
    }
    ++lb[lev];
    --nr[lev];

    while(1) {
        alen[lev] = alen[lev - 1] + fact[lb[lev]];
        if (lev >= nc1s)
            break;

        nn1 = nt[lev];
        nrt = nr[lev];
        ++lev;
        nc1 = nco - lev;
        nct = ico[lev];
        lb[lev] = (int) ((((double) nrt + 1) * (nct + 1)) /
                  (double) (nn1 + nr1 * nc1 + 1) - *tol);
        nu[lev] = (int) ((((double) nrt + nc1) * (nct + nr1)) /
                  (double) (nn1 + nr1 + nc1) - lb[lev] + 1);
        nr[lev] = nrt - lb[lev];
    }
    alen[nco] = alen[lev] + fact[nr[lev]];
    lb[nco] = nr[lev];

    v = val + alen[nco];

    if (nro == 2) { /* Only 1 row left */
        v += fact[ico[1] - lb[1]] + fact[ico[2] - lb[2]];
        for (i = 3; i <= nco; ++i)
            v += fact[ico[i] - lb[i]];

        if (v < vmn)
            vmn = v;
    } else if (nro == 3 && nco == 2) { /* 3 rows and 2 columns */
	nn1 = nn - iro[irl] + 2;
	ic1 = ico[1] - lb[1];
	ic2 = ico[2] - lb[2];
	n11 = (iro[irl + 1] + 1) * (ic1 + 1) / nn1;
	n12 = iro[irl + 1] - n11;
	v += fact[n11] + fact[n12] + fact[ic1 - n11] + fact[ic2 - n12];
	if (v < vmn)
	    vmn = v;

    } else { /* Column marginals are new node */

	for (i = 1; i <= nco; ++i)
	    it[i] = imax2(ico[i] - lb[i], 0);
	
	/* Sort column marginals it[] : */
	if (nco == 2) {
	    if (it[1] > it[2]) { /* swap */
		ii = it[1]; it[1] = it[2]; it[2] = ii;
	    }
	} else
	    isort(&nco, &it[1]);

	/* Compute hash value */
	key = it[1] * kyy + it[2];
	for (i = 3; i <= nco; ++i) {
	    key = it[i] + key * kyy;
	}
	if (key < -1){
        if (apop_opts.verbose)
	        printf("Bug in FEXACT: gave negative key.\n");
        return -1;
    }
	/* Table index */
	ipn = key % ldst + 1;
	/* Find empty position */
	for (itp = ipn, ii = ks + ipn; itp <= ldst; ++itp, ++ii) {
	    if (ist[ii] < 0) {
		goto L180;
	    } else if (ist[ii] == key) {
		goto L190;
	    }
	}

	for (itp = 1, ii = ks + 1; itp <= ipn - 1; ++itp, ++ii) {
	    if (ist[ii] < 0) {
		goto L180;
	    } else if (ist[ii] == key) {
		goto L190;
	    }
	}

	/* this happens less, now that we check for negative key above: */
	Apop_stopif(1, has_error=true; return GSL_NAN, 0, "Stack length exceeded in f3xact. This problem should not occur.");

L180: /* Push onto stack */
	ist[ii] = key;
	stv[ii] = v;
	++nst;
	ii = nst + ks;
	itc[ii] = itp;
	goto LoopNode;

L190: /* Marginals already on stack */
	stv[ii] = fmin2(v, stv[ii]);
    }
    goto LoopNode;


L200: /* Pop item from stack */
    if (nitc > 0) {
	/* Stack index */
	itp = itc[nitc + k] + k;
	--nitc;
	val = stv[itp];
	key = ist[itp];
	ist[itp] = -1;
	/* Compute marginals */
	for (i = nco; i >= 2; --i) {
	    ico[i] = key % kyy;
	    key /= kyy;
	}
	ico[1] = key;
	/* Set up nt array */
	nt[1] = nn - ico[1];
	for (i = 2; i <= nco; ++i)
	    nt[i] = nt[i - 1] - ico[i];

	/* Test for optimality (L90) */
	if (iro[nro] <= iro[irl] + nco) {
	    xmin = f10act(nro, &iro[irl], nco, &ico[1], &val, fact,
			  &lb[1], &nu[1], &nr[1]);
	} else xmin = FALSE;

	if (!xmin && ico[nco] <= ico[1] + nro)
	    xmin = f10act(nco, &ico[1], nro, &iro[irl], &val, fact,
			  &lb[1], &nu[1], &nr[1]);
	if (xmin) {
	    if (vmn > val)
		vmn = val;
	    goto L200;
	}
	else goto LnewNode;

    } else if (nro > 2 && nst > 0) {
        /* Go to next level */
        nitc = nst;
        nst = 0;
        k = ks;
        ks = ldst - ks;
        nn -= iro[irl];
        ++irl;
        --nro;
        goto L200;
    }
    return  - vmn;
}

static double f4xact(int nrow, int *irow, int ncol, int *icol, double dspt,
       double *fact, int *icstk, int *ncstk, int *lstk, int *mstk,
       int *nstk, int *nrstk, int *irstk, double *ystk, const double *tol) {
/* -----------------------------------------------------------------------
  Name:	      F4XACT
  Purpose:    Computes the shortest path length for a given table.

  Arguments:
     NROW   - The number of rows in the table.	(Input)
     IROW   - Vector of length NROW containing the row sums for the
	      table.  (Input)
     NCOL   - The number of columns in the table.  (Input)
     ICOL   - Vector of length K containing the column sums for the
	      table.  (Input)
     DSPT   - "offset"  for SP computation
     FACT   - Vector containing the logarithms of factorials.  (Input)
     ICSTK  - NCOL by NROW+NCOL+1 work array.
     NCSTK  - Work vector of length NROW+NCOL+1.
     LSTK   - Work vector of length NROW+NCOL+1.
     MSTK   - Work vector of length NROW+NCOL+1.
     NSTK   - Work vector of length NROW+NCOL+1.
     NRSTK  - Work vector of length NROW+NCOL+1.
     IRSTK  - NROW by MAX(NROW,NCOL) work array.
     YSTK   - Work vector of length NROW+NCOL+1.
     TOL    - Tolerance.					(Input)

  Return Value :

    SP	    - The shortest path for the table.			(Output)
  ----------------------------------------------------------------------- */

    int i, j, k, l, m, n, ic1, ir1, ict, irt, istk, nco, nro;
    double y, amx, SP;

    /* Take care of the easy cases first */
    if (nrow == 1) {
        SP = 0.;
        for (i = 0; i < ncol; ++i)
            SP -= fact[icol[i]];
        return SP;
    }
    if (ncol == 1) {
        SP = 0.;
        for (i = 0; i < nrow; ++i)
            SP -= fact[irow[i]];
        return SP;
    }
    if (nrow * ncol == 4) {
        if (irow[1] <= icol[1])
            return -(fact[irow[1]] + fact[icol[1]] + fact[icol[1] - irow[1]]);
        else
            return -(fact[icol[1]] + fact[irow[1]] + fact[irow[1] - icol[1]]);
    }

    /* Parameter adjustments */
    irstk -= nrow + 1;
    icstk -= ncol + 1;

    --nrstk;
    --ncstk;
    --lstk;
    --mstk;
    --nstk;
    --ystk;

    /* initialization before loop */
    for (i = 1; i <= nrow; ++i)
        irstk[i + nrow] = irow[nrow - i];

    for (j = 1; j <= ncol; ++j)
        icstk[j + ncol] = icol[ncol - j];

    nro = nrow;
    nco = ncol;
    nrstk[1] = nro;
    ncstk[1] = nco;
    ystk[1] = 0.;
    y = 0.;
    istk = 1;
    l = 1;
    amx = 0.;
    SP = dspt;

    /* First LOOP */
    do {
	ir1 = irstk[istk * nrow + 1];
	ic1 = icstk[istk * ncol + 1];
	if (ir1 > ic1) {
	    if (nro >= nco) {
            m = nco - 1;	n = 2;
	    } else {
            m = nro;	n = 1;
	    }
	} else if (ir1 < ic1) {
	    if (nro <= nco) {
            m = nro - 1;	n = 1;
	    } else {
            m = nco;	n = 2;
	    }
	} else {
	    if (nro <= nco) {
            m = nro - 1;	n = 1;
	    } else {
            m = nco - 1;	n = 2;
	    }
	}

    L60:
	if (n == 1) {
	    i = l; j = 1;
	} else {
	    i = 1; j = l;
	}

	irt = irstk[i + istk * nrow];
	ict = icstk[j + istk * ncol];
	y += fact[imin2(irt, ict)];
	if (irt == ict) {
	    --nro;
	    --nco;
	    f11act(&irstk[istk * nrow + 1], i, nro,
		   &irstk[(istk + 1) * nrow + 1]);
	    f11act(&icstk[istk * ncol + 1], j, nco,
		   &icstk[(istk + 1) * ncol + 1]);
	} else if (irt > ict) {
	    --nco;
	    f11act(&icstk[istk * ncol + 1], j, nco,
		   &icstk[(istk + 1) * ncol + 1]);
	    f8xact(&irstk[istk * nrow + 1], irt - ict, i, nro,
		   &irstk[(istk + 1) * nrow + 1]);
	} else {
	    --nro;
	    f11act(&irstk[istk * nrow + 1], i, nro,
		   &irstk[(istk + 1) * nrow + 1]);
	    f8xact(&icstk[istk * ncol + 1], ict - irt, j, nco,
		   &icstk[(istk + 1) * ncol + 1]);
	}

	if (nro == 1) {
	    for (k = 1; k <= nco; ++k)
            y += fact[icstk[k + (istk + 1) * ncol]];
	    break;
	}
	if (nco == 1) {
	    for (k = 1; k <= nro; ++k)
            y += fact[irstk[k + (istk + 1) * nrow]];
	    break;
	}

	lstk[istk] = l;
	mstk[istk] = m;
	nstk[istk] = n;
	++istk;
	nrstk[istk] = nro;
	ncstk[istk] = nco;
	ystk[istk] = y;
	l = 1;
    } while(1);/* end do */

    if (y > amx) {
        amx = y;
        if (SP - amx <= *tol)
            return -dspt;
    }

    do {
	--istk;
	if (istk == 0) {
	    SP -= amx;
	    if (SP - amx <= *tol)
		return -dspt;
	    else
		return SP - dspt;
	}
	l = lstk[istk] + 1;

	for(;; ++l) {
	    if (l > mstk[istk])	break;

	    n = nstk[istk];
	    nro = nrstk[istk];
	    nco = ncstk[istk];
	    y = ystk[istk];
	    if (n == 1) {
		if (irstk[l	+ istk * nrow] <
		    irstk[l - 1 + istk * nrow])	goto L60;
	    }
	    else if (n == 2) {
		if (icstk[l	+ istk * ncol] <
		    icstk[l - 1 + istk * ncol])	goto L60;
	    }
	}
    } while(1);
}


void f5xact(double *pastp, const double *tol, int *kval, int *key, int *ldkey,
       int *ipoin, double *stp, int *ldstp, int *ifrq, int *npoin,
       int *nr, int *nl, int *ifreq, int *itop, Rboolean psh) {
/* -----------------------------------------------------------------------
  Name:	      F5XACT aka "PUT"
  Purpose:    Put node on stack in network algorithm.

  Arguments:
     PASTP  - The past path length.				(Input)
     TOL    - Tolerance for equivalence of past path lengths.  	(Input)
     KVAL   - Key value.  					(Input)
     KEY    - Vector of length LDKEY containing the key values.	(in/out)
     LDKEY  - Length of vector KEY.  				(Input)
     IPOIN  - Vector of length LDKEY pointing to the
	      linked list of past path lengths.  		(in/out)
     STP    - Vector of length LSDTP containing the
	      linked lists of past path lengths.  		(in/out)
     LDSTP  - Length of vector STP.  				(Input)
     IFRQ   - Vector of length LDSTP containing the past path
	      frequencies.  					(in/out)
     NPOIN  - Vector of length LDSTP containing the pointers to
	      the next past path length.  			(in/out)
     NR	    - Vector of length LDSTP containing the right object
	      pointers in the tree of past path lengths.        (in/out)
     NL	    - Vector of length LDSTP containing the left object
	      pointers in the tree of past path lengths.        (in/out)
     IFREQ  - Frequency of the current path length.             (Input)
     ITOP   - Pointer to the top of STP.  			(Input)
     PSH    - Logical.		 				(Input)
	      If PSH is true, the past path length is found in the
	      table KEY.  Otherwise the location of the past path
	      length is assumed known and to have been found in
	      a previous call. ==>>>>> USING "static" variables
  ----------------------------------------------------------------------- */

    static int itmp, ird, ipn, itp; /* << *need* static, see PSH above */
    double test1, test2;

    --nl;
    --nr;
    --npoin;
    --ifrq;
    --stp;

    /* Function Body */
    if (psh) {
	/* Convert KVAL to int in range 1, ..., LDKEY. */
	ird = *kval % *ldkey;
	/* Search for an unused location */
	for (itp = ird; itp < *ldkey; ++itp) {
	    if (key[itp] == *kval)
		goto L40;

	    if (key[itp] < 0)
		goto L30;
	}
	for (itp = 0; itp < ird; ++itp) {
	    if (key[itp] == *kval)
		goto L40;

	    if (key[itp] < 0)
		goto L30;
	}
	/* Return if KEY array is full */
	/* KH
	  prterr(6, "LDKEY is too small for this problem.\n"
	  "It is not possible to estimate the value of LDKEY "
	  "required,\n"
	  "but twice the current value may be sufficient.");
	  */
	prterr(6, "LDKEY is too small for this problem.\n"
	       "Try increasing the size of the workspace.");


L30: /* Update KEY */

	key[itp] = *kval;
	++(*itop);
	ipoin[itp] = *itop;
	/* Return if STP array full */
	if (*itop > *ldstp) {
	    /* KH
	       prterr(7, "LDSTP is too small for this problem.\n"
	       "It is not possible to estimate the value of LDSTP "
	       "required,\n"
	       "but twice the current value may be sufficient.");
	       */
	    prterr(7, "LDSTP is too small for this problem.\n"
		   "Try increasing the size of the workspace.");
	}
	/* Update STP, etc. */
	npoin[*itop] = -1;
	nr   [*itop] = -1;
	nl   [*itop] = -1;
	stp  [*itop] = *pastp;
	ifrq [*itop] = *ifreq;
	return;
    }

L40: /* Find location, if any, of pastp */

    ipn = ipoin[itp];
    test1 = *pastp - *tol;
    test2 = *pastp + *tol;

    do {
	if (stp[ipn] < test1)
	    ipn = nl[ipn];
	else if (stp[ipn] > test2)
	    ipn = nr[ipn];
	else {
	    ifrq[ipn] += *ifreq;
	    return;
	}
    } while (ipn > 0);

    /* Return if STP array full */
    ++(*itop);
    if (*itop > *ldstp) {
	/*
	  prterr(7, "LDSTP is too small for this problem.\n"
	  "It is not possible to estimate the value of LDSTP "
	  "required,\n"
	  "but twice the current value may be sufficient.");
	  */
	prterr(7, "LDSTP is too small for this problem.\n"
	       "Try increasing the size of the workspace.");
	return;
    }

    /* Find location to add value */
    ipn = ipoin[itp];
    itmp = ipn;

L60:
    if (stp[ipn] < test1) {
	itmp = ipn;
	ipn = nl[ipn];
	if (ipn > 0)
	    goto L60;
	/* else */
	nl[itmp] = *itop;
    }
    else if (stp[ipn] > test2) {
	itmp = ipn;
	ipn = nr[ipn];
	if (ipn > 0)
	    goto L60;
	/* else */
	nr[itmp] = *itop;
    }
    /* Update STP, etc. */
    npoin[*itop] = npoin[itmp];
    npoin[itmp] = *itop;
    stp	 [*itop] = *pastp;
    ifrq [*itop] = *ifreq;
    nl	 [*itop] = -1;
    nr	 [*itop] = -1;
}


Rboolean f6xact(int nrow, int *irow, int *kyy, int *key, int *ldkey, int *last, int *ipn) {
/* -----------------------------------------------------------------------
  Name:	      F6XACT  aka "GET"
  Purpose:    Pop a node off the stack.

  Arguments:
    NROW    - The number of rows in the table.			(Input)
    IROW    - Vector of length nrow containing the row sums on
              output.						(Output)
    KYY     - Constant mutlipliers used in forming the hash
              table key.					(Input)
    KEY     - Vector of length LDKEY containing the hash table
              keys.						(In/out)
    LDKEY   - Length of vector KEY.				(Input)
    LAST    - Index of the last key popped off the stack.	(In/out)
    IPN     - Pointer to the linked list of past path lengths.	(Output)

  Return value :
    TRUE if there are no additional nodes to process.           (Output)
  ----------------------------------------------------------------------- */
    int kval, j;

    --key;

L10:
    ++(*last);
    if (*last <= *ldkey) {
	if (key[*last] < 0)
	    goto L10;

	/* Get KVAL from the stack */
	kval = key[*last];
	key[*last] = -9999;
	for (j = nrow-1; j > 0; j--) {
	    irow[j] = kval / kyy[j];
	    kval -= irow[j] * kyy[j];
	}
	irow[0] = kval;
	*ipn = *last;
	return FALSE;
    } else {
	*last = 0;
	return TRUE;
    }
}


void f7xact(int nrow, int *imax, int *idif, int *k, int *ks, int *iflag) {
/* -----------------------------------------------------------------------
  Name:	      F7XACT
  Purpose:    Generate the new nodes for given marginal totals.

  Arguments:
    NROW    - The number of rows in the table.			(Input)
    IMAX    - The row marginal totals.				(Input)
    IDIF    - The column counts for the new column.		(in/out)
    K	    - Indicator for the row to decrement.		(in/out)
    KS	    - Indicator for the row to increment.		(in/out)
    IFLAG   - Status indicator.					(Output)
	      If IFLAG is zero, a new table was generated.  For
	      IFLAG = 1, no additional tables could be generated.
  ----------------------------------------------------------------------- */
    int i, m, kk, mm;

    /* Parameter adjustments */
    --idif;
    --imax;

    /* Function Body */
    *iflag = 0;
    /* Find node which can be incremented, ks */
    if (*ks == 0)
	do {
	    ++(*ks);
	} while (idif[*ks] == imax[*ks]);

    /* Find node to decrement (>ks) */
    if (idif[*k] > 0 && *k > *ks) {
	--idif[*k];
	do {
	    --(*k);
	} while(imax[*k] == 0);

	m = *k;

	/* Find node to increment (>=ks) */
	while (idif[m] >= imax[m]) {
	    --m;
	}
	++idif[m];
	/* Change ks */
	if (m == *ks && idif[m] == imax[m])
	    *ks = *k;
    }
    else {
 Loop:
	/* Check for finish */
	for (kk = *k + 1; kk <= nrow; ++kk) {
	    if (idif[kk] > 0) {
		goto L70;
	    }
	}
	*iflag = 1;
	return;

 L70:
	/* Reallocate counts */
	mm = 1;
	for (i = 1; i <= *k; ++i) {
	    mm += idif[i];
	    idif[i] = 0;
	}
	*k = kk;

	do {
	    --(*k);
	    m = imin2(mm, imax[*k]);
	    idif[*k] = m;
	    mm -= m;
	} while (mm > 0 && *k != 1);

	/* Check that all counts reallocated */
	if (mm > 0) {
	    if (kk != nrow) {
		*k = kk;
		goto Loop;
	    }
	    *iflag = 1;
	    return;
	}
	/* Get ks */
	--idif[kk];
	*ks = 0;
	do {
	    ++(*ks);
	    if (*ks > *k) {
		return;
	    }
	} while (idif[*ks] >= imax[*ks]);
    }
}


void f8xact(int *irow, int is, int i1, int izero, int *new) {
/* -----------------------------------------------------------------------
  Name:	      F8XACT
  Purpose:    Routine for reducing a vector when there is a zero
	      element.
  Arguments:
     IROW   - Vector containing the row counts.			(Input)
     IS	    - Indicator.					(Input)
     I1	    - Indicator.					(Input)
     IZERO  - Position of the zero.				(Input)
     NEW    - Vector of new row counts.				(Output)
  ----------------------------------------------------------------------- */

    int i;

    /* Parameter adjustments */
    --new;
    --irow;

    /* Function Body */
    for (i = 1; i < i1; ++i)
        new[i] = irow[i];

    for (i = i1; i <= izero - 1; ++i) {
        if (is >= irow[i + 1]) break;
        new[i] = irow[i + 1];
    }

    new[i] = is;

    for(;;) {
        ++i;
        if (i > izero) return;
        new[i] = irow[i];
    }
}

static double f9xact(int n, int ntot, int *ir, double *fact) {
/* -----------------------------------------------------------------------
  Name:	      F9XACT
  Purpose:    Computes the log of a multinomial coefficient.

  Arguments:
     N	    - Length of IR.					(Input)
     NTOT   - Number for factorial in numerator.		(Input)
     IR	    - Vector of length N containing the numbers for
              the denominator of the factorial.			(Input)
     FACT   - Table of log factorials.				(Input)
  Returns:
     	    - The log of the multinomal coefficient.		(Output)
  ----------------------------------------------------------------------- */
    double d = fact[ntot];
    for (int k = 0; k < n; k++)
        d -= fact[ir[k]];
    return d;
}


Rboolean f10act(int nrow, int *irow, int ncol, int *icol, double *val,
       double *fact, int *nd, int *ne, int *m) {
/* -----------------------------------------------------------------------
  Name:	    F10ACT
  Purpose:  Computes the shortest path length for special tables.

  Arguments:
     NROW   - The number of rows in the table.			(Input)
     IROW   - Vector of length NROW containing the row totals.	(Input)
     NCOL   - The number of columns in the table.  		(Input)
     ICO    - Vector of length NCOL containing the column totals.(Input)
     VAL    - The shortest path.  				(Input/Output)
     FACT   - Vector containing the logarithms of factorials.   (Input)
     ND	    - Workspace vector of length NROW.			(Input)
     NE	    - Workspace vector of length NCOL.			(Input)
     M	    - Workspace vector of length NCOL.			(Input)

  Returns (VAL and):
     XMIN   - Set to true if shortest path obtained.  		(Output)
  ----------------------------------------------------------------------- */
    int i, is, ix;

    for (i = 0; i < nrow - 1; ++i)
        nd[i] = 0;

    is = icol[0] / nrow;
    ix = icol[0] - nrow * is;
    ne[0] = is;
    m[0] = ix;
    if (ix != 0) ++nd[ix-1];

    for (i = 1; i < ncol; ++i) {
        ix = icol[i] / nrow;
        ne[i] = ix;
        is += ix;
        ix = icol[i] - nrow * ix;
        m[i] = ix;
        if (ix != 0) ++nd[ix-1];
    }

    for (i = nrow - 3; i >= 0; --i)
        nd[i] += nd[i + 1];

    ix = 0;
    for (i = nrow; i >= 2; --i) {
        ix += is + nd[nrow - i] - irow[i-1];
        if (ix < 0) return FALSE;
    }

    for (i = 0; i < ncol; ++i) {
        ix = ne[i];
        is = m[i];
        *val +=  is * fact[ix + 1] + (nrow - is) * fact[ix];
    }
    return TRUE;
}

void f11act(int *irow, int i1, int i2, int *new) {
/* -----------------------------------------------------------------------
  Name:	      F11ACT
  Purpose:    Routine for revising row totals.

  Arguments:
     IROW   - Vector containing the row totals.	(Input)
     I1	    - Indicator.			(Input)
     I2	    - Indicator.  			(Input)
     NEW    - Vector containing the row totals.	(Output)
  ----------------------------------------------------------------------- */
    int i;
    for (i = 0;  i < (i1 - 1); ++i)	new[i] = irow[i];
    for (i = i1; i <= i2; ++i)	      new[i-1] = irow[i];
}

static int iwork(int iwkmax, int *iwkpt, int number, int itype) {
/* -----------------------------------------------------------------------
  Name:	      iwork
  Purpose:    Routine for allocating workspace.

  Arguments:
     iwkmax - Maximum (int) amount of workspace.		(Input)
     iwkpt  - Amount of (int) workspace currently allocated.	(in/out)
     number - Number of elements of workspace desired.		(Input)
     itype  - Workspace type.					(Input)
	      ITYPE  TYPE
		2    integer
		3    float
		4    double
     iwork(): Index in rwrk, dwrk, or iwrk of the beginning of
              the first free element in the workspace array.	(Output)
  ----------------------------------------------------------------------- */
    int i = *iwkpt;
    if (itype == 2 || itype == 3)
        *iwkpt += number;
    else { /* double */
        if (i % 2 != 0) ++i;
        *iwkpt += (number << 1);
        i /= 2;
    }
    Apop_stopif(*iwkpt >iwkmax, has_error=true;return i, 0, "Out of workspace: %i > %i", *iwkpt, iwkmax);
    return i;
}

#ifndef USING_R

void isort(int *n, int *ix) {
/* -----------------------------------------------------------------------
  Name:	      ISORT
  Purpose:    Shell sort for an int vector.

  Arguments:
     N	    - Lenth of vector IX.	(Input)
     IX	    - Vector to be sorted.	(in/out)
  ----------------------------------------------------------------------- */
    static int ikey, i, j, m, il[10], kl, it, iu[10], ku;

    /* Parameter adjustments */
    --ix;

    /* Function Body */
    m = 1;
    i = 1;
    j = *n;

L10:
    if (i >= j) 
        goto L40;
    kl = i;
    ku = j;
    ikey = i;
    ++j;
    /* Find element in first half */
L20:
    ++i;
    if (i < j) 
        if (ix[ikey] > ix[i]) 
            goto L20;
    /* Find element in second half */
L30:
    --j;
    if (ix[j] > ix[ikey]) {
        goto L30;
    }
    /* Interchange */
    if (i < j) {
        it = ix[i];
        ix[i] = ix[j];
        ix[j] = it;
        goto L20;
    }
    it = ix[ikey];
    ix[ikey] = ix[j];
    ix[j] = it;
    /* Save upper and lower subscripts of the array yet to be sorted */
    if (m < 11) {
        if (j - kl < ku - j) {
            il[m - 1] = j + 1;
            iu[m - 1] = ku;
            i = kl;
            --j;
        } else {
            il[m - 1] = kl;
            iu[m - 1] = j - 1;
            i = j + 1;
            j = ku;
        }
        ++m;
        goto L10;
    } else Apop_stopif(1, return, 0, "This should never occur.");
    /* Use another segment */
L40:
    --m;
    if (m == 0) 
        return;
    i = il[m - 1];
    j = iu[m - 1];
    goto L10;
}

static double gammds(double *y, double *p, int *ifault) {
/* -----------------------------------------------------------------------
  Name:	      GAMMDS
  Purpose:    Cumulative distribution for the gamma distribution.
  Usage:      PGAMMA (Q, ALPHA,IFAULT)
  Arguments:
     Q	    - Value at which the distribution is desired.  (Input)
     ALPHA  - Parameter in the gamma distribution.  (Input)
     IFAULT - Error indicator.	(Output)
	       IFAULT  DEFINITION
		 0     No error
		 1     An argument is misspecified.
		 2     A numerical error has occurred.
     PGAMMA - The cdf for the gamma distribution with parameter alpha
	      evaluated at Q.  (Output)
  -----------------------------------------------------------------------

  Algorithm AS 147 APPL. Statist. (1980) VOL. 29, P. 113

  Computes the incomplete gamma integral for positive parameters Y, P
  using and infinite series.
  */

    static double a, c, f, g;

    /* Checks for the admissibility of arguments and value of F */
    *ifault = 1;
    g = 0.;
    if (*y <= 0. || *p <= 0.)
        return g;
    *ifault = 2;

    /*
      ALOGAM is natural log of gamma function no need to test ifail as
      an error is impossible

      BK edit: using gsl_sf_lngamma instead. It has more methods--> maybe slower; more precise.

      */

    a = *p + 1.;
    f = exp(*p * log(*y) - gsl_sf_lngamma(a) - *y);
    if (f == 0.) 
        return g;
    *ifault = 0;

    /* Series begins */
    c = 1.;
    g = 1.;
    a = *p;
    do {
        a += 1.;
        c *= (*y / a);
        g += c;
    } while (c > 1e-6 * g);

    g *= f;
    return g;
}

/** Convert from an \ref apop_data set to a table of integers.

Not too necessary, but I needed it for the Fisher exact test.
*/
static int *apop_data_to_int_array(apop_data *intab){
  int rowct = intab->matrix->size1,
      colct = intab->matrix->size2,
      *out  = malloc(sizeof(int)*(rowct* colct));
    for (int i=0; i< rowct; i++)
        for (int j=0; j< colct; j++)
            out[j*rowct + i] = (int) gsl_matrix_get(intab->matrix, i, j);
    return out;
}

/** Run the Fisher exact test on an input contingency table.

\return     An \ref apop_data set with two rows:<br>
    "probability of table": Probability of the observed table for fixed marginal totals.	<br>
    "p value":  Table p-value.	The probability of a more extreme table,
	      where `extreme' is in a probabilistic sense.

\li If there are processing errors, these values will be NaN.

\exception out->error=='p' Processing error in the test.

For example: 

\include test_fisher.c
*/
apop_data *apop_test_fisher_exact(apop_data *intab){
    double  prt, pre,
            expect  = -1,
            percent = 80,
            emin    = 1;
    int     *intified = apop_data_to_int_array(intab),
            workspace = 200000,
            mult      = 30,
            rowct     = intab->matrix->size1,
            colct     = intab->matrix->size2;
    has_error=0;
OMP_critical (fexact) //f3xact and f5exact use static vars for some state-keeping.
    fexact(&rowct, 
       &colct,
       intified,
       &rowct,
       // Cochran condition for asym.chisq. decision:
       &expect,
       &percent,
       &emin,
       &prt,
       &pre,
       &workspace,
       &mult);
    free(intified);
    apop_data *out = apop_data_alloc();
    Asprintf(&out->names->title, "Fisher Exact test");
    apop_data_add_named_elmt(out, "probability of table", prt);
    apop_data_add_named_elmt(out, "p value", pre);
    Apop_stopif(has_error, out->error='p'; return out, 0, "processing error; don't trust the results.");
    return out;
}
#endif /* not USING_R */