Statistiques
| Branche: | Révision :

cvp / src / cvp-run-02.sage @ 6a967c2d

Historique | Voir | Annoter | Télécharger (7,77 ko)

1
#! /opt/sage/sage
2
# @file cvp-run-02.sage
3
# Compute a polynomial from the CVP of the function vector.
4
# Return the polynomial, the error and the (one of) point(s) where this error
5
# is reached.
6
#
7
# @par Notes
8
# Change from version 01:
9
#   Instead of computing a single vector, compute several of them et pick
10
#   the one that gives the smallest error.
11
#   The points list is only used to complement the chebyshev points lists
12
#   that are computed at each iteration.
13
#
14
scriptName = os.path.basename(__file__)
15
var('x')
16
contFracMaxErr = 10^-7
17
#
18
def initialize_env():
19
    """
20
    Load all necessary modules.
21
    """
22
    compiledSpyxDir = \
23
        "/home/storres/recherche/arithmetique/pobysoPythonSage/compiledSpyx"
24
    if compiledSpyxDir not in sys.path:
25
        sys.path.append(compiledSpyxDir)
26
    if not 'mpfi' in sage.misc.cython.standard_libs:
27
        sage.misc.cython.standard_libs.append('mpfi')
28
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sollya_lib.sage")
29
#    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageMpfr.spyx")
30
#    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageGMP.spyx")
31
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/pobyso.py")
32
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageSLZ.sage")
33
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageNumericalOperations.sage")
34
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageRationalOperations.sage")
35
    # Matrix operations are loaded by polynomial operations.
36
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage")
37
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageRunSLZ.sage")
38
    load("/home/storres/recherche/arithmetique/cvp/src/functions_for_cvp.sage")
39

    
40
# End initialize_env.
41
#
42
initialize_env()
43
from sageMpfr import *
44
from sageGMP  import *
45
import sys
46
#
47
def usage(scriptName):
48
    write = sys.stderr.write
49
    write("Usage:\n")
50
    write("  " + scriptName + " <func> <lowerBound> <upperBound> <coeffsExpList> \n")
51
    write(" " * (len(scriptName) + 3))
52
    write("<coeffsPrecList> <minCoeffsBoundExpList>\n")
53
    write(" " * (len(scriptName) + 3))
54
    write("<maxCoeffsBoundExpList> <pointList> <precision>\n")
55
    write("\nArguments:\n")
56
    write("  func\n")
57
    write("  lowerBound\n")
58
    write("  upperBound\n")
59
    write("  coeffsExpList\n")
60
    write("  coeffsExpList\n")
61
    write("  minCoeffsBoundExpList\n")
62
    write("  maxCoeffsBoundExpList\n")
63
    write("  maxCoeffsBoundExpList\n")
64
    write("  maxCoeffsBoundExpList\n\n")
65
    ##
66
    sys.exit(1)
67
# End usage
68
#
69
argsCount = len(sys.argv)
70
#
71
## Arguments recovery
72
if argsCount < 10:
73
    usage(scriptName)
74
## Shell weird behavior  may oblige to single quote arguments.
75
#  They should be removed before evaluation to avoid inadequate typing. 
76
funcAsString                    = re.sub("^'|'$", "", sys.argv[1])
77
lowerBoundAsString              = re.sub("^'|'$", "", sys.argv[2])
78
upperBoundAsString              = re.sub("^'|'$", "", sys.argv[3])
79
coeffsExpListAsString           = re.sub("^'|'$", "", sys.argv[4])
80
coeffsPrecListAsString          = re.sub("^'|'$", "", sys.argv[5])
81
minCoeffsBoundExpListAsString   = re.sub("^'|'$", "", sys.argv[6])
82
maxCoeffsBoundExpListAsString   = re.sub("^'|'$", "", sys.argv[7])
83
pointsListAsString              = re.sub("^'|'$", "", sys.argv[8])
84
precisionAsString               = re.sub("^'|'$", "", sys.argv[9])
85
#
86
## Arguments conversion
87
### Create first the rational field that will be used throughout the script.
88
precision               = int(precisionAsString)
89
realField               = RealField(precision)
90
#
91
func(x)                 = sage_eval(funcAsString, cmds="var('x')")
92
try:
93
    lowerBound          = realField(lowerBoundAsString)
94
except:
95
    lowerBoundAsRat     = QQ(lowerBoundAsString)
96
    lowerBound          = realField(lowerBoundAsRat)
97
try:
98
    upperBound          = realField(upperBoundAsString)
