Newer
Older
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
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
/*--------------------------------------------------------------
This file contains six recursive C routines for calculating
the volume and derivatives of a convex polyhedron in n
dimensions. The routines volume, volumef, volumeb and dvda are
fortran callable interfaces to their C counterparts cvolume,
cvolumeb and cdvda.
ROUTINE COMMENT CALLS
cvolume calculates volume of region cvolume
which satisfies Ax <= b.
cvolumef calculates volume of region cvolumef
which satisfies Ax <= b.
(faster version, see below)
cvolumeb calculates volume and the cvolume
derivative d(vol)/db(0).
cdvda calculates derivatives cdvda, cvolumeb,
d(vol)/da(0,j) (j=1,n). & cvolumebj
cdvdaf calculates derivatives cdvdaf, cvolumeb,
d(vol)/da(0,j) (j=1,n). & cvolumebj
(faster version, see below)
cvolumebj calculates partial volumes
and derivatives d(vol)/d(b(j)
(j=1,n). (called by cdvda.)
Here b(j) is the jth element of vector b, and a(i,j) is the
(i,j)th coefficient of the matrix A corresponding to the
ith constraint and the jth dimension.
The two faster versions cvolumef and cdvdaf do not continue down
the recursive tree if b(j) = 0 for any j at any time. This avoids
extra work but restricts the applicability of the routines to
cases where the origin does not pass through any inconsistent
constraints.
Malcolm Sambridge, April 1995.
--------------------------------------------------------------*/
/*--------------------------------------------------------------
ROUTINE: cvolume
This routine calculates the `volume' in dimension n of the region
bounded by a set of m linear inequality constraints of the form
A x <= b, where a has m rows and n columns and is given by a(m,n),
b is the n-vector and is contained in b(m). The recursive formula
of Lasserre (1983) is used. Redundant constraints are allowed
If the inequality constraints are inconsistent then the volume
is returned as zero. If the original polyhedron is unbounded
then a warning is issued and the volume is return as -1.0
(even though it is undefined).
Jean Braun and Malcolm Sambridge, Jan 1995.
Lasserre, J. B., 1983. "An analytical expression and an Algorithm
for the Volume of a Convex Polyhedron in R^n.", JOTA, vol. 39,
no. 3, 363-377.
--------------------------------------------------------------*/
float cvolume (a,b,m,n,mmax,nmax)
int *n, *m, *mmax, *nmax;
float *a, *b;
{
float v,amax,pivot;
int i,j,t,k,l;
int jj,kk;
float *ai, *aj, *ajt, *apjj, *bi;
int kmm,tmm;
int nn,mm,nn_max,mm_max;
int nm1,mm1;
float *ap, *bp;
int firstmin,firstmax;
float bmin,bmax,bb;
float partialv;
nn= *n;
mm= *m;
nn_max= *nmax;
mm_max= *mmax;
/* one-dimensional case (full reduction) */
if (nn == 1)
{
firstmin=0;
firstmax=0;
for (l=0;l<mm;l++)
{
if ( *(a+l) > 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmin==0) {firstmin=1;bmin=bb;}
else if (bb<bmin) bmin=bb;
}
else if ( *(a+l) < 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmax==0) {firstmax=1;bmax=bb;}
else if (bb>bmax) bmax=bb;
}
else if ( *(b+l) < 0.) /* Constraints are inconsistent */
{
printf("Inconsistent constraints found after reduction to n = 1 \n");
return(0.);
}
}
v=0.;
if (firstmin*firstmax == 1) v=bmin-bmax;
else
{
/* printf("Volume is unbounded at 1-D; volume returned as -1\n");*/
return(-1.0);
}
if (v<0.) {v=0.;}
return(v);
}
nm1=nn-1;
mm1=mm-1;
v=0.;
for (i=0;i<mm;i++)
{
ai=a+i;
bi=b+i;
/* find largest pivot */
amax=0.;
for (j=0;j<nn;j++)
if (fabs( *(ai+j*mm_max)) >= amax) {amax= fabs( *(ai+j*mm_max)); t=j;}
tmm=t*mm_max;
pivot=*(ai+tmm);
/* finds contribution to v from this pivot (if not nil) */
if (amax == 0.)
{
/* Constraint is inconsistent */
if ( *(bi) < 0.)
{ printf("Constraint %d is inconsistent\n",i+1); return(0.); }
/* otherwise constraint is redundant */
printf("Constraint %d is redundant\n",i+1);
}
else
{
/* allocate memory */
ap = (float *) malloc((sizeof *a)*nm1*mm1);
bp = (float *) malloc((sizeof *b)*mm1);
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
/* reduce a and b into ap and bp eliminating variable t and constraint i */
jj=-1;
for (j=0;j<mm;j++)
if (j != i)
{
jj=jj+1;
aj=a+j;
ajt=aj+tmm;
*(bp+jj)= *(b+j) - *(bi) * *(ajt) / pivot;
apjj=ap+jj;
kk=-1;
for (k=0;k<nn;k++)
if (k != t)
{
kk=kk+1;
kmm=k*mm_max;
*(apjj+kk*mm1)= *(aj+kmm)- *(ajt) * *(ai+kmm)/ pivot;
}
}
/* add contribution to volume from volume calculated in smaller dimension */
partialv=cvolume(ap,bp, &mm1, &nm1, &mm1, &nm1);
if(partialv == -1.0)return(-1.0);
v=v+ *(bi)/amax*partialv/nn;
free(ap);
free(bp);
}
}
return(v);
}
/*--------------------------------------------------------------
ROUTINE: volume
A dummy routine used to call cvolume from a fortran routine
Jean Braun and Malcolm Sambridge, Jan 1995.
----------------------------------------------------------------*/
volume_(a,b,m,n,mmax,nmax,result)
int *n, *m, *mmax, *nmax;
float *a, *b;
float *result;
int *n, *m, *mmax, *nmax;
float *a, *b;
float *result;
{
*result=cvolume(a,b,m,n,mmax,nmax);
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
}
/*--------------------------------------------------------------
ROUTINE: cvolumeb
This routine calculates the `volume' in dimension n of the region
bounded by a set of m linear inequality constraints of the form
A x <= b, where a has m rows and n columns and is given by a(m,n),
b is the n-vector and is contained in b(m). The recursive formula
of Lasserre (1983) is used. Redundant constraints are allowed and
a warning is issued if any are encountered. If the inequality
constraints are inconsistent then the volume is returned as zero.
If the original polyhedron is unbounded then a warning is issued
and the volume is return as zero (even though it is undefined).
This version also calculates the derivative of the volume with
respect to the parameter b(0) using the simple formula of
Lasserre (1983). If *opt == 2 then only the derivative is
calculated and not the volume.
This routine is a variation from the routine `cvolume'.
Calls are made to routine cvolume.
Malcolm Sambridge, March 1995.
--------------------------------------------------------------*/
float cvolumeb (a,b,m,n,mmax,nmax,opt,dvdb)
int *n, *m, *opt, *mmax, *nmax;
float *a, *b;
float *dvdb;
{
float v,amax,pivot;
int i,j,t,k,l;
int jj,kk;
float *ai, *aj, *ajt, *apjj, *bi;
int kmm,tmm;
int nn,mm,nn_max,mm_max;
int nm1,mm1;
int lmin,lmax;
float *ap, *bp;
int firstmin,firstmax;
float bmin,bmax,bb;
float vol;
nn= *n;
mm= *m;
nn_max= *nmax;
mm_max= *mmax;
/* one-dimensional case (full reduction) */
if (nn == 1)
{
firstmin=0;
firstmax=0;
lmax=0;
lmin=0;
for (l=0;l<mm;l++)
{
if ( *(a+l) > 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmin==0) {firstmin=1;bmin=bb;lmin=l;}
else if (bb<bmin) {bmin=bb;lmin=l;}
}
else if ( *(a+l) < 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmax==0) {firstmax=1;bmax=bb;lmax=l;}
else if (bb>bmax) {bmax=bb;lmax=l;}
}
else if ( *(b+l) < 0.)
{
/* Constraints are inconsistent.
Set volume and derivative to zero. */
printf("Inconsistent constraints found after reduction to n = 1 \n");
*dvdb = 0.;
return(0.);
}
}
v=0.;
*dvdb = 0.;
if (firstmin*firstmax == 1) v=bmin-bmax;
else
{
printf("Volume is unbounded; volume returned as -1\n derivatives returned as zero\n");
return(-1.0);
}
if (v<0.) {v=0.;*dvdb=0.;}
else if (v>0. && lmin == 0) *dvdb = 1. / *a;
else if (v>0. && lmax == 0) *dvdb = -1. / *a;
return(v);
}
nm1=nn-1;
mm1=mm-1;
v=0.;
/* perform main loop over constraints */
for (i=0;i<mm;i++)
{
ai=a+i;
bi=b+i;
/* find largest pivot */
amax=0.;
for (j=0;j<nn;j++)
if (fabs( *(ai+j*mm_max)) >= amax) {amax= fabs( *(ai+j*mm_max)); t=j;}
tmm=t*mm_max;
pivot=*(ai+tmm);
/* finds contribution to v from this pivot (if not nil) */
if (amax == 0.)
{
/* Constraint is inconsistent */
if ( *(bi) < 0.)
{
printf("Constraint %d is inconsistent\n",i+1);
return(0.);
}
/* otherwise constraint is redundant */
printf("Constraint %d is redundant\n",i+1);
}
else
{
/* allocate memory */
ap = (float *) malloc(4*nm1*mm1);
bp = (float *) malloc(4*mm1);
/* reduce a and b into ap and bp eliminating variable t and constraint i */
jj=-1;
for (j=0;j<mm;j++)
if (j != i)
{
jj=jj+1;
aj=a+j;
ajt=aj+tmm;
*(bp+jj)= *(b+j) - *(bi) * *(ajt) / pivot;
apjj=ap+jj;
kk=-1;
for (k=0;k<nn;k++)
if (k != t)
{
kk=kk+1;
kmm=k*mm_max;
*(apjj+kk*mm1)= *(aj+kmm)- *(ajt) * *(ai+kmm)/ pivot;
}
}
/* add contribution to volume from volume calculated in smaller dimension */
vol=cvolume(ap,bp, &mm1, &nm1, &mm1, &nm1);
if(vol == -1.0)
{
*dvdb = 0.;
return(-1.0);
}
v=v+ *(bi)/amax*vol/nn;
free(ap);
free(bp);
/* calculate derivatives for first constraint only */
/* calculate volume and derivative
with respect to b_0 */
if (i == 0) *dvdb = vol/amax;
/* calculate derivative with respect
to b_0 but not volume */
if (*opt == 2) return (0.);
}
}
return(v);
}
/*--------------------------------------------------------------
ROUTINE: volumeb
A dummy routine used to call cvolumeb from a fortran routine
----------------------------------------------------------------*/
volumeb_ (a,b,m,n,mmax,nmax,opt,volume,dvdb)
int *n, *m, *mmax, *nmax, *opt;
float *a, *b;
float *volume;
float *dvdb;
{
volumeb(a,b,m,n,mmax,nmax,opt,volume,dvdb);
}
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
int *n, *m, *mmax, *nmax, *opt;
float *a, *b;
float *volume;
float *dvdb;
{
if(*opt == 1) /* calculate volume and derivative with
respect to b(0) */
{
*volume=cvolumeb(a,b,m,n,mmax,nmax,opt,dvdb);
}
else if(*opt == 2) /* calculate derivative with respect to b(0)
but not volume */
{
*volume=cvolumeb(a,b,m,n,mmax,nmax,opt,dvdb);
}
else
{
printf(" Warning: routine volumeb called with an invalid option parameter\n");
printf(" valid options are 1 and 2, option given = %d \n",*opt);
}
}
/*--------------------------------------------------------------
ROUTINE: cvolumebj
This routine only calculates the j-th outer loop of the recursive
routine cvolumeb. It returns the partial volume for projection
onto the j-th constraint and the derivative of the total volume
with respect to parameter b(j) using the simple formula of
Lasserre(1983). (See cvolumeb for more details.)
The reason for this routine is so that the derivatives
with respect to parameter b(j) can be calculated and passed back
to a fortran routine without creating and an extra array of size
m (because only one derivative is calculated per call). In
this way it also avoids recalculating the total volume m times
(which would be the case if we used a simple variation of routine
cvolumeb).
To calculate the total volume this routine must be called m times
and the partial volume summed
Constraint j is determined by the value of *con.
Calls are made to routine cvolume.
Malcolm Sambridge, March 1995.
--------------------------------------------------------------*/
float cvolumebj (a,b,m,n,mmax,nmax,con,dvdb)
int *n, *m, *mmax, *nmax, con;
float *a, *b;
float *dvdb;
{
float v,amax,pivot;
int i,j,t,k,l;
int jj,kk;
float *ai, *aj, *ajt, *apjj, *bi;
int kmm,tmm;
int nn,mm,nn_max,mm_max;
int nm1,mm1;
int lmin,lmax;
float *ap, *bp;
int firstmin,firstmax;
float bmin,bmax,bb;
float vol;
nn= *n;
mm= *m;
nn_max= *nmax;
mm_max= *mmax;
/* one-dimensional case (full reduction) */
if (nn == 1)
{
firstmin=0;
firstmax=0;
lmax=0;
lmin=0;
for (l=0;l<mm;l++)
{
if ( *(a+l) > 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmin==0) {firstmin=1;bmin=bb;lmin=l;}
else if (bb<bmin) {bmin=bb;lmin=l;}
}
else if ( *(a+l) < 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmax==0) {firstmax=1;bmax=bb;lmax=l;}
else if (bb>bmax) {bmax=bb;lmax=l;}
}
else if ( *(b+l) < 0.)
{
/* Constraints are inconsistent.
Set volume and derivative to zero. */
printf("Inconsistent constraints found after reduction to n = 1 \n");
*dvdb = 0.;
return(0.);
}
}
v=0.;
*dvdb = 0.;
if (firstmin*firstmax == 1) v=bmin-bmax;
else
{
printf("Volume is unbounded; volume returned as -1\n derivatives returned as zero\n");
return(-1.0);
}
if (v<0.) {v=0.;*dvdb=0.;}
else if (v>0. && lmin == con) *dvdb = 1. / *(a+lmin);
else if (v>0. && lmax == con) *dvdb = -1. / *(a+lmax);
return(v);
}
nm1=nn-1;
mm1=mm-1;
v=0.;
/* perform main loop over constraints */
/* for (i=0;i<mm;i++) */
i = con;
ai=a+i;
bi=b+i;
/* find largest pivot */
amax=0.;
for (j=0;j<nn;j++)
if (fabs( *(ai+j*mm_max)) >= amax) {amax= fabs( *(ai+j*mm_max)); t=j;}
tmm=t*mm_max;
pivot=*(ai+tmm);
/* finds contribution to v from this pivot (if not nil) */
if (amax == 0.)
{
/* Constraint is inconsistent */
if ( *(bi) < 0.)
{
printf("Constraint %d is inconsistent\n",i+1);
*dvdb = 0.;
return(0.);
}
/* otherwise constraint is redundant */
printf("Constraint %d is redundant\n",i+1);
*dvdb = 0.;
}
else
{
/* allocate memory */
ap = (float *) malloc(4*nm1*mm1);
bp = (float *) malloc(4*mm1);
/* reduce a and b into ap and bp eliminating variable t and constraint i */
jj=-1;
for (j=0;j<mm;j++)
if (j != i)
{
jj=jj+1;
aj=a+j;
ajt=aj+tmm;
*(bp+jj)= *(b+j) - *(bi) * *(ajt) / pivot;
apjj=ap+jj;
kk=-1;
for (k=0;k<nn;k++)
if (k != t)
{
kk=kk+1;
kmm=k*mm_max;
*(apjj+kk*mm1)= *(aj+kmm)- *(ajt) * *(ai+kmm)/ pivot;
}
}
/* calculate partial volume */
vol=cvolume(ap,bp, &mm1, &nm1, &mm1, &nm1);
if(vol == -1.0)
{
*dvdb = 0.;
return(-1.0);
}
v=*(bi)/amax*vol/nn;
/* calculate derivative of total volume
with respect to current constraint */
*dvdb = vol/amax;
free(ap);
free(bp);
}
return(v);
}
/*--------------------------------------------------------------
ROUTINE: volumebj
A dummy routine used to call cvolumebj from a fortran routine
----------------------------------------------------------------*/
volumebj_ (a,b,m,n,mmax,nmax,con,volume,dvdb)
int *n, *m, *mmax, *nmax,*con;
float *a, *b;
float *volume;
float *dvdb;
{
volumebj(a,b,m,n,mmax,nmax,con,volume,dvdb);
}
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
int *n, *m, *mmax, *nmax,*con;
float *a, *b;
float *volume;
float *dvdb;
{
int tcon;
tcon = *con - 1;
/* calculate partial volume and
derivative with respect to b(con) */
*volume=cvolumebj(a,b,m,n,mmax,nmax,tcon,dvdb);
}
/*--------------------------------------------------------------
ROUTINE: cdvda
This routine calculates the derivative with respect to a(0,tdim)
of the `volume' in dimension n of the region bounded by a set
of m linear inequality constraints of the form A x <= b, where
a has m rows and n columns and is given by a(m,n), b is the
n-vector and is contained in b(m). The derivative expression
is recursive and derived from the formula of Lasserre (1983).
Redundant constraints are allowed and a warning is issued if any are
encountered. If the inequality constraints are inconsistent then the
derivative is returned as zero. If any constraint is orthogonal to
the component a(0,idim) then the reduction can only take place onto
variable idim. A special case is used to handle this which involves
no further recursive calls.
If the original polyhedron is unbounded then a warning is issued
and the derivative is return as zero.
Note: This code takes advantage of the fact that during recursive calls
constraint 0 does not change its position in the list of remaining
constraints (if it has not been eliminated), i.e. it is always the
first constraint. This would not be the case if the algorithm were
adapated to deal with other constraints, i.e. evaluate dvda_i,j
where i .ne. 0.
Calls itself cvolumeb, and cvolumebj.
Malcolm Sambridge, March 1995.
--------------------------------------------------------------*/
float cdvda (a,b,m,n,mmax,nmax,tdim,temp,jval,code)
int *n, *m, *nmax, *mmax, *tdim, *jval, *code;
float *a, *b, *temp;
{
float v,amax,pivot;
int i,j,t,k,l;
int jj,kk;
float *ai, *aj, *ajt, *apjj, *bi;
int kmm,tmm;
int nn,mm,nn_max,mm_max,ttdim;
int nm1,mm1;
int lmin,lmax, jjval, kval;
float *ap, *bp, *ttemp;
int firstmin,firstmax;
float bmin,bmax,bb;
float deriv, junk, vol, dvdb, dbda;
int special, opt;
nn= *n;
mm= *m;
nn_max= *nmax;
mm_max= *mmax;
/* one-dimensional case (full reduction) */
*code = 0;
if (nn == 1)
{
firstmin=0;
firstmax=0;
lmax=0;
lmin=0;
for (l=0;l<mm;l++)
{
if ( *(a+l) > 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmin==0) {firstmin=1;bmin=bb;lmin=l;}
else if (bb<bmin) {bmin=bb;lmin=l;}
}
else if ( *(a+l) < 0.)
{
bb= *(b+l)/ *(a+l);
if (firstmax==0) {firstmax=1;bmax=bb;lmax=l;}
else if (bb>bmax) {bmax=bb;lmax=l;}
}
else if ( *(b+l) < 0.)
{
/* Constraint is inconsistent.
Set derivative to zero. */
printf("Inconsistent constraints found after reduction to n = 1 \n");
*code = 1;
return(0.);
}
}
v=0.;
if (firstmin*firstmax == 1) v=bmin-bmax;
else
{
printf("Volume is unbounded; derivative returned is zero\n");
*code = -1;
return(0.);
}
if (v<0.) return(0.);
if(*jval == 1) /* Constraint 0 has not yet been encountered */
{
if (lmin == 0) deriv = -bmin/ *a;
else if (lmax == 0) deriv = bmax/ *a;
else deriv = 0.;
return(deriv);
}
else if(*jval == 0) /* Constraint 0 has already been encountered */
{
deriv = ( *(temp+lmax) * (bmax/ *(a+lmax)) ) -
( *(temp+lmin) * (bmin/ *(a+lmin)) );
return(deriv);
}
}
nm1=nn-1;
mm1=mm-1;
v=0.;
/* perform main loop over constraints */
for (i=0;i<mm;i++)
{
ai=a+i;
bi=b+i;
ttdim = *tdim;
special = 0;
/* find largest pivot */
amax=0.;
t = 0;
for (j=0;j<nn;j++)
if (fabs( *(ai+j*mm_max)) >= amax && j != ttdim)
{amax= fabs( *(ai+j*mm_max)); t=j;}
/* finds contribution to v from
this pivot (if not nil) */
if (amax == 0.)
{
if(*(ai + ttdim * mm_max) == 0.0)
{
/* Constraint is inconsistent */
if ( *(bi) < 0.)
{
printf("Constraint %d is inconsistent\n",i+1);
*code = 1;
return(0.);
}
/* otherwise constraint is redundant */
printf("Constraint %d is redundant\n",i+1);
}
else
{
/* if projection can only be peformed
on dimension tdim then activate
special case */
special = 1;
t = ttdim;
amax = fabs(*(ai+t * mm_max));
}
}
tmm=t*mm_max;
pivot=*(ai+tmm);
if(t < ttdim) ttdim = ttdim -1;
if (amax != 0)
{
/* determine if constraint 0 has been encountered */
kval = 0;
if ( i == 0 && *jval == 1)
{
/* This is the first encounter of
constraint 0 on this path so we
allocate memory and store parameters
to be used when n = 1 */
if (special == 0)
{
ttemp = (float *) malloc(4*mm1);
for (j=0;j<mm1;j++) *(ttemp+j) = - *(a+j+1+tmm)/pivot;
kval = 1;
}
jjval = 0;
}
else if (*jval == 0) /* Constraint 0 has already been
encountered */
{
jjval = 0;
/* perform recursive update of component
derivative array temp. This eliminates
row i and copies into a new vector */
if(special == 0)
{
ttemp = (float *) malloc(4*mm1);
for (j=0;j<i;j++) *(ttemp+j) = *(temp+j)
-(*(temp+i) * *(a+j+tmm)/pivot);
for (j=i;j<mm1;j++) *(ttemp+j) = *(temp+j+1)
-(*(temp+i) * *(a+j+1+tmm)/pivot);
}
}
else
{ /* Constraint 0 has not yet been
encountered */
jjval = 1;
}
/* allocate memory */
ap = (float *) malloc(4*nm1*mm1);
bp = (float *) malloc(4*mm1);
/* reduce a and b into ap and bp eliminating variable t and constraint i */
jj=-1;
for (j=0;j<mm;j++)
if (j != i)
{
jj=jj+1;
aj=a+j;
ajt=aj+tmm;
*(bp+jj)= *(b+j) - *(bi) * *(ajt) / pivot;
apjj=ap+jj;
kk=-1;
for (k=0;k<nn;k++)
if (k != t)
{
kk=kk+1;
kmm=k*mm_max;
*(apjj+kk*mm1)= *(aj+kmm)- *(ajt) * *(ai+kmm)/ pivot;
}
}
/* add contribution to derivative from that calculated in smaller dimension */
/* Normal case method */
if(special == 0)
{
deriv=cdvda(ap,bp, &mm1, &nm1, &mm1, &nm1, &ttdim, ttemp, &jjval,code);
v=v+ *(bi)/amax*deriv/nn;
if (kval == 1 || *jval == 0) free(ttemp);
if(*code != 0)return (0.);
}
else /* Use special case method */
{
if( *jval == 1)
{
if(i == 0) /* This is constraint 0 */
{
deriv = 0.;
vol = 0.;
dvdb = 0.;
for (j=1;j<mm;j++)
{
k = j - 1;
junk=cvolumebj(ap,bp,&mm1,&nm1,&mm1,&nm1,k,&dvdb);
if(junk == -1.)
{
*code = -1;
return(0.);
}
deriv = deriv + dvdb * *(a + j + tmm) ;
vol = vol + junk;
}
if(nm1 == 1)vol = junk;
deriv = *(bi) * deriv/pivot;
deriv = (deriv - vol) /pivot;
v=v+ *(bi)/amax*deriv/nn;
}
else /* Constraint 0 not yet encountered */
{
opt = 2;
junk=cvolumeb(ap,bp,&mm1,&nm1,&mm1,&nm1,&opt,&deriv);
if(junk == -1.)
{
*code = -1;
return(0.);
}
deriv= -(deriv * *(bi)/pivot);
v=v+ *(bi)/amax*deriv/nn;