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 =
[([( \displaystyle 1 , \displaystyle 3 ), ( \displaystyle 1 , \displaystyle 2 ), ( \displaystyle -1 , \displaystyle 1 )], \displaystyle 7 ), ([( \displaystyle 2 , \displaystyle 3 )], \displaystyle 5 ), ([( \displaystyle 1 , \displaystyle 9 ), ( \displaystyle 1 , \displaystyle 0 )], \displaystyle 10 )]
field_elems =
[ \displaystyle \zeta_{7}^{3} + \zeta_{7}^{2} - \zeta_{7} , \displaystyle 2 \zeta_{5}^{3} , \displaystyle -\zeta_{10}^{3} + \zeta_{10}^{2} - \zeta_{10} + 2 ]
m =
\displaystyle 70
L =
\displaystyle \Bold{Q}(\zeta_{70})
common_field_elems =
[ \displaystyle \zeta_{70}^{23} + \zeta_{70}^{20} - \zeta_{70}^{16} - \zeta_{70}^{10} + \zeta_{70}^{9} - \zeta_{70}^{2} , \displaystyle -2 \zeta_{70}^{7} , \displaystyle -\zeta_{70}^{21} + \zeta_{70}^{14} - \zeta_{70}^{7} + 2 ]
sum =
\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