File: rootIsolation.mac

package info (click to toggle)
maxima 5.42.1-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 150,192 kB
  • sloc: lisp: 382,565; fortran: 14,666; perl: 14,365; tcl: 11,123; sh: 4,622; makefile: 2,688; ansic: 444; xml: 23; awk: 17; sed: 17
file content (814 lines) | stat: -rw-r--r-- 21,667 bytes parent folder | download | duplicates (6)
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
/* ------------------------------------------------------------------- */
/* SARAG - Root Isolation                                              */
/* by Fabrizio Caruso modified by Alexandre Le Meur and Marie-Françoise Roy */       

	
/* i-th Bernstein polynomial of degree p for c and d */ 
bernstein(p,i,c,d,var) :=
  binomial(p,i) * (var-c)^i * (d-var)^(p-i) / (d-c)^p;


/* -------------------------------------- */
/* Bounds on real roots                   */

/* Cauchy Root Upper Bound */
/* Lemma 10.2 */
/* (Defined for non-zero polynomials) */
cauchyRootUpperBound(pol,var) :=
  block([p,q,i,den],
    p : expandIf(p),
    p : degree(pol,var),
    q : 0,
    while coeff(pol,var,q)=0 do
       q : q + 1,
    den : abs(coeff(pol,var,p)),

    return(sum(abs(coeff(pol,var,i)),i,q,p)/den)
    ); /* end proc */


/* Cauchy Root Lower Bound for non-zero roots */
cauchyRootLowerBound(pol,var) :=
   block([p,q,i,den],
    p : expandIf(p),
    p : degree(pol,var),
    q : 0,
    while coeff(pol,var,q)=0 do
       q : q + 1,
    den : abs(coeff(pol,var,q)),
    return((sum(abs(coeff(pol,var,i)),i,q,p)/den)^(-1))
    ); /* end proc */



/* Prime Cauchy Root Upper Bound */
/* Lemma 10.5 */
primeCauchyRootUpperBound(pol,var) :=
  block([p,q,i,den],
    p : expandIf(p),
    p : degree(pol,var),
    q : 0,
    while coeff(pol,var,q)=0 do
       q : q + 1,
    den : coeff(pol,var,p)^2,
    return((p+1)* sum(coeff(pol,var,i)^2,i,q,p)/den)
    ); /* end proc */


/* Prime Cauchy Root Lower Bound for non-zero roots */
primeCauchyRootLowerBound(pol,var) :=
  block([p,q,i,den],
    p : expandIf(p),
    p : degree(pol,var),
    q : 0,
    while coeff(pol,var,q)=0 do
       q : q + 1,
    den : coeff(pol,var,q)^2,
    return(((p+1)* sum(coeff(pol,var,i)^2,i,q,p)/den)^(-1))
    ); /* end proc */


/* ----------------------------------------- */
/* Auxiliary routines for sign determination */

/* It determines the sign */
/* to the right of the left end of open interval  */
signToTheLeft(bern) :=
  block([i],
    i : 1,
    while(sgn(bern[i])=0) do 
      i : i+1,
    return(sgn(bern[i]))
    ); /* end proc */


/* It determines the sign */
/* to the left of the right end of open interval  */
signToTheLeft(bern) :=
  block([i],
    i : length(bern),
    while(sgn(bern[i])=0) do 
      i : i-1,
    return(sgn(bern[i]))
    ); /* end proc */




/* ------------------------------------- */
/* Splitting of Bezier curves */