99
except:
100
    upperBoundAsRat     = QQ(upperBoundAsString)
101
    upperBound          = realField(upperBoundAsRat)
102
coeffsExpList           = sage_eval(coeffsExpListAsString)
103
coeffsPrecList          = sage_eval(coeffsPrecListAsString)
104
minCoeffsBoundExpList   = sage_eval(minCoeffsBoundExpListAsString)
105
maxCoeffsBoundExpList   = sage_eval(maxCoeffsBoundExpListAsString)
106
pointsList              = sage_eval(pointsListAsString)
107
#
108
maxExponent             = max(coeffsExpList)
109
## Debug printing.
110
sys.stderr.write("func: " + func._assume_str().replace("_SAGE_VAR_","",100) + \
111
                 "\n")
112
sys.stderr.write("precision: " + str(precision) + "\n")
113
sys.stderr.write("lowerBound: " + str(lowerBound) + "\n")
114
sys.stderr.write("upperBound: " + str(upperBound) + "\n")
115
sys.stderr.write("coeffsExpList: " + str(coeffsExpList) + "\n")
116
sys.stderr.write("coeffsPrecList: " + str(coeffsPrecList) + "\n")
117
sys.stderr.write("minCoeffsBoundExpList: " + str(minCoeffsBoundExpList) + "\n")
118
sys.stderr.write("maxCoeffsBoundExpList: " + str(maxCoeffsBoundExpList) + "\n")
119
sys.stderr.write("pointsList: " + str(pointsList) + "\n")
120
sys.stderr.write("maxExponent: " + str(maxExponent) + "\n")
121
#
122
##
123
chebyshevPointsList = []
124
minMaxErrPoint  = oo
125
minMaxErr       = oo
126
for pointsNum in xrange(maxExponent+1, round(maxExponent * 8/2)+1):
127
    ## Compute the Chebyshev points and append the user given points list.
128
    chebyshevPointsList =  \
129
        cvp_chebyshev_maxis_for_interval(lowerBound, 
130
                                         upperBound, 
131
                                         pointsNum,
132
                                         realField, 
133
                                         contFracMaxErr)
134
    #print "cr-02 - Chebyshe points list computed."
135
    chebyshevPointsList = chebyshevPointsList + pointsList
136
    #
137
    ## Filter out those points that do not have a defined image.
138
    chebyshevPointsList = cvp_filter_out_undefined_image(chebyshevPointsList, func)
139
    #
140
    ##
141
    poly = cvp_cvp_polynomial(func, 
142
                              lowerBound, 
143
                              upperBound,
144
                              coeffsExpList,
145
                              coeffsPrecList, 
146
                              minCoeffsBoundExpList, 
147
                              maxCoeffsBoundExpList,
148
                              chebyshevPointsList,
149
                              realField)
150
    #sys.stderr.write(str(poly) + "\n")
151
    (maxisList,maxErr)  = cvp_polynomial_error_func_maxis(func,
152
                                                          poly, 
153
                                                          lowerBound, 
154
                                                          upperBound,
155
                                                          realField,
156
                                                          contFracMaxErr)
157
    ## In some cases
158
    #print "cr-02 - mcr-02 maxis list:", maxisList
159
    #print "cr-02 - maxErr:", maxErr
160
    #polyRing = PolynomialRing(realField, func.variables()[0])
161
    #errFunc(x) = func(x) - polyRing(poly)
162
    errFunc(x) = func(x) - poly(x)
163
    if len(maxisList) != 0:
164
        (maxErrPoint, maxErrPointErr) = \
165
            cvp_func_abs_max_for_points(errFunc,
166
                                        maxisList,
167
                                        realField)
168
        sys.stderr.write("# points: " + str(pointsNum))
169
        sys.stderr.write(" - MaxErr: " + str(maxErr.n(prec=53)))
170
        sys.stderr.write(" - MaxErrPoint: " + str(maxErrPoint) + "\n")
171
        if maxErr < minMaxErr:
172
            minMaxErr      = maxErr
173
            minMaxErrPoint = maxErrPoint
174
# End for
175
sys.stdout.write(str(minMaxErrPoint) + " " + str(minMaxErr.n(prec=53)) + "\n")
176
## Use int(0) as argument for sys.exit() in Sage because otherwise
177
# sys.exit(0) -> sys.exit(Integer(0)) (where Integer(0) is a Sage
178
# object, not the integer value 0 expected by Python) hence Python 
179
# returns 1!
180
sys.exit(int(0))