Ticket #12418: 12418_delsart_bounds.2.patch

File 12418_delsart_bounds.2.patch, 20.5 KB (added by dimpase, 8 years ago)

rebased for Sage 5.10 and fixed some outstanding issues

  • doc/en/reference/coding/index.rst

    # HG changeset patch
    # User Dmitrii Pasechnik <dimpase@gmail.com>
    # Date 1351442118 -28800
    # Node ID 2692d70c4a04b9695564c1b29c0c28127ec472af
    # Parent  93dbe38d8658d073e1000f9e92b6a5bc22dafcb5
    initial implementation of Delsarte bounds; added Krawchuk speedup by ppurka; more docstrings updated; trailing whitespaces removed
    
    diff --git a/doc/en/reference/coding/index.rst b/doc/en/reference/coding/index.rst
    a b  
    88   sage/coding/code_constructions
    99   sage/coding/sd_codes
    1010   sage/coding/code_bounds
     11   sage/coding/delsarte_bounds
    1112   sage/coding/source_coding/huffman
    1213
    1314.. include:: ../footer.txt
  • sage/coding/all.py

    diff --git a/sage/coding/all.py b/sage/coding/all.py
    a b  
    5555
    5656from sd_codes import self_dual_codes_binary
    5757
     58from delsarte_bounds import (Krawtchouk,
     59                             delsarte_bound_hamming_space,
     60                             delsarte_bound_additive_hamming_space)
  • sage/coding/code_bounds.py

    diff --git a/sage/coding/code_bounds.py b/sage/coding/code_bounds.py
    a b  
    1515  integer, added example to elias_bound_asymp.
    1616
    1717- " (2009-05): removed all calls to Guava but left it as an option.
    18  
     18
     19- Dima Pasechnik (2012-10): added LP bounds.
     20
    1921Let `F` be a finite field (we denote the finite field with
    2022`q` elements by `\GF{q}`).
    2123A subset `C` of `V=F^n` is called a code of
     
    3234
    3335.. math::
    3436
    35      R={\frac{\log_q\vert C\vert}{n}}, 
     37     R={\frac{\log_q\vert C\vert}{n}},
    3638
    3739
    3840where `\vert C\vert` denotes the number of elements of
     
    4345
    4446.. math::
    4547
    46      d({\bf v},{\bf w}) =\vert\{i\ \vert\ 1\leq i\leq n,\ v_i\not= w_i\}\vert 
     48     d({\bf v},{\bf w}) =\vert\{i\ \vert\ 1\leq i\leq n,\ v_i\not= w_i\}\vert
    4749
    4850
    4951to be the Hamming distance between `{\bf v}` and
     
    5759
    5860.. math::
    5961
    60      \delta = d/n. 
     62     \delta = d/n.
    6163
    6264A linear code with length
    6365`n`, dimension `k`, and minimum distance
     
    156158-  mrrw1_bound_asymp(delta,q), "first" asymptotic
    157159   McEliese-Rumsey-Rodemich-Welsh bound for the information rate.
    158160
     161-  Delsarte (a.k.a. Linear Programming (LP)) upper bounds.
    159162
    160163PROBLEM: In this module we shall typically either (a) seek bounds
    161164on k, given n, d, q, (b) seek bounds on R, delta, q (assuming n is
     
    188191from sage.rings.arith import factorial
    189192from sage.functions.all import log, sqrt
    190193from sage.misc.decorators import rename_keyword
     194from delsarte_bounds import delsarte_bound_hamming_space, \
     195                delsarte_bound_additive_hamming_space
    191196
    192197@rename_keyword(deprecation=6094, method="algorithm")
    193198def codesize_upper_bound(n,d,q,algorithm=None):
    194199    r"""
    195     This computes the minimum value of the upper bound using the 
    196     algorithms of Singleton, Hamming, Plotkin and Elias.
     200    This computes the minimum value of the upper bound using the
     201    methods of Singleton, Hamming, Plotkin, and Elias.
    197202
    198     If algorithm="gap" then this returns the best known upper 
    199     bound `A(n,d)=A_q(n,d)` for the size of a code of length n, 
    200     minimum distance d over a field of size q. The function first 
    201     checks for trivial cases (like d=1 or n=d), and if the value 
    202     is in the built-in table. Then it calculates the minimum value 
    203     of the upper bound using the algorithms of Singleton, Hamming, 
    204     Johnson, Plotkin and Elias. If the code is binary, 
     203    If algorithm="gap" then this returns the best known upper
     204    bound `A(n,d)=A_q(n,d)` for the size of a code of length n,
     205    minimum distance d over a field of size q. The function first
     206    checks for trivial cases (like d=1 or n=d), and if the value
     207    is in the built-in table. Then it calculates the minimum value
     208    of the upper bound using the algorithms of Singleton, Hamming,
     209    Johnson, Plotkin and Elias. If the code is binary,
    205210    `A(n, 2\ell-1) = A(n+1,2\ell)`, so the function
    206211    takes the minimum of the values obtained from all algorithms for the
    207212    parameters `(n, 2\ell-1)` and `(n+1, 2\ell)`. This
    208     wraps GUAVA's UpperBound( n, d, q ).
    209    
     213    wraps GUAVA's (i.e. GAP's package Guava) UpperBound( n, d, q ).
     214
     215    If algorithm="LP" then this returns the Delsarte (a.k.a. Linear
     216    Programming) upper bound.
     217
    210218    EXAMPLES::
     219
    211220        sage: codesize_upper_bound(10,3,2)
    212221        93
     222        sage: codesize_upper_bound(24,8,2,algorithm="LP")
     223        4096
    213224        sage: codesize_upper_bound(10,3,2,algorithm="gap")  # optional - gap_packages (Guava package)
    214225        85
     226        sage: codesize_upper_bound(11,3,4,algorithm=None)
     227        123361
     228        sage: codesize_upper_bound(11,3,4,algorithm="gap")  # optional - gap_packages (Guava package)
     229        123361
     230        sage: codesize_upper_bound(11,3,4,algorithm="LP")
     231        109226
    215232
    216233    """
    217234    if algorithm=="gap":
    218235        return int(gap.eval("UpperBound(%s,%s,%s)"%( n, d, q )))
     236    if algorithm=="LP":
     237        return int(delsarte_bound_hamming_space(n,d,q))
    219238    else:
    220239        eub = elias_upper_bound(n,q,d)
    221240        gub = griesmer_upper_bound(n,q,d)
     
    224243        sub = singleton_upper_bound(n,q,d)
    225244        return min([eub,gub,hub,pub,sub])
    226245
    227 def dimension_upper_bound(n,d,q):
     246@rename_keyword(deprecation=6094, method="algorithm")
     247def dimension_upper_bound(n,d,q,algorithm=None):
    228248    r"""
    229249    Returns an upper bound `B(n,d) = B_q(n,d)` for the
    230250    dimension of a linear code of length n, minimum distance d over a
    231251    field of size q.
    232    
     252    Parameter "algorithm" has the same meaning as in :func:`codesize_upper_bound`
     253
    233254    EXAMPLES::
    234    
     255
    235256        sage: dimension_upper_bound(10,3,2)
    236257        6
     258        sage: dimension_upper_bound(30,15,4)
     259        13
     260        sage: dimension_upper_bound(30,15,4,algorithm="LP")
     261        12
     262
    237263    """
    238264    q = ZZ(q)
    239     return int(log(codesize_upper_bound(n,d,q),q))
    240    
     265    if algorithm=="LP":
     266        return delsarte_bound_additive_hamming_space(n,d,q)
     267
     268    else:       # algorithm==None or algorithm="gap":
     269        return int(log(codesize_upper_bound(n,d,q,algorithm=algorithm),q))
     270
     271
    241272def volume_hamming(n,q,r):
    242273    r"""
    243274    Returns number of elements in a Hamming ball of radius r in `\GF{q}^n`.
    244275    Agrees with Guava's SphereContent(n,r,GF(q)).
    245276
    246277    EXAMPLES::
    247    
     278
    248279        sage: volume_hamming(10,2,3)
    249280        176
    250281    """
     
    255286    r"""
    256287    Returns lower bound for number of elements in the largest code of
    257288    minimum distance d in `\GF{q}^n`.
    258    
     289
    259290    EXAMPLES::
    260    
     291
    261292        sage: gilbert_lower_bound(10,2,3)
    262293        128/7
    263294    """
     
    268299def plotkin_upper_bound(n,q,d, algorithm=None):
    269300    r"""
    270301    Returns Plotkin upper bound for number of elements in the largest
    271     code of minimum distance d in `\GF{q}^n`. 
     302    code of minimum distance d in `\GF{q}^n`.
    272303
    273304    The algorithm="gap" option wraps Guava's UpperBoundPlotkin.
    274    
     305
    275306    EXAMPLES::
    276         sage: plotkin_upper_bound(10,2,3)
     307
     308        sage: plotkin_upper_bound(10,2,3)
    277309        192
    278310        sage: plotkin_upper_bound(10,2,3,algorithm="gap")  # optional - gap_packages (Guava package)
    279311        192
     
    302334    Returns the Griesmer upper bound for number of elements in the
    303335    largest code of minimum distance d in `\GF{q}^n`.
    304336    Wraps GAP's UpperBoundGriesmer.
    305    
     337
    306338    EXAMPLES::
     339
    307340        sage: griesmer_upper_bound(10,2,3)
    308341        128
    309342        sage: griesmer_upper_bound(10,2,3,algorithm="gap")  # optional - gap_packages (Guava package)
     
    322355            if not(add == 1):
    323356                if d%den==0:
    324357                    add = int(d/den)
    325                 else: 
     358                else:
    326359                    add = int(d/den)+1
    327360            s = s + add
    328361            den = den * q
     
    336369    Returns the Elias upper bound for number of elements in the largest
    337370    code of minimum distance d in `\GF{q}^n`. Wraps
    338371    GAP's UpperBoundElias.
    339    
     372
    340373    EXAMPLES::
    341    
     374
    342375        sage: elias_upper_bound(10,2,3)
    343376        232
    344377        sage: elias_upper_bound(10,2,3,algorithm="gap")  # optional - gap_packages (Guava package)
     
    368401    Returns the Hamming upper bound for number of elements in the
    369402    largest code of minimum distance d in `\GF{q}^n`.
    370403    Wraps GAP's UpperBoundHamming.
    371    
     404
    372405    The Hamming bound (also known as the sphere packing bound) returns
    373406    an upper bound on the size of a code of length n, minimum distance
    374407    d, over a field of size q. The Hamming bound is obtained by
     
    376409    `\GF{q}^n` by the contents of a ball with radius
    377410    floor((d-1)/2). As all these balls are disjoint, they can never
    378411    contain more than the whole vector space.
    379    
    380    
     412
     413
    381414    .. math::
    382    
    383          M \leq {q^n \over V(n,e)}, 
    384    
    385    
    386    
     415
     416         M \leq {q^n \over V(n,e)},
     417
     418
     419
    387420    where M is the maximum number of codewords and `V(n,e)` is
    388421    equal to the contents of a ball of radius e. This bound is useful
    389422    for small values of d. Codes for which equality holds are called
    390423    perfect.
    391    
     424
    392425    EXAMPLES::
    393    
    394         sage: hamming_upper_bound(10,2,3) 
     426
     427        sage: hamming_upper_bound(10,2,3)
    395428        93
    396429    """
    397430    return int((q**n)/(volume_hamming(n, q, int((d-1)/2))))
     
    401434    Returns the Singleton upper bound for number of elements in the
    402435    largest code of minimum distance d in `\GF{q}^n`.
    403436    Wraps GAP's UpperBoundSingleton.
    404    
     437
    405438    This bound is based on the shortening of codes. By shortening an
    406439    `(n, M, d)` code d-1 times, an `(n-d+1,M,1)` code
    407440    results, with `M \leq q^n-d+1`. Thus
    408    
    409    
     441
     442
    410443    .. math::
    411    
    412          M \leq q^{n-d+1}. 
    413    
    414    
    415    
     444
     445         M \leq q^{n-d+1}.
     446
     447
     448
    416449    Codes that meet this bound are called maximum distance separable
    417450    (MDS).
    418    
     451
    419452    EXAMPLES::
    420         sage: singleton_upper_bound(10,2,3)
     453
     454        sage: singleton_upper_bound(10,2,3)
    421455        256
    422456    """
    423457    return q**(n - d + 1)
     
    426460    """
    427461    GV lower bound for information rate of a q-ary code of length n
    428462    minimum distance delta\*n
    429    
     463
    430464    EXAMPLES::
    431    
     465
    432466        sage: RDF(gv_info_rate(100,1/4,3))
    433467        0.367049926083
    434468    """
     
    482516def gv_bound_asymp(delta,q):
    483517    """
    484518    Computes the asymptotic GV bound for the information rate, R.
    485    
     519
    486520    EXAMPLES::
     521
    487522        sage: RDF(gv_bound_asymp(1/4,2))
    488523        0.188721875541
    489524        sage: f = lambda x: gv_bound_asymp(x,2)
     
    495530def hamming_bound_asymp(delta,q):
    496531    """
    497532    Computes the asymptotic Hamming bound for the information rate.
    498    
     533
    499534    EXAMPLES::
     535
    500536        sage: RDF(hamming_bound_asymp(1/4,2))
    501537        0.4564355568
    502538        sage: f = lambda x: hamming_bound_asymp(x,2)
     
    507543def singleton_bound_asymp(delta,q):
    508544    """
    509545    Computes the asymptotic Singleton bound for the information rate.
    510    
     546
    511547    EXAMPLES::
     548
    512549        sage: singleton_bound_asymp(1/4,2)
    513550        3/4
    514551        sage: f = lambda x: singleton_bound_asymp(x,2)
     
    520557    """
    521558    Computes the asymptotic Plotkin bound for the information rate,
    522559    provided `0 < \delta < 1-1/q`.
    523    
     560
    524561    EXAMPLES::
    525    
     562
    526563        sage: plotkin_bound_asymp(1/4,2)
    527564        1/2
    528565    """
     
    532569def elias_bound_asymp(delta,q):
    533570    """
    534571    Computes the asymptotic Elias bound for the information rate,
    535     provided `0 < \delta 1-1/q`.
    536    
     572    provided `0 < \delta < 1-1/q`.
     573
    537574    EXAMPLES::
    538    
     575
    539576        sage: elias_bound_asymp(1/4,2)
    540577        0.39912396330...
    541578    """
     
    546583    """
    547584    Computes the first asymptotic McEliese-Rumsey-Rodemich-Welsh bound
    548585    for the information rate, provided `0 < \delta < 1-1/q`.
    549    
     586
    550587    EXAMPLES::
    551    
     588
    552589        sage: mrrw1_bound_asymp(1/4,2)
    553590        0.354578902665
    554591    """
    555592    return RDF(entropy((q-1-delta*(q-2)-2*sqrt((q-1)*delta*(1-delta)))/q,q))
    556 
    557 
    558 
    559 
    560 
  • new file sage/coding/delsarte_bounds.py

    diff --git a/sage/coding/delsarte_bounds.py b/sage/coding/delsarte_bounds.py
    new file mode 100644
    - +  
     1r"""
     2Delsarte, a.k.a. Linear Programming (LP), upper bounds.
     3
     4This module provides  LP upper bounds for the parameters of codes.
     5Exact LP solver, PPL, is used by defaut, ensuring that no rounding/overflow
     6problems occur.
     7
     8AUTHORS:
     9
     10- Dmitrii V. (Dima) Pasechnik (2012-10): initial implementation.
     11"""
     12#*****************************************************************************
     13#       Copyright (C) 2012 Dima Pasechnik <dimpase@gmail.com>
     14#
     15#  Distributed under the terms of the GNU General Public License (GPL)
     16#
     17#                  http://www.gnu.org/licenses/
     18#*****************************************************************************
     19def Krawtchouk(n,q,l,i):
     20   """
     21   Compute ``K^{n,q}_l(i)``, the Krawtchouk polynomial: see
     22   `en.wikipedia.org/wiki/Kravchuk_polynomials <http://en.wikipedia.org/wiki/Kravchuk_polynomials>`_.
     23   It is given by
     24
     25   .. math::
     26
     27      K^{n,q}_l(i)=\sum_{j=0}^l (-1)^j(q-1)^{(l-j)}{i \choose j}{n-i \choose l-j}
     28
     29   EXAMPLES::
     30
     31        sage: Krawtchouk(24,2,5,4)
     32        2224
     33        sage: Krawtchouk(12300,4,5,6)
     34        567785569973042442072
     35
     36   """
     37   from sage.rings.arith import binomial
     38   # Use the expression in equation (55) of MacWilliams & Sloane, pg 151
     39   # We write jth term = some_factor * (j-1)th term
     40   kraw = jth_term = (q-1)**l * binomial(n, l) # j=0
     41   for j in range(1,l+1):
     42      jth_term *= -q*(l-j+1)*(i-j+1)/((q-1)*j*(n-j+1))
     43      kraw += jth_term
     44   return kraw
     45
     46
     47def delsarte_bound_hamming_space(n, d, q,
     48                    isinteger=False, return_data=False, solver="PPL"):
     49   """
     50   Find the classical Delsarte bound [1]_ on codes in Hamming space
     51   ``H_q^n`` of minimal distance ``d``
     52
     53
     54   INPUT:
     55
     56   - ``n`` -- the code length
     57
     58   - ``d`` -- the (lower bound on) minimal distance of the code
     59
     60   - ``q`` -- the size of the alphabet
     61
     62   - ``isinteger`` -- if ``True``, uses an integer programming solver (ILP), rather
     63     that an LP solver. Can be very slow if set to ``True``.
     64
     65   - ``return_data`` -- if ``True``, return a triple ``(W,LP,bound)``, where ``W`` is
     66     a weights vector,  and ``LP`` the Delsarte bound LP; both of them are Sage LP
     67     data.  ``W`` need not be a weight distribution of a code, or,
     68     if ``isinteger==False``, even have integer entries.
     69
     70   - ``solver`` -- the LP/ILP solver to be used. Defaults to ``PPL``. It is arbitrary
     71     precision, thus there will be no rounding errors. With other solvers
     72     (see :class:`MixedIntegerLinearProgram`), you are on your own!
     73
     74
     75   EXAMPLES::
     76
     77   The bound on the size of the `F_2`-codes of length 11 and minimal distance 6::
     78
     79       sage: delsarte_bound_hamming_space(11, 6, 2)
     80       12
     81       sage: a, p, val = delsarte_bound_hamming_space(11, 6, 2, return_data=True)
     82       sage: [j for i,j in p.get_values(a).iteritems()]
     83       [1, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0]
     84
     85   The bound on the size of the `F_2`-codes of length 24 and minimal distance
     86   8, i.e. parameters of the extened binary Golay code::
     87
     88       sage: a,p,x=delsarte_bound_hamming_space(24,8,2,return_data=True)
     89       sage: x
     90       4096
     91       sage: [j for i,j in p.get_values(a).iteritems()]
     92       [1, 0, 0, 0, 0, 0, 0, 0, 759, 0, 0, 0, 2576, 0, 0, 0, 759, 0, 0, 0, 0, 0, 0, 0, 1]
     93
     94   The bound on the size of `F_4`-codes of length 11 and minimal distance 3::
     95
     96       sage: delsarte_bound_hamming_space(11,3,4)
     97       327680/3
     98
     99   REFERENCES:
     100
     101    .. [1] P. Delsarte, An algebraic approach to the association schemes of coding theory,
     102           Philips Res. Rep., Suppl., vol. 10, 1973.
     103
     104
     105
     106   """
     107   from sage.numerical.mip import MixedIntegerLinearProgram
     108
     109   p = MixedIntegerLinearProgram(maximization=True, solver=solver)
     110   A = p.new_variable(integer=isinteger) # A>=0 is assumed
     111   p.set_objective(sum([A[r] for r in range(n+1)]))
     112   p.add_constraint(A[0]==1)
     113   for i in range(1,d):
     114      p.add_constraint(A[i]==0)
     115   for j in range(1,n+1):
     116      rhs = sum([Krawtchouk(n,q,j,r)*A[r] for r in range(n+1)])
     117      p.add_constraint(0*A[0] <= rhs)
     118   try:
     119      bd=p.solve()
     120   except sage.numerical.mip.MIPSolverException, exc:
     121      print "Solver exception: ", exc, exc.args
     122      if return_data:
     123         return A,p,False
     124      return False
     125
     126   if return_data:
     127      return A,p,bd
     128   else:
     129      return bd
     130
     131def delsarte_bound_additive_hamming_space(n, d, q, d_star=1, q_base=0,
     132                    isinteger=False, return_data=False, solver="PPL"):
     133   """
     134   Find the Delsarte LP bound on ``F_{q_base}``-dimension of additive codes in
     135   Hamming space ``H_q^n`` of minimal distance ``d`` with minimal distance of the dual
     136   code at least ``d_star``.  If ``q_base`` is set to
     137   non-zero, then  ``q`` is a power of ``q_base``, and the code is, formally, linear over
     138   ``F_{q_base}``. Otherwise it is assumed that ``q_base==q``.
     139
     140
     141   INPUT:
     142
     143   - ``n`` -- the code length
     144
     145   - ``d`` -- the (lower bound on) minimal distance of the code
     146
     147   - ``q`` -- the size of the alphabet
     148
     149   - ``d_star`` -- the (lower bound on) minimal distance of the dual code;
     150     only makes sense for additive codes.
     151
     152   - ``q_base`` -- if ``0``, the code is assumed to be nonlinear. Otherwise,
     153     ``q=q_base^m`` and the code is linear over ``F_{q_base}``.
     154
     155   - ``isinteger`` -- if ``True``, uses an integer programming solver (ILP), rather
     156     that an LP solver. Can be very slow if set to ``True``.
     157
     158   - ``return_data`` -- if ``True``, return a triple ``(W,LP,bound)``, where ``W`` is
     159     a weights vector,  and ``LP`` the Delsarte bound LP; both of them are Sage LP
     160     data.  ``W`` need not be a weight distribution of a code, or,
     161     if ``isinteger==False``, even have integer entries.
     162
     163   - ``solver`` -- the LP/ILP solved to be used. Defaults to ``PPL``. It is arbitrary
     164     precision, thus there will be no rounding errors. With other solvers, you are on
     165     your own!
     166
     167   EXAMPLES::
     168
     169   The bound on dimension of linear `F_2`-codes of length 11 and minimal distance 6::
     170
     171       sage: delsarte_bound_additive_hamming_space(11, 6, 2)
     172       3
     173       sage: a,p,val=delsarte_bound_additive_hamming_space(11, 6, 2,\
     174                                      return_data=True)
     175       sage: [j for i,j in p.get_values(a).iteritems()]
     176       [1, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0]
     177
     178   The bound on the dimension of linear `F_4`-codes of length 11 and minimal distance 3::
     179
     180       sage: delsarte_bound_additive_hamming_space(11,3,4)
     181       8
     182
     183   The bound on the `F_2`-dimension of additive `F_4`-codes of length 11 and minimal
     184   distance 3::
     185
     186       sage: delsarte_bound_additive_hamming_space(11,3,4,q_base=2)
     187       16
     188
     189   """
     190   from sage.numerical.mip import MixedIntegerLinearProgram
     191
     192   if q_base == 0:
     193      q_base = q
     194
     195   kk = 0
     196   while q_base**kk < q:
     197      kk += 1
     198
     199   if q_base**kk != q:
     200      print "Wrong q_base=", q_base, " for q=", q, kk
     201      return False
     202
     203   # this implementation assumes that our LP solver to be unable to do a hot
     204   # restart with an adjusted constraint
     205
     206   m = kk*n # this is to emulate repeat/until block
     207   bd = q**n+1
     208
     209   while q_base**m < bd: # need to solve the LP repeatedly, as this is a new constraint!
     210                         # we might become infeasible. More precisely, after rounding down
     211                         # to the closest value of q_base^m, the LP, with the constraint that
     212                         # the objective function is at most q_base^m,
     213      p = MixedIntegerLinearProgram(maximization=True, solver=solver)
     214      A = p.new_variable(integer=isinteger) # A>=0 is assumed
     215      p.set_objective(sum([A[r] for r in xrange(n+1)]))
     216      # the following constraint comes from rounding
     217      p.add_constraint(sum([A[r] for r in xrange(n+1)]), max=q_base**m)
     218      p.add_constraint(A[0]==1)
     219      for i in xrange(1,d):
     220        p.add_constraint(A[i]==0)
     221      for j in xrange(1,n+1):
     222        rhs = sum([Krawtchouk(n,q,j,r)*A[r] for r in xrange(n+1)])
     223        if j >= d_star:
     224          p.add_constraint(0*A[0] <= rhs)
     225        else: # rhs is proportional to j-th weight of the dual code
     226          p.add_constraint(0*A[0] == rhs)
     227      try:
     228        bd=p.solve()
     229      except sage.numerical.mip.MIPSolverException, exc:
     230        print "Solver exception: ", exc, exc.args
     231        if return_data:
     232           return A,p,False
     233        return False
     234    # rounding the bound down to the nearest power of q_base, for q=q_base^m
     235#      bd_r = roundres(log(bd, base=q_base))
     236      m = -1
     237      while q_base**(m+1) < bd:
     238        m += 1
     239      if q_base**(m+1) == bd:
     240        m += 1
     241
     242   if return_data:
     243      return A, p, m
     244   else:
     245      return m
     246