/* Alg. 10.31 */
bernsteinSplit(coeffList, c, d,e) :=
  block([p : length(coeffList)-1,
         b : make_array('any),
         bern_ce,
         bern_ed,
         alpha, beta,
         i,j],
  
  b : make_array('any,p+1),
  for i : 0 thru p do
    b[i] : coeffList[i+1],

  bern_ce : [b[0]],
  bern_ed : [b[p]],
  alpha : (d-e)/(d-c),
  beta : (e-c)/(d-c),
  for i : 1 thru p do
    (
    for j : 0 thru p-i do
       b[j] : alpha * b[j] + beta * b[j+1],
    bern_ce : endcons(b[0],bern_ce),
  
    bern_ed : cons(b[p-i],bern_ed)

    ), /* end for */
  return([bern_ce,bern_ed])
  ); /* end proc */


/* Algorithm 10.34 */
specialBernsteinSplit(coeffList,c,d) :=
 block([p : length(coeffList)-1,
        bern_first,
        bern_second,
        b : make_array('any),
        i,j],
  
  b : make_array('any,p+1),
  for i : 0 thru p do
    b[i] : coeffList[i+1],

  bern_first : [2^p * b[0]],
  bern_second : [2^p * b[p]],

  for i : 1 thru p do
    (
    for j : 0 thru p-i do
        b[j] : b[j] + b[j+1],

   bern_first : endcons(2^(p-i) * b[0],bern_first),

   bern_second : cons(2^(p-i)*b[p-i],bern_second)

    ), /* end for */

  return([bern_first,bern_second])
  ); /* end proc */



/* Square free part of a polynomial */
squareFree(pol,var) := gcdFreePartWithZ(pol,diff(pol,var),var)[2];

/* ---------------------------------------------- */
/* Auxiliary routines for polynomial manipulation */

translated(pol,var,c) := subst(var-c,var,pol);

reciprocal(pol,var,p) := var^p * subst(1/var,var,pol);
 
/* contraction */
/* (defined for l different from zero) */
contracted(pol,var,l) := subst(var*l,var,pol);

/* auxiliary coefficients as defined in page 340 */
/* (in the book notation: a_(c,k))               */
specialCoeff(l,c,k) := 
  (-2^(l+k) + c*2^(l+1))/(2^k);
 


/* Generic base logarithm */
logInBase(val,base) :=
   log(val)/log(base); 

discreteLogInBaseTwo(val) :=
   block([i],
   i : 0,
   while 2^i < val do
     i : i + 1,
   return(i)
   ); /* end proc */

bernsteinCoeffList(pol,var,c,d) :=
  convert2Bernstein(pol,degree(pol,var),var,c,d);

/* Bernstein coefficients */
/* computed by using Proposition 10.24 */
convert2Bernstein(p,polDeg,var,c,d) :=
  block([pol,auxPol,transMinusC, conDMinusC, recP, transMinusOne,res,i],

pol:p,

    if polDeg<degree(pol,var) then ( print("bad degree, d must be more than degree(P)"),
    return(false))

    else

    pol : expandIf(pol),
    polDeg : max(degree(pol,var),polDeg),
    
    transMinusC : expand(translated(pol,var,-c)),
    conDMinusC : expand(contracted(transMinusC,var,d-c)),

    recP : expand(reciprocal(conDMinusC,var,polDeg)),

    transMinusOne : expand(translated(recP,var,-1)),

    auxPol : translated(reciprocal(
             contracted(translated(
             pol,var,-c),var,d-c),var,polDeg),var,-1),
    auxPol : ratsimp(auxPol,var),

    res : makelist(ratcoeff(auxPol,var,polDeg-i)/
                   binomial(polDeg,i),i,0,polDeg),
    return(res)
    ); /* end proc */


/* For a given squarefree polynonial SFP of degree p  */
/* Bernstein coefficients of the polynomial           */
/* 2^(k*p)*SFP in the Bernstein basis                 */
/* (specialCoeff(l,c,k),specialCoeff(l,c+1,k)))           */
bernsteinSpecialCoeffList(pol,var,leftEnd,rightEnd) :=
   bernsteinCoeffList(pol,var,leftEnd,rightEnd);





/* --------------------------------------------------- */
/* Real root isolation */

isListLess(lhs,rhs) :=
  if length(lhs) = 1 then
    if length(rhs) = 1 then
      lhs[1]<rhs[1]
    else
      lhs[1]<=rhs[1]
  else
    if length(rhs) = 1 then
      lhs[2]<=rhs[1]
    else
      lhs[1]<rhs[1];
   

/* Order Isolating List */
isListSort(isList) := 
  sort(isList,isListLess);


isListWithSignsLess(lhs,rhs) :=
  isListLess(first(lhs),first(rhs));

/* Order Isolating list with signs */
isListWithSignsSort(isListWithSigns) :=
  sort(isListWithSigns,isListWithSignsLess);



/* Real root isolation in an archimedean real closed field */
/* the output is a list of elements of the following form: */
/* single point : [p] */
/* interval : [[a,b],[c,k]], where c, k are the coeffs in a_{c,k} */

/* Aliases for "deCasteljauIsolateRoots..." */

/* Aliases for "deCasteljauIsolateRootsGeneral" */

deCasteljauIsolateRootsWithARCF(pol,var) := /* ARCF stands for Archimedean Real Closed Field */
  deCasteljauIsolateRootsGenStruct(pol,var,false);

deCasteljauIsolateRootsWithZ(pol,var) := 
  deCasteljauIsolateRootsGenStruct(pol,var,true); 

/* Aliases for "deCasteljauIsolateRootsGenStructVerbose" */

deCasteljauIsolateRootsGenStruct(pol,var,integer_flag) :=
  deCasteljauFindRootsGenStruct(pol,var,inf,integer_flag);

deCasteljauIsolateRootsBetweenWithARCF(pol,var,a,b) :=
  deCasteljauFindRootsGenIntGenStructCert(pol,var,inf,a,b,false,false)[1];

deCasteljauIsolateRootsBetweenWithZ(pol,var,a,b) :=
  deCasteljauFindRootsGenIntGenStructCert(pol,var,inf,a,b,true,false)[1];

deCasteljauIsolateRootsBetweenWithZCert(pol,var,a,b) :=
  deCasteljauFindRootsGenIntGenStructCert(pol,var,inf,a,b,true,true);


/* Aliases for "deCasteljauFindRootsGeneral..." */

/* Aliases for "deCasteljauFindRootsGeneral" */

deCasteljauFindRootsWithARCF(pol,var,threshold) :=
  deCasteljauFindRootsGenStruct(pol,var,threshold,false);

deCasteljauFindRootsWithZ(pol,var,threshold) :=
  deCasteljauFindRootsGenStruct(pol,var,threshold,true);

/* Aliases for "deCasteljauFindRootsGenStruct" */

deCasteljauFindRootsBetweenWithZ(pol,var,threshold,a,b) :=
  deCasteljauFindRootsGenIntGenStructCert(pol,var,threshold,a,b,true,false)[1];

deCasteljauFindRootsWithZ(pol,var,threshold) :=
  deCasteljauFindRootsGenStruct(pol,var,threshold,true);

deCasteljauFindRootsGenStruct(pol,var,threshold,integer_flag) :=
  deCasteljauFindRootsGenIntGenStructCert(pol,var,threshold,false,0,integer_flag,false)[1];

deCasteljauFindRootsGenStructCert(pol,var,threshold,integer_flag) :=
  deCasteljauFindRootsGenIntGenStructCert(pol,var,threshold,false,0,integer_flag,true)[1];

/* definition de deCasteljauFindRootsGenIntGenStructCert */

deCasteljauFindRootsGenIntGenStructCert(pol,var,threshold,a,b,integer_flag,modifCert) :=
  block([leftEnd,rightEnd,midPoint,n,sqPol,bCoeff,pos,deg,
         resList,item,sgnCh,splitRes,c,k,count,gcdTest,normFact,searchLeftEnd, searchRightEnd,mod,sg,certificate,P,resCert,bernIntSet,bsplit,lhsSplit, rhsSplit,bernInt],
  

  P : expandIf(pol),
  deg : degree(P,var),


  if a = false then
     (
     n : discreteLogInBaseTwo(cauchyRootUpperBound(P,var)),
     searchLeftEnd : -(2^n),
     searchRightEnd : 2^n
     ) /* end if */
  else
     (
     searchLeftEnd : a,
     searchRightEnd : b
     ), /* end else */

  sqPol : expand(factor(squareFree(P,var))), 
  
  resList : [],  
  resCert : [],
  if modifCert then ( 
  bCoeff : bernsteinCoeffList(P,var,searchLeftEnd,searchRightEnd)
  
     ) /* end if */
 else ( 
       bCoeff : bernsteinCoeffList(sqPol,var,searchLeftEnd,searchRightEnd)
             ), /* end else */
  sg: sgn(bCoeff[1]),
  normFact : 0, 

/* normFact is the multiplicative coefficient which makes the berstein coefficients integers*/

  if integer_flag then
     (
     normFact : apply(lcm,map(denom,bCoeff)),
     bCoeff : normFact*bCoeff
     ), /* end if */
  

  bernIntSet : [[[searchLeftEnd,searchRightEnd],normFact,bCoeff]],
  certificate : true,
  while not(bernIntSet=[]) and certificate do
    (
    item : first(bernIntSet),
    bernIntSet : rest(bernIntSet),
    
    bernInt : first(item),
    bCoeff : third(item),    

    leftEnd : first(bernInt),
    rightEnd : second(bernInt),

  if modifCert then 
   (
    if first(bCoeff)=0 then
      (
      certificate:false,
      resCert:[false,[[leftEnd],sqPol],true,false]
      )
    else
      ( 
      if last(bCoeff)=0 then
       (
       certificate:false,
       resCert:[false,[[rightEnd],sqPol],true,false]
       ) /* end if */
     ) /* end else */
    ), /* end if */
    sgnCh : signChanges(bCoeff),
   if (sgnCh = 1) and (rightEnd-leftEnd)<threshold then 

        (if modifCert then      /* Loop which is useful for the function in the certificate file */
         ((certificate : false),
         resCert : [false,[[leftEnd,rightEnd],sqPol],true,false]
               ) /* end if */
         else 
          resList : endcons([leftEnd,rightEnd],resList)
            ) /* end if */
    
    else 
       ( 

       midPoint : (leftEnd+rightEnd)/2,
       if (sgnCh>=1) then
          (
          if subst(midPoint,var,pol)=0 then
             (
             resList : endcons([midPoint],resList) 
                          ), /* end if */
          
         bsplit : bernsteinSplitWithZ(bCoeff,leftEnd,rightEnd,midPoint),
         lhsSplit : first(bsplit),

        rhsSplit : second(bsplit),
       
        bernIntSet : endcons([[leftEnd,midPoint],
                             normFact,lhsSplit],bernIntSet),
        bernIntSet : endcons([[midPoint,rightEnd],
                             normFact,rhsSplit],bernIntSet),

        if rightEnd-midPoint<1 then 

         ( normFact : normFact*2^deg ) /* end if */
   


                   ) /* end if */
      else ( if modifCert then 
               resCert : endcons([[leftEnd,rightEnd],normFact,bCoeff],resCert)
               
	      )  /* end else */
       ) /* end else */
    ), /* end while */
  if certificate then (
        if modifCert then
            if isListCertSort(resCert)[1][3][1]>0 then 
                  return([positive,compressCertificate(isListCertSort(resCert)),0,false,true])
            else 
                  return([negative,compressCertificate(isListCertSort(resCert)),0,false,true])
        else
        return([isListSort(resList),sqPol,false,false])
         ) /* end if */
  else 
  return(resCert)
  ); 
 /* end proc */


deCasteljauNumberOfRoots(pol,var) :=
  length(deCasteljauIsolateRoots(pol,var));  

deCasteljauNumberOfRootsBetween(pol,var,a,b) :=
  length(deCasteljauIsolateRootsBetween(pol,var,a,b));  

/* Refine an open interval describing a unique real root */
refineRoot(rootInterval,pol,var) :=
  block([leftEnd,rightEnd,midPoint,n,sqPol,bCoeffList,pos,
         resList,item,sgnCh,splitRes,c,k,count,gcdTest,
         lhs,rhs],
  leftEnd : rootInterval[1],
  rightEnd : rootInterval[2],
  midPoint : (leftEnd+rightEnd)/2,
  if subst(midPoint,var,pol)=0 then
    return([midPoint])
  else
    (
    resList : [],
    sqPol : expand(factor(squareFree(pol,var))),


    bCoeffList : bernsteinCoeffList(sqPol,var,leftEnd,rightEnd),

    splitRes : specialBernsteinSplit(bCoeffList,leftEnd,rightEnd),
    lhs : splitRes[1],
    rhs : splitRes[2],
    if signChanges(lhs)=1 then
      return([leftEnd,midPoint])
    else
      return([midPoint,rightEnd])
    ) /* end else */
); /* end proc */
  


/* Finds an intermidiate point (already in the right order) */
intermidiatePoint(lhs,rhs,poly,var) :=
  if length(lhs)=1 then
    if length(rhs) = 1 then
      (lhs[1]+rhs[1])/2
    else
       if not(lhs[1]=rhs[1]) then
         (lhs[1]+rhs[1])/2
       else
         intermidiatePoint(lhs,refineRoot(rhs,poly,var),poly,var)
  else
    if length(rhs)=1 then
      if not(lhs[2]=rhs[1]) then
        (lhs[2]+rhs[1])/2
      else
        intermidiatePoint(refineRoot(lhs,poly,var),rhs,poly,var)
    else
      if not(lhs[2]=rhs[1]) then
        (lhs[2]+rhs[1])/2
      else
        lhs[2];
  

/* -------------------------------------------------------- */
/* Sign or real roots */

/* Sign of real roots */
/* in an archimedean real closed field */

deCasteljauRootsSign(isolListForP,p,q,var) := 
  block([sfP,sfQ,g,commonPtList,nonCommonPtList,commonInList,nonCommonInList,
         candList,nCom,sfPCoeffList,gCoeffList,
         bound,i,l,c,k,
         leftEnd,rightEnd,midPt,item,bernG, bernSfP, bernQ,
         sgnM,sgnL,sgnR, bernSplitSfP, bernSplitG],

  p : expandIf(p),
  q : expandIf(q),
  sfP : expand(factor(squareFree(p,var))),  
  g : gcd(sfP,q),

  commonPtList : [],  /* In the book's notation : N(P) */
  commonInList : [],
  nonCommonPtList : [],
  nonCommonInList : [],
  candList : [], /* In the book's notation : Pos */
  nCom : [],
  for i : 1 thru length(isolListForP) do
    (
    
    if length(isolListForP[i])=1 then
       (
       sgnM : sgn(subst(isolListForP[i][1],var,q)),
       if sgnM = 0 then
          commonPtList : endcons([isolListForP[i],sgnM],commonPtList)
       else
          nonCommonPtList : endcons([isolListForP[i],sgnM],nonCommonPtList)
       ) /* end if */
    else
       (

       leftEnd : isolListForP[i][1],
       rightEnd : isolListForP[i][2], 
      
       candList : endcons([[bernsteinCoeffList(sfP,var,leftEnd,rightEnd),
                         bernsteinCoeffList(g,var,leftEnd,rightEnd)],
                         isolListForP[i]],
                         candList)
       ) /* end else */
    ), /* end for */

  while not(candList=[]) do
    (
    item : first(candList), 

    candList : rest(candList),
    bernG : item[1][2],
    bernSfP : item[1][1],
    leftEnd : item[2][1],
    rightEnd : item[2][2], 

    if signChanges(bernG) = 1 then
      commonInList : endcons([item[2],0],commonInList)
    else 
      if signChanges(bernG) = 0 then
        nCom : endcons([bernSfP,[leftEnd,rightEnd]],nCom)
      else
        (
        midPt : (leftEnd+rightEnd)/2,
        sgnL : subst(leftEnd,var,sqP),
        sgnM : subst(midPt,var,sqP),
        sgnR : subst(rightEnd,var,sqP),

        if sgnM = 0 then
           ( 
           if sgn(subst(midPt,var,q)) = 0 then
             commonPtList : endcons([[midPt],0],commonPtList)
           else 
             nonCommonPtList : endcons([[midPt],sgn(subst(midPt,var,q))],nonPtCommonList)
           ) /* end if */
        else
           (
           bernSplitSfP : specialBernsteinSplit(bernSfP,leftEnd,rightEnd),
           bernSplitG : specialBernsteinSplit(bernG,leftEnd,rightEnd),
           if sgnL*sgnM<0 then
              candList : endcons([[bernSplitSfP[1],bernSplitG[1]],
                               [leftEnd,midPt] 
                              ],candList)
           
           else
              candList : endcons([[bernSplitSfP[2],bernSplitG[2]],
                                [midPt,rightEnd] 
                              ],candList)
           ) /* end else */

        ) /* end else */
     ), /* end while */

/* second part */
  candList : nCom,

  while not(candList=[]) do
    (
    item : first(candList),
    candList : rest(candList),
    leftEnd : item[2][1],
    rightEnd : item[2][2],
   
    bernSfP : item[1],    
   
    bernQ : bernsteinCoeffList(q,var,leftEnd,rightEnd),
   
    if signChanges(bernQ)=0 then
      (

      i : 1,
      while(bernQ[i]=0) do
        i : i+1,
      

      nonCommonInList : endcons([[leftEnd,rightEnd],
                               sgn(bernQ[i])],nonCommonInList)
      ) /* end if */
    else
      (
      midPt : (leftEnd+rightEnd)/2,
      sgnM : sgn(subst(midPt,var,sfP)),

      i : 1,
      while(bernSfP[i]=0) do
        i : i+1,
      sgnL : sgn(bernSfP[i]),

      i : length(bernSfP),
      while(bernSfP[i]=0) do
        i : i - 1,
      sgnR : sgn(bernSfP[i]),

      
      if sgnM=0 then
         nonCommonPtList : endcons([[midPt],sgn(subst(midPt,var,q))],nonCommonPtList)
      else
         (
         if sgnL*sgnM<0 then
            candList : endcons([bernsteinCoeffList(sfP,var,leftEnd,midPt),
                             [leftEnd,midPt]
                            ],candList)
         else
            candList : endcons([bernsteinCoeffList(sfP,var,midPt,rightEnd),
                             [midPt,rightEnd]
                            ],candList)
         ) /* end else */
      ) /* end ele */
    ), /* end if */

  return([[nonCommonPtList,nonCommonInList],[commonPtList,commonInList]])
  ); /* end proc */


deCasteljauSignsAtRoots(isolListForP,p,q,var) := 
  isListWithSignsSort(lambda([l],append(l[1][1],l[1][2],
                    l[2][1],l[2][2]))(
         deCasteljauRootsSign(isolListForP,p,q,var)));


/* --------------------------------------- */
/* Sign Comparison of real roots */
/* in the Z ring */


/* Rewrite a set of isolating points with signs as intervals and removes the sign */
makeAsIn(ptList) :=
  if length(ptList)=0 then 
    []
  else 
    cons([ptList[1][1][1],ptList[1][1][1]],makeAsIn(rest(ptList)));

/* Rewrite dummy intervals as points */
makeAsPt(inList) :=
  if length(inList)=0 then
   []
  else
   if inList[1][1]=inList[1][2] then
     cons([inList[1][1]],makeAsPt(rest(inList)))
   else
     cons(inList[1],makeAsPt(rest(inList)));


/* Removes the sign */
removeSign(inList) :=
  if length(inList)=0 then
    []
  else
    cons(first(first(inList)),removeSign(rest(inList)));

/* Interval Intersection */
intersectIn(inList1,inList2) := 
  block([i,len1,len2, leftEnd1,rightEnd1,leftEnd2,rightEnd2,res],

  len1 : length(inList1),
  len2 : length(inList2),
    

  if len1=0 then
    return(inList2)
  else
    if len2=0 then
      return(intList1),

  res : [],
  i : 1,
  
  while (i<=len1)  do
    (
    leftEnd1 : inList1[i][1],
    leftEnd2 : inList2[i][1],
    rightEnd1 : inList1[i][2],
    rightEnd2 : inList2[i][2],
    
    if (leftEnd1<=leftEnd2) and
       (leftEnd2<=rightEnd1) then
       (
       res : endcons([leftEnd2,min(rightEnd1,rightEnd2)],res),
       i : i + 1
       )   /* end if */
    else 
      if (leftEnd2<=leftEnd1) and
         (leftEnd1<=rightEnd2) then
         (
         res : endcons([leftEnd1,min(rightEnd1,rightEnd2)],res),
         i : i + 1
         ) /* end if */
      else
         (
         print("impossible configuration"),
         return([])
         ) /* end else */
    ), /* end while */
    return(res)
    

  );  /* end proc */



/* Evaluation of the signs of two polynomials at their roots */
/* in an archimedean real closed field */

deCasteljauEvaluateSignsAtRoots(p,q,var):=
  block([l,isolP,isolQ,
         signComPtP,signComInP,signComPtQ,signComInQ,
         signNComPtP,signNComInP,signNComPtQ,signNComInQ,
         signP,signQ,signComP,signNComP,signComQ,signNComQ,
         signComAsInP,signComAsInQ,comIn],
  
  p : expandIf(p),
  q : expandIf(q),

  isolP : deCasteljauIsolateRoots(p,var),

  isolQ : deCasteljauIsolateRoots(q,var),
  signP : deCasteljauRootsSign(isolP,p,q,var),
  signComP : second(signP),

  signComPtP : first(signComP),
  signComInP : second(signComP),
  signNComP : first(signP), 
  signNComP : sort(append(first(signNComP),second(signNComP))),

  signQ : deCasteljauRootsSign(isolQ,q,p,var), 
  signComQ : second(signQ),

  signComPtQ : signComQ[1],
  signComInQ : signComQ[2],
  signNComQ : first(signQ),
  signNComQ : sort(append(first(signNComQ),second(signNComQ))),

  signComAsInP : sort(append(makeAsIn(signComPtP),removeSign(signComInP))),
  signComAsInQ : sort(append(makeAsIn(signComPtQ),removeSign(signComInQ))),

comIn : makeAsPt(intersectIn(signComAsInP,signComAsInQ)),
return([comIn,signNComP,signNComQ])
  ); /* end proc */


/* Real root sample points */
/* for an archimedean real closed field */
realSamplePointsARCF(polList) :=
  block([i,j,listSize,compRes],
  listSize : length(polList),
  for i : 1 thru listSize do
    for j : 1 thru listSize do
      compRes : realRootComparisonARCF(p,q,var),
  return([0]) 
  );