Exemplo n.º 1
0
def exact_fft(x, debug=False):
    ''' Calculates the exact DFT using gmpy2

    Has ~N^2 efficiency but I tried to optimize the calculation a bit.
    '''
    N = x.shape[0]
    start = time.clock()
    arg = -2j * (gmpy2.const_pi() / N)
    exp1d = np.array([gmpy2.exp(arg * idx) for idx in range(N)])
    exp = np.diag(exp1d)
    for i in range(N):
        for j in range(i+1):
            exp[j, i] = exp[i, j] = exp1d[(j * i) % N]
    stop = time.clock()
    if debug:
        print('preparation %.2fms' % ((stop-start)*1e3))

    start = time.clock()
    x = np.array([gmpy2.mpc(xi) for xi in x])
    # fsum solution - I noticed no difference except that it ran slower
#    y = []
#    for i in range(N):
#        mul = x * exp[i, :]
#        y.append(gmpy2.fsum([m.real for m in mul]) + 1j *
#                 gmpy2.fsum([m.imag for m in mul]))
#    y = np.array(y, dtype=np.complex128)
    y = np.sum(exp * x, axis=-1)
    y = np.array(y, dtype=np.complex128)
    stop = time.clock()
    if debug:
        print('calculation %.2fms' % ((stop-start)*1e3))
    return y
Exemplo n.º 2
0
def calc_pi():
    if mem.libmath == np:
        pi = np.pi
    elif mem.libmath == gmpy2:
        pi = gmpy2.const_pi()
    else:
        raise TypeError("Constants not implemented. Error in 'mem.libmath'")

    return pi
Exemplo n.º 3
0
### 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))


pi = const_pi(bits)
e = const_euler(bits)


def propagation1D(i_max, n_max, ratio, l):

    global pi
    global e

    eps_0 = mp(8.85418782) * 10**-12  # Farad/m
    mi_0 = 4 * pi * 10**-7  # Hen/m
    c = mp(299792458)  # m/s

    dx = mp(0.001)
    dt = 0.9 * dx / c
Exemplo n.º 4
0
parser.add_argument(
    '--prefix',
    help='Prefix to the C style functions to generate (include the "_"!)',
    default="my_")
parser.add_argument(
    '--lgamma',
    help='Whether or not to include an implementation of the `lgamma` function',
    action='store_true')

args = parser.parse_args()

# set precision to the requested one
gmpy2.get_context().precision = args.prec

# pi & e, but to full precision within gmpy2
pi = const_pi()
e = exp(1)


# factorial, product of all integers <= x
@lru_cache
def factorial(x):
    return math.factorial(x)


# double factorial, if n is even, product of all even numbers <= n, otherwise the product of all odd numbers <= n
@lru_cache
def double_factorial(n):
    if n <= 1:
        return 1
    else:
>>> from gmpy2 import mpq
>>> mpq(1,7)
mpq(1,7)
>>> mpq(1,7) * 11
mpq(11,7)
>>> mpq(11,7)/13
mpq(11,91)

#gmpy2: Multiple-precision Reals


>>> import gmpy2
>>> from gmpy2 import mpfr
>>> mpfr('1.2')

>>> gmpy2.const_pi(100000)
>>> gmpy2.get_context().precision=100
>>> gmpy2.atan2(mpfr("+0.0"),mpfr("-0.0"))

>>> gmpy2.sqrt(5)
mpfr('2.2360679774997898')
>>> gmpy2.get_context().precision=100
>>> gmpy2.sqrt(5)
mpfr('2.2360679774997896964091736687316',100)


>>> with gmpy2.local_context(gmpy2.context(), precision=100) as ctx:
	print(gmpy2.sqrt(2))
	ctx.precision += 100
	print(gmpy2.sqrt(2))
        initializer = (str(num1) + "." + str(num2) + str(num3) + "e" +
                       str(exponent))

        if (randint(0, 1) == 1):
            initializer = "-" + initializer

        return Numeric(initializer)

    def eval(self):
        return self

    def getexponent(self):
        return int(gmpy2.floor(self.log().val))


PI = Numeric(gmpy2.const_pi())
PI.latex = lambda: '\pi'
E = Numeric(gmpy2.exp(1))


class Model1(Expression):
    def __init__(self):
        self.value = Numeric.new()
        self.text = str(self.value)

    def gen(self):
        x = randint(1, 550)
        if (x < 120):
            z = Numeric.create(self.value.getexponent() - 1,
                               self.value.getexponent() + 1)
            self.text = z.latex() + " + " + self.value.latex()
Exemplo n.º 7
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.º 8
0
#//////////////////////////////////////////////////
#Code developed to genetare ADS-B message//////////
#//////////////////////////////////////////////////
import math
import sys
import gmpy2  #multi precision module
from gmpy2 import mpz, mpq, mpfr, mpc
import numpy
from numpy import *

#-------------Globals-----------------------------------
gmpy2.get_context().precision = 32  #set gmpy precision to 128 bits
Dlat0 = 6  # Even message
Dlat1 = mpfr(6.101694915254237288135593220339)  # Odd message
pi = mpfr(gmpy2.const_pi(), 56)
airID = 0x8D  # bit 1 to 5 DF format 17 and 6 to 8 CA type 3
altitude = 0
evenMsg = mpz(1)
oddMsg = mpz(1)
CRCOdd = mpz(1)
CRCEven = mpz(1)

#-----------For testing -------------------------
t_aid = 'ab7444'
t_alt = 40000
t_lat = mpfr(49.925827)
t_lon = mpfr(89.193542)

#-------------------------------------------------