File: SINGULAR.c

package info (click to toggle)
qepcad 1.74%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,848 kB
  • sloc: ansic: 27,242; cpp: 2,995; makefile: 1,287; perl: 91
file content (338 lines) | stat: -rw-r--r-- 8,306 bytes parent folder | download | duplicates (2)
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
#include "SINGULAR.h"
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;


SingularServer::SingularServer(string SingularBase)
{
  string SingularCall = SingularBase + "/Singular";
  
  // Fork
  childpid = fork();
  if (childpid == -1) { perror("Failed to fork!"); exit(1); }
  // Child process's code
  if (childpid == 0) {
    intoSingular.setStdinToPipe();
    outofSingular.setStdoutToPipe();
    outofSingular.setStderrToPipe();
    intoSingular.closeIn();
    intoSingular.closeOut();
    outofSingular.closeIn();
    outofSingular.closeOut();
    setsid();

    // Begin: Just for debug!!
    //system("/home/wcbrown/bin/Singular -q --no-warn --min-time=0.001 --ticks-per-sec=1000 | tee /tmp/SingOutLog");
    //exit(0);
    // End:   Just for debug!!


    execlp(SingularCall.c_str(),
	   SingularCall.c_str(),
	   "-q",
	   "--no-warn",
	   "--min-time=0.001",
	   "--ticks-per-sec=1000",
	   NULL);
      perror("SingularServer Constructor: Singular startup failed! (Set SINGULAR environment variable)");
      exit(0);
  }
  intoSingular.closeIn();
  outofSingular.closeOut();
}

SingularServer::~SingularServer()
{
  intoSingular.out() << endl << "quit;" << endl << flush;
}

int SingularServer::serverTime()
  {
    intoSingular.out() << "timer;" << endl;
    int t;
    outofSingular.in() >> t;
    return t;
  }

void SingularServer::reportStats(ostream &out)
{
  out << "Time in Singular: " 
      << serverTime()
      << " ms." << endl;    
}


char peekNonWS(istream &in) 
{ 
  char c; while((c = in.peek()) && (c == ' ' || c == '\t' || c == '\n')) in.get(); return c; 
}


Word readSingularPoly(Word r, Word V, istream &in)
{
  Word A, t;
  string s;
  in >> s;
  for(size_t i = 0; i < s.length(); ++i)
    if (s[i] == '*') s[i] = ' ';
  s += ".\n";
  istringstream si(s);
  PushInputContext(si);
  IPEXPREADR(r,V,&A,&t);
  PopInputContext();

  if (t == 0) 
  { 
    cerr << "QEPCADB could not read Singular's output!" << endl
         << s << endl;
    exit(1);
  }
  return A;
}

Word CreateVariableList(Word r)
{
  Word V = NIL;
  char s[] = {'a','\0'};
  for(int i = 'a' + r - 1; i >= 'a'; --i)
  {
    s[0] = i;
    V = COMP(LFS(s),V);
  }
  return V;
}

string WritePolyForSingular(Word r, Word P, Word V)
{
  //write poly to string
  string out;
  {
    ostringstream sout;
    PushOutputContext(sout);
    IPDWRITE(r,P,V);
    PopOutputContext();
    out = sout.str();
  }
  // Put in * symbols
  for(size_t i = 1; i < out.length() - 1U; ++i)
    if (out[i] == ' ' && out[i+1] != '+' && out[i+1] != '-'
	&& out[i-1] != '+' && out[i-1] != '-'
	)
      out[i] = '*';

  return out;
}

void DefineRing(ostream &out, const string &name, Word r, const string &order="lp")
{
  out << "ring " << name << " = 0, (";
  for(int i = r-1; i > 0; --i)
    out << char('a' + i) << ',';
  out << "a), " << order << ";" << endl;
  out << "short = 0;" << endl;
}

Word SingularServer::IPRES(Word r, Word A, Word B)
{
  DefineRing(intoSingular.out(),"myring",r);
  Word V = CreateVariableList(r);
  string s_A =  WritePolyForSingular(r,A,V);
  intoSingular.out() << "poly A = " << s_A << ";" << endl;
  string s_B =  WritePolyForSingular(r,B,V);
  intoSingular.out() << "poly B = " << s_B << ";" << endl;
  intoSingular.out() << "resultant(A,B," << char('a' + r - 1) << ");" << endl;
  
  // Read & parse Singular output
  Word p = readSingularPoly(r-1,V,outofSingular.in());

  return p;
}

Word SingularServer::IPDSCR(Word r, Word A)
{
       Word Ap,B,a,n,rp;

Step1: /* Compute. */
       Ap = IPDMV(r,A);
       B = IPRES(r,A,Ap);
       a = PLDCF(A);
       rp = r - 1;
       B = IPEQ(rp,B,a);
       n = PDEG(A);
       if (REM(n,4) >= 2)
          B = IPNEG(rp,B);

Return: /* Prepare for return. */
       return(B);
}

