Révision 155
| pobysoPythonSage/src/pobyso.py (revision 155) | ||
|---|---|---|
| 282 | 282 |
return(constSa.value) |
| 283 | 283 |
# End pobyso_constant_from_int_so_sa |
| 284 | 284 |
|
| 285 |
def pobyso_error_so(): |
|
| 286 |
return sollya_lib_error(None) |
|
| 287 |
# End pobyso_error(). |
|
| 288 |
|
|
| 285 | 289 |
def pobyso_function_type_as_string(funcType): |
| 286 | 290 |
""" Legacy function. See pobyso_function_type_as_string_so_sa. """ |
| 287 | 291 |
return(pobyso_function_type_as_string_so_sa(funcType)) |
| ... | ... | |
| 761 | 765 |
return(int(prec.value)) |
| 762 | 766 |
# End pobyso_get_prec_of_range_so_sa() |
| 763 | 767 |
|
| 764 |
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, errorSa, weightSa=None, \ |
|
| 765 |
degreeBoundSa=None): |
|
| 768 |
def pobyso_guess_degree_sa_sa(functionSa, intervalSa, approxErrorSa, |
|
| 769 |
weightSa=None, degreeBoundSa=None): |
|
| 770 |
""" |
|
| 771 |
Sa_sa variant of the solly_guessdegree function. |
|
| 772 |
Return 0 if something goes wrong. |
|
| 773 |
""" |
|
| 766 | 774 |
functionAsStringSa = functionSa._assume_str() |
| 767 | 775 |
functionSo = pobyso_parse_string_sa_so(functionAsStringSa) |
| 776 |
if pobyso_is_error_so_sa(functionSo): |
|
| 777 |
sollya_lib_clear_obj(functionSo) |
|
| 778 |
return 0 |
|
| 768 | 779 |
rangeSo = pobyso_interval_to_range_sa_so(intervalSa) |
| 769 |
errorSo = pobyso_constant_sa_so(errorSa) |
|
| 780 |
# The approximation error is expected to be a floating point number. |
|
| 781 |
if pobyso_is_floating_point_number_sa_sa(approxErrorSa): |
|
| 782 |
approxErrorSo = pobyso_constant_sa_so(approxErrorSa) |
|
| 783 |
else: |
|
| 784 |
approxErrorSo = pobyso_constant_sa_so(RR(approxErrorSa)) |
|
| 770 | 785 |
if not weightSa is None: |
| 771 | 786 |
weightAsStringSa = weightSa._assume_str() |
| 772 | 787 |
weightSo = pobyso_parse_string_sa_so(weightAsStringSa) |
| 788 |
if pobyso_is_error(weightSo): |
|
| 789 |
sollya_lib_clear_obj(functionSo) |
|
| 790 |
sollya_lib_clear_obj(rangeSo) |
|
| 791 |
sollya_lib_clear_obj(approxErrorSo) |
|
| 792 |
sollya_lib_clear_obj(weightSo) |
|
| 793 |
return 0 |
|
| 773 | 794 |
else: |
| 774 | 795 |
weightSo = None |
| 775 | 796 |
if not degreeBoundSa is None: |
| ... | ... | |
| 778 | 799 |
degreeBoundSo = None |
| 779 | 800 |
guessedDegreeSa = pobyso_guess_degree_so_sa(functionSo, |
| 780 | 801 |
rangeSo, |
| 781 |
errorSo,
|
|
| 802 |
approxErrorSo,
|
|
| 782 | 803 |
weightSo, |
| 783 | 804 |
degreeBoundSo) |
| 784 | 805 |
sollya_lib_clear_obj(functionSo) |
| 785 | 806 |
sollya_lib_clear_obj(rangeSo) |
| 786 |
sollya_lib_clear_obj(errorSo)
|
|
| 807 |
sollya_lib_clear_obj(approxErrorSo)
|
|
| 787 | 808 |
if not weightSo is None: |
| 788 | 809 |
sollya_lib_clear_obj(weightSo) |
| 789 | 810 |
if not degreeBoundSo is None: |
| ... | ... | |
| 851 | 872 |
return(intervalSo) |
| 852 | 873 |
# End pobyso_interval_to_range_sa_so |
| 853 | 874 |
|
| 875 |
def pobyso_is_error_so_sa(objSo): |
|
| 876 |
""" |
|
| 877 |
Thin wrapper around the sollya_lib_obj_is_error() function. |
|
| 878 |
""" |
|
| 879 |
if sollya_lib_obj_is_error(objSo) != 0: |
|
| 880 |
return True |
|
| 881 |
else: |
|
| 882 |
return False |
|
| 883 |
# End pobyso_is_error-so_sa |
|
| 884 |
|
|
| 885 |
def pobyso_is_floating_point_number_sa_sa(numberSa): |
|
| 886 |
""" |
|
| 887 |
Check whether a Sage number is floating point |
|
| 888 |
""" |
|
| 889 |
return numberSa.parent().__class__ is RR.__class__ |
|
| 890 |
|
|
| 854 | 891 |
def pobyso_lib_init(): |
| 855 | 892 |
sollya_lib_init(None) |
| 856 | 893 |
|
| ... | ... | |
| 873 | 910 |
|
| 874 | 911 |
def pobyso_parse_string_sa_so(string): |
| 875 | 912 |
""" |
| 876 |
Get the Sollya expression computed from a Sage string. |
|
| 913 |
Get the Sollya expression computed from a Sage string or |
|
| 914 |
a Sollya error object if parsing failed. |
|
| 877 | 915 |
""" |
| 878 | 916 |
return(sollya_lib_parse_string(string)) |
| 879 | 917 |
|
| pobysoPythonSage/src/sollya_lib.sage (revision 155) | ||
|---|---|---|
| 32 | 32 |
sollya.sollya_lib_constant_from_uint64 |
| 33 | 33 |
sollya_lib_copy_obj = sollya.sollya_lib_copy_obj |
| 34 | 34 |
sollya_lib_cos = sollya.sollya_lib_cos |
| 35 |
sollya_lib_error=sollya.sollya_lib_error |
|
| 35 | 36 |
sollya_lib_evaluate = sollya.sollya_lib_evaluate |
| 36 | 37 |
sollya_lib_free = sollya.sollya_lib_free |
| 37 | 38 |
sollya_lib_get_canonical = sollya.sollya_lib_get_canonical |
| pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage (revision 155) | ||
|---|---|---|
| 55 | 55 |
" " + str(knownMonomials[knownMonomialIndex]) |
| 56 | 56 |
print monomialAsString, " " * \ |
| 57 | 57 |
(columnsWidth - len(monomialAsString)), |
| 58 |
# Add the coefficient to the proto matrix row and delete the \
|
|
| 58 |
# Add the coefficient to the proto matrix row and delete the |
|
| 59 | 59 |
# known monomial from the current pMonomial list |
| 60 | 60 |
#(and the corresponding coefficient as well). |
| 61 | 61 |
protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials]) |
| ... | ... | |
| 839 | 839 |
return polynomialsList |
| 840 | 840 |
# End spo_polynomial_to_proto_matrix_5 |
| 841 | 841 |
|
| 842 |
def spo_polynomial_to_polynomials_list_6(p, alpha, N, iBound, tBound, |
|
| 843 |
columnsWidth=0): |
|
| 844 |
""" |
|
| 845 |
From p, alpha, N build a list of polynomials use to create a base |
|
| 846 |
that will eventually be reduced with LLL. |
|
| 847 |
|
|
| 848 |
The bounds are computed for the coefficients that will be used to |
|
| 849 |
form the base. |
|
| 850 |
|
|
| 851 |
We try to introduce only one new monomial at a time, whithout trying to |
|
| 852 |
obtain a triangular matrix. |
|
| 853 |
|
|
| 854 |
There are many possibilities to introduce the monomials: our goal is also |
|
| 855 |
to introduce each of them on the diagonal with the smallest coefficient. |
|
| 856 |
|
|
| 857 |
The method depends on the structure of the polynomial. Here it is adapted |
|
| 858 |
to the a_n*i^n + ... + a_1 * i - t + b form. |
|
| 859 |
|
|
| 860 |
Parameters |
|
| 861 |
---------- |
|
| 862 |
p: the (bivariate) polynomial; |
|
| 863 |
alpha: |
|
| 864 |
N: |
|
| 865 |
iBound: |
|
| 866 |
tBound: |
|
| 867 |
columsWidth: if == 0, no information is displayed, otherwise data is |
|
| 868 |
printed in colums of columnsWitdth width. |
|
| 869 |
""" |
|
| 870 |
pRing = p.parent() |
|
| 871 |
polynomialsList = [] |
|
| 872 |
pVariables = p.variables() |
|
| 873 |
iVariable = pVariables[0] |
|
| 874 |
tVariable = pVariables[1] |
|
| 875 |
polynomialAtPower = copy(p) |
|
| 876 |
currentPolynomial = pRing(1) # Constant term. |
|
| 877 |
pIdegree = p.degree(iVariable) |
|
| 878 |
pTdegree = p.degree(tVariable) |
|
| 879 |
maxIdegree = pIdegree * alpha |
|
| 880 |
currentIdegree = currentPolynomial.degree(iVariable) |
|
| 881 |
nAtAlpha = N^alpha |
|
| 882 |
nAtPower = nAtAlpha |
|
| 883 |
polExpStr = "" |
|
| 884 |
# |
|
| 885 |
## Shouldn't we start at tPower == 1 because polynomials without |
|
| 886 |
# t monomials are useless? |
|
| 887 |
for tPower in xrange(0, alpha+1): |
|
| 888 |
## Start at iPower == 0 because here there are i monomials |
|
| 889 |
# in p even if iPower is zero. |
|
| 890 |
for iPower in xrange(0, alpha-tPower+1): |
|
| 891 |
if iPower + tPower <= alpha: |
|
| 892 |
print "iPower:", iPower, " tPower:", tPower |
|
| 893 |
q = pRing(iVariable * iBound)^iPower * ((p * N)^tPower) |
|
| 894 |
print q.monomials() |
|
| 895 |
polynomialsList.append(q) |
|
| 896 |
return polynomialsList |
|
| 897 |
|
|
| 898 |
""" |
|
| 899 |
# We first introduce all the monomials in i alone multiplied by N^alpha. |
|
| 900 |
for iPower in xrange(0, maxIdegree + 1): |
|
| 901 |
if columnsWidth !=0: |
|
| 902 |
polExpStr = spo_expression_as_string(iPower, iBound, |
|
| 903 |
0, tBound, |
|
| 904 |
0, alpha) |
|
| 905 |
print "->", polExpStr |
|
| 906 |
currentExpression = iVariable^iPower * nAtAlpha * iBound^iPower |
|
| 907 |
currentPolynomial = pRing(currentExpression) |
|
| 908 |
polynomialsList.append(currentPolynomial) |
|
| 909 |
# End for iPower |
|
| 910 |
# We work from p^1 * N^alpha-1 to p^alpha * N^0 |
|
| 911 |
for pPower in xrange(1, alpha + 1): |
|
| 912 |
# First of all the p^pPower * N^(alpha-pPower) polynomial. |
|
| 913 |
nAtPower /= N |
|
| 914 |
if columnsWidth !=0: |
|
| 915 |
polExpStr = spo_expression_as_string(0, iBound, |
|
| 916 |
0, tBound, |
|
| 917 |
pPower, alpha-pPower) |
|
| 918 |
print "->", polExpStr |
|
| 919 |
currentPolynomial = polynomialAtPower * nAtPower |
|
| 920 |
polynomialsList.append(currentPolynomial) |
|
| 921 |
# Exit when pPower == alpha |
|
| 922 |
if pPower == alpha: |
|
| 923 |
return polynomialsList |
|
| 924 |
for iPower in xrange(1, pIdegree + 1): |
|
| 925 |
iCurrentPower = pIdegree + iPower |
|
| 926 |
for tPower in xrange(pPower-1, 0, -1): |
|
| 927 |
#print "tPower:", tPower |
|
| 928 |
if columnsWidth != 0: |
|
| 929 |
polExpStr = spo_expression_as_string(iCurrentPower, iBound, |
|
| 930 |
tPower, tBound, |
|
| 931 |
0, alpha) |
|
| 932 |
print "->", polExpStr |
|
| 933 |
currentExpression = i^iCurrentPower * iBound^iCurrentPower * t^tPower * tBound^tPower *nAtAlpha |
|
| 934 |
currentPolynomial = pRing(currentExpression) |
|
| 935 |
polynomialsList.append(currentPolynomial) |
|
| 936 |
iCurrentPower += pIdegree |
|
| 937 |
# End for tPower |
|
| 938 |
# We now introduce the mixed i^k * t^l monomials by i^m * p^n * N^(alpha-n) |
|
| 939 |
if columnsWidth != 0: |
|
| 940 |
polExpStr = spo_expression_as_string(iPower, iBound, |
|
| 941 |
0, tBound, |
|
| 942 |
pPower, alpha-pPower) |
|
| 943 |
print "->", polExpStr |
|
| 944 |
currentExpression = i^iPower * iBound^iPower * nAtPower |
|
| 945 |
currentPolynomial = pRing(currentExpression) * polynomialAtPower |
|
| 946 |
polynomialsList.append(currentPolynomial) |
|
| 947 |
# End for iPower |
|
| 948 |
polynomialAtPower *= p |
|
| 949 |
# End for pPower loop |
|
| 950 |
""" |
|
| 951 |
return polynomialsList |
|
| 952 |
# End spo_polynomial_to_proto_matrix_6 |
|
| 953 |
|
|
| 842 | 954 |
def spo_proto_to_column_matrix(protoMatrixColumns): |
| 843 | 955 |
""" |
| 844 | 956 |
Create a column (each row holds the coefficients for one monomial) matrix. |
| pobysoPythonSage/src/testPobyso.sage (revision 155) | ||
|---|---|---|
| 93 | 93 |
return timing |
| 94 | 94 |
# End test_pobyso_bounds_to_range_sa_so |
| 95 | 95 |
|
| 96 |
def test_pobyso_error_so(repeat=1000, number=10): |
|
| 97 |
functionName = inspect.stack()[0][3] |
|
| 98 |
print "Running", inspect.stack()[0][3], "..." |
|
| 99 |
# |
|
| 100 |
def test(): |
|
| 101 |
errorSo = pobyso_error_so() |
|
| 102 |
sollya_lib_clear_obj(errorSo) |
|
| 103 |
# |
|
| 104 |
wrapped = test_pobyso_wrapper(test) |
|
| 105 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
|
| 106 |
print "\t...", functionName, "done." |
|
| 107 |
return timing |
|
| 108 |
# End test_pobyso_error_so |
|
| 109 |
|
|
| 96 | 110 |
def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
| 97 | 111 |
functionName = inspect.stack()[0][3] |
| 98 | 112 |
print "Running", functionName, "..." |
| ... | ... | |
| 176 | 190 |
intervalSo, |
| 177 | 191 |
errorSo, |
| 178 | 192 |
weightSo, |
| 179 |
degreeBoundSo) |
|
| 193 |
degreeBoundSo) |
|
| 194 |
""" |
|
| 195 |
pobyso_guess_degree_so_sa(funcExpSo, |
|
| 196 |
intervalSo, |
|
| 197 |
errorSo, |
|
| 198 |
None, |
|
| 199 |
None) |
|
| 200 |
""" |
|
| 180 | 201 |
wrapped = test_pobyso_wrapper(test) |
| 181 | 202 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
| 182 | 203 |
print "\t...", functionName, "done." |
| ... | ... | |
| 188 | 209 |
return timing |
| 189 | 210 |
# End test_pobyso_guess_degree_so_sa |
| 190 | 211 |
|
| 212 |
def test_pobyso_is_error_so_sa(repeat=1000, number=10): |
|
| 213 |
functionName = inspect.stack()[0][3] |
|
| 214 |
print "Running", inspect.stack()[0][3], "..." |
|
| 215 |
# |
|
| 216 |
def test(): |
|
| 217 |
errorSo = pobyso_error_so() |
|
| 218 |
if not pobyso_is_error_so_sa(errorSo): |
|
| 219 |
print "Error!" |
|
| 220 |
sollya_lib_clear_obj(errorSo) |
|
| 221 |
# |
|
| 222 |
wrapped = test_pobyso_wrapper(test) |
|
| 223 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
|
| 224 |
print "\t...", functionName, "done." |
|
| 225 |
return timing |
|
| 226 |
# End test_pobyso_error_so |
|
| 227 |
|
|
| 191 | 228 |
def test_pobyso_lib_init(iterationsNum=10000): |
| 192 | 229 |
""" |
| 193 | 230 |
Must be called exactly once. Leaks lots of memory otherwise. |
Formats disponibles : Unified diff