Changeset c7f3b7 in git


Ignore:
Timestamp:
Dec 3, 1999, 12:03:52 AM (24 years ago)
Author:
Moritz Wenk <wenk@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
9f3e9bef10a5b0f7fd4ed74bc83407329b62b4a6
Parents:
2983b3377c3bd9abf59dd57780cedc5d940ca219
Message:
wenk: added new commands: simplex, convhull (mit Greuel abgesprochen)


git-svn-id: file:///usr/local/Singular/svn/trunk@3951 2c84dea3-7e68-4137-9b89-c4e89433aadc
Location:
Singular
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • Singular/LIB/solve.lib

    r2983b3 rc7f3b7  
    11///////////////////////////////////////////////////////////////////////////////
    22
    3 version="$Id: solve.lib,v 1.13 1999-11-14 21:35:02 wenk Exp $";
     3version="$Id: solve.lib,v 1.14 1999-12-02 23:03:52 wenk Exp $";
    44info="
    55LIBRARY: solve.lib     PROCEDURES TO SOLVE POLYNOMIAL SYSTEMS
     
    6262        {
    6363            ERROR("Third parameter l must be positive!");
    64         }
    65     }
     64        }    }
    6665    if ( size(#) >= 3 )
    6766    {
     
    883882}
    884883
    885 
    886884///////////////////////////////////////////////////////////////////////////////
    887885
     
    946944}
    947945
     946///////////////////////////////////////////////////////////////////////////////
     947
     948proc simplexOut(list l)
     949"USAGE:   simpelxOut(l); l list
     950ASSUME:
     951RETURN:
     952EXAMPLE: example simplexOut; shows an example
     953"
     954{
     955  int i,j;
     956  matrix m= l[1];
     957  intvec iposv= l[3];
     958  int icase= l[2];
     959
     960  int cols= ncols(m);
     961  int rows= nrows(m);
     962
     963  int N= l[6];
     964
     965  if ( -1 == icase )  // objective function is unbound
     966  {
     967    "objective function is unbound";
     968    return;
     969  }
     970  if ( 1 == icase )  // no solution satisfies the given constraints
     971  {
     972    "no solution satisfies the given constraints";
     973    return;
     974  }
     975  if ( -2 == icase )  // other error
     976  {
     977    "an error occurred during simplex computation!";
     978    return;
     979  }
     980
     981  for ( i = 1; i <= rows; i++ )
     982  {
     983    if (i == 1)
     984    {
     985      "z = "+string(m[1][1]);
     986    }
     987    else
     988    {
     989      if ( iposv[i-1] <= N+1 )
     990      {
     991        "x"+string(i-1)+" = "+string(m[1][iposv[i-1]]);
     992      }
     993//        else
     994//        {
     995//           "Y"; iposv[i-1]-N+1;
     996//        }
     997    }
     998  }
     999}
     1000example
     1001{
     1002    "EXAMPLE:";echo=2;
     1003    ring r = (real,10),(x),lp;
     1004
     1005    // suppose we have the
     1006   
     1007    matrix sm[5][5]=(  0, 1, 1, 3,-0.5,
     1008                     740,-1, 0,-2, 0,
     1009                       0, 0,-2, 0, 7,
     1010                     0.5, 0,-1, 1,-2,
     1011                       9,-1,-1,-1,-1);
     1012
     1013    // simplex input:
     1014    // 1: matrix
     1015    // 2: number of variables
     1016    // 3: total number of constraints (#4+#5+#6)
     1017    // 4: number of <= constraints
     1018    // 5: number of >= constraints
     1019    // 6: number of == constraints
     1020
     1021    list sol= simplex(sm, 4, 4, 2, 1, 1);
     1022    simplexOut(sol);
     1023}
     1024
     1025///////////////////////////////////////////////////////////////////////////////
    9481026
    9491027// local Variables: ***
  • Singular/iparith.cc

    r2983b3 rc7f3b7  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: iparith.cc,v 1.193 1999-12-01 13:22:52 Singular Exp $ */
     4/* $Id: iparith.cc,v 1.194 1999-12-02 23:03:48 wenk Exp $ */
    55
    66/*
     
    138138  { "continue",    0, CONTINUE_CMD ,      CONTINUE_CMD},
    139139  { "contract",    0, CONTRACT_CMD ,      CMD_2},
     140  { "convhull",    0, NEWTONPOLY_CMD,     CMD_1},
    140141  { "dbprint",     0, DBPRINT_CMD ,       CMD_M},
    141142  { "def",         0, DEF_CMD ,           ROOT_DECL},
     
    278279  { "rvar",        0, IS_RINGVAR ,        CMD_1},
    279280  { "setring",     0, SETRING_CMD ,       SETRING_CMD},
     281  { "simplex",     0, SIMPLEX_CMD,        CMD_M},
    280282  { "simplify",    0, SIMPLIFY_CMD ,      CMD_2},
    281283  { "size",        0, COUNT_CMD ,         CMD_1},
     
    36123614,{kWeight,      WEIGHT_CMD,      INTVEC_CMD,     MODUL_CMD }
    36133615,{jjLOAD1,      LOAD_CMD,        NONE,           STRING_CMD }
     3616,{loNewtonP,    NEWTONPOLY_CMD,  IDEAL_CMD,      IDEAL_CMD}
    36143617,{NULL,         0,               0,              0}
    36153618};
     
    49134916,{jjUNLOAD,    UNLOAD_CMD,      NONE,               -2 }
    49144917#endif /* HAVE_NAMESPACES */
     4918,{loSimplex,   SIMPLEX_CMD,     LIST_CMD,            6 }
    49154919,{nuUResSolve, URSOLVE_CMD,     LIST_CMD,            4 }
    49164920,{NULL,        0,               0,                   0 }
  • Singular/mpr_base.cc

    r2983b3 rc7f3b7  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: mpr_base.cc,v 1.15 1999-11-23 17:58:47 Singular Exp $ */
     4/* $Id: mpr_base.cc,v 1.16 1999-12-02 23:03:49 wenk Exp $ */
    55
    66/*
     
    99 */
    1010
     11#include <math.h>
    1112#include <limits.h>
    12 #include <math.h>
    1313
    1414#include "mod2.h"
     
    2626#include "numbers.h"
    2727#include "longalg.h"
    28 #include "tok.h"
    2928#ifdef HAVE_FACTORY
    3029#include "clapsing.h"
     
    4342#define FreeSizeOf(X,Y) Free(X,sizeof(Y))
    4443#endif
    45 
    4644//<-
    4745
     
    7068//-> sparse resultant matrix
    7169
    72 //-> typedefs and structs
    73 
    7470/* set of points */
    7571class pointSet;
    7672
    77 /* Linear Program stuff */
    78 struct linProg
    79 {
    80   mprfloat **LiPM;
    81   int *izrov, *iposv;
    82   int LiPM_cols,LiPM_rows;
    83 };
    84 
    85 typedef linProg *linProgP;
     73
    8674
    8775/* sparse resultant matrix class */
     
    144132  ideal rmat;        // sparse matrix representation
    145133
    146   linProg LP;
     134  simplex * LP;      // linear programming stuff
    147135};
    148136//<-
    149137
     138//-> typedefs and structs
     139poly monomAt( poly p, int i );
    150140
    151141typedef unsigned int Coord_t;
     
    270260{
    271261public:
    272   convexHull( linProgP _pLP ) : pLP(_pLP) {}
     262  convexHull( simplex * _pLP ) : pLP(_pLP) {}
    273263  ~convexHull() {}
    274264
     
    277267   * Returns Q[].
    278268   */
    279    pointSet ** newtonPolytopes( ideal gls );
     269  pointSet ** newtonPolytopesP( const ideal gls );
     270  ideal newtonPolytopesI( const ideal gls );
    280271
    281272private:
     
    288279  pointSet **Q;
    289280  int n;
    290   linProgP pLP;
     281  simplex * pLP;
    291282};
    292283//<-
     
    297288{
    298289public:
    299   mayanPyramidAlg( linProgP _pLP ) : n(pVariables), pLP(_pLP) {}
     290  mayanPyramidAlg( simplex * _pLP ) : n(pVariables), pLP(_pLP) {}
    300291  ~mayanPyramidAlg() {}
    301292
     
    342333  Coord_t acoords[MAXVARS+2];
    343334
    344   linProgP pLP;
     335  simplex * pLP;
    345336};
    346 
    347 poly monomAt( poly p, int i );
     337//<-
    348338
    349339#ifndef ASO_GENERATE
    350 //<-
    351340
    352341//-> debug output stuff
     
    441430  for ( i= 0; i <= max; i++ )
    442431  {
    443     points[i]= (onePointP)AllocSizeOf( onePoint );
     432    points[i]= (onePointP)Alloc( sizeof(onePoint) );
    444433    points[i]->point= (Coord_t *)Alloc0( (dim+2) * sizeof(Coord_t) );
    445434  }
     
    454443  {
    455444    Free( (ADDRESS) points[i]->point, fdim * sizeof(Coord_t) );
    456     FreeSizeOf( (ADDRESS) points[i], onePoint );
     445    Free( (ADDRESS) points[i], sizeof(onePoint) );
    457446  }
    458447  Free( (ADDRESS) points, (max+1) * sizeof(onePointP) );
     
    476465    for ( i= max+1; i <= max*2; i++ )
    477466    {
    478       points[i]= (onePointP)AllocSizeOf( onePoint );
     467      points[i]= (onePointP)Alloc( sizeof(struct onePoint) );
    479468      points[i]->point= (Coord_t *)Alloc0( fdim * sizeof(Coord_t) );
    480469    }
     
    720709  }
    721710
    722 #ifdef mprDEBUG_PROT
     711#ifdef mprDEBUG_ALL
    723712  Print(" lift vector: ");
    724713  for ( j=1; j < dim; j++ ) Print(" %d ",l[j] );
     
    754743bool convexHull::inHull(poly p, poly pointPoly, int m, int site)
    755744{
    756   int i, j, col, icase;
    757   int numcons;                // num of constraints
    758   int numpts;                // num of pts in defining support
    759   int numcols;                // tot number of cols
    760 
    761   numcons = n+1;
    762   numpts = m-1;
    763   numcols = numpts+1;                // this includes col of cts
    764 
    765   pLP->LiPM[1][1] = +0.0;  pLP->LiPM[1][2] = +1.0;        // optimize (arbitrary) var
    766   pLP->LiPM[2][1] = +1.0;  pLP->LiPM[2][2] = -1.0;         // lambda vars sum up to 1
    767   for ( j=3; j<=numcols; j++)
    768   {
    769     pLP->LiPM[1][j] = +0.0; pLP->LiPM[2][j] = -1.0;
     745  int i, j, col;
     746
     747  pLP->m = n+1;
     748  pLP->n = m;                // this includes col of cts
     749
     750  pLP->LiPM[1][1] = +0.0; 
     751  pLP->LiPM[1][2] = +1.0;        // optimize (arbitrary) var
     752  pLP->LiPM[2][1] = +1.0; 
     753  pLP->LiPM[2][2] = -1.0;         // lambda vars sum up to 1
     754
     755  for ( j=3; j <= pLP->n; j++)
     756  {
     757    pLP->LiPM[1][j] = +0.0;
     758    pLP->LiPM[2][j] = -1.0;
    770759  }
    771760
     
    785774#ifdef mprDEBUG_ALL
    786775  Print("Matrix of Linear Programming\n");
    787   print_mat( pLP->LiPM, numcons+1, numcols);
    788 #endif
    789 #if 1
    790   if ( numcons + 1 > pLP->LiPM_rows )
    791     WerrorS("convexHull::inHull: #rows > #pLP->LiPM_rows!");
    792   if ( numcols + 1 > pLP->LiPM_cols )
    793     WerrorS("convexHull::inHull: #cols > #pLP->LiPM_cols!");
    794 #endif
    795 
    796   simplx( pLP->LiPM, numcons, numcols-1, 0, 0, numcons, &icase, pLP->izrov, pLP->iposv);
    797 
    798   return (icase == 0);
     776  print_mat( pLP->LiPM, pLP->m+1,pLP->n);
     777#endif
     778
     779  pLP->m3= pLP->m;
     780
     781  pLP->compute();
     782
     783  return (pLP->icase == 0);
    799784}
    800785
     
    802787// ST_SPARSE_VADD: new vertex of convex hull added
    803788// ST_SPARSE_VREJ: point rejected (-> inside hull)
    804 pointSet ** convexHull::newtonPolytopes( ideal gls )
     789pointSet ** convexHull::newtonPolytopesP( const ideal gls )
    805790{
    806791  int i, j, k;
     
    856841  return Q;
    857842}
     843
     844// mprSTICKYPROT:
     845// ST_SPARSE_VADD: new vertex of convex hull added
     846// ST_SPARSE_VREJ: point rejected (-> inside hull)
     847ideal convexHull::newtonPolytopesI( const ideal gls )
     848{
     849  int i, j;
     850  int m;  // Anzahl der Exponentvektoren im i-ten Polynom (gls->m)[i] des Ideals gls
     851  int idelem= IDELEMS(gls);
     852  ideal id;
     853  poly p,pid,pd;
     854  Exponent_t * vert;
     855
     856  n= pVariables;
     857  vert= (Exponent_t *)Alloc( (idelem+1) * sizeof(Exponent_t) );
     858  id= idInit( idelem, 1 );
     859
     860  for( i= 0; i < idelem; i++ )
     861  {
     862    m = pLength( (gls->m)[i] );
     863
     864    p= (gls->m)[i];
     865    for( j= 1; j <= m; j++) {  // für jeden Exponentvektor
     866      if( !inHull( (gls->m)[i], p, m, j ) )
     867      {
     868        if ( (id->m)[i] == NULL ) {
     869          (id->m)[i]= pCopy(p);
     870          pNext((id->m)[i])= NULL;
     871          pid=(id->m)[i];
     872        } else {
     873          pNext(pid)= pCopy(p);
     874          pIter(pid);
     875          pNext(pid)= NULL;
     876        }
     877        mprSTICKYPROT(ST_SPARSE_VADD);
     878      }
     879      else
     880      {
     881        mprSTICKYPROT(ST_SPARSE_VREJ);
     882      }
     883      pIter( p );
     884    } // j
     885    mprSTICKYPROT("\n");
     886  } // i
     887
     888  Free( (ADDRESS) vert, (idelem+1) * sizeof(Exponent_t) );
     889
     890#ifdef mprDEBUG_PROT
     891  PrintLn();
     892  for( i= 0; i < idelem; i++ )
     893  {
     894  }
     895#endif
     896
     897  return id;
     898}
    858899//<-
    859900
     
    880921{
    881922  int i, ii, j, k, col, r;
    882   int icase, constr, numverts, cols;
     923  int numverts, cols;
    883924
    884925  numverts = 0;
     
    924965    Werror("mayanPyramidAlg::vDistance:"
    925966           "setting up matrix for udist: col %d != cols %d",col,cols);
    926   constr = n+dim+1;
     967
     968  pLP->m = n+dim+1;
     969  pLP->m3= pLP->m;
     970  pLP->n=cols-1;
    927971
    928972#ifdef mprDEBUG_ALL
    929   Print("vDistance LP, known koords dim=%d, constr %d, cols %d, acoords= ",dim,constr,cols);
     973  Print("vDistance LP, known koords dim=%d, constr %d, cols %d, acoords= ",dim,pLP->m,cols);
    930974  for( i= 0; i < dim; i++ ) Print(" %d",acoords[i]);
    931975  PrintLn();
    932   print_mat( pLP->LiPM, constr+1, cols);
    933 #endif
    934 
    935 #if 1
    936   if ( constr + 1 > pLP->LiPM_rows )
    937     WerrorS("mayanPyramidAlg::vDistance: #rows > #pLP->LiPM_rows!");
    938   if ( cols + 1 > pLP->LiPM_cols )
    939     WerrorS("mayanPyramidAlg::vDistance: #cols > #pLP->LiPM_cols!");
    940 #endif
    941 
    942   // 3rd arg is #columns excluding column of constants
    943   //            M       N       m1  m2  m3,   M= m1+m2+m3
    944   simplx( pLP->LiPM, constr, cols-1,  0,  0, constr, &icase, pLP->izrov, pLP->iposv);
     976  print_mat( pLP->LiPM, pLP->m+1, cols);
     977#endif
     978 
     979  pLP->compute();
    945980
    946981#ifdef mprDEBUG_ALL
    947982  Print("LP returns matrix\n");
    948   print_bmat( pLP->LiPM, constr+1, cols+1-constr, cols, pLP->iposv);
    949 #endif
    950 
    951   if( icase != 0 ) {  // check for errors
     983  print_bmat( pLP->LiPM, pLP->m+1, cols+1-pLP->m, cols, pLP->iposv);
     984#endif
     985
     986  if( pLP->icase != 0 ) {  // check for errors
    952987    WerrorS("mayanPyramidAlg::vDistance:");
    953     if( icase == 1 )
    954       WerrorS(" vDistance: Unbounded v-distance: probably 1st v-coor=0");
    955     if( icase == -1 )
    956       WerrorS(" vDistance: Infeasible v-distance");
     988    if( pLP->icase == 1 )
     989      WerrorS(" Unbounded v-distance: probably 1st v-coor=0");
     990    else if( pLP->icase == -1 )
     991      WerrorS(" Infeasible v-distance");
     992    else
     993      WerrorS(" Unknown error");
    957994    return -1.0;
    958995  }
     
    9641001{
    9651002  int i, j, k, cols, cons;
    966   int icase, la_cons_row;
     1003  int la_cons_row;
    9671004
    9681005  cons = n+dim+2;
     
    10121049#endif
    10131050
    1014 #if 1
    1015   if ( cons + 1 > pLP->LiPM_rows )
    1016     WerrorS(" mn_mx_MinkowskiSum: #rows > #pLP->LiPM_rows!");
    1017   if ( cols + 1 > pLP->LiPM_cols )
    1018     WerrorS(" mn_mx_MinkowskiSum: #cols > #pLP->LiPM_cols!");
    1019 #endif
    1020 
    10211051  // simplx finds MIN for obj.fnc, puts it in [1,1]
    1022   simplx(pLP->LiPM, cons, cols-1, 0, 0, cons, &icase, pLP->izrov, pLP->iposv);
    1023 
    1024   if ( icase != 0 ) { // check for errors
    1025     if( icase < 0)
     1052  pLP->m= cons;
     1053  pLP->n= cols-1;
     1054  pLP->m3= cons;
     1055
     1056  pLP->compute();
     1057
     1058  if ( pLP->icase != 0 ) { // check for errors
     1059    if( pLP->icase < 0)
    10261060      WerrorS(" mn_mx_MinkowskiSum: LinearProgram: minR: infeasible");
    1027     else if( icase > 0)
     1061    else if( pLP->icase > 0)
    10281062      WerrorS(" mn_mx_MinkowskiSum: LinearProgram: minR: unbounded");
    10291063  }
     
    10711105#endif
    10721106
    1073 #if 1
    1074   if ( cons + 1 > pLP->LiPM_rows )
    1075     WerrorS(" mn_mx_MinkowskiSum: #rows > #pLP->LiPM_rows!");
    1076   if ( cols + 1 > pLP->LiPM_cols )
    1077     WerrorS(" mn_mx_MinkowskiSum: #cols > #pLP->LiPM_cols!");
    1078 #endif
     1107  pLP->m= cons;
     1108  pLP->n= cols-1;
     1109  pLP->m3= cons;
    10791110
    10801111  // simplx finds MAX for obj.fnc, puts it in [1,1]
    1081   simplx(pLP->LiPM, cons, cols-1, 0, 0, cons, &icase, pLP->izrov, pLP->iposv);
    1082 
    1083   if ( icase != 0 )
    1084   {
    1085     if( icase < 0)
     1112  pLP->compute();
     1113
     1114  if ( pLP->icase != 0 )
     1115  {
     1116    if( pLP->icase < 0)
    10861117      WerrorS(" mn_mx_MinkowskiSum: LinearProgram: maxR: infeasible");
    1087     else if( icase > 0)
     1118    else if( pLP->icase > 0)
    10881119      WerrorS(" mn_mx_MinkowskiSum: LinearProgram: maxR: unbounded");
    10891120  }
     
    11981229{
    11991230  int i, j, k,c ;
    1200   int ncols, size, icase, NumCons;
     1231  int size;
    12011232  bool found= true;
    12021233  mprfloat cd;
     
    12051236  setID *optSum;
    12061237
    1207   ncols = 1;
    1208   NumCons = n + n + 1;   // number of constrains
     1238  LP->n = 1;
     1239  LP->m = n + n + 1;   // number of constrains
    12091240
    12101241  // fill in LP matrix
     
    12141245    for ( k= 1; k <= size; k++ )
    12151246    {
    1216       ncols++;
     1247      LP->n++;
    12171248
    12181249      // objective funtion, minimize
    1219       LP.LiPM[1][ncols] = - ( (mprfloat) (*pQ[i])[k]->point[pQ[i]->dim] / SCALEDOWN );
     1250      LP->LiPM[1][LP->n] = - ( (mprfloat) (*pQ[i])[k]->point[pQ[i]->dim] / SCALEDOWN );
    12201251
    12211252      // lambdas sum up to 1
    12221253      for ( j = 0; j <= n; j++ )
    12231254        if ( i==j )
    1224           LP.LiPM[j+2][ncols] = -1.0;
     1255          LP->LiPM[j+2][LP->n] = -1.0;
    12251256        else
    1226           LP.LiPM[j+2][ncols] = 0.0;
     1257          LP->LiPM[j+2][LP->n] = 0.0;
    12271258
    12281259      // the points
    12291260      for ( j = 1; j <= n; j++ )
    12301261      {
    1231         LP.LiPM[j+n+2][ncols] =  - ( (mprfloat) (*pQ[i])[k]->point[j] );
    1232       }
    1233     }
    1234   }
    1235 
    1236   for ( j = 0; j <= n; j++ ) LP.LiPM[j+2][1] = 1.0;
     1262        LP->LiPM[j+n+2][LP->n] =  - ( (mprfloat) (*pQ[i])[k]->point[j] );
     1263      }
     1264    }
     1265  }
     1266
     1267  for ( j = 0; j <= n; j++ ) LP->LiPM[j+2][1] = 1.0;
    12371268  for ( j= 1; j <= n; j++ )
    12381269  {
    1239     LP.LiPM[j+n+2][1]= (mprfloat)(*E)[vert]->point[j] - shift[j];
    1240   }
    1241   ncols--;
    1242 
    1243   LP.LiPM[1][1] = 0.0;
     1270    LP->LiPM[j+n+2][1]= (mprfloat)(*E)[vert]->point[j] - shift[j];
     1271  }
     1272  LP->n--;
     1273
     1274  LP->LiPM[1][1] = 0.0;
    12441275
    12451276#ifdef mprDEBUG_ALL
    12461277  PrintLn();
    1247   Print(" n= %d, NumCons=M= %d, ncols=N= %d\n",n,NumCons,ncols);
    1248   print_mat(LP.LiPM, NumCons+1, ncols+1);
    1249 #endif
    1250 
    1251 #if 1
    1252   if ( NumCons + 1 > LP.LiPM_rows )
    1253     WerrorS("resMatrixSparse::RC: #rows > #LP.LiPM_rows!");
    1254   if ( ncols + 1 > LP.LiPM_cols )
    1255     WerrorS("resMatrixSparse::RC: #cols > #LP.LiPM_cols!");
    1256 #endif
    1257 
    1258   //           M        N      m1  m2  m3,   M= m1+m2+m3
    1259   simplx(LP.LiPM, NumCons, ncols,  0,  0, NumCons, &icase, LP.izrov, LP.iposv);
    1260 
    1261   if (icase < 0)
     1278  Print(" n= %d, LP->m=M= %d, LP->n=N= %d\n",n,LP->m,LP->n);
     1279  print_mat(LP->LiPM, LP->m+1, LP->n+1);
     1280#endif
     1281
     1282  LP->m3= LP->m;
     1283
     1284  LP->compute();
     1285
     1286  if ( LP->icase < 0 )
    12621287  {
    12631288    // infeasibility: the point does not lie in a cell -> remove it
     
    12661291
    12671292  // store result
    1268   (*E)[vert]->point[E->dim]= (int)(-LP.LiPM[1][1] * SCALEDOWN);
     1293  (*E)[vert]->point[E->dim]= (int)(-LP->LiPM[1][1] * SCALEDOWN);
    12691294
    12701295#ifdef mprDEBUG_ALL
    1271   Print(" simplx returned %d, Objective value = %f\n", icase, LP.LiPM[1][1]);
    1272   //print_bmat(LP.LiPM, NumCons + 1, ncols+1-NumCons, ncols+1, LP.iposv); // ( rows= M+1, cols= N+1-m3 )
    1273   //print_mat(LP.LiPM, NumCons+1, ncols);
     1296  Print(" simplx returned %d, Objective value = %f\n", LP->icase, LP->LiPM[1][1]);
     1297  //print_bmat(LP->LiPM, NumCons + 1, LP->n+1-NumCons, LP->n+1, LP->iposv); // ( rows= M+1, cols= N+1-m3 )
     1298  //print_mat(LP->LiPM, NumCons+1, LP->n);
    12741299#endif
    12751300
     
    12791304  {
    12801305    found=false;
    1281     for ( i= 1; i < NumCons; i++ )
    1282     {
    1283       if ( LP.iposv[i] > LP.iposv[i+1] )
    1284       {
    1285 
    1286         c= LP.iposv[i];
    1287         LP.iposv[i]=LP.iposv[i+1];
    1288         LP.iposv[i+1]=c;
    1289 
    1290         cd=LP.LiPM[i+1][1];
    1291         LP.LiPM[i+1][1]=LP.LiPM[i+2][1];
    1292         LP.LiPM[i+2][1]=cd;
     1306    for ( i= 1; i < LP->m; i++ )
     1307    {
     1308      if ( LP->iposv[i] > LP->iposv[i+1] )
     1309      {
     1310
     1311        c= LP->iposv[i];
     1312        LP->iposv[i]=LP->iposv[i+1];
     1313        LP->iposv[i+1]=c;
     1314
     1315        cd=LP->LiPM[i+1][1];
     1316        LP->LiPM[i+1][1]=LP->LiPM[i+2][1];
     1317        LP->LiPM[i+2][1]=cd;
    12931318
    12941319        found= true;
     
    12991324
    13001325#ifdef mprDEBUG_ALL
    1301   print_bmat(LP.LiPM, NumCons + 1, ncols+1-NumCons, ncols+1, LP.iposv);
     1326  print_bmat(LP->LiPM, LP->m + 1, LP->n+1-LP->m, LP->n+1, LP->iposv);
    13021327  Print(" now split into sets\n");
    13031328#endif
     
    13081333  // remap results of LP to sets Qi
    13091334  c=0;
    1310   optSum= (setID*)Alloc( (NumCons) * sizeof(struct setID) );
    1311   for ( i= 0; i < NumCons; i++ )
    1312   {
    1313     //Print("% .15f\n",LP.LiPM[i+2][1]);
    1314     if ( LP.LiPM[i+2][1] > 1e-12 )
    1315     {
    1316       if ( !remapXiToPoint( LP.iposv[i+1], pQ, &(optSum[c].set), &(optSum[c].pnt) ) )
    1317       {
    1318         Werror(" resMatrixSparse::RC: Found bad solution in LP: %d!",LP.iposv[i+1]);
     1335  optSum= (setID*)Alloc( (LP->m) * sizeof(struct setID) );
     1336  for ( i= 0; i < LP->m; i++ )
     1337  {
     1338    //Print("% .15f\n",LP->LiPM[i+2][1]);
     1339    if ( LP->LiPM[i+2][1] > 1e-12 )
     1340    {
     1341      if ( !remapXiToPoint( LP->iposv[i+1], pQ, &(optSum[c].set), &(optSum[c].pnt) ) )
     1342      {
     1343        Werror(" resMatrixSparse::RC: Found bad solution in LP: %d!",LP->iposv[i+1]);
    13191344        WerrorS(" resMatrixSparse::RC: remapXiToPoint faild!");
    13201345        return -1;
     
    13481373  if ( (*E)[vert]->rc.set == linPolyS ) numSet0++;
    13491374
    1350 #ifdef mprDEBUG_ALL
     1375#ifdef mprDEBUG_PROT
    13511376  Print("\n Point E[%d] was <",vert);print_exp((*E)[vert],E->dim-1);Print(">, bucket={");
    13521377  for ( j= 0; j < E->dim; j++ )
     
    13551380  }
    13561381  Print(" }\n optimal Sum: Qi ");
    1357   for ( j= 0; j < NumCons; j++ )
     1382  for ( j= 0; j < LP->m; j++ )
    13581383  {
    13591384    Print(" [ %d, %d ]",optSum[j].set,optSum[j].pnt);
     
    13631388
    13641389  // clean up
    1365   Free( (ADDRESS) optSum, (NumCons) * sizeof(struct setID) );
     1390  Free( (ADDRESS) optSum, (LP->m) * sizeof(struct setID) );
    13661391
    13671392  mprSTICKYPROT(ST_SPARSE_RC);
    13681393
    1369   return (int)(-LP.LiPM[1][1] * SCALEDOWN);
     1394  return (int)(-LP->LiPM[1][1] * SCALEDOWN);
    13701395}
    13711396
     
    14651490  int i,j;
    14661491  i= 1;
    1467   /*
    1468   shift[1]= (mprfloat) (RVMULT*(siRand()%MAXRVVAL)/(mprfloat)MAXRVVAL);
    1469   i++;
    1470   while ( i <= dim )
    1471   {
    1472     shift[i]=shift[1];
    1473     i++;
    1474   }
    1475   */
     1492  time_t *tp = NULL;
     1493
    14761494  while ( i <= dim )
    14771495  {
     
    14871505    }
    14881506  }
    1489 
    14901507}
    14911508
     
    15671584  idelem= IDELEMS(gls);  // should be n+1
    15681585
    1569   // prepare matrix LP.LiPM for Linear Programming
     1586  // prepare matrix LP->LiPM for Linear Programming
    15701587  totverts = 0;
    15711588  for( i=0; i < idelem; i++) totverts += pLength( (gls->m)[i] );
    15721589
    1573   LP.LiPM_rows= idelem+totverts*2+5;  // very approximal
    1574   LP.LiPM_cols= totverts+5;
    1575 
    1576 #ifdef mprDEBUG_ALL
    1577   Print("LP.LiPM size: %d, %d\n",LP.LiPM_rows,LP.LiPM_cols);
    1578 #endif
    1579 
    1580   // need AllocAligned since we allocate mem for type double
    1581   LP.LiPM = (mprfloat **)Alloc( LP.LiPM_rows * sizeof(mprfloat *) );  // LP matrix
    1582   for( i= 0; i < LP.LiPM_rows; i++ )
    1583   {
    1584     // Mem must be allocated aligned, also for type double!
    1585     LP.LiPM[i] = (mprfloat *)AllocAligned0( LP.LiPM_cols * sizeof(mprfloat) );
    1586     // LP.LiPM[i] = (mprfloat *)Alloc0( LP.LiPM_cols * sizeof(mprfloat) );
    1587   }
    1588 
    1589   LP.iposv = (int *)Alloc0( (idelem * MAXPOINTS) * sizeof(int) );
    1590   LP.izrov = (int *)Alloc0( (idelem * MAXPOINTS) * sizeof(int) );
     1590  LP = new simplex( idelem+totverts*2+5, totverts+5 ); // rows, cols
    15911591
    15921592  // get shift vector
     
    16041604
    16051605  // evaluate convex hull for supports of gls
    1606   convexHull chnp( &LP );
    1607   Qi= chnp.newtonPolytopes( gls );
     1606  convexHull chnp( LP );
     1607  Qi= chnp.newtonPolytopesP( gls );
    16081608
    16091609#ifdef mprMINKSUM
     
    16111611#else
    16121612  // get inner points
    1613   mayanPyramidAlg mpa( &LP );
     1613  mayanPyramidAlg mpa( LP );
    16141614  E= mpa.getInnerPoints( Qi, shift );
    16151615#endif
    16161616
    1617 #ifdef mprDEBUG_ALL
     1617#ifdef mprDEBUG_PROT
    16181618#ifdef mprMINKSUM
    16191619  Print("(MinkSum)");
     
    16751675  E->sort();
    16761676
    1677 #ifdef mprDEBUG_ALL
     1677#ifdef mprDEBUG_PROT
    16781678  Print(" points with a[ij] (%d):\n",E->num);
    16791679  for ( pnt= 1; pnt <= E->num; pnt++ )
     
    17061706  delete E;
    17071707
    1708   for( i= 0; i < LP.LiPM_rows; i++ )
    1709   {
    1710     FreeAligned( (ADDRESS) LP.LiPM[i], LP.LiPM_cols * sizeof(mprfloat) );
    1711     //    Free( (ADDRESS) LP.LiPM[i], LP.LiPM_cols * sizeof(mprfloat) );
    1712   }
    1713   Free( (ADDRESS) LP.LiPM, LP.LiPM_rows * sizeof(mprfloat *) );
    1714 
    1715   Free( (ADDRESS) LP.iposv, (idelem * MAXPOINTS) * sizeof(int) );
    1716   Free( (ADDRESS) LP.izrov, (idelem * MAXPOINTS) * sizeof(int) );
     1708  delete LP;
    17171709}
    17181710
     
    25342526
    25352527  // evaluate determinant of matrix m using factory singclap_det
    2536   #ifdef HAVE_FACTORY
    25372528  poly res= singclap_det( m );
    2538   #else
    2539   poly res= mpDetBareiss( m );
    2540   #endif
    25412529
    25422530  // avoid errors for det==0
     
    25982586  }
    25992587
    2600   #ifdef HAVE_FACTORY
    26012588  poly res= singclap_det( mat );
    2602   #else
    2603   poly res= mpDetBareiss( mat );
    2604   #endif
    26052589
    26062590  number numres;
     
    26232607
    26242608#define MAXEVPOINT 1000000 // 0x7fffffff
     2609//#define MPR_MASI
    26252610
    26262611//-> unsigned long over(unsigned long n,unsigned long d)
     
    29382923        else if ( i <= uvar + 2 )
    29392924        {
    2940           pevpoint[i]=nInit(1+(siRand() % MAXEVPOINT));
     2925          pevpoint[i]=nInit(1+siRand()%MAXEVPOINT);
    29412926          //pevpoint[i]=nInit(383);
    29422927        }
     
    30583043        if ( i <= uvar + 2 )
    30593044        {
    3060           pevpoint[i]=nInit(1+(siRand() % MAXEVPOINT));
     3045          pevpoint[i]=nInit(1+siRand()%MAXEVPOINT);
    30613046          //pevpoint[i]=nInit(383);
    30623047        } else pevpoint[i]=nInit(0);
     
    30803065    number *ncpoly= (number *)Alloc( (tdg+1) * sizeof(number) );
    30813066
    3082 #ifdef MPR_TIMING
    3083        BOOLEAN masi=true;
    3084 #endif
     3067#ifdef MPR_MASI
     3068    BOOLEAN masi=true;
     3069#endif
     3070
    30853071    piter= pures;
    30863072    for ( i= tdg; i >= 0; i-- )
     
    30913077        ncpoly[i]= nCopy( pGetCoeff( piter ) );
    30923078        pIter( piter );
    3093 #ifdef MPR_TIMING
    3094        masi=false;
     3079#ifdef MPR_MASI
     3080        masi=false;
    30953081#endif
    30963082      }
     
    31013087      mprPROTNnl("", ncpoly[i] );
    31023088    }
    3103 
    3104 #ifdef MPR_TIMING
    3105      if ( masi ) Print("MASI MASI MASI\n");
     3089#ifdef MPR_MASI
     3090    if ( masi ) mprSTICKYPROT("MASI");
    31063091#endif
    31073092
     
    31493134}
    31503135//<-
     3136
     3137//-----------------------------------------------------------------------------
     3138
     3139//-> loNewtonPolytope(...)
     3140ideal loNewtonPolytope( const ideal id )
     3141{
     3142  simplex * LP;
     3143  int i;
     3144  int n,totverts,idelem;
     3145  ideal idr;
     3146
     3147  n= pVariables;
     3148  idelem= IDELEMS(id);  // should be n+1
     3149
     3150  totverts = 0;
     3151  for( i=0; i < idelem; i++) totverts += pLength( (id->m)[i] );
     3152
     3153  LP = new simplex( idelem+totverts*2+5, totverts+5 ); // rows, cols
     3154
     3155  // evaluate convex hull for supports of id
     3156  convexHull chnp( LP );
     3157  idr = chnp.newtonPolytopesI( id );
     3158
     3159  delete LP;
     3160
     3161  return idr;
     3162}
     3163//<-
     3164
    31513165//%e
    31523166
     
    31643178// leave fold: C-c y
    31653179//   foldmode: F10
    3166 
  • Singular/mpr_base.h

    r2983b3 rc7f3b7  
    44*  Computer Algebra System SINGULAR     *
    55****************************************/
    6 /* $Id: mpr_base.h,v 1.4 1999-11-15 17:20:30 obachman Exp $ */
     6/* $Id: mpr_base.h,v 1.5 1999-12-02 23:03:50 wenk Exp $ */
    77
    88/*
     
    9494};
    9595//<-
     96
     97ideal loNewtonPolytope( const ideal id );
    9698//%e
    9799#endif MPR_BASE_H
  • Singular/mpr_global.h

    r2983b3 rc7f3b7  
    44*  Computer Algebra System SINGULAR     *
    55****************************************/
    6 /* $Id: mpr_global.h,v 1.8 1999-11-15 17:20:30 obachman Exp $ */
     6/* $Id: mpr_global.h,v 1.9 1999-12-02 23:03:50 wenk Exp $ */
    77
    88/*
     
    1212
    1313// to get detailed timigs, define MPR_TIMING
    14 // #define MPR_TIMING
     14//#define MPR_TIMING
    1515
    1616// Set to double or long double. double is recomended.
  • Singular/mpr_inout.cc

    r2983b3 rc7f3b7  
    33****************************************/
    44
    5 /* $Id: mpr_inout.cc,v 1.6 1999-11-15 17:20:30 obachman Exp $ */
     5/* $Id: mpr_inout.cc,v 1.7 1999-12-02 23:03:51 wenk Exp $ */
    66
    77/*
     
    2525#include "numbers.h"
    2626#include "lists.h"
     27#include "matpol.h"
    2728
    2829#include <math.h>
     
    3637#ifdef MPR_TIMING
    3738#define TIMING
     39#endif
    3840#include "../factory/timing.h"
    3941TIMING_DEFINE_PRINT(mpr_overall)
     
    4446TIMING_DEFINE_PRINT(mpr_arrange)
    4547TIMING_DEFINE_PRINT(mpr_solver)
     48
    4649#define TIMING_EPR(t,msg) TIMING_END_AND_PRINT(t,msg);TIMING_RESET(t);
    47 #else
    48 #define TIMING_START(x)
    49 #define TIMING_EPR(x,y)
    50 #endif
    51 
    5250
    5351enum mprState
     
    181179  if ( v->Typ() != IDEAL_CMD )
    182180    return TRUE;
    183   else gls= (ideal)(args->Data());
     181  else gls= (ideal)(v->Data());
    184182  v= v->next;
    185183
     
    211209  BOOLEAN interpolate_det= (mtype==uResultant::denseResMat)?TRUE:FALSE;
    212210
    213   //emptylist= (lists)AllocSizeOf( slists );
     211  //emptylist= (lists)Alloc( sizeof(slists) );
    214212  //emptylist->Init( 0 );
    215213
     
    314312
    315313  //emptylist->Clean();
    316   //  FreeSizeOf( (ADDRESS) emptylist, slists );
     314  //  Free( (ADDRESS) emptylist, sizeof(slists) );
    317315
    318316  TIMING_EPR(mpr_overall,"overall time\t\t")
     
    356354  int howclean= (int)arg3->Data();
    357355
     356  if ( !(rField_is_R() ||
     357         rField_is_Q() ||
     358         rField_is_long_R() ||
     359         rField_is_long_C()) )
     360  {
     361    WerrorS("Ground field not implemented!");
     362    return TRUE;
     363  }
     364 
    358365  if ( !(rField_is_R()||rField_is_long_R()||rField_is_long_C()) )
    359366  {
    360367    setGMPFloatDigits( (unsigned long int)arg2->Data() );
     368  }
     369
     370  if ( gls == NULL || pIsConstant( gls ) )
     371  {
     372    WerrorS("Input polynomial is constant!");
     373    return TRUE;
    361374  }
    362375
     
    369382  lists rlist;
    370383
    371   elist= (lists)AllocSizeOf( slists );
     384  elist= (lists)Alloc( sizeof(slists) );
    372385  elist->Init( 0 );
    373 
    374   if ( !(rField_is_R() ||
    375          rField_is_Q() ||
    376          rField_is_long_R() ||
    377          rField_is_long_C()) )
    378          {
    379     WerrorS("Ground field not implemented!");
    380     return TRUE;
    381   }
    382386
    383387  if ( pVariables > 1 )
     
    395399        if ( (vpos != i) && (pGetExp( piter, i ) != 0) )
    396400        {
    397           Werror("The polynomial %s must be univariate!",arg1->Name());
     401          WerrorS("The input polynomial must be univariate!");
    398402          return TRUE;
    399403        }
     
    438442  int j;
    439443
    440   rlist= (lists)AllocSizeOf( slists );
     444  rlist= (lists)Alloc( sizeof(slists) );
    441445  rlist->Init( elem );
    442446
     
    461465
    462466  elist->Clean();
    463   //FreeSizeOf( (ADDRESS) elist, slists );
     467  //Free( (ADDRESS) elist, sizeof(slists) );
    464468
    465469  for ( i= deg; i >= 0; i-- ) nDelete( &pcoeffs[i] );
     
    629633//<-
    630634
     635//-> BOOLEAN loNewtonP( leftv res, leftv arg1 )
     636BOOLEAN loNewtonP( leftv res, leftv arg1 )
     637{
     638  res->data= (void*)loNewtonPolytope( (ideal)arg1->Data() );
     639  return FALSE;
     640}
     641//<-
     642
     643//-> BOOLEAN loSimplex( leftv res, leftv args )
     644BOOLEAN loSimplex( leftv res, leftv args )
     645{
     646  if ( !(rField_is_long_R()) )
     647  {
     648    WerrorS("Ground field not implemented!");
     649    return TRUE;
     650  }
     651
     652  simplex * LP;
     653  matrix m;
     654
     655  leftv v= args;
     656  if ( v->Typ() != MATRIX_CMD ) // 1: matrix
     657    return TRUE;
     658  else
     659    m= (matrix)(v->Data());
     660 
     661  LP = new simplex(MATROWS(m),MATCOLS(m));
     662  LP->mapFromMatrix(m);
     663
     664  v= v->next;
     665  if ( v->Typ() != INT_CMD )    // 2: m = number of constraints
     666    return TRUE;
     667  else
     668    LP->m= (int)(v->Data());
     669
     670  v= v->next;
     671  if ( v->Typ() != INT_CMD )    // 3: n = number of variables
     672    return TRUE;
     673  else
     674    LP->n= (int)(v->Data());
     675
     676  v= v->next;
     677  if ( v->Typ() != INT_CMD )    // 4: m1 = number of <= constraints
     678    return TRUE;
     679  else
     680    LP->m1= (int)(v->Data());
     681
     682  v= v->next;
     683  if ( v->Typ() != INT_CMD )    // 5: m2 = number of >= constraints
     684    return TRUE;
     685  else
     686    LP->m2= (int)(v->Data());
     687
     688  v= v->next;
     689  if ( v->Typ() != INT_CMD )    // 6: m3 = number of == constraints
     690    return TRUE;
     691  else
     692    LP->m3= (int)(v->Data());
     693
     694#ifdef mprDEBUG_PROT
     695  Print("m (constraints) %d\n",LP->m);
     696  Print("n (columns) %d\n",LP->n);
     697  Print("m1 (<=) %d\n",LP->m1);
     698  Print("m2 (>=) %d\n",LP->m2);
     699  Print("m3 (==) %d\n",LP->m3);
     700#endif
     701
     702  LP->compute();
     703
     704  lists lres= (lists)Alloc( sizeof(slists) );
     705  lres->Init( 6 );
     706
     707  lres->m[0].rtyp= MATRIX_CMD; // output matrix
     708  lres->m[0].data=(void*)LP->mapToMatrix(m);
     709
     710  lres->m[1].rtyp= INT_CMD;   // found a solution?
     711  lres->m[1].data=(void*)LP->icase;
     712
     713  lres->m[2].rtyp= INTVEC_CMD;
     714  lres->m[2].data=(void*)LP->posvToIV();
     715 
     716  lres->m[3].rtyp= INTVEC_CMD;
     717  lres->m[3].data=(void*)LP->zrovToIV();
     718
     719  lres->m[4].rtyp= INT_CMD;
     720  lres->m[4].data=(void*)LP->m;
     721
     722  lres->m[5].rtyp= INT_CMD;
     723  lres->m[5].data=(void*)LP->n;
     724
     725  res->data= (void*)lres;
     726
     727  return FALSE;
     728}
     729//<-
     730
    631731//-----------------------------------------------------------------------------
    632732
  • Singular/mpr_inout.h

    r2983b3 rc7f3b7  
    55****************************************/
    66
    7 /* $Id: mpr_inout.h,v 1.5 1999-11-15 17:20:31 obachman Exp $ */
     7/* $Id: mpr_inout.h,v 1.6 1999-12-02 23:03:51 wenk Exp $ */
    88
    99/*
     
    5858BOOLEAN nuVanderSys( leftv res, leftv arg1, leftv arg2, leftv arg3 );
    5959
     60/** compute Newton Polytopes of input polynomials
     61 */
     62BOOLEAN loNewtonP( leftv res, leftv arg1 );
     63
     64/** Implementation of the Simplex Algorithm.
     65 * For args, see class simplex.
     66 */
     67BOOLEAN loSimplex( leftv res, leftv args );
     68
    6069#endif
    6170
  • Singular/mpr_numeric.cc

    r2983b3 rc7f3b7  
    33****************************************/
    44
    5 /* $Id: mpr_numeric.cc,v 1.6 1999-11-15 17:20:31 obachman Exp $ */
     5/* $Id: mpr_numeric.cc,v 1.7 1999-12-02 23:03:51 wenk Exp $ */
    66
    77/*
     
    2424#include "intvec.h"
    2525#include "longalg.h"
     26#include "matpol.h"
    2627#include "ring.h"
    2728//#include "longrat.h"
     
    749750  int elem= roots[0]->getAnzElems();  // number of koordinates per root
    750751
    751   lists listofroots= (lists)AllocSizeOf( slists ); // must be done this way!
     752  lists listofroots= (lists)Alloc( sizeof(slists) ); // must be done this way!
    752753
    753754  if ( found_roots )
     
    757758    for (i=0; i < count; i++)
    758759    {
    759       lists onepoint= (lists)AllocSizeOf(slists); // must be done this way!
     760      lists onepoint= (lists)Alloc(sizeof(slists)); // must be done this way!
    760761      onepoint->Init(elem);
    761762      for ( j= 0; j < elem; j++ )
     
    791792
    792793//-----------------------------------------------------------------------------
    793 //-------------- ludcmp/lubksb ------------------------------------------------
     794//-------------- simplex ----- ------------------------------------------------
    794795//-----------------------------------------------------------------------------
    795796
    796 //#define error(a) a
    797 #define error(a)
    798 
    799 //-> simplex
     797//  #ifdef mprDEBUG_PROT
     798//  #define error(a) a
     799//  #else
     800//  #define error(a)
     801//  #endif
     802
     803#define error(a) a
     804
     805#define MAXPOINTS      1000
     806
     807//-> simplex::*
    800808//
    801 void simp1( mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax );
    802 void simp2( mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1 );
    803 void simp3( mprfloat **a, int i1, int k1, int ip, int kp );
    804 
    805 void simplx( mprfloat **a, int m, int n, int m1, int m2, int m3, int *icase, int izrov[], int iposv[] )
     809simplex::simplex( int rows, int cols )
     810   : LiPM_cols(cols), LiPM_rows(rows)
     811{
     812  int i;
     813
     814  LiPM_rows=LiPM_rows+3;
     815  LiPM_cols=LiPM_cols+2;
     816
     817  LiPM = (mprfloat **)Alloc( LiPM_rows * sizeof(mprfloat *) );  // LP matrix
     818  for( i= 0; i < LiPM_rows; i++ )
     819  {
     820    // Mem must be allocated aligned, also for type double!
     821    LiPM[i] = (mprfloat *)AllocAligned0( LiPM_cols * sizeof(mprfloat) );
     822  }
     823
     824  iposv = (int *)Alloc0( 2*LiPM_rows*sizeof(int) );
     825  izrov = (int *)Alloc0( 2*LiPM_rows*sizeof(int) );
     826
     827  m=n=m1=m2=m3=icase=0;
     828
     829#ifdef mprDEBUG_ALL
     830  Print("LiPM size: %d, %d\n",LiPM_rows,LiPM_cols);
     831#endif
     832}
     833
     834simplex::~simplex()
     835{
     836  // clean up
     837  int i;
     838  for( i= 0; i < LiPM_rows; i++ )
     839  {
     840    FreeAligned( (ADDRESS) LiPM[i], LiPM_cols * sizeof(mprfloat) );
     841  }
     842  Free( (ADDRESS) LiPM, LiPM_rows * sizeof(mprfloat *) );
     843
     844  Free( (ADDRESS) iposv, 2*LiPM_rows*sizeof(int) );
     845  Free( (ADDRESS) izrov, 2*LiPM_rows*sizeof(int) );
     846}
     847
     848BOOLEAN simplex::mapFromMatrix( matrix m )
     849{
     850  int i,j;
     851//    if ( MATROWS( m ) > LiPM_rows ||  MATCOLS( m ) > LiPM_cols ) {
     852//      WerrorS("");
     853//      return FALSE;
     854//    }
     855
     856  number coef;
     857  for ( i= 1; i <= MATROWS( m ); i++ )
     858  {
     859     for ( j= 1; j <= MATCOLS( m ); j++ )
     860     {
     861        if ( MATELEM(m,i,j) != NULL )
     862        {
     863           coef= pGetCoeff( MATELEM(m,i,j) );
     864           if ( coef != NULL && !nIsZero(coef) )
     865              LiPM[i][j]= (double)(*(gmp_float*)coef);
     866           //#ifdef mpr_DEBUG_PROT
     867           //Print("%f ",LiPM[i][j]);
     868           //#endif
     869        }
     870     }
     871     //     PrintLn();
     872  }
     873
     874  return TRUE;
     875}
     876
     877matrix simplex::mapToMatrix( matrix m )
     878{
     879  int i,j;
     880//    if ( MATROWS( m ) < LiPM_rows-3 ||  MATCOLS( m ) < LiPM_cols-2 ) {
     881//      WerrorS("");
     882//      return NULL;
     883//    }
     884
     885//Print(" %d x %d\n",MATROWS( m ),MATCOLS( m ));
     886
     887  number coef;
     888  gmp_float * bla;
     889  for ( i= 1; i <= MATROWS( m ); i++ )
     890  {
     891    for ( j= 1; j <= MATCOLS( m ); j++ )
     892    {
     893       pDelete( &(MATELEM(m,i,j)) );
     894       MATELEM(m,i,j)= NULL;
     895//Print(" %3.0f ",LiPM[i][j]);
     896       if ( LiPM[i][j] != 0.0 )
     897       {
     898          bla= new gmp_float(LiPM[i][j]);
     899          coef= (number)bla;
     900          MATELEM(m,i,j)= pOne();
     901          pSetCoeff( MATELEM(m,i,j), coef );
     902       }
     903    }
     904//PrintLn();
     905  }
     906 
     907  return m;
     908}
     909
     910intvec * simplex::posvToIV()
     911{
     912   int i;
     913   intvec * iv = new intvec( m );
     914   for ( i= 1; i <= m; i++ )
     915   {
     916      IMATELEM(*iv,i,1)= iposv[i];
     917   }
     918   return iv;
     919}
     920
     921intvec * simplex::zrovToIV()
     922{
     923   int i;
     924   intvec * iv = new intvec( n );
     925   for ( i= 1; i <= n; i++ )
     926   {
     927      IMATELEM(*iv,i,1)= izrov[i];
     928   }
     929   return iv;
     930}
     931
     932void simplex::compute()
    806933{
    807934  int i,ip,ir,is,k,kh,kp,m12,nl1,nl2;
     
    809936  mprfloat q1, bmax;
    810937
    811   if ( m != (m1+m2+m3))
     938  if ( m != (m1+m2+m3) )
    812939  {
    813940    // error: bad input
    814     error(WerrorS(" bad input constraint counts in simplex ");)
    815     *icase=-2;
     941    error(WerrorS("simplex::compute: Bad input constraint counts!");)
     942    icase=-2;
    816943    return;
    817944  }
     
    826953  for ( i=1; i<=m; i++ )
    827954  {
    828     if ( a[i+1][1] < 0.0 )
     955    if ( LiPM[i+1][1] < 0.0 )
    829956    {
    830957      // error: bad input
    831       error(WerrorS(" bad input tableau in simplex ");)
    832       *icase=-2;
     958      error(WerrorS("simplex::compute: Bad input tableau!");)
     959      error(Werror("simplex::compute: in input Matrix row %d, column 1, value %f",i+1,LiPM[i+1][1]);)
     960      icase=-2;
    833961      // free mem l1,l2,l3;
    834962      Free( (ADDRESS) l3, (m+1) * sizeof(int) );
     
    848976    {
    849977      q1=0.0;
    850       for ( i=m1+1; i <= m; i++ ) q1+= a[i+1][k];
    851       a[m+2][k]= -q1;
     978      for ( i=m1+1; i <= m; i++ ) q1+= LiPM[i+1][k];
     979      LiPM[m+2][k]= -q1;
    852980    }
    853981
    854982    do
    855983    {
    856       simp1(a,m+1,l1,nl1,0,&kp,&bmax);
    857       if ( bmax <= SIMPLEX_EPS && a[m+2][1] < -SIMPLEX_EPS )
    858       {
    859         *icase= -1; // no solution found
     984      simp1(LiPM,m+1,l1,nl1,0,&kp,&bmax);
     985      if ( bmax <= SIMPLEX_EPS && LiPM[m+2][1] < -SIMPLEX_EPS )
     986      {
     987        icase= -1; // no solution found
    860988        // free mem l1,l2,l3;
    861989        Free( (ADDRESS) l3, (m+1) * sizeof(int) );
     
    864992        return;
    865993      }
    866       else if ( bmax <= SIMPLEX_EPS && a[m+2][1] <= SIMPLEX_EPS )
     994      else if ( bmax <= SIMPLEX_EPS && LiPM[m+2][1] <= SIMPLEX_EPS )
    867995      {
    868996        m12= m1+m2+1;
     
    8731001            if ( iposv[ip] == (ip+n) )
    8741002            {
    875               simp1(a,ip,l1,nl1,1,&kp,&bmax);
     1003              simp1(LiPM,ip,l1,nl1,1,&kp,&bmax);
    8761004              if ( fabs(bmax) >= SIMPLEX_EPS)
    8771005                goto one;
     
    8851013            if ( l3[i-m1] == 1 )
    8861014              for ( k=1; k <= n+1; k++ )
    887                 a[i+1][k] = -a[i+1][k];
     1015                LiPM[i+1][k] = -(LiPM[i+1][k]);
    8881016        break;
    8891017      }
     
    8911019      //print_bmat( a, m+2, n+3);
    8921020      //#endif
    893       simp2(a,n,l2,nl2,&ip,kp,&q1);
     1021      simp2(LiPM,n,l2,nl2,&ip,kp,&q1);
    8941022      if ( ip == 0 )
    8951023      {
    896         *icase = -1; // no solution found
     1024        icase = -1; // no solution found
    8971025        // free mem l1,l2,l3;
    8981026        Free( (ADDRESS) l3, (m+1) * sizeof(int) );
     
    9011029        return;
    9021030      }
    903     one: simp3(a,m+1,n,ip,kp);
     1031    one: simp3(LiPM,m+1,n,ip,kp);
    9041032      // #if DEBUG
    9051033      // print_bmat(a,m+2,n+3);
     
    9071035      if ( iposv[ip] >= (n+m1+m2+1))
    9081036      {
    909         for ( k=1; k<= nl1; k++ )
     1037        for ( k= 1; k <= nl1; k++ )
    9101038          if ( l1[k] == kp ) break;
    9111039        --nl1;
    9121040        for ( is=k; is <= nl1; is++ ) l1[is]= l1[is+1];
    913         ++a[m+2][kp+1];
    914         for ( i= 1; i <= m+2; i++ ) a[i][kp+1] = -a[i][kp+1];
     1041        ++(LiPM[m+2][kp+1]);
     1042        for ( i= 1; i <= m+2; i++ ) LiPM[i][kp+1] = -(LiPM[i][kp+1]);
    9151043      }
    9161044      else
     
    9221050          {
    9231051            l3[kh]= 0;
    924             ++a[m+2][kp+1];
     1052            ++(LiPM[m+2][kp+1]);
    9251053            for ( i=1; i<= m+2; i++ )
    926               a[i][kp+1] = -a[i][kp+1];
     1054              LiPM[i][kp+1] = -(LiPM[i][kp+1]);
    9271055          }
    9281056        }
     
    9391067    // print_bmat( a, m+1, n+5);
    9401068    // #endif
    941     simp1(a,0,l1,nl1,0,&kp,&bmax);
     1069    simp1(LiPM,0,l1,nl1,0,&kp,&bmax);
    9421070    if (bmax <= /*SIMPLEX_EPS*/0.0)
    9431071    {
    944       *icase=0; // finite solution found
     1072      icase=0; // finite solution found
    9451073      // free mem l1,l2,l3
    9461074      Free( (ADDRESS) l3, (m+1) * sizeof(int) );
     
    9491077      return;
    9501078    }
    951     simp2(a,n,l2,nl2,&ip,kp,&q1);
     1079    simp2(LiPM,n,l2,nl2,&ip,kp,&q1);
    9521080    if (ip == 0)
    9531081    {
     
    9561084      //       print_bmat( a, m+1, n+1);
    9571085      // #endif
    958       *icase=1;                /* unbounded */
     1086      icase=1;                /* unbounded */
    9591087      // free mem
    9601088      Free( (ADDRESS) l3, (m+1) * sizeof(int) );
     
    9631091      return;
    9641092    }
    965     simp3(a,m,n,ip,kp);
     1093    simp3(LiPM,m,n,ip,kp);
    9661094    is= izrov[kp];
    9671095    izrov[kp]= iposv[ip];
     
    9701098}
    9711099
    972 void simp1( mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax )
     1100void simplex::simp1( mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax )
    9731101{
    9741102  int k;
     
    10051133}
    10061134
    1007 void simp2( mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1 )
     1135void simplex::simp2( mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1 )
    10081136{
    10091137  int k,ii,i;
     
    10441172}
    10451173
    1046 void simp3( mprfloat **a, int i1, int k1, int ip, int kp )
     1174void simplex::simp3( mprfloat **a, int i1, int k1, int ip, int kp )
    10471175{
    10481176  int kk,ii;
  • Singular/mpr_numeric.h

    r2983b3 rc7f3b7  
    55****************************************/
    66
    7 /* $Id: mpr_numeric.h,v 1.4 1999-11-15 17:20:31 obachman Exp $ */
     7/* $Id: mpr_numeric.h,v 1.5 1999-12-02 23:03:52 wenk Exp $ */
    88
    99/*
     
    162162#define SIMPLEX_EPS 1.0e-12
    163163
    164 void simplx( mprfloat **a, int m, int n, int m1, int m2, int m3, int *icase, int izrov[], int iposv[] );
     164/** Linear Programming / Linear Optimization using Simplex - Algorithm
     165 *
     166 * On output, the tableau LiPM is indexed by two arrays of integers.
     167 * ipsov[j] contains, for j=1..m, the number i whose original variable
     168 * is now represented by row j+1 of LiPM. (left-handed vars in solution)
     169 * (first row is the one with the objective function)
     170 * izrov[j] contains, for j=1..n, the number i whose original variable
     171 * x_i is now a right-handed variable, rep. by column j+1 of LiPM.
     172 * These vars are all zero in the solution. The meaning of n<i<n+m1+m2
     173 * is the same as above.
     174 */
     175class simplex
     176{
     177public:
     178 
     179  int m;         // number of constraints, make sure m == m1 + m2 + m3 !!
     180  int n;         // # of independent variables
     181  int m1,m2,m3;  // constraints <=, >= and ==
     182  int icase;     // == 0: finite solution found;
     183                 // == +1 objective funtion unbound; == -1: no solution
     184  int *izrov,*iposv;   
     185
     186  mprfloat **LiPM; // the matrix (of size [m+2, n+1])
     187
     188  /** #rows should be >= m+2, #cols >= n+1
     189   */
     190  simplex( int rows, int cols );
     191  ~simplex();
     192
     193  BOOLEAN mapFromMatrix( matrix m );
     194  matrix mapToMatrix( matrix m );
     195  intvec * posvToIV();
     196  intvec * zrovToIV();
     197
     198  void compute();
     199
     200private:
     201  simplex( const simplex & );
     202  void simp1( mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax );
     203  void simp2( mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1 );
     204  void simp3( mprfloat **a, int i1, int k1, int ip, int kp );
     205
     206  int LiPM_cols,LiPM_rows;
     207};
     208
    165209//<-
    166210
  • Singular/tok.h

    r2983b3 rc7f3b7  
    77* ABSTRACT: tokens, types for interpreter; general macros
    88*/
    9 /* $Id: tok.h,v 1.33 1999-11-29 14:46:55 Singular Exp $ */
     9/* $Id: tok.h,v 1.34 1999-12-02 23:03:52 wenk Exp $ */
    1010
    1111#ifndef MYYSTYPE
     
    8989  NAMEOF_CMD,
    9090  NAMES_CMD,
     91  NEWTONPOLY_CMD,
    9192  NPARS_CMD,
    9293  NVARS_CMD,
     
    106107  RESULTANT_CMD,
    107108  ROWS_CMD,
     109  SIMPLEX_CMD,
    108110  SQR_FREE_DEC_CMD,
    109111  STATUS_CMD,
Note: See TracChangeset for help on using the changeset viewer.