Index: pobyso.py
===================================================================
--- pobyso.py	(revision 217)
+++ pobyso.py	(revision 218)
@@ -90,7 +90,7 @@
     return(sollya_lib_absolute(None))
 
 def pobyso_autoprint(arg):
-    sollya_lib_autoprint(arg,None)
+    sollya_lib_autoprint(arg, None)
 
 def pobyso_autoprint_so_so(arg):
     sollya_lib_autoprint(arg,None)
@@ -369,17 +369,18 @@
     ## Precision. If a precision is given, convert the polynomial
     #  into the right polynomial field. If not convert it straight
     #  to Sollya.
-    if not precSa is None:
-        RRR = RealField(precSa)
-        ## Create a Sage polynomial in the "right" precision.
-        P_RRR = RRR[polySa.variables()[0]]
-        polyFloatSa = P_RRR(polySa)
-    else:
-        polyFloatSa = polySa
+    sollyaPrecChanged = False 
+    (curSollyaPrecSo, curSollyaPrecSa) = pobyso_get_prec_so_so_sa()
+    if precSa is None:
         precSa = polySa.parent().base_ring().precision()
+    if (precSa != curSollyaPrecSa):
+        precSo = pobyso_constant_from_int(precSa)
+        pobyso_set_prec_so_so(precSo)
+        sollya_lib_clear_obj(precSo)
+        sollyaPrecChanged = True    
     ## Get exponents and coefficients.
-    exponentsSa = polyFloatSa.exponents()
-    coefficientsSa = polyFloatSa.coefficients()
+    exponentsSa     = polySa.exponents()
+    coefficientsSa  = polySa.coefficients()
     ## Build the polynomial.
     polySo = None
     for coefficientSa, exponentSa in zip(coefficientsSa, exponentsSa):
@@ -393,20 +394,21 @@
         monomialSo = sollya_lib_build_function_pow(
                        sollya_lib_build_function_free_variable(),
                        exponentSo)
+        polyTermSo = sollya_lib_build_function_mul(coefficientSo,
+                                                       monomialSo)
         if polySo is None:
-            polySo = sollya_lib_build_function_mul(coefficientSo,
-                                                   monomialSo)
+            polySo = polyTermSo
         else:
-            polyTermSo = sollya_lib_build_function_mul(coefficientSo,
-                                                       monomialSo)
             polySo = sollya_lib_build_function_add(polySo, polyTermSo)
+    if sollyaPrecChanged:
+        pobyso_set_prec_so_so(curSollyaPrecSo)
+        sollya_lib_clear_obj(curSollyaPrecSo)
     return polySo
 # End pobyso_float_poly_sa_so    
 
 def pobyso_float_poly_so_sa(polySo, realFieldSa=None):
     """
     Convert a Sollya polynomial into a Sage floating-point polynomial.
-    We assume that the polynomial is in canonical form.
     If no realField is given, a RealField corresponding to the maximum 
     precision of the coefficients is internally computed.
     The real field is not returned but can be easily retrieved from 
@@ -415,18 +417,17 @@
     - (optional) compute the RealField of the coefficients;
     - convert the Sollya expression into a Sage expression;
     - convert the Sage expression into a Sage polynomial
-    TODO: the canonical thing for the polynomial.
     """    
     if realFieldSa is None:
         expressionPrecSa = pobyso_get_max_prec_of_exp_so_sa(polySo)
-        print "Maximum precision of Sollya polynomial coefficients:", expressionPrecSa
+        #print "Maximum precision of Sollya polynomial coefficients:", expressionPrecSa
         realFieldSa      = RealField(expressionPrecSa)
     #print "Sollya expression before...",
     #pobyso_autoprint(polySo)
 
     expressionSa = pobyso_get_sage_exp_from_sollya_exp_so_sa(polySo,
                                                              realFieldSa)
-    #print "...Sollya expression after.",
+    #print "...Sollya expression after."
     #pobyso_autoprint(polySo)
     polyVariableSa = expressionSa.variables()[0]
     polyRingSa     = realFieldSa[str(polyVariableSa)]
