
// Make newform 108.3.g.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_108_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_108_g_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_108_3_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_108_3_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [9, -3, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-9, 16, 2, 1], [-9, -2, 2, 1], [12, 8, 1, -4]];
    Rf_basisdens := [1, 6, 6, 3];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_108_g();"
function MakeCharacter_108_g()
    N := 108;
    order := 6;
    char_gens := [55, 29];
    v := [6, 5];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_108_g_Hecke();"
function MakeCharacter_108_g_Hecke(Kf)
    N := 108;
    order := 6;
    char_gens := [55, 29];
    char_values := [[1, 0, 0, 0], [1, 0, 1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 3;
    raw_aps := [[0, 0, 0, 0], [0, 0, 0, 0], [-2, 0, 1, -1], [-1, 1, 0, -2], [12, -2, 5, 0], [2, 2, 3, -1], [-8, 1, -15, -1], [0, -1, -1, -1], [-16, 0, 17, 1], [21, 7, 14, 0], [-2, -6, -5, 3], [-14, 4, 4, 4], [7, 0, 1, 8], [0, 0, 23, 0], [27, -3, 12, 0], [8, 8, 24, -8], [-25, 0, 17, -8], [-3, 3, -22, -6], [55, 12, 61, -6], [16, -2, 30, 2], [20, -3, -3, -3], [-5, 5, -44, -10], [27, -3, 12, 0], [64, -8, 120, 8], [-2, 2, 97, -4], [-147, -1, -74, 0], [-98, -6, -101, 3], [-16, 11, -21, -11], [86, 6, 6, 6], [60, 0, -39, 21], [38, 18, 18, 18], [4, 0, 25, 29], [-132, -2, -67, 0], [-45, 4, -43, -2], [8, 0, -31, -23], [9, -9, 182, 18], [-122, 42, -101, -21], [130, 4, 4, 4], [-6, 0, -15, -21], [153, -21, 66, 0], [-88, 20, -156, -20], [94, -14, -14, -14], [-39, -37, -38, 0], [49, 0, 49, 0], [-8, 64, 48, -64], [-126, 8, 8, 8], [-232, -34, -249, 17], [-15, 15, -34, -30], [288, 18, 153, 0], [160, 30, 175, -15], [-136, -19, -291, 19], [72, 0, -63, 9], [32, -32, -33, 64], [-16, -61, -93, 61], [-21, 0, 33, 12], [-75, 23, -26, 0], [-136, 44, -228, -44], [14, -42, -42, -42], [21, -21, -286, 42], [-213, 61, -76, 0], [364, 6, 367, -3], [-58, 0, 65, 7], [-148, -3, -3, -3], [238, 0, -191, 47], [-4, 4, 291, -8], [51, 5, 28, 0], [-41, 41, 136, -82], [-9, 76, 29, -38], [75, 0, -159, -84], [-43, 43, -30, -86], [480, 34, 257, 0], [288, -18, 558, 18], [-33, 33, 80, -66], [-142, 2, -141, -1], [256, 31, 31, 31], [10, 0, -95, -85], [-183, -37, -110, 0], [170, -24, -24, -24], [-11, 0, 1, -10], [135, 76, 173, -38], [156, 0, -87, 69], [59, -59, 156, 118], [400, 22, 822, -22], [20, -27, -27, -27], [57, -57, 38, 114], [486, 24, 255, 0], [-120, 87, -153, -87], [-6, 6, 101, -12], [-285, -11, -148, 0], [-412, 38, -393, -19], [-160, -25, -345, 25], [-63, 99, 18, 0], [-150, 68, 68, 68], [137, 0, -127, 10], [-53, -108, -107, 54], [-96, -24, -216, 24], [-332, 0, 337, 5], [-56, 43, -69, -43], [578, -12, -12, -12], [154, -56, -56, -56], [36, -36, 227, 72], [40, -86, -6, 86], [-573, 0, 561, -12], [438, -136, 151, 0], [-105, 172, -19, -86], [-124, -3, -3, -3], [276, -154, 61, 0], [-368, 28, -708, -28], [242, 0, -175, 67], [-66, 66, -415, -132], [558, 58, 587, -29], [-190, -36, -36, -36], [421, 0, -431, -10], [30, -30, -487, 60], [586, 52, 52, 52], [-96, 130, 17, 0], [577, 0, 577, 0], [-288, -90, -666, 90], [-418, 0, 497, 79], [-201, 185, -8, 0], [-1088, 30, -1073, -15], [79, -79, 376, 158], [-1029, -19, -524, 0], [-96, 57, -135, -57], [27, -27, -652, 54], [152, 80, 384, -80], [25, -25, -2, 50], [144, -198, 90, 198], [45, -45, 626, 90], [328, -210, 223, 105], [572, -3, -3, -3], [102, 0, -15, 87], [132, 70, 167, -35], [-742, 48, 48, 48], [684, 0, -711, -27], [-748, 86, -705, -43], [-312, 156, -468, -156], [-524, -138, -593, 69], [330, 0, -255, 75], [568, -17, 1119, 17], [-304, -81, -81, -81], [-687, -61, -374, 0], [-280, 14, -273, -7], [376, -92, 660, 92], [426, -40, -40, -40], [-477, 69, -204, 0], [-3, 3, -1174, -6], [-549, 45, -252, 0], [525, -8, 521, 4], [-176, -50, -402, 50], [-762, 10, -757, -5], [464, 68, 996, -68], [-456, 59, 59, 59], [-374, 0, 385, 11], [-68, 68, -197, -136], [-81, -15, -48, 0], [-1210, 42, 42, 42], [459, -99, 180, 0], [-614, -116, -116, -116], [-112, 0, 65, -47], [396, 126, 261, 0], [72, -243, -99, 243], [352, -258, 223, 129], [-216, 324, -108, -324], [-151, 0, 305, 154], [1221, -25, 598, 0], [398, 150, 150, 150], [-15, 15, 902, -30]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_108_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_108_3_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_108_3_g_a(:prec:=4)
    chi := MakeCharacter_108_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_108_3_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_108_3_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_108_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;