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

print('sum = ')
sum(common_field_elems)

elems =
[([(, ), (, ), (, )], ), ([(, )], ), ([(, ), (, )], )]
field_elems =
[, , ]
m =
L =
common_field_elems =
[, , ]
sum =