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