Exercise 3 and 4:

(Farid).
def return_powers_gen(grouplet ,n):
    G=DihedralGroup(n)
    for i in range(n):
        for j in range(2):
            if grouplet==(G.gen(0)^i)*(G.gen(1)^j):
                return (i,j)

R=QQ['a1,a2']
a=R.gens()

def multi(grouplet ,monomial ,n):
    k=monomial[1].degree(a[0])
    l=monomial[1].degree(a[1])
    if (grouplet[1]/2)==1 or grouplet[1]==0:
        return monomial[0]*((CyclotomicField(n).zeta()^((n-1)*grouplet[0])*a[1])^l)*((CyclotomicField(n).zeta()^(grouplet[0])*a[0])^k)
    else:
        return monomial[0]*((CyclotomicField(n).zeta()^((n-1)*grouplet[0])*a[1])^k)*((CyclotomicField(n).zeta()^(grouplet[0]*l)*a[0])^l)

multi((1,0),(2,a[0]^2*a[1]^3),5)
(-2*zeta5^3 - 2*zeta5^2 - 2*zeta5 - 2)*a1^2*a2^3

multi((1,1),(2,a[0]^2),6)
(-2*zeta6)*a2^2

def dihed_action(grouplet,poly,n): S=Set(list(poly))
    a=[]
    b=0
    while len(S)>0:
        g=S[0]
        c={(g[0],g[1])}
        a=(multi(grouplet ,(g[0],g[1]),n))
        S=S.difference(c)
        b=b+a
    return b

dihed_action((1,1),a[0]*a[1]+2*a[0]^2,6)
a1*a2 + (-2*zeta6)*a2^2

def dihed_char(deg,grouplet,n):
    coef =[]
    actout =[]
    char =0
    for i in range(deg+1):
        actout.append(dihed_action(grouplet ,(a[0]^i)*(a[1]^(deg-i)),n))
        if actout[i].degree(a[0])==i and actout[i].degree(a[1])==deg-i:
            coef.append(actout[i].coefficients())
    for j in range(len(coef)):
        SS=Set(coef[j])
        char=char+SS[0]
    return char

dihed_char(4,(0,1),6)
1

def inn_product_char(deg,n,t):
    G=DihedralGroup(n);
    CT=G.character_table()
    GC=G.conjugacy_classes_representatives()
    S=[]; D=[]; E=[]; b=0; c=0
    for h in GC:
        for g in G:
            S.append(g*h*g^(-1))
        SS=Set(S)
        for i in range(len(SS)):
            D.append(dihed_char(deg,return_powers_gen(h,n),n))
            E.append(CT[t][b])
        S=[]
        b=b+1
    for j in range(len(D)):
        c=c+(D[j]*(E[j].conjugate()))
    return c/(2*n)

def dihed_char_decom(deg,n):
    deg_of_char =[]
    G=DihedralGroup(n)
    CT=G.character_table()
    b=0
    vx=[var("X"+str(i)) for i in range(1,len(CT[0])+1)]
    for t in range(len(CT[0])):
        b=b+inn_product_char(deg,n,t)*vx[t]
    return b

dihed_char_decom(8,6)
2*X1 + X2 + 3*X6

dihed_char_decom(3,5)
X3 + X4

dihed_char_decom(5,6)
X3 + X4 + 2*X5

dihed_char_decom(5,4)
3*X5

dihed_char_decom(9,7)
X1 + X2 + X3 + 2*X4 + X5

dihed_char_decom(7,9)
X3 + X4 + X5 + X6

dihed_char_decom(15,7)
X1 + X2 + 3*X3 + 2*X4 + 2*X5