void SingularServer::IPFAC(Word r, Word P, Word *s_, Word *c_, Word *L_)
{
  Word V = CreateVariableList(r);
  string out =  WritePolyForSingular(r,P,V);

  // Send Singular commands
  DefineRing(intoSingular.out(),"myring",r);
  intoSingular.out() << "poly F = " << out << ";" << endl;
  intoSingular.out() << "factorize(F);" << endl;
  
  // Read & parse Singular output
  istream &in = outofSingular.in();
  string s1, s2;
  in >> s1;
  Word L = NIL;
  while(true)
  {
    char c;
    while((c = in.get()) && c != '=' && c != ':');
    if (c == ':') break;
    Word p = readSingularPoly(r,V,in);
    L = COMP(p,L);
  }
  Word M = NIL;
  int m;
  char c;
  in >> m;
  M = COMP(m,M);
  for(int i = 1; i < LENGTH(L); ++i) { in >> c >> m;  M = COMP(m,M); }
  while(in.get() != '\n');

  // Pair factors and multiplicities (and make sure factors are positive)
  Word FML = NIL;
  Word sn = 1;
  for(;RED(L) != NIL; L = RED(L), M = RED(M))
  {
    Word pL = FIRST(L);
    if (ISIGNF(PLBCF(r,pL)) == -1)
    {
      if (ODD(FIRST(M)))
	sn *= -1;
      pL = IPNEG(r,pL);
    }
    FML = COMP(LIST2(FIRST(M),pL),FML);
  }
  // Sign and content
  Word lcf = PLBCF(r,FIRST(L));
  Word ct = IABSF(lcf);
  sn *= ISIGNF(lcf);

  // RETURN
  *s_ = sn;
  *c_ = ct;
  *L_ = FML;
  return;
}

Word SingularServer::IPFACTGB(Word r, Word I, Word N)
{
  // WRITE SINGULAR INPUT
  DefineRing(intoSingular.out(),"myring",r);
  Word V = CreateVariableList(r);
  intoSingular.out() << "ideal I = ";
  for(Word L = I; L != NIL; L = RED(L))
  {
    intoSingular.out() << WritePolyForSingular(r,FIRST(L),V);
    if (RED(L) != NIL)
      intoSingular.out() << ", ";
    else
      intoSingular.out() << ";" << endl;
  }
  intoSingular.out() << "option(\"redSB\");" << endl;
  intoSingular.out() << "facstd(I);" << endl;
  intoSingular.out() << "\"[end]:\";" << endl;

  // READ SINGULAR OUTPUT
  Word F = NIL;
  string marker; 
  while(outofSingular.in() >> marker && marker != "[end]:")
  {
    Word GBi = NIL;
    while(peekNonWS(outofSingular.in()) == '_')
    {
      while(outofSingular.in().get() != '=');
      Word p = readSingularPoly(r,V,outofSingular.in());
      GBi = COMP(p,GBi);      
    }
    F = COMP(CINV(GBi),F);
  }
  return CINV(F);
}

Word TDEG(Word r, Word A)
{
  if (r == 0) return 0;
  Word M = 0;
  for(Word L = A; L != NIL; L = RED2(L))
    M = IMAX(M,FIRST(L) + TDEG(r-1,SECOND(L)));
  return M;
}

Word SingularServer::CONSTORDTEST(Word r, Word A, Word L)
{
  // SINGULAR INPUT: Define Ideal A + L & compute groebner basis G
  DefineRing(intoSingular.out(),"myring",r,"dp");
  Word V = CreateVariableList(r);
  intoSingular.out() << "ideal I = ";
  for(Word Lp = L; Lp != NIL; Lp = RED(Lp))
    intoSingular.out() << WritePolyForSingular(r,FIRST(Lp),V) << ", ";
  intoSingular.out() 
    << WritePolyForSingular(r,A,V) << ";" << endl
    << "ideal G = std(I);" << endl
    << "G;" << endl
    << "\"[end]:\";" << endl;

  // SINGULAR OUTPUT: Read G
  Word G = NIL;
  while(peekNonWS(outofSingular.in()) == 'G')
  {
    while(outofSingular.in().get() != '=');
    Word p = readSingularPoly(r,V,outofSingular.in());
    G = COMP(p,G);      
  }
  { string em; while(outofSingular.in() >> em && em != "[end]:"); }
  G = CINV(G);

  // SINGULAR INPUT: Find lowest order partials that are not all reduced to 0 by G
  Word M = NIL, k;
  for(k = 1; M == NIL; k++)
  {
    for(Word P = IPALLPARTIALS(r,A,k,1); P != NIL; P = RED(P))
    {
      intoSingular.out() 
	<< "reduce(" << WritePolyForSingular(r,FIRST(P),V) << ",G);" << endl;
      Word p = readSingularPoly(r,V,outofSingular.in());
      if (p != 0)
	M = COMP(p,M);
    }    
  }
  
  // SINGULAR INPUT: Compute groebner basis for G + all partials
  intoSingular.out() << "ideal J = ";
  for(Word Lp = G; Lp != NIL; Lp = RED(Lp))
    intoSingular.out() << WritePolyForSingular(r,FIRST(Lp),V) << ", ";
  for(Word Lp = M; Lp != NIL; Lp = RED(Lp))
    intoSingular.out() << WritePolyForSingular(r,FIRST(Lp),V) << (RED(Lp) != NIL ? ", " : ";\n");
  intoSingular.out() << "std(J);" << endl;
  intoSingular.out() << "\"[end]:\";" << endl;

  // SINGULAR OUTPUT:
  Word GBi = NIL;
  while(peekNonWS(outofSingular.in()) == '_')
  {
    while(outofSingular.in().get() != '=');
    Word p = readSingularPoly(r,V,outofSingular.in());
    GBi = COMP(p,GBi);      
  }
  { string em; while(outofSingular.in() >> em && em != "[end]:"); }
  GBi = CINV(GBi);
  
  if (LENGTH(GBi) == 1) 
  { 
    Word rs,ps; PSIMREP(r,FIRST(GBi),&rs,&ps); 
    if (rs == 0) return TRUE;
  }
  return GBi;
    
}