#! /opt/sage/sage
# @file runSLZ-113proj.sage
#
#@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.
    """
    compiledSpyxDir = "/home/storres/recherche/arithmetique/pobysoPythonSage/compiledSpyx"
    if compiledSpyxDir not in sys.path:
        sys.path.append(compiledSpyxDir)
    if not 'mpfi' in sage.misc.cython.standard_libs:
        sage.misc.cython.standard_libs.append('mpfi')
    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()
from sageMpfr import *
from sageGMP  import *
import sys
from subprocess import call
#
## Main variables and parameters.
x            = var('x')
func(x)      = exp(x)
precision    = 53
emin         = -1022
emax         = 1023 
RRR          = RealField(precision)
degree       = 0
alpha        = 0
htrn         = 0
signAsString = ""
binade       = 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("  binade          the binade we want to  explore\n")
    write("                  as a positive integral expression\n")
    write("  sign            the sign of the interval we want to expolore\n")
    write("                  either \"pos\" or \"neg\"\n")
    write("  debug           debug mode (\"debug\", in any case)\n\n")
    sys.exit(2)
# End usage.
#
argsCount = len(sys.argv)
scriptName = os.path.basename(__file__)
if argsCount < 6:
    usage()
for index in xrange(1,argsCount):
    if index == 1:
        degree = int(sys.argv[index])
    elif index == 2:
        alpha = int(sys.argv[index])
    elif index == 3:
        htrn = int(eval(sys.argv[index]))
    elif index == 4:
        binade = int(eval(sys.argv[index]))
    elif index == 5:
        signAsString = sys.argv[index]
        signAsString = signAsString.lower()
    elif index == 6:
        debugMode = sys.argv[index].upper()
        debugMode = (debugMode == "DEBUG")
# Done with command line arguments collection.
#
## Set the terminal window title.
terminalWindowTitle = ['stt', 
                        str(degree), 
                        str(alpha), 
                        str(htrn), 
                        str(binade),
                        signAsString]
call(terminalWindowTitle)
#
binadeUlp    = 2^(binade - precision + 1)
if signAsString == "pos": 
    lowerBound   = RRR(2^binade)
    upperBound   = RRR(2^(binade+1)) - binadeUlp
elif signAsString == "neg":
    lowerBound  = -RRR(2^(binade+1)) + binadeUlp
    upperBound  =  -RRR(2^binade)
    print lowerBound
    print upperBound
else:
    exceptionErrorMess = "\"" + signAsString + "\" is an invalid sign." \
    ". Aborting!\n"
    raise Exception(exceptionErrorMess)
#
## Debug printing
print "degree         :", degree
print "alpha          :", alpha
print "htrn           :", htrn
print "binade         :", binade
print "lower bound    :", lowerBound.str(truncate=False)
print "upper bound    :", upperBound.str(truncate=False)
print "sign           :", signAsString
print "debug mode     :", debugMode
print
#
srs_run_SLZ_v05_proj(inputFunction=func, 
                     inputLowerBound         = lowerBound, 
                     inputUpperBound         = upperBound, 
                     alpha                   = alpha, 
                     degree                  = degree, 
                     precision               = precision, 
                     emin                    = emin, 
                     emax                    = emax, 
                     targetHardnessToRound   = htrn, 
                     debug                   = debugMode)
    