Convert a Galois Field's number into a nice polynomial
187349
def _to_binpoly(x):
        ''''''
        if x <= 0: return "0"
        b = 1 # init to 2^0 = 1
        c = [] # stores the degrees of each term of the polynomials
        i = 0 # counter for b = 2^i
        while x > 0:
            b = (1 << i) # generate a number power of 2: 2^0, 2^1, 2^2, ..., 2^i. Equivalent to b = 2^i
            if x & b : # then check if x is divisible by the power of 2. Equivalent to x % 2^i == 0
                # If yes, then...
                c.append(i) # append this power (i, the exponent, gives us the coefficient)
                x ^= b # and compute the remainder of x / b
            i = i+1 # increment to compute the next power of 2
        return " + ".join(["x^%i" % y for y in c[::-1]])
172545
def format_field(self, value: Any, spec: str) -> str:
        """"""
        from cirq import ops  # HACK: avoids cyclic dependency.
        if isinstance(value, (float, int)):
            if isinstance(value, float):
                value = round(value, self.precision)
            if spec == 'half_turns':
                value = 'pi*{}'.format(value) if value != 0 else '0'
                spec = ''
        elif isinstance(value, ops.Qid):
            value = self.qubit_id_map[value]
        elif isinstance(value, str) and spec == 'meas':
            value = self.meas_key_id_map[value]
            spec = ''
        return super().format_field(value, spec)
562356
def secp256k1():
        
        GFp = FiniteField(2 ** 256 - 2 ** 32 - 977)  # This is P from below... aka FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
        ec = EllipticCurve(GFp, 0, 7)
        return ECDSA(ec, ec.point(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8), 2 ** 256 - 432420386565659656852420866394968145599)
854898
function addLeadingZeros(numField, length) {
		while (numField.toString().length < length) {
			numField = '0' + numField.toString();
		}

		return numField;
	}
1256288
def _get_facvar(self, polynomial):
        
        facvar = [0] * (self.n_vars + 1)
        # Preprocess the polynomial for uniform handling later
        if is_number_type(polynomial):
            facvar[0] = polynomial
            return facvar
        polynomial = polynomial.expand()
        if polynomial.is_Mul:
            elements = [polynomial]
        else:
            elements = polynomial.as_coeff_mul()[1][0].as_coeff_add()[1]
        for element in elements:
            results = self._get_index_of_monomial(element)
            for (k, coeff) in results:
                facvar[k] += coeff
        return facvar
804259
def check(self, r, k=None):
        '''
        '''
        n = self.n
        if not k: k = self.k
        #h = self.h[k]
        g = self.g[k]

        # If we were given a string, convert to a list (important to support fields above 2^8)
        if isinstance(r, _str):
            r = [ord(x) for x in r]

        # Turn r into a polynomial
        c = Polynomial([GF2int(x) for x in r])

        # This works too, but takes longer. Both checks are just as valid.
        #return (c*h)%gtimesh == Polynomial(x0=0)

        # Since all codewords are multiples of g, checking that codeword c divides g
        # suffices for validating a codeword.
        return c % g == Polynomial(x0=0)
861139
function numberToString (x) { // avoids scientific notation for too large and too small numbers

    if (isString (x)) return x

    if (Math.abs (x) < 1.0) {
        const s = x.toString ()
        const e = parseInt (s.split ('e-')[1])
        const neg = (s[0] === '-')
        if (e) {
            x *= Math.pow (10, e-1)
            x = (neg ? '-' : '') + '0.' + (new Array (e)).join ('0') + x.toString ().substring (neg ? 3 : 2)
        }
    } else {
        let e = parseInt (x.toString ().split ('+')[1])
        if (e > 20) {
            e -= 20
            x /= Math.pow (10, e)
            x += (new Array (e+1)).join ('0')
        }
    }
    return x.toString ()
}
730271
def description(self):
        ''''''
        if 0 < self.algo <= 3:
            return "Reed-Solomon with polynomials in Galois field of characteristic %i (2^%i) with generator=%s, prime poly=%s and first consecutive root=%s." % (self.field_charac, self.c_exp, self.gen_nb, hex(self.prim), self.fcr)
        elif self.algo == 4:
            return "Reed-Solomon with polynomials in Galois field of characteristic %i (2^%i) under US FAA ADSB UAT RS FEC standard with generator=%s, prime poly=%s and first consecutive root=%s." % (self.field_charac, self.c_exp, self.gen_nb, hex(self.prim), self.fcr)
        else:
            return "No description for this ECC algorithm."
