cvp / src / cvp-run-02.sage @ 6bcf334c
Historique | Voir | Annoter | Télécharger (8,49 ko)
1 | 1c8b6220 | Serge Torres | #! /opt/sage/sage |
---|---|---|---|
2 | 1c8b6220 | Serge Torres | # @file cvp-run-02.sage |
3 | 1c8b6220 | Serge Torres | # Compute a polynomial from the CVP of the function vector. |
4 | 1c8b6220 | Serge Torres | # Return the polynomial, the error and the (one of) point(s) where this error |
5 | 1c8b6220 | Serge Torres | # is reached. |
6 | 1c8b6220 | Serge Torres | # |
7 | 1c8b6220 | Serge Torres | # @par Notes |
8 | 1c8b6220 | Serge Torres | # Change from version 01: |
9 | e6d557f3 | Serge Torres | # Take the approximation mode as an argument. |
10 | e6d557f3 | Serge Torres | # Instead of computing a single vector, compute several of them and pick |
11 | 1c8b6220 | Serge Torres | # the one that gives the smallest error. |
12 | e6d557f3 | Serge Torres | # The points list is only used to complement the Chebyshev points lists |
13 | 1c8b6220 | Serge Torres | # that are computed at each iteration. |
14 | 1c8b6220 | Serge Torres | # |
15 | 1c8b6220 | Serge Torres | scriptName = os.path.basename(__file__) |
16 | 1c8b6220 | Serge Torres | var('x') |
17 | 1c8b6220 | Serge Torres | contFracMaxErr = 10^-7 |
18 | 1c8b6220 | Serge Torres | # |
19 | 1c8b6220 | Serge Torres | def initialize_env(): |
20 | 1c8b6220 | Serge Torres | """ |
21 | 1c8b6220 | Serge Torres | Load all necessary modules. |
22 | 1c8b6220 | Serge Torres | """ |
23 | 1c8b6220 | Serge Torres | compiledSpyxDir = \ |
24 | 1c8b6220 | Serge Torres | "/home/storres/recherche/arithmetique/pobysoPythonSage/compiledSpyx" |
25 | 1c8b6220 | Serge Torres | if compiledSpyxDir not in sys.path: |
26 | 1c8b6220 | Serge Torres | sys.path.append(compiledSpyxDir) |
27 | e6d557f3 | Serge Torres | """ |
28 | e6d557f3 | Serge Torres | As of Sage versions above or equal to 7.0, appending MPFI is not needed and |
29 | e6d557f3 | Serge Torres | crashes the program. |
30 | 1c8b6220 | Serge Torres | if not 'mpfi' in sage.misc.cython.standard_libs: |
31 | 1c8b6220 | Serge Torres | sage.misc.cython.standard_libs.append('mpfi') |
32 | e6d557f3 | Serge Torres | """ |
33 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sollya_lib.sage") |
34 | 1c8b6220 | Serge Torres | # load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageMpfr.spyx") |
35 | 1c8b6220 | Serge Torres | # load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageGMP.spyx") |
36 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/pobyso.py") |
37 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageSLZ.sage") |
38 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageNumericalOperations.sage") |
39 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageRationalOperations.sage") |
40 | 1c8b6220 | Serge Torres | # Matrix operations are loaded by polynomial operations. |
41 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage") |
42 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageRunSLZ.sage") |
43 | 1c8b6220 | Serge Torres | load("/home/storres/recherche/arithmetique/cvp/src/functions_for_cvp.sage") |
44 | 1c8b6220 | Serge Torres | |
45 | 1c8b6220 | Serge Torres | # End initialize_env. |
46 | 1c8b6220 | Serge Torres | # |
47 | 1c8b6220 | Serge Torres | initialize_env() |
48 | 1c8b6220 | Serge Torres | from sageMpfr import * |
49 | 1c8b6220 | Serge Torres | from sageGMP import * |
50 | 1c8b6220 | Serge Torres | import sys |
51 | 1c8b6220 | Serge Torres | # |
52 | 1c8b6220 | Serge Torres | def usage(scriptName): |
53 | 1c8b6220 | Serge Torres | write = sys.stderr.write |
54 | 1c8b6220 | Serge Torres | write("Usage:\n") |
55 | e6d557f3 | Serge Torres | write(" " + scriptName + " <func> <lowerBound> <upperBound> <mode> <coeffsExpList> \n") |
56 | 1c8b6220 | Serge Torres | write(" " * (len(scriptName) + 3)) |
57 | 1c8b6220 | Serge Torres | write("<coeffsPrecList> <minCoeffsBoundExpList>\n") |
58 | 1c8b6220 | Serge Torres | write(" " * (len(scriptName) + 3)) |
59 | 1c8b6220 | Serge Torres | write("<maxCoeffsBoundExpList> <pointList> <precision>\n") |
60 | 1c8b6220 | Serge Torres | write("\nArguments:\n") |
61 | 1c8b6220 | Serge Torres | write(" func\n") |
62 | 1c8b6220 | Serge Torres | write(" lowerBound\n") |
63 | 1c8b6220 | Serge Torres | write(" upperBound\n") |
64 | e6d557f3 | Serge Torres | write(" mode one of \"abs\" or \"rel\"\n") |
65 | 1c8b6220 | Serge Torres | write(" coeffsExpList\n") |
66 | 6bcf334c | Serge Torres | write(" coeffsPrecList\n") |
67 | 1c8b6220 | Serge Torres | write(" minCoeffsBoundExpList\n") |
68 | 1c8b6220 | Serge Torres | write(" maxCoeffsBoundExpList\n") |
69 | 6bcf334c | Serge Torres | write(" pointsList\n") |
70 | 6bcf334c | Serge Torres | write(" precision\n\n") |
71 | 1c8b6220 | Serge Torres | ## |
72 | 1c8b6220 | Serge Torres | sys.exit(1) |
73 | 1c8b6220 | Serge Torres | # End usage |
74 | 1c8b6220 | Serge Torres | # |
75 | 1c8b6220 | Serge Torres | argsCount = len(sys.argv) |
76 | 1c8b6220 | Serge Torres | # |
77 | 1c8b6220 | Serge Torres | ## Arguments recovery |
78 | e6d557f3 | Serge Torres | if argsCount < 11: |
79 | 1c8b6220 | Serge Torres | usage(scriptName) |
80 | 6bcf334c | Serge Torres | ## Shell weird behavior may oblige to single quote arguments. |
81 | 3296902f | Serge Torres | # They should be removed before evaluation to avoid inadequate typing. |
82 | 3296902f | Serge Torres | funcAsString = re.sub("^'|'$", "", sys.argv[1]) |
83 | 3296902f | Serge Torres | lowerBoundAsString = re.sub("^'|'$", "", sys.argv[2]) |
84 | 3296902f | Serge Torres | upperBoundAsString = re.sub("^'|'$", "", sys.argv[3]) |
85 | e6d557f3 | Serge Torres | modeAsString = re.sub("^'|'$", "", sys.argv[4]) |
86 | e6d557f3 | Serge Torres | coeffsExpListAsString = re.sub("^'|'$", "", sys.argv[5]) |
87 | e6d557f3 | Serge Torres | coeffsPrecListAsString = re.sub("^'|'$", "", sys.argv[6]) |
88 | e6d557f3 | Serge Torres | minCoeffsBoundExpListAsString = re.sub("^'|'$", "", sys.argv[7]) |
89 | e6d557f3 | Serge Torres | maxCoeffsBoundExpListAsString = re.sub("^'|'$", "", sys.argv[8]) |
90 | e6d557f3 | Serge Torres | pointsListAsString = re.sub("^'|'$", "", sys.argv[9]) |
91 | e6d557f3 | Serge Torres | precisionAsString = re.sub("^'|'$", "", sys.argv[10]) |
92 | 1c8b6220 | Serge Torres | # |
93 | 1c8b6220 | Serge Torres | ## Arguments conversion |
94 | 1c8b6220 | Serge Torres | ### Create first the rational field that will be used throughout the script. |
95 | 1c8b6220 | Serge Torres | precision = int(precisionAsString) |
96 | 1c8b6220 | Serge Torres | realField = RealField(precision) |
97 | 1c8b6220 | Serge Torres | # |
98 | 1c8b6220 | Serge Torres | func(x) = sage_eval(funcAsString, cmds="var('x')") |
99 | 3296902f | Serge Torres | try: |
100 | 3296902f | Serge Torres | lowerBound = realField(lowerBoundAsString) |
101 | 3296902f | Serge Torres | except: |
102 | 3296902f | Serge Torres | lowerBoundAsRat = QQ(lowerBoundAsString) |
103 | 3296902f | Serge Torres | lowerBound = realField(lowerBoundAsRat) |
104 | 3296902f | Serge Torres | try: |
105 | 3296902f | Serge Torres | upperBound = realField(upperBoundAsString) |
106 | 3296902f | Serge Torres | except: |
107 | 3296902f | Serge Torres | upperBoundAsRat = QQ(upperBoundAsString) |
108 | 3296902f | Serge Torres | upperBound = realField(upperBoundAsRat) |
109 | e6d557f3 | Serge Torres | approxMode = modeAsString.lower() |
110 | 1c8b6220 | Serge Torres | coeffsExpList = sage_eval(coeffsExpListAsString) |
111 | 1c8b6220 | Serge Torres | coeffsPrecList = sage_eval(coeffsPrecListAsString) |
112 | 1c8b6220 | Serge Torres | minCoeffsBoundExpList = sage_eval(minCoeffsBoundExpListAsString) |
113 | 1c8b6220 | Serge Torres | maxCoeffsBoundExpList = sage_eval(maxCoeffsBoundExpListAsString) |
114 | 1c8b6220 | Serge Torres | pointsList = sage_eval(pointsListAsString) |
115 | 1c8b6220 | Serge Torres | # |
116 | e6d557f3 | Serge Torres | if approxMode != "abs" and approxMode != "rel": |
117 | e6d557f3 | Serge Torres | sys.stderr.write("\n" + scriptName + ": " + "\"" + approxMode + "\"") |
118 | e6d557f3 | Serge Torres | sys.stderr.write("is an invalide mode.\n") |
119 | e6d557f3 | Serge Torres | sys.stderr.write("Mode must be either \"abs\" or \"rel\". Aborting!\n") |
120 | e6d557f3 | Serge Torres | sys.exit(int(1)) |
121 | e6d557f3 | Serge Torres | |
122 | 1c8b6220 | Serge Torres | maxExponent = max(coeffsExpList) |
123 | 1c8b6220 | Serge Torres | ## Debug printing. |
124 | 1c8b6220 | Serge Torres | sys.stderr.write("func: " + func._assume_str().replace("_SAGE_VAR_","",100) + \ |
125 | 1c8b6220 | Serge Torres | "\n") |
126 | 1c8b6220 | Serge Torres | sys.stderr.write("precision: " + str(precision) + "\n") |
127 | 1c8b6220 | Serge Torres | sys.stderr.write("lowerBound: " + str(lowerBound) + "\n") |
128 | 1c8b6220 | Serge Torres | sys.stderr.write("upperBound: " + str(upperBound) + "\n") |
129 | e6d557f3 | Serge Torres | sys.stderr.write("mode :" + approxMode + "\n") |
130 | 1c8b6220 | Serge Torres | sys.stderr.write("coeffsExpList: " + str(coeffsExpList) + "\n") |
131 | 1c8b6220 | Serge Torres | sys.stderr.write("coeffsPrecList: " + str(coeffsPrecList) + "\n") |
132 | 1c8b6220 | Serge Torres | sys.stderr.write("minCoeffsBoundExpList: " + str(minCoeffsBoundExpList) + "\n") |
133 | 1c8b6220 | Serge Torres | sys.stderr.write("maxCoeffsBoundExpList: " + str(maxCoeffsBoundExpList) + "\n") |
134 | 1c8b6220 | Serge Torres | sys.stderr.write("pointsList: " + str(pointsList) + "\n") |
135 | 1c8b6220 | Serge Torres | sys.stderr.write("maxExponent: " + str(maxExponent) + "\n") |
136 | 1c8b6220 | Serge Torres | # |
137 | 1c8b6220 | Serge Torres | ## |
138 | 1c8b6220 | Serge Torres | chebyshevPointsList = [] |
139 | 3296902f | Serge Torres | minMaxErrPoint = oo |
140 | 1c8b6220 | Serge Torres | minMaxErr = oo |
141 | 3296902f | Serge Torres | for pointsNum in xrange(maxExponent+1, round(maxExponent * 8/2)+1): |
142 | 1c8b6220 | Serge Torres | ## Compute the Chebyshev points and append the user given points list. |
143 | 1c8b6220 | Serge Torres | chebyshevPointsList = \ |
144 | 1c8b6220 | Serge Torres | cvp_chebyshev_maxis_for_interval(lowerBound, |
145 | 1c8b6220 | Serge Torres | upperBound, |
146 | 1c8b6220 | Serge Torres | pointsNum, |
147 | 1c8b6220 | Serge Torres | realField, |
148 | 1c8b6220 | Serge Torres | contFracMaxErr) |
149 | 3296902f | Serge Torres | #print "cr-02 - Chebyshe points list computed." |
150 | 1c8b6220 | Serge Torres | chebyshevPointsList = chebyshevPointsList + pointsList |
151 | a0725e69 | Serge Torres | # |
152 | a0725e69 | Serge Torres | ## Filter out those points that do not have a defined image. |
153 | a0725e69 | Serge Torres | chebyshevPointsList = cvp_filter_out_undefined_image(chebyshevPointsList, func) |
154 | a0725e69 | Serge Torres | # |
155 | a0725e69 | Serge Torres | ## |
156 | 1c8b6220 | Serge Torres | poly = cvp_cvp_polynomial(func, |
157 | 1c8b6220 | Serge Torres | lowerBound, |
158 | 1c8b6220 | Serge Torres | upperBound, |
159 | 1c8b6220 | Serge Torres | coeffsExpList, |
160 | 1c8b6220 | Serge Torres | coeffsPrecList, |
161 | 1c8b6220 | Serge Torres | minCoeffsBoundExpList, |
162 | 1c8b6220 | Serge Torres | maxCoeffsBoundExpList, |
163 | 1c8b6220 | Serge Torres | chebyshevPointsList, |
164 | 1c8b6220 | Serge Torres | realField) |
165 | 1c8b6220 | Serge Torres | #sys.stderr.write(str(poly) + "\n") |
166 | 1c8b6220 | Serge Torres | (maxisList,maxErr) = cvp_polynomial_error_func_maxis(func, |
167 | 1c8b6220 | Serge Torres | poly, |
168 | 1c8b6220 | Serge Torres | lowerBound, |
169 | 1c8b6220 | Serge Torres | upperBound, |
170 | 1c8b6220 | Serge Torres | realField, |
171 | 1c8b6220 | Serge Torres | contFracMaxErr) |
172 | a0725e69 | Serge Torres | ## In some cases |
173 | 3296902f | Serge Torres | #print "cr-02 - mcr-02 maxis list:", maxisList |
174 | 3296902f | Serge Torres | #print "cr-02 - maxErr:", maxErr |
175 | a0725e69 | Serge Torres | #polyRing = PolynomialRing(realField, func.variables()[0]) |
176 | a0725e69 | Serge Torres | #errFunc(x) = func(x) - polyRing(poly) |
177 | e6d557f3 | Serge Torres | if approxMode == "abs": |
178 | e6d557f3 | Serge Torres | errFunc(x) = func(x) - poly(x) |
179 | e6d557f3 | Serge Torres | else: |
180 | e6d557f3 | Serge Torres | errFunc(x) = 1 - poly(x) / func(x) |
181 | 3296902f | Serge Torres | if len(maxisList) != 0: |
182 | 3296902f | Serge Torres | (maxErrPoint, maxErrPointErr) = \ |
183 | a0725e69 | Serge Torres | cvp_func_abs_max_for_points(errFunc, |
184 | a0725e69 | Serge Torres | maxisList, |
185 | a0725e69 | Serge Torres | realField) |
186 | 3296902f | Serge Torres | sys.stderr.write("# points: " + str(pointsNum)) |
187 | 3296902f | Serge Torres | sys.stderr.write(" - MaxErr: " + str(maxErr.n(prec=53))) |
188 | 3296902f | Serge Torres | sys.stderr.write(" - MaxErrPoint: " + str(maxErrPoint) + "\n") |
189 | 3296902f | Serge Torres | if maxErr < minMaxErr: |
190 | 3296902f | Serge Torres | minMaxErr = maxErr |
191 | 3296902f | Serge Torres | minMaxErrPoint = maxErrPoint |
192 | 1c8b6220 | Serge Torres | # End for |
193 | 1c8b6220 | Serge Torres | sys.stdout.write(str(minMaxErrPoint) + " " + str(minMaxErr.n(prec=53)) + "\n") |
194 | 3296902f | Serge Torres | ## Use int(0) as argument for sys.exit() in Sage because otherwise |
195 | 3296902f | Serge Torres | # sys.exit(0) -> sys.exit(Integer(0)) (where Integer(0) is a Sage |
196 | 3296902f | Serge Torres | # object, not the integer value 0 expected by Python) hence Python |
197 | 3296902f | Serge Torres | # returns 1! |
198 | 3296902f | Serge Torres | sys.exit(int(0)) |