Ticket #12418: 12418_delsart_bounds.patch

File 12418_delsart_bounds.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 71260c4f7840a7527415e947a76a2e9901360206
    # Parent  5d8c13e7b81a1e152855073f97a4d48ee97255d1
    initial implementation of Delsarte bounds; added Krawchuk speedup by ppurka; more docstrings updated; trailing whitespaces removed; refactored
    
    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
     46def _delsarte_LP_building(n, d, d_star, q, isinteger,  solver, maxc = 0):
     47    """
     48    LP builder - common for the two functions; not exported.
     49    """
     50    from sage.numerical.mip import MixedIntegerLinearProgram
     51
     52    p = MixedIntegerLinearProgram(maximization=True, solver=solver)
     53    A = p.new_variable(integer=isinteger) # A>=0 is assumed
     54    p.set_objective(sum([A[r] for r in xrange(n+1)]))
     55    p.add_constraint(A[0]==1)
     56    for i in xrange(1,d):
     57        p.add_constraint(A[i]==0)
     58    for j in xrange(1,n+1):
     59        rhs = sum([Krawtchouk(n,q,j,r)*A[r] for r in xrange(n+1)])
     60        p.add_constraint(0*A[0] <= rhs)
     61        if j >= d_star:
     62          p.add_constraint(0*A[0] <= rhs)
     63        else: # rhs is proportional to j-th weight of the dual code
     64          p.add_constraint(0*A[0] == rhs)
     65
     66    if maxc > 0:
     67        p.add_constraint(sum([A[r] for r in xrange(n+1)]), max=maxc)
     68    return A, p
     69 
     70def delsarte_bound_hamming_space(n, d, q,
     71                    isinteger=False, return_data=False, solver="PPL"):
     72    """
     73    Find the classical Delsarte bound [1]_ on codes in Hamming space
     74    ``H_q^n`` of minimal distance ``d``
     75
     76
     77    INPUT:
     78
     79    - ``n`` -- the code length
     80
     81    - ``d`` -- the (lower bound on) minimal distance of the code
     82
     83    - ``q`` -- the size of the alphabet
     84
     85    - ``isinteger`` -- if ``True``, uses an integer programming solver (ILP), rather
     86        that an LP solver. Can be very slow if set to ``True``.
     87
     88    - ``return_data`` -- if ``True``, return a triple ``(W,LP,bound)``, where ``W`` is
     89        a weights vector,  and ``LP`` the Delsarte bound LP; both of them are Sage LP
     90        data.  ``W`` need not be a weight distribution of a code, or,
     91        if ``isinteger==False``, even have integer entries.
     92
     93    - ``solver`` -- the LP/ILP solver to be used. Defaults to ``PPL``. It is arbitrary
     94        precision, thus there will be no rounding errors. With other solvers
     95        (see :class:`MixedIntegerLinearProgram` for the list), you are on your own!
     96
     97
     98    EXAMPLES:
     99
     100    The bound on the size of the `F_2`-codes of length 11 and minimal distance 6::
     101
     102       sage: delsarte_bound_hamming_space(11, 6, 2)
     103       12
     104       sage: a, p, val = delsarte_bound_hamming_space(11, 6, 2, return_data=True)
     105       sage: [j for i,j in p.get_values(a).iteritems()]
     106       [1, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0]
     107
     108    The bound on the size of the `F_2`-codes of length 24 and minimal distance
     109    8, i.e. parameters of the extened binary Golay code::
     110
     111       sage: a,p,x=delsarte_bound_hamming_space(24,8,2,return_data=True)
     112       sage: x
     113       4096
     114       sage: [j for i,j in p.get_values(a).iteritems()]
     115       [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]
     116
     117    The bound on the size of `F_4`-codes of length 11 and minimal distance 3::
     118
     119       sage: delsarte_bound_hamming_space(11,3,4)
     120       327680/3
     121
     122    REFERENCES:
     123
     124    .. [1] P. Delsarte, An algebraic approach to the association schemes of coding theory,
     125           Philips Res. Rep., Suppl., vol. 10, 1973.
     126
     127
     128
     129    """
     130    A, p = _delsarte_LP_building(n, d, 0, q, isinteger,  solver)
     131    try:
     132        bd=p.solve()
     133    except sage.numerical.mip.MIPSolverException, exc:
     134        print "Solver exception: ", exc, exc.args
     135        if return_data:
     136            return A,p,False
     137        return False
     138
     139    if return_data:
     140        return A,p,bd
     141    else:
     142        return bd
     143
     144def delsarte_bound_additive_hamming_space(n, d, q, d_star=1, q_base=0,
     145                    isinteger=False, return_data=False, solver="PPL"):
     146   """
     147   Find the Delsarte LP bound on ``F_{q_base}``-dimension of additive codes in
     148   Hamming space ``H_q^n`` of minimal distance ``d`` with minimal distance of the dual
     149   code at least ``d_star``.  If ``q_base`` is set to
     150   non-zero, then  ``q`` is a power of ``q_base``, and the code is, formally, linear over
     151   ``F_{q_base}``. Otherwise it is assumed that ``q_base==q``.
     152
     153
     154   INPUT:
     155
     156   - ``n`` -- the code length
     157
     158   - ``d`` -- the (lower bound on) minimal distance of the code
     159
     160   - ``q`` -- the size of the alphabet
     161
     162   - ``d_star`` -- the (lower bound on) minimal distance of the dual code;
     163     only makes sense for additive codes.
     164
     165   - ``q_base`` -- if ``0``, the code is assumed to be nonlinear. Otherwise,
     166     ``q=q_base^m`` and the code is linear over ``F_{q_base}``.
     167
     168   - ``isinteger`` -- if ``True``, uses an integer programming solver (ILP), rather
     169     that an LP solver. Can be very slow if set to ``True``.
     170
     171   - ``return_data`` -- if ``True``, return a triple ``(W,LP,bound)``, where ``W`` is
     172     a weights vector,  and ``LP`` the Delsarte bound LP; both of them are Sage LP
     173     data.  ``W`` need not be a weight distribution of a code, or,
     174     if ``isinteger==False``, even have integer entries.
     175
     176   - ``solver`` -- the LP/ILP solver to be used. Defaults to ``PPL``. It is arbitrary
     177     precision, thus there will be no rounding errors. With other solvers
     178     (see :class:`MixedIntegerLinearProgram` for the list), you are on your own!
     179
     180   EXAMPLES:
     181
     182   The bound on dimension of linear `F_2`-codes of length 11 and minimal distance 6::
     183
     184       sage: delsarte_bound_additive_hamming_space(11, 6, 2)
     185       3
     186       sage: a,p,val=delsarte_bound_additive_hamming_space(11, 6, 2,\
     187                                      return_data=True)
     188       sage: [j for i,j in p.get_values(a).iteritems()]
     189       [1, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0]
     190
     191   The bound on the dimension of linear `F_4`-codes of length 11 and minimal distance 3::
     192
     193       sage: delsarte_bound_additive_hamming_space(11,3,4)
     194       8
     195
     196   The bound on the `F_2`-dimension of additive `F_4`-codes of length 11 and minimal
     197   distance 3::
     198
     199       sage: delsarte_bound_additive_hamming_space(11,3,4,q_base=2)
     200       16
     201
     202   """
     203   if q_base == 0:
     204      q_base = q
     205
     206   kk = 0
     207   while q_base**kk < q:
     208      kk += 1
     209
     210   if q_base**kk != q:
     211      print "Wrong q_base=", q_base, " for q=", q, kk
     212      return False
     213
     214   # this implementation assumes that our LP solver to be unable to do a hot
     215   # restart with an adjusted constraint
     216
     217   m = kk*n # this is to emulate repeat/until block
     218   bd = q**n+1
     219
     220   while q_base**m < bd: # need to solve the LP repeatedly, as this is a new constraint!
     221                         # we might become infeasible. More precisely, after rounding down
     222                         # to the closest value of q_base^m, the LP, with the constraint that
     223                         # the objective function is at most q_base^m,
     224      A, p = _delsarte_LP_building(n, d, d_star, q, isinteger,  solver, q_base**m)
     225      try:
     226        bd=p.solve()
     227      except sage.numerical.mip.MIPSolverException, exc:
     228        print "Solver exception: ", exc, exc.args
     229        if return_data:
     230           return A,p,False
     231        return False
     232    # rounding the bound down to the nearest power of q_base, for q=q_base^m
     233#      bd_r = roundres(log(bd, base=q_base))
     234      m = -1
     235      while q_base**(m+1) < bd:
     236        m += 1
     237      if q_base**(m+1) == bd:
     238        m += 1
     239
     240   if return_data:
     241      return A, p, m
     242   else:
     243      return m
     244