Exemplo n.º 1
0
    def create(self):
        ctx = gmpy2.ieee(self.length * 8)  # Emulate IEEE for requested format size
        ctx.round = self.rmodeo  # Set the requested gmpy2 rounding mode
        gmpy2.set_context(ctx)  # Make this the active context.

        # Convert the floating point string to an mpfr object
        return gmpy2.mpfr(self.fpstr)
Exemplo n.º 2
0
Arquivo: sha2.py Projeto: mmgen/mmgen
	def initConstants(cls):

		from math import sqrt

		def nextPrime(n=2):
			while True:
				for factor in range(2,int(sqrt(n))+1):
					if n % factor == 0:
						break
				else:
					yield n
				n += 1

		# Find the first nRounds primes
		npgen = nextPrime()
		primes = [next(npgen) for x in range(cls.nRounds)]

		fb_mul = 2 ** cls.wordBits
		def getFractionalBits(n):
			return int((n - int(n)) * fb_mul)

		if cls.use_gmp:
			from gmpy2 import context,set_context,sqrt,cbrt
			set_context(context(precision=75))
		else:
			cbrt = lambda n: pow(n, 1 / 3)

		# First wordBits bits of the fractional parts of the square roots of the first 8 primes
		H = (getFractionalBits(sqrt(n)) for n in primes[:8])

		# First wordBits bits of the fractional parts of the cube roots of the first nRounds primes
		K = (getFractionalBits(cbrt(n)) for n in primes)

		cls.H_init = tuple(H)
		cls.K      = tuple(K)
Exemplo n.º 3
0
def main23(N):
    c=gmpy2.context()
    c.precision=len(str(N))+20
    c.real_prec=len(str(N))+20
    gmpy2.set_context(c)
    assert N==gmpy2.mpz(N)
    N=gmpy2.mpz(N)
    A=find_sqrt(6*N)
    A = gmpy2.mpz(A)
    assert ( (A-1)**2<6*N<A**2 ),'not found'
    Atag = A
    for iA in xrange(-2**20,2**20):
        A = Atag + iA
        s =A**2-N
        x = gmpy2.sqrt(s)
        try:
            x = gmpy2.mpz(x)
        except:continue
        '''print x**2 > s ,  x**2 < s
        while x**2>s:
            x-=1
            print 1,
        print x**2 > s ,  x**2 < s'''
        for p in [(A-x)/2,(A-x)/3,(A+x)/2,(A+x)/3   ]:
            q = N/p
            
            if p*q==N:
                print gmpy2.is_prime(q)
                print gmpy2.is_prime(p)
                print min(p,q)
                assert False,'bla'
Exemplo n.º 4
0
def main(N):
    c=gmpy2.context()
    c.precision=len(str(N))+20
    c.real_prec=len(str(N))+20
    gmpy2.set_context(c)
    assert N==gmpy2.mpz(N)
    N=gmpy2.mpz(N)
    A=find_sqrt(N)
    A = gmpy2.mpz(A)
    assert ( (A-1)**2<N<A**2 ),'not found'
    Atag = A
    for iA in xrange(-2**20,2**20):
        A = Atag + iA
        s =A**2-N
        x = gmpy2.sqrt(s)
        try:
            x = gmpy2.mpz(x)
        except:continue
        print x**2 > s ,  x**2 < s
        '''while x**2>s:
            x-=1
            print 1,'''
        print x**2 > s ,  x**2 < s

        p = A-x
        q = A+x
        q = N/p

        if p*q==N:
            print gmpy2.is_prime(q)
            print gmpy2.is_prime(p)
            print min(p,q)
            break
Exemplo n.º 5
0
def set_prec(prc=None):
    """
    set the precision
    pass None to get current precision
    prc is the number of digits that should be accurate in base two
    """
    #note:
    #  precision is defined in base 2. It needs to be redefined in the base
    #  that is being used so as the number in the new base so it doesn't lose
    #  precision. That is, the number shouldn't return with trailing garbage
    #  (45.0000000000000000052...). The conversions are automatically done in
    #  the functions that need the precision - it is not done here.

    global prec
    if prc is None: return prec
    prc = int(abs(prc))

    if backend == 'mpmath':  # base ten precision
        mp.prec = int(prc * log(2, 10))
    elif backend == 'gmpy2':  # base two precision
        gm.set_context(gm.context(precision=prc))
    elif backend == 'decimal':  # base ten precision
        dm.getcontext().prec = int(prc * log(2, 10))
    else:
        pass

    prec = prc
    return prc