@@ -575,7 +576,10 @@
     The precision of the floating-point number returned is that of the Sollya
     constant.
     """
+    #print "Before computing precision of variable..."
+    #pobyso_autoprint(constExpSo)
     precisionSa  = pobyso_get_prec_of_constant_so_sa(constExpSo)
+    #print "precisionSa:", precisionSa
     ## If the expression can not be exactly converted, None is returned.
     #  In this case opt for the Sollya current expression.
     if precisionSa is None:
@@ -641,7 +645,7 @@
 
 def pobyso_get_head_function_so_sa(expressionSo):
     functionType = c_int(0)
-    sollya_lib_get_head_function(byref(functionType), expressionSo, None)
+    sollya_lib_get_head_function(byref(functionType), expressionSo)
     return int(functionType.value)
 
 def pobyso_get_interval_from_range_so_sa(soRange, realIntervalFieldSa = None ):
@@ -813,10 +817,13 @@
     Tries to find a precision to represent ctExpSo without rounding.
     If not possible, returns None.
     """
+    #print "Entering pobyso_get_prec_of_constant_so_sa..."
     prec = c_int(0)
     retc = sollya_lib_get_prec_of_constant(byref(prec), ctExpSo, None)
     if retc == 0:
+        #print "pobyso_get_prec_of_constant_so_sa failed."
         return None
+    #print "...exiting pobyso_get_prec_of_constant_so_sa."
     return int(prec.value)
 
 def pobyso_get_prec_of_range_so_sa(rangeSo):
@@ -876,7 +883,7 @@
         #print "This is a constant"
         return pobyso_get_constant_as_rn_with_rf_so_sa(sollyaExpSo, realFieldSa)
     elif operatorSa == SOLLYA_BASE_FUNC_FREE_VARIABLE:
-        #print "This is free variable"
+        #print "This is the free variable"
         return eval(sollyaLibFreeVariableName)
     else:
         print "Unexpected"
@@ -1123,15 +1130,15 @@
                                                            maxPrecSo,
                                                            maxErrSo):
     print "Input arguments:"
-    pobyso_autoprint(polySo)
-    pobyso_autoprint(funcSo)
-    pobyso_autoprint(icSo)
-    pobyso_autoprint(intervalSo)
-    pobyso_autoprint(itpSo)
-    pobyso_autoprint(ftpSo)
-    pobyso_autoprint(maxPrecSo)
-    pobyso_autoprint(maxErrSo)
-    print "________________"
+    #pobyso_autoprint(polySo)
+    #pobyso_autoprint(funcSo)
+    #pobyso_autoprint(icSo)
+    #pobyso_autoprint(intervalSo)
+    #pobyso_autoprint(itpSo)
+    #pobyso_autoprint(ftpSo)
+    #pobyso_autoprint(maxPrecSo)
+    #pobyso_autoprint(maxErrSo)
+    #print "________________"
     
     ## Higher order function see: 
     #  http://effbot.org/pyfaq/how-do-you-make-a-higher-order-function-in-python.htm
@@ -1148,17 +1155,25 @@
     
     #
     degreeSa        = pobyso_polynomial_degree_so_sa(polySo)
+    print "degreeSa:", degreeSa
     ratio           = precision_decay_ratio_function(degreeSa)
+    print "ratio:", ratio
     itpSa           = pobyso_constant_from_int_so_sa(itpSo)
+    print "itpsSa:", itpSa
     ftpSa           = pobyso_constant_from_int_so_sa(ftpSo)
+    print "ftpSa:", ftpSa
     maxPrecSa       = pobyso_constant_from_int_so_sa(maxPrecSo)
+    print "maxPrecSa:", maxPrecSa
     maxErrSa        = pobyso_get_constant_as_rn_so_sa(maxErrSo)
-    resPolySo       = pobyso_constant_0_sa_so()
+    print "maxErrSa:", maxErrSa
     lastResPolySo   = None
+    lastInfNormSo   = None
+    print "About to enter the while loop..."
     while True: 
+        resPolySo   = pobyso_constant_0_sa_so()
         pDeltaSa    = ftpSa - itpSa
         for indexSa in reversed(xrange(0,degreeSa+1)):
-            print "Index:", indexSa
+            #print "Index:", indexSa
             indexSo = pobyso_constant_from_int_sa_so(indexSa)
             #pobyso_autoprint(indexSo)
             #print ratio(indexSa)
@@ -1177,56 +1192,73 @@
             #pobyso_autoprint(cmonrSo)
             resPolySo = sollya_lib_build_function_add(resPolySo,
                                                       cmonrSo)
