# This file was *autogenerated* from the file ./runSLZ-113proj-02.sage
from sage.all_cmdline import *   # import sage library
_sage_const_3 = Integer(3); _sage_const_2 = Integer(2); _sage_const_1 = Integer(1); _sage_const_0 = Integer(0); _sage_const_6 = Integer(6); _sage_const_5 = Integer(5); _sage_const_4 = Integer(4); _sage_const_113 = Integer(113); _sage_const_10 = Integer(10); _sage_const_16383 = Integer(16383); _sage_const_16382 = Integer(16382)#! /opt/sage/sage
# @file runSLZ-113proj-92.sage
#
#@par Changes from runSLZ-113proj.sage
# The uniform random matrix element can be integers in the
# [-2^(n-1),2^(n-1)-1] range. The value of "n" is set in function 
# slz_reduce_lll_proj_02.  
#
#@par Changes from runSLZ-113.sage
# LLL reduction is not performed on the matrix itself but rather on the
# product of the matrix with a uniform random matrix.
# The reduced matrix obtained is discarded but the transformation matrix 
# obtained is used to multiply the original matrix in order to reduced it.
# If a sufficient level of reduction is obtained, we stop here. If not
# the product matrix obtained above is LLL reduced. But as it has been
# pre-reduced at the above step, reduction is supposed to be much faster.
#
# Both reductions combined should hopefully be faster than a straight single
# reduction.
#
# Run SLZ for p=113
#from scipy.constants.codata import precision
def initialize_env():
    """
    Load all necessary modules.
    """
    if version().split()[_sage_const_2 ].replace(',','') > '6.6':
        compiledSpyxDir = \
            "/home/storres/recherche/arithmetique/pobysoPythonSage/compiledSpyx"
        if compiledSpyxDir not in sys.path:
            sys.path.append(compiledSpyxDir)
    else:
        if not 'mpfi' in sage.misc.cython.standard_libs:
            sage.misc.cython.standard_libs.append('mpfi')
        load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageMpfr.spyx")
        load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageGMP.spyx")
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sollya_lib.sage")
#    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageMpfr.spyx")
#    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageGMP.spyx")
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/pobyso.py")
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageSLZ.sage")
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageNumericalOperations.sage")
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageRationalOperations.sage")
    # Matrix operations are loaded by polynomial operations.
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage")
    load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageRunSLZ.sage")


print "Running SLZ..."
initialize_env()
if version().split()[_sage_const_2 ].replace(',','') > '6.6':
    from sageMpfr import *
    from sageGMP  import *
import sys
from subprocess import call
#
## Main variables and parameters.
x         = var('x')
__tmp__=var("x"); func = symbolic_expression(exp(x)).function(x)
precision = _sage_const_113 
emin      = -_sage_const_16382 
emax      = _sage_const_16383  
RRR = RealField(precision)
degree              = _sage_const_0 
alpha               = _sage_const_0 
htrn                = _sage_const_0 
intervalCenter      = _sage_const_0 
intervalRadius      = _sage_const_0 
debugMode           = False          
## Local functions
#
def usage():
    write = sys.stderr.write
    write("\nUsage:\n")
    write("  " + scriptName + " <degree> <alpha> <htrn> <intervalCenter>\n")
    write("               <numberOfNumbers> [debug]\n")
    write("\nArguments:\n")
    write("  degree          the degree of the polynomial (integer)\n")
    write("  alpha           alpha (integer)\n")
    write("  htrn            hardness-to-round - a number of bits (integer)\n")
    write("  intervalCenter  the interval center (a floating-point number)\n")
    write("  numberOfNumbers the number of floating-point numbers in the interval\n")
    write("                  as a positive integral expression\n")
    write("  debug           debug mode (\"debug\", in any case)\n\n")
    sys.exit(_sage_const_2 )
# End usage.
#
argsCount = len(sys.argv)
scriptName = os.path.basename(__file__)
if argsCount < _sage_const_5 :
    usage()
for index in xrange(_sage_const_1 ,argsCount):
    if index == _sage_const_1 :
        degree = int(sys.argv[index])
    elif index == _sage_const_2 :
        alpha = int(sys.argv[index])
    elif index == _sage_const_3 :
        htrn = int(eval(sys.argv[index]))
    elif index == _sage_const_4 :
        try:
            intervalCenter = QQ(sage_eval(sys.argv[index]))
        except:
            intervalCenter = RRR(sys.argv[index])
        intervalCenter = RRR(intervalCenter)
    elif index == _sage_const_5 :
        ## Can be read as rational number but must end up as an integer.
        numberOfNumbers = QQ(sage_eval(sys.argv[index]))
        if numberOfNumbers != numberOfNumbers.round():
            raise Exception("Invalid number of numbers: " + sys.argv[index] + ".")
        numberOfNumbers = numberOfNumbers.round()
        ## The number must be strictly positive.
        if numberOfNumbers <= _sage_const_0 :
            raise Exception("Invalid number of numbers: " + sys.argv[index] + ".")
    elif index == _sage_const_6 :
        debugMode = sys.argv[index].upper()
        debugMode = (debugMode == "DEBUG")
# Done with command line arguments collection.
#
## Debug printing
print "degree         :", degree
print "alpha          :", alpha
print "htrn           :", htrn
print "interval center:", intervalCenter.n(prec=_sage_const_10 ).str(truncate=False)
print "num of nums    :", RR(numberOfNumbers).log2().n(prec=_sage_const_10 ).str(truncate=False)
print "debug mode     :", debugMode
print
#
## Set the terminal window title.
terminalWindowTitle = ['stt', str(degree), str(alpha), str(htrn), 
                       intervalCenter.n(prec=_sage_const_10 ).str(truncate=False), 
                       RRR(numberOfNumbers).log2().n(prec=_sage_const_10 ).str(truncate=False)]
call(terminalWindowTitle)
#
intervalCenterBinade = slz_compute_binade(intervalCenter)
intervalRadius       = \
    _sage_const_2 **intervalCenterBinade * _sage_const_2 **(-precision + _sage_const_1 ) * numberOfNumbers / _sage_const_2 
srs_run_SLZ_v05_proj_02(inputFunction=func, 
                        inputLowerBound         = intervalCenter - intervalRadius, 
                        inputUpperBound         = intervalCenter + intervalRadius, 
                        alpha                   = alpha, 
                        degree                  = degree, 
                        precision               = precision, 
                        emin                    = emin, 
                        emax                    = emax, 
                        targetHardnessToRound   = htrn, 
                        debug                   = debugMode)
    