Exemplo n.º 6
0
Arquivo: bfp.py Projeto: mstram/SATK
 def create(self, string, round=0):
     ctx = gmpy2.ieee(self.format)
     ctx.round = MPFR_Data.rounding[round]
     gmpy2.set_context(ctx)
     fpo = gmpy2.mpfr(string)
     ctx = gmpy2.get_context()  # Get the results
     self.ctx = ctx.copy()  # Copy the context for later status
     return fpo
Exemplo n.º 7
0
Arquivo: bfp.py Projeto: mstram/SATK
 def create(self,string,round=0):
     ctx=gmpy2.ieee(self.format)
     ctx.round=MPFR_Data.rounding[round]
     gmpy2.set_context(ctx)
     fpo=gmpy2.mpfr(string)
     ctx=gmpy2.get_context()     # Get the results
     self.ctx=ctx.copy()         # Copy the context for later status
     return fpo
Exemplo n.º 8
0
    def to_bytes(self, byteorder="big"):

        self.digits()

        print(format(self.fpo, "A"))
        ctx = gmpy2.ieee(self.length * 8)
        gmpy2.set_context(ctx)
        b = gmpy2.to_binary(self.fpo)
        return b
Exemplo n.º 9
0
def main():
    gmpy2.set_context(gmpy2.context())
    gmpy2.get_context().precision = 75
    T = int(input())
    if T < 1 or T > 100:
        raise RuntimeError("T is not within the valid range")
    for i in range(T):
        n = int(input())
        r = Numbers(n)
        print("Case #" + str(i + 1) + ": " + r)
Exemplo n.º 10
0
def recover_msg(N1, N2, N3, C1, C2, C3):

    m = 42
    # your code starts here: to calculate the original message - m
    # Note 'm' should be an integer
    N = N1 * N2 * N3
    #y1 = N / N1
    #y2 = N / N2
    #y3 = N / N3

    s = [1, 0]
    r = [(N / N1), N1]
    i = 2
    temp = (N / N1) % N1
    while (temp > 0):
        temp = r[i - 2] % r[i - 1]
        r.append(temp)
        s.append(s[i - 2] - (r[i - 2] / r[i - 1]) * s[i - 1])
        i += 1
    z1 = s[i - 2]

    s = [1, 0]
    r = [(N / N2), N2]
    i = 2
    temp = (N / N2) % N2
    while (temp > 0):
        temp = r[i - 2] % r[i - 1]
        r.append(temp)
        s.append(s[i - 2] - (r[i - 2] / r[i - 1]) * s[i - 1])
        i += 1
    z2 = s[i - 2]

    s = [1, 0]
    r = [(N / N3), N3]
    i = 2
    temp = (N / N3) % N3
    while (temp > 0):
        temp = r[i - 2] % r[i - 1]
        r.append(r[i - 2] % r[i - 1])
        s.append(s[i - 2] - (r[i - 2] / r[i - 1]) * s[i - 1])
        i += 1
    z3 = s[i - 2]
    if z1 < 0: z1 += N1
    if z2 < 0: z2 += N2
    if z3 < 0: z3 += N3
    x = (z1 * C1 * (N / N1) + z2 * C2 * (N / N2) + z3 * C3 * (N / N3)) % N
    gmpy2.set_context(gmpy2.context())
    gmpy2.get_context().precision = 1000000
    m = int(gmpy2.cbrt(x))
    # your code ends here

    # convert the int to message string
    msg = hex(m).rstrip('L')[2:].decode('hex')
    return msg
Exemplo n.º 11
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
Exemplo n.º 12
0
 def checkPQDistance(self, keys):
     if not isinstance(keys, (tuple, list)):
         keys = [keys]
     ctx = gmpy2.get_context()
     ctx.precision = 5000
     ctx.round = gmpy2.RoundDown
     ctx.real_round = gmpy2.RoundDown
     gmpy2.set_context(ctx)
     for key in keys:
         gmpy_key = gmpy2.mpfr(key)
         skey = int(gmpy2.sqrt(gmpy_key))
         skey2 = skey ** 2
         if (skey2 > key) or (((skey + 1) ** 2) < key):
             print skey2
             print (skey+1)**2
             raise Exception("WTF")
         bits = int(gmpy2.log2(key - skey2))
         if bits < 480:
             print '%d Has p, q distance of %d bits' % (key, bits)
