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