malt-wr
dsimplx.c File Reference
#include <math.h>
#include "qnrutil.h"
Include dependency graph for dsimplx.c:

Go to the source code of this file.

Macros

#define EPS1   1.0e-9
 
#define EPS2   1.0e-9
 
#define FREEALL   free_ivector(l2,1,m); free_ivector(l1,1,n+1);
 

Functions

int simplx (double **a, int m, int n, int *izrov, int *iposv)
 
void simp1 (double **a, int mm, int *ll, int nll, int iabf, int *kp, double *bmax)
 
void simp2 (double **a, int n, int *l2, int nl2, int *ip, int kp, double *q1)
 
void simp3 (double **a, int i1, int k1, int ip, int kp)
 

Macro Definition Documentation

#define EPS1   1.0e-9

Definition at line 7 of file dsimplx.c.

#define EPS2   1.0e-9

Definition at line 8 of file dsimplx.c.

#define FREEALL   free_ivector(l2,1,m); free_ivector(l1,1,n+1);

Definition at line 9 of file dsimplx.c.

Function Documentation

void simp1 ( double **  a,
int  mm,
int *  ll,
int  nll,
int  iabf,
int *  kp,
double *  bmax 
)

Definition at line 87 of file dsimplx.c.

88 {
89  int k;
90  double test;
91 
92  *kp=ll[1];
93  *bmax=a[mm+1][*kp+1];
94  for (k=2;k<=nll;k++) {
95  if (iabf == 0)
96  test=a[mm+1][ll[k]+1]-(*bmax);
97  else
98  test=fabs(a[mm+1][ll[k]+1])-fabs(*bmax);
99  if (test > 0.0) {
100  *bmax=a[mm+1][ll[k]+1];
101  *kp=ll[k];
102  }
103  }
104 }
void simp2 ( double **  a,
int  n,
int *  l2,
int  nl2,
int *  ip,
int  kp,
double *  q1 
)

Definition at line 106 of file dsimplx.c.

107 {
108  int k,ii,i;
109  double qp,q0,q;
110 
111  *ip=0;
112  for (i=1;i<=nl2;i++) {
113  if (a[l2[i]+1][kp+1] < -EPS2) {
114  *q1 = -a[l2[i]+1][1]/a[l2[i]+1][kp+1];
115  *ip=l2[i];
116  for (i=i+1;i<=nl2;i++) {
117  ii=l2[i];
118  if (a[ii+1][kp+1] < -EPS2) {
119  q = -a[ii+1][1]/a[ii+1][kp+1];
120  if (q < *q1) {
121  *ip=ii;
122  *q1=q;
123  } else if (q == *q1) {
124  for (k=1;k<=n;k++) {
125  qp = -a[*ip+1][k+1]/a[*ip+1][kp+1];
126  q0 = -a[ii+1][k+1]/a[ii+1][kp+1];
127  if (q0 != qp) break;
128  }
129  if (q0 < qp) *ip=ii;
130  }
131  }
132  }
133  }
134  }
135 }
#define EPS2
Definition: dsimplx.c:8
void simp3 ( double **  a,
int  i1,
int  k1,
int  ip,
int  kp 
)

Definition at line 137 of file dsimplx.c.

138 {
139  int kk,ii;
140  double piv;
141 
142  piv=1.0/a[ip+1][kp+1];
143  for (ii=1;ii<=i1+1;ii++)
144  if (ii-1 != ip) {
145  a[ii][kp+1] *= piv;
146  for (kk=1;kk<=k1+1;kk++)
147  if (kk-1 != kp)
148  a[ii][kk] -= a[ip+1][kk]*a[ii][kp+1];
149  }
150  for (kk=1;kk<=k1+1;kk++)
151  if (kk-1 != kp) a[ip+1][kk] *= -piv;
152  a[ip+1][kp+1]=piv;
153 }
int simplx ( double **  a,
int  m,
int  n,
int *  izrov,
int *  iposv 
)

Definition at line 11 of file dsimplx.c.

12 {
13  int i,ip,ir,is,k,kh,kp,m12,nl1,nl2;
14  int *l1,*l2;
15  double q1,bmax;
16  void simp1(),simp2(),simp3();
17 
18  l1=ivector(1,n+1);
19  l2=ivector(1,m);
20  nl1=n;
21  for (k=1;k<=n;k++) l1[k]=izrov[k]=k;
22  nl2=m;
23  for (i=1;i<=m;i++) {
24  if (a[i+1][1] < 0.0) nrerror("Bad input tableau in SIMPLX");
25  l2[i]=i;
26  iposv[i]=n+i;
27  }
28 /* stage one */
29  ir=1;
30  for (k=1;k<=(n+1);k++) {
31  q1=0.0;
32  for (i=1;i<=m;i++) q1 += a[i+1][k];
33  a[m+2][k] = -q1;
34  }
35  do {
36  simp1(a,m+1,l1,nl1,0,&kp,&bmax);
37  if (bmax <= EPS1 && a[m+2][1] < -EPS1) {
38  FREEALL
39  return -1;
40  } else if (bmax <= EPS1 && a[m+2][1] <= EPS1) {
41  for (ip=1;ip<=m;ip++) {
42  if (iposv[ip] == (ip+n)) {
43  simp1(a,ip,l1,nl1,1,&kp,&bmax);
44  if (bmax > 0.0) goto one;
45  }
46  }
47  ir=0;
48  break;
49  }
50  simp2(a,n,l2,nl2,&ip,kp,&q1);
51  if (ip == 0) {
52  FREEALL
53  return -1;
54  }
55 one: simp3(a,m+1,n,ip,kp);
56  if (iposv[ip] >= (n+1)) {
57  for (k=1;k<=nl1;k++)
58  if (l1[k] == kp) break;
59  --nl1;
60  for (is=k;is<=nl1;is++) l1[is]=l1[is+1];
61  a[m+2][kp+1] += 1.0;
62  for (i=1;i<=m+2;i++) a[i][kp+1] = -a[i][kp+1];
63  }
64  is=izrov[kp];
65  izrov[kp]=iposv[ip];
66  iposv[ip]=is;
67  } while (ir);
68 /* phase two */
69  for (;;) {
70  simp1(a,0,l1,nl1,0,&kp,&bmax);
71  if (bmax <= 0.0) {
72  FREEALL
73  return 0;
74  }
75  simp2(a,n,l2,nl2,&ip,kp,&q1);
76  if (ip == 0) {
77  FREEALL
78  return 1;
79  }
80  simp3(a,m,n,ip,kp);
81  is=izrov[kp];
82  izrov[kp]=iposv[ip];
83  iposv[ip]=is;
84  }
85 }
int * ivector(int nl, int nh)
Definition: qnrutil.c:8
void nrerror(char *error_text)
Definition: lineread.c:7
void simp2(double **a, int n, int *l2, int nl2, int *ip, int kp, double *q1)
Definition: dsimplx.c:106
void simp1(double **a, int mm, int *ll, int nll, int iabf, int *kp, double *bmax)
Definition: dsimplx.c:87
#define EPS1
Definition: dsimplx.c:7
#define FREEALL
Definition: dsimplx.c:9
void simp3(double **a, int i1, int k1, int ip, int kp)
Definition: dsimplx.c:137