Exemplo n.º 13
0
 def checkPQDistance(self, keys):
     if not isinstance(keys, (tuple, list)):
         keys = [keys]
     ctx = gmpy2.get_context()
     ctx.precision = 5000
     ctx.round = gmpy2.RoundDown
     ctx.real_round = gmpy2.RoundDown
     gmpy2.set_context(ctx)
     for key in keys:
         gmpy_key = gmpy2.mpfr(key)
         skey = int(gmpy2.sqrt(gmpy_key))
         skey2 = skey**2
         if (skey2 > key) or (((skey + 1)**2) < key):
             print skey2
             print(skey + 1)**2
             raise Exception("WTF")
         bits = int(gmpy2.log2(key - skey2))
         if bits < 480:
             print '%d Has p, q distance of %d bits' % (key, bits)
Exemplo n.º 14
0
    def __init__(self, src, ic=None, format=32, round=0):
        self.ic = ic  # Interchange format hex data string
        self.fpo = None  # gnoy2.mpfr object
        self.format = format  # interchange format being created

        # These values are supplied by the gmpy2.mpfr.digits() method
        self.digits = None  # the binary digits of the signigicand
        self.dexp = None  # the signed exponent
        self.dprec = None  # the precision of the object

        # These attributes are produced below and are destined for the interchange
        # format
        self.isign = None  # The value's sign
        self.ibits = None  # The actual bits destined for the significand
        self.iexp = None  # The signed exponent destined for the int

        if isinstance(src, gmpy2.mpfr):
            self.fpo = src
        elif isinstance(src, str):
            ctx = gmpy2.ieee(format)
            ctx.round = gmpy2.round = round
            gmpy2.set_context(ctx)
            self.fpo = gmpy2.mpfr(src)
        else:
            raise ValueError(
                "%s 'byts' argument unrecognized: %s" % (fp.eloc(self, "__init__", module=this_module), byts)
            )

        self.digits, self.dexp, self.dprec = self.fpo.digits(2)

        if self.digits[0] == "-":
            self.isign = 1
            self.ibits = self.digits[2:]  # Remove the sign and implied first 1
        else:
            self.isign = 0
            self.ibits = self.digits[1:]  # Remove the implied first 1

        # The exponent assumes the leading one is part of the significand, so the
        # exponent is one larger than is required for the interchange format.
        self.iexp = self.dexp - 1
Exemplo n.º 15
0
    def initConstants(cls):

        from math import sqrt

        def nextPrime(n=2):
            while True:
                for factor in range(2, int(sqrt(n)) + 1):
                    if n % factor == 0:
                        break
                else:
                    yield n
                n += 1

        # Find the first nRounds primes
        npgen = nextPrime()
        primes = [next(npgen) for x in range(cls.nRounds)]

        fb_mul = 2**cls.wordBits

        def getFractionalBits(n):
            return int((n - int(n)) * fb_mul)

        if cls.use_gmp:
            from gmpy2 import context, set_context, sqrt, cbrt
            # context() parameters are platform-dependent!
            set_context(context(precision=75,
                                round=1))  # OK for gmp 6.1.2 / gmpy 2.1.0
        else:
            cbrt = lambda n: pow(n, 1 / 3)

        # First wordBits bits of the fractional parts of the square roots of the first 8 primes
        H = (getFractionalBits(sqrt(n)) for n in primes[:8])

        # First wordBits bits of the fractional parts of the cube roots of the first nRounds primes
        K = (getFractionalBits(cbrt(n)) for n in primes)

        cls.H_init = tuple(H)
        cls.K = tuple(K)
Exemplo n.º 16
0
when dividing by dx^2, the error grows.

Don't kno why the error gets bigger for dx=10e-5.
'''
#
# On double precision (64) approximation dx=10e-60 gets 0, and dx=10e-20 evaluates the cossine 5 times on the same point.
'''
Here, the error gets bigger from dx=10e-20 to dx=10e-60 because
they don't have enough digits to register the changes in the step,
they evaluate the function different times on same points, then,
when dividing by dx^2, the error grows.
'''
#
#
#
'''

gmpy2.set_context(gmpy2.context(precision=32))
pi = mp(pi)
x = pi/4
func = lambda x: cos(2*pi*60*x)



gmpy2.set_context(gmpy2.context(precision=64))
pi = mp(pi)
x = pi/4
func = lambda x: cos(2*pi*60*x)


%clear
Exemplo n.º 17
0
Arquivo: gslib.py Projeto: PKizin/gsa
 def update():
     ctx = gmpy2.get_context()
     ctx.precision = GSParams.bits
     gmpy2.set_context(ctx)
Exemplo n.º 18
0
import gmpy2
from gmpy2 import mpz

__author__ = 'Qubo'

N1 = '17976931348623159077293051907890247336179769789423065727343008115'
N1 += '77326758055056206869853794492129829595855013875371640157101398586'
N1 += '47833778606925583497541085196591615128057575940752635007475935288'
N1 += '71082364994994077189561705436114947486504671101510156394068052754'
N1 += '0071584560878577663743040086340742855278549092581'

