Sharedcyclo_fields.sagewsOpen in CoCalc
def make_cyclo_field_elem(coeffs, n):
    """
    Takes a list of (coefficient, power) pairs and a positive integer n and returns an element of the nth cyclotomic field

    Input:
     - coeffs: a list of pairs [(a_1, e_1), ... , (a_k, d_k)] of integers
     - n: an integer specifying the root of unity

    Output: A field element a_1 * z^e_1 + ... a_k * z^d_k where z is the primitive nth root of unity

    Example:
    sage: coeffs = [(5, 4), (1, 2), (3, 0)]
    sage: n = 7
    sage: make_cyclo_field_elem(coeffs, n)
    5*zeta_7^4 + zeta_7^2 + 3
    """
    k = CyclotomicField(n)
    zeta = k.gen()
    result = k(0)
    for coeff, exponent in coeffs:
        result += k(coeff * zeta^exponent)

    return result
# Compute (zeta_7^3 + zeta_7^2 - zeta_7) + (2 * zeta_5^3) + (zeta_11^9 + 1)

typeset_mode(True) #don't type this if using from the command line

# elems is a representation of the elements as lists of (coefficient, power) pairs
# could get this from stdin or something or read from file.
print('elems =')
elems = [
    ([(1, 3), (1, 2), (-1, 1)], 7),
    ([(2, 3)], 5),
    ([(1, 9), (1, 0)], 10)
]; elems


#convert list of pairs into field elements
print("field_elems =")
field_elems = [make_cyclo_field_elem(coeffs, n) for coeffs, n in elems]; field_elems

#make a common field
print("m =")
m = lcm(elem[1] for elem in elems); m
print("L =")
L = CyclotomicField(m); L
print("common_field_elems =")
common_field_elems = [L(x) for x in field_elems]; common_field_elems

# add them all together
print('sum = ')
sum(common_field_elems)
elems =
[([(1\displaystyle 1, 3\displaystyle 3), (1\displaystyle 1, 2\displaystyle 2), (1\displaystyle -1, 1\displaystyle 1)], 7\displaystyle 7), ([(2\displaystyle 2, 3\displaystyle 3)], 5\displaystyle 5), ([(1\displaystyle 1, 9\displaystyle 9), (1\displaystyle 1, 0\displaystyle 0)], 10\displaystyle 10)]
field_elems =
[ζ73+ζ72ζ7\displaystyle \zeta_{7}^{3} + \zeta_{7}^{2} - \zeta_{7}, 2ζ53\displaystyle 2 \zeta_{5}^{3}, ζ103+ζ102ζ10+2\displaystyle -\zeta_{10}^{3} + \zeta_{10}^{2} - \zeta_{10} + 2]
m =
70\displaystyle 70
L =
Q(ζ70)\displaystyle \Bold{Q}(\zeta_{70})
common_field_elems =
[ζ7023+ζ7020ζ7016ζ7010+ζ709ζ702\displaystyle \zeta_{70}^{23} + \zeta_{70}^{20} - \zeta_{70}^{16} - \zeta_{70}^{10} + \zeta_{70}^{9} - \zeta_{70}^{2}, 2ζ707\displaystyle -2 \zeta_{70}^{7}, ζ7021+ζ7014ζ707+2\displaystyle -\zeta_{70}^{21} + \zeta_{70}^{14} - \zeta_{70}^{7} + 2]
sum =
ζ7023ζ7021+ζ7020ζ7016+ζ7014ζ7010+ζ7093ζ707ζ702+2\displaystyle \zeta_{70}^{23} - \zeta_{70}^{21} + \zeta_{70}^{20} - \zeta_{70}^{16} + \zeta_{70}^{14} - \zeta_{70}^{10} + \zeta_{70}^{9} - 3 \zeta_{70}^{7} - \zeta_{70}^{2} + 2