Example #1
0
    def getFinalMaxValue(self, supVal):
        if not gmpy2.is_finite(supVal):
            print("Error cannot compute intervals with infinity")
            exit(-1)
        bkpCtx = gmpy2.get_context().copy()

        i = 0
        while not gmpy2.is_finite(gmpy2.next_above(supVal)):
            set_context_precision(self.precision + i, self.exponent + i)
            i = i + 1

        prec = printMPFRExactly(gmpy2.next_above(supVal))
        gmpy2.set_context(bkpCtx)
        return prec
Example #2
0
    def __init__(self, input_distribution, input_name, precision, exponent, polynomial_precision=[0, 0]):
        """
        Constructor interpolates the density function using Chebyshev interpolation
        then uses this interpolation to build a PaCal object:
        the self.distribution attribute which contains all the methods we could possibly want
        Inputs:
        input_distribution: a PaCal object representing the distribution for which we want to compute
                            the rounding error distribution
        precision, exponent: specify the gmpy2 precision environment
        polynomial_precision: default precision as implemented in AbstractErrorModel will typically not converge
                              so it is re-implemented as dynamically setting polynomial_precision. For very low
                              precision, polynomial_precision needs to be high because the function is very
                              discontinuous (there are few floating points so it won't impact performance).
                              For higher precision it needs to be low for performance reason (but it won't impact
                              accuracy because the function becomes much more regular).
        Warning: the relative error is not defined in the interval rounding to 0. In low precision this interval might
                 have a large probability. This will be reflected by the distribution not integrating to 1.
                 Example: Uniform(-2,2) with 3 bit exponent, 4 bit mantissa and default polynomial_precision integrates
                 to 0.926 !
        """

        super(LowPrecisionErrorModel, self).__init__(input_distribution, precision, exponent, polynomial_precision)
        #self.name = "LPError(" + input_distribution.getName() + ")"
        self.name = "LPE_" + input_name
        set_context_precision(self.precision, self.exponent)
        self.inf_val = mpfr(str(self.input_distribution.range_()[0]))
        self.sup_val = mpfr(str(self.input_distribution.range_()[1]))
        if not gmpy2.is_finite(self.inf_val):
            self.inf_val = gmpy2.next_above(self.inf_val)
        if not gmpy2.is_finite(self.sup_val):
            self.sup_val = gmpy2.next_below(self.sup_val)
        self.max_exp = 2 ** (exponent - 1)
        if self.inf_val == 0:
            # take most negative exponent
            self.exp_inf_val = -self.max_exp
        else:
            self.exp_inf_val = floor(log(abs(self.inf_val), 2))
        if self.sup_val == 0:
            # take most negative exponent
            self.exp_sup_val = -self.max_exp
        else:
            self.exp_sup_val = floor(log(abs(self.sup_val), 2))
        reset_default_precision()
        if polynomial_precision == [0, 0]:
            self.polynomial_precision = [floor(400.0 / float(self.precision)), floor(100.0 / float(self.precision))]
 def _get_min_exponent(self):
     set_context_precision(self.precision, self.exponent)
     inf_val = gmpy2.mpfr(str(self.input_distribution.range_()[0]))
     self.min_sign = gmpy2.sign(inf_val)
     # For some reason the exponent returned by get_exp() is 1 too high and 0 for infinities
     if gmpy2.is_finite(inf_val):
         e = gmpy2.get_exp(inf_val) - 1
     else:
         e = 2 ** (self.exponent - 1)
     if self.min_sign > 0:
         self.min_exp = e
     else:
         if inf_val < -2 ** (float)(e):
             self.min_exp = e + 1
         else:
             self.min_exp = e
     reset_default_precision()
 def _get_max_exponent(self):
     set_context_precision(self.precision, self.exponent)
     sup_val = gmpy2.mpfr(str(self.input_distribution.range_()[1]))
     self.max_sign = gmpy2.sign(sup_val)
     # For some reason the exponent returned by get_exp() is 1 too high and 0 if sup_val is infinite
     if gmpy2.is_finite(sup_val):
         e = gmpy2.get_exp(sup_val) - 1
     else:
         e = 2 ** (self.exponent - 1)
     if self.max_sign < 0:
         self.max_exp = e
     else:
         if sup_val > 2 ** float(e):
             self.max_exp = e + 1
         else:
             self.max_exp = e
     reset_default_precision()
Example #5
0
 def __getpdf(self, t):
     '''
 Constructs the EXACT probability density function at point t in [-1,1]
 Exact values are used to build the interpolating polynomial
     '''
     ctx = gmpy2.get_context()
     ctx.precision = self.precision
     ctx.emin = self.minexp
     ctx.emax = self.maxexp
     eps = 2**-self.precision
     sums = []
     #test if  the input is scalar or an array
     if np.isscalar(t):
         tt = []
         tt.append(t)
     else:
         tt = t
     # main loop through all floating point numbers in reduced precision
     for ti in tt:
         sum = 0.0
         # if ti=0 the result is 0 since it definitely covers the whole interval
         if float(ti) < 1.0:
             x = gmpy2.next_above(gmpy2.inf(-1))
             y = gmpy2.next_above(x)
             z = gmpy2.next_above(y)
             err = float(ti) * eps
             # Deal with the very first interval [x,(x+y)/2]
             ctx.precision = 53
             ctx.emin = -1023
             ctx.emax = 1023
             xmin = float(x)
             xmax = (xmin + float(y)) / 2.0
             xp = xmin / (1.0 - err)
             if xmin < xp < xmax:
                 sum += self.inputdistribution.pdf(xp) * abs(xp) * eps / (
                     1.0 - err)
             # Deal with all standard intervals
             while gmpy2.is_finite(z):
                 ctx.precision = 53
                 ctx.emin = -1023
                 ctx.emax = 1023
                 xmin = xmax
                 xmax = (float(y) + float(z)) / 2.0
                 xp = float(y) / (1.0 - err)
                 if xmin < float(xp) < xmax:
                     sum += self.inputdistribution.pdf(xp) * abs(
                         xp) * eps / (1.0 - err)
                 ctx.precision = self.precision
                 ctx.emin = self.minexp
                 ctx.emax = self.maxexp
                 x = y
                 y = z
                 z = gmpy2.next_above(z)
             # Deal with the very last interval [x,(x+y)/2]
             xmin = xmax
             xp = float(y) / (1.0 - err)
             xmax = float(y)
             if xmin < xp < xmax:
                 sum += self.inputdistribution.pdf(xp) * abs(xp) * eps / (
                     1.0 - err)
         #print('Evaluated at '+repr(ti)+'    Result='+repr(sum))
         sums.append(sum)
     if np.isscalar(t):
         return sum
     else:
         return sums