gmpy2.set_context(gmpy2.context(precision=2000))

N = mpz(N1)
A = gmpy2.ceil(gmpy2.sqrt(N))
SquareA = gmpy2.square(A)

if gmpy2.is_square(mpz(gmpy2.sub(SquareA, N))):
    x = gmpy2.sqrt(gmpy2.sub(SquareA, N))
    print 'p = ' + str(mpz(gmpy2.sub(A, x)))
    print 'q = ' + str(mpz(gmpy2.add(A, x)))
else:
    print 'x is not square, must be wrong...'
Exemplo n.º 19
0
# ------------------------------------------------
### IMPORTS ###

from gmpy2 import context, set_context
from gmpy2 import mpfr, sqrt, const_pi, exp, sin, cos, polar, norm

import matplotlib.pyplot as plt
from seaborn import set_style

# ------------------------------------------------
### DEFINITIONS ###

set_style('darkgrid')

bits = 64
set_context(context(precision=bits, allow_complex=True))

I = sqrt(-1)

pi = const_pi(bits)

# ------------------------------------------------
### FUNCTIONS ###


def backf(val, digits=8):
    return [backf(v, digits)
            for v in val] if isinstance(val,
                                        (list, tuple, map,
                                         zip)) else float(round(val, digits))
Exemplo n.º 20
0

def mp(num):
    return mpfr(str(num))


def backf(num, digits=5):
    return float(round(num, digits))

def listf( l ):
    return list(map(backf, l))

# ------------------------------------------------
### TESTS ###

gmpy2.set_context(gmpy2.context(precision=32))


# for the 1st order Euler EDO method

dy_dt = lambda t, y: 4*t - (2/t)*y
t_0 = mp(1)
y_0 = mp(2)
dt = 8*mp(10)**-3
interv = [mp(0.2), mp(1.8)]

sol1_t = arange( *interv, dt)
sol1_y = list(map(lambda t: t**2 + 1/(t**2), sol1_t))

[apr1_t , apr1_y] = [listf(l) for l in edo1(dy_dt, t_0, y_0, dt, interv)]
Exemplo n.º 21
0
    # Not passing tests!
    import decimal
    Real = decimal.Decimal
    decimal.getcontext().prec = 80
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'numpy':
    import numpy
    Real = numpy.float64
    del numpy
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'gmpy2':
    # Not passing tests!
    import gmpy2
    gmpy2.set_context(gmpy2.ieee(128))
    Real = gmpy2.mpz
    NUM_EPS = Real(float("1e-10"))
    NUM_INF = gmpy2.get_emax_max()
    del gmpy2
else:
    raise Exception("Type not found")

NUM_EPS_SQ = NUM_EPS * NUM_EPS
NUM_ZERO = Real(0.0)
NUM_ONE = Real(1.0)