-            pobyso_autoprint(resPolySo)
+            #pobyso_autoprint(resPolySo)
         # End for index
         freeVarSo     = sollya_lib_build_function_free_variable()
         changeVarSo   = sollya_lib_sub(freeVarSo, icSo)
         resPolyCvSo   = sollya_lib_evaluate(resPolySo, changeVarSo)
-        errFuncSo = sollya_lib_build_function_sub(funcSo, resPolyCvSo)
-        infNormSo = sollya_lib_dirtyinform(errFuncSo, intervalSo)
-        print "Infnorm (Sollya):", ; pobyso_autoprint(infNormSo)
+        errFuncSo = sollya_lib_build_function_sub(sollya_lib_copy_obj(funcSo), 
+                                                  resPolyCvSo)
+        infNormSo = sollya_lib_dirtyinfnorm(errFuncSo, intervalSo)
         cerrSa    = pobyso_get_constant_as_rn_so_sa(infNormSo)
-        sollya_lib_clear_obj(resPolyCvSo)
-        sollya_lib_clear_obj(infNormSo)
-        print "Infnorm  (Sage):", cerrSa
+        print "Infnorm (Sollya):", pobyso_autoprint(infNormSo)
+        sollya_lib_clear_obj(errFuncSo)
+        #print "Infnorm  (Sage):", cerrSa
         if (cerrSa > maxErrSa):
             print "Error is too large."
-            if not lastResPolySo is None:
+            if lastResPolySo is None:
                 print "Enlarging prec."
                 ntpSa = floor(ftpSa + ftpSa/50)
                 ## Can't enlarge (numerical)
                 if ntpSa == ftpSa:
-                    sollya_lib_clear_obj(lastResPolySo)
                     sollya_lib_clear_obj(resPolySo)
                     return None
                 ## Can't enlarge (not enough precision left)
                 if ntpSa > maxPrecSa:
-                    sollya_lib_clear_obj(lastResPolySo)
                     sollya_lib_clear_obj(resPolySo)
                     return None
                 ftpSa = ntpSa
-                lastResPolySo = resPolySo
                 continue
             ## One enlargement took place.
             else:
+                print "Exit with the last before last polynomial."
                 sollya_lib_clear_obj(resPolySo)
-                return lastResPolySo
-        # cerrSa <= maxErrSa: scrap more bits.
+                sollya_lib_clear_obj(infNormSo)
+                return (lastResPolySo, lastInfNormSo)
+        # cerrSa <= maxErrSa: scrap more bits, possibly.
         else:
-            print "Shrinking prec."
-            ntpSa = floor(ftpSa - ftpSa/50)
-            ## Can't shrink (numerical)
-            if ntpSa == ftpSa:
+            print "Error is too small"
+            if cerrSa <= (maxErrSa/2): 
+                print "Shrinking prec."
+                ntpSa = floor(ftpSa - ftpSa/50)
+                ## Can't shrink (numerical)
+                if ntpSa == ftpSa:
+                    if not lastResPolySo is None:
+                        sollya_lib_clear_obj(lastResPolySo)
+                    if not lastInfNormSo is None:
+                        sollya_lib_clear_obj(lastInfNormSo)
+                    return (resPolySo, infNormSo)
+                ## Can't shrink (not enough precision left)
+                if ntpSa <= itpSa:
+                    if not lastResPolySo is None:
+                        sollya_lib_clear_obj(lastResPolySo)
+                    if not lastInfNormSo is None:
+                        sollya_lib_clear_obj(lastInfNormSo)
+                    return (resPolySo, infNormSo)
+                ftpSa = ntpSa
                 if not lastResPolySo is None:
                     sollya_lib_clear_obj(lastResPolySo)
-                return resPolySo
-            ## Can't enlarge (not enough precision left)
-            if ntpSa <= itpSa:
+                if not lastInfNormSo is None:
+                    sollya_lib_clear_obj(lastInfNormSo)
+                lastResPolySo = resPolySo
+                lastInfNormSo = infNormSo
+                continue
+            else: # Error is not that small, just return
                 if not lastResPolySo is None:
                     sollya_lib_clear_obj(lastResPolySo)
-                return resPolySo
-            ftpSa = ntpSa
-            continue
+                if not lastInfNormSo is None:
+                    sollya_lib_clear_obj(lastInfNormSo)
+                return (resPolySo, infNormSo)                
     # End wile True
     return None
 # End pobyso_polynomial_coefficients_progressive_truncate_so_so.