941251
def django_field_to_index(field, **attr):
    '''
    
    '''

    dj_type = field.get_internal_type()

    if dj_type in ('DateField', 'DateTimeField'):
        return DateField(**attr)
    elif dj_type in ('BooleanField', 'NullBooleanField'):
        return BooleanField(**attr)
    elif dj_type in ('DecimalField', 'FloatField'):
        return NumberField(coretype='float', **attr)
    elif dj_type in ('PositiveSmallIntegerField', 'SmallIntegerField'):
        return NumberField(coretype='short', **attr)
    elif dj_type in ('IntegerField', 'PositiveIntegerField', 'AutoField'):
        return NumberField(coretype='integer', **attr)
    elif dj_type in ('BigIntegerField'):
        return NumberField(coretype='long', **attr)

    return StringField(**attr)
305165
def get_support(variables, polynomial):
    
    support = []
    if is_number_type(polynomial):
        support.append([0] * len(variables))
        return support
    for monomial in polynomial.expand().as_coefficients_dict():
        tmp_support = [0] * len(variables)
        mon, _ = __separate_scalar_factor(monomial)
        symbolic_support = flatten(split_commutative_parts(mon))
        for s in symbolic_support:
            if isinstance(s, Pow):
                base = s.base
                if is_adjoint(base):
                    base = base.adjoint()
                tmp_support[variables.index(base)] = s.exp
            elif is_adjoint(s):
                tmp_support[variables.index(s.adjoint())] = 1
            elif isinstance(s, (Operator, Symbol)):
                tmp_support[variables.index(s)] = 1
        support.append(tmp_support)
    return support
949588
def _construct_lookup_table(self, polynomial):
        

        self._lookup_table = []

        for i in range(0, 256):
            lookup_value = i

            for _ in range(0, 8):
                if lookup_value & 0x1 == 0x1:
                    lookup_value = (lookup_value >> 1) ^ polynomial

                else:
                    lookup_value = lookup_value >> 1

            self._lookup_table.append(lookup_value)
465049
def coerce_to_decimal(value):
    """"""
    if isinstance(value, decimal.Decimal):
        return value
    else:
        try:
            return decimal.Decimal(value)
        except decimal.InvalidOperation as e:
            raise GraphQLInvalidArgumentError(e)
300568
def secp256r1():
        
        GFp = FiniteField(int("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", 16))
        ec = EllipticCurve(GFp, 115792089210356248762697446949407573530086143415290314195533631308867097853948, 41058363725152142129326129780047268409114441015993725554835256314039467401291)
        # return ECDSA(GFp, ec.point(0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296,0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5),int("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", 16))
        return ECDSA(ec,
                     ec.point(0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296, 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5),
                     GFp)
348674
def format_number_field(__, prec, number, locale):
    """"""
    prec = NUMBER_DECIMAL_DIGITS if prec is None else int(prec)
    locale = Locale.parse(locale)
    pattern = locale.decimal_formats.get(None)
    return pattern.apply(number, locale, force_frac=(prec, prec))
29153
def gf_poly_mul(p, q):
    ''''''
    # Pre-allocate the result array
    r = bytearray(len(p) + len(q) - 1)
    # Precompute the logarithm of p
    lp = [gf_log[p[i]] for i in xrange(len(p))]
    # Compute the polynomial multiplication (just like the outer product of two vectors, we multiply each coefficients of p with all coefficients of q)
    for j in xrange(len(q)):
        qj = q[j] # optimization: load the coefficient once
        if qj != 0: # log(0) is undefined, we need to check that
            lq = gf_log[qj] # Optimization: precache the logarithm of the current coefficient of q
            for i in xrange(len(p)):
                if p[i] != 0: # log(0) is undefined, need to check that...
                    r[i + j] ^= gf_exp[lp[i] + lq] # equivalent to: r[i + j] = gf_add(r[i+j], gf_mul(p[i], q[j]))
    return r
653929
def discretize(value, factor=100):
    """"""
    if not isinstance(value, Iterable):
        return int(value * factor)
    int_value = list(deepcopy(value))
    for i in range(len(int_value)):
        int_value[i] = int(int_value[i] * factor)
    return int_value
929925
def from_size(value):
    '''
    
    '''
    match_size = re_zfs_size.match(str(value))
    if match_size:
        v_unit = match_size.group(2).upper()[0]
        v_size = float(match_size.group(1))
        v_multiplier = math.pow(1024, zfs_size.index(v_unit) + 1)
        value = v_size * v_multiplier
        if int(value) == value:
            value = int(value)
    elif value is not None:
        value = str(value)

    return from_numeric(value)