class Event:
    __slots__ = (
        "type",
Exemplo n.º 22
0
def reset_default_precision():
    gmpy2.set_context(gmpy2.context())
import gmpy2
import binascii
gmpy2.set_context(gmpy2.get_context())
gmpy2.get_context().precision = 1050


def question1():
    N = gmpy2.mpfr(
        "179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581",
        1050)
    #print("N",N)
    A = gmpy2.ceil((gmpy2.sqrt(N)))
    #print("A:",A)
    k = gmpy2.square(A) - N
    #print("K:",k)
    x = gmpy2.ceil(gmpy2.sqrt(k))

    print("x:", x)
    p = A + x
    q = A - x
    if (N == gmpy2.mul(p, q)):
        if (p > q):
            p, q = q, p
        print("found pq:", p, q)


def question2():
    #25464796146996183438008816563973942229341454268524157846328581927885777969985222835143851073249573454107384461557193173304497244814071505790566593206419759

    N = gmpy2.mpfr(
        "648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877",
Exemplo n.º 24
0
import gmpy2 as g
import binascii
import sys

ctx = g.context(precision=10000000,round=g.RoundUp)
g.set_context(ctx)

def bruteSmall(N):
  zero = g.mpz(0)
  two = g.mpz(2)
  if g.f_mod(N, two) == zero:
    return two, g.mpz(g.div(N, two))

  i = g.mpz(3)
  while True:
    if g.f_mod(N, i) == zero:
      p = g.mpz(i)
      q = g.mpz( g.div(N,i) )
      if checkFactors(p,q,N) :
        return p,q
    i = g.add(i, two)

def checkFactors(p,q,N):
  x = g.f_mod( N, p )
  y = g.f_mod( N, q )
  zero = g.mpz('0')
  if g.is_prime(p) and g.is_prime(q):
    return N == g.mul(p,q) and x == zero and y == zero
  return False

def checkD(e,d,phi):
Exemplo n.º 25
0
import gmpy2 
import binascii

ctx = gmpy2.context(precision=10000000,round=gmpy2.RoundUp)
gmpy2.set_context(ctx)


def FermatFactor(N):
  A = gmpy2.mpz( gmpy2.ceil( gmpy2.sqrt(N) ) )
  B2 = gmpy2.sub( gmpy2.square(A), N )

  while not gmpy2.is_square(B2): 
    A = gmpy2.add( A, gmpy2.mpz("1") )
    B2 = gmpy2.sub( gmpy2.square(A), N )
  
  B = gmpy2.sqrt(B2)
  P = gmpy2.mpz( gmpy2.sub( A, B ) )
  Q = gmpy2.mpz( gmpy2.add( A, B ) )
  if not checkFactors(P,Q,N):
    raise Exception("Bad factors generated")
  return ( P, Q )

def checkFactors(p,q,N):
  x = gmpy2.f_mod( N, p )
  y = gmpy2.f_mod( N, q )
  zero = gmpy2.mpz('0')
  if g.is_prime(p) and g.is_prime(q):
    return N == g.mul(p,q) and x == zero and y == zero
  return False

def checkD(e,d,phi):
Exemplo n.º 26
0
def compute_timings():
    # operations = [bf.add, bf.mul, bf.sub, bf.div]
    operations = [gp.add, gp.mul, gp.sub, gp.div]
    labels = ["add", "mul", "sub", "div"]

    for label, operation in zip(labels, operations):
        precisions, times = [], []
        for i in range(3, 45):
            p = int(1.3**i)
            precisions.append(p)
            gp.set_context(gp.context(precision=p))
            start = time.time()
            pi = gp.const_pi()
            e = gp.exp(1)
            a = operation(e, pi)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)

            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            # pi = bf.const_pi(context)
            # e = bf.exp(1, context)
            # a = operation(e, pi, context)
            end = time.time()
            times.append(end-start)
        plt.plot(precisions, times, label=label)
        plt.legend()
        plt.xlabel('Precision')
        plt.ylabel('Time')
    plt.show()
Exemplo n.º 27
0
    # Not passing tests!
    import decimal
    Real = decimal.Decimal
    decimal.getcontext().prec = 80
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'numpy':
    import numpy
    Real = numpy.float64
    del numpy
    NUM_EPS = Real("1e-10")
    NUM_INF = Real(float("inf"))
elif NUMBER_TYPE == 'gmpy2':
    # Not passing tests!
    import gmpy2
    gmpy2.set_context(gmpy2.ieee(128))
    Real = gmpy2.mpz
    NUM_EPS = Real(float("1e-10"))
    NUM_INF = gmpy2.get_emax_max()
    del gmpy2
else:
    raise Exception("Type not found")

NUM_EPS_SQ = NUM_EPS * NUM_EPS
NUM_ZERO = Real(0.0)
NUM_ONE = Real(1.0)


class Event:
    __slots__ = (
        "type",
Exemplo n.º 28
0
import builtins
import contextlib
import inspect
import os

import gmpy2
gmpy2.set_context(gmpy2.ieee(64))

import soap
from soap.context.base import _Context, ConfigError

_repr = builtins.repr
_str = builtins.str
_soap_classes = [c for c in dir(soap) if inspect.isclass(c)]


def _run_line_magic(magic, value):
    from soap.shell import shell
    with open(os.devnull, 'w') as null:
        with contextlib.redirect_stdout(null):
            shell.run_line_magic(magic, value)


class SoapContext(_Context):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for c in _soap_classes:
            c._repr = c.__repr__

    def precision_hook(self, value):
        fp_format = {'single': 32, 'double': 64}.get(value, None)
Exemplo n.º 29
0
import gmpy2
from gmpy2 import mpfr, const_pi, const_euler
from numpy import meshgrid, array

import matplotlib.pyplot as plt
import seaborn as sns

import matplotlib.animation as anim

import os
import cv2  # conda install -c conda-forge opencv=4.1.0

bits = 32
# bits = 64

gmpy2.set_context(gmpy2.context(precision=bits))

# ------------------------------------------------
### FUNCTIONS ###


def mp(num):
    return mpfr(str(num))


def backf(num, digits=5):
    return float(round(num, digits))


def listf(l):
    return list(map(backf, l))
Exemplo n.º 30
0
 def update():
     ctx = gmpy2.get_context()
     ctx.precision = GSParams.bits
     gmpy2.set_context(ctx)