
// Make newform 36.3.g.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_36_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_36_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_36_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_36_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], [-3, -2, 2, 1], [0, 5, 1, -1], [-9, 2, 1, 2]];
    Rf_basisdens := [1, 6, 3, 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_36_g();"
function MakeCharacter_36_g()
    N := 36;
    order := 6;
    char_gens := [19, 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_36_g_Hecke();"
function MakeCharacter_36_g_Hecke(Kf)
    N := 36;
    order := 6;
    char_gens := [19, 29];
    char_values := [[1, 0, 0, 0], [0, 1, 0, 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], [1, -1, 0, 1], [2, -2, -2, 1], [1, 2, 3, -3], [-7, -5, -2, -2], [0, 4, 0, -3], [-8, 14, -1, 2], [2, 0, 3, 0], [34, -16, 2, -1], [-7, -14, 7, 7], [0, -8, 0, 9], [-22, 0, -12, 0], [2, 7, 16, -8], [-23, 23, 0, 0], [-15, -12, -3, -3], [8, -32, -8, 16], [34, -25, -16, 8], [25, -16, 9, -9], [0, 67, 0, -18], [16, -28, 2, -4], [26, 0, 9, 0], [49, -34, 15, -15], [-15, -12, -3, -3], [64, -112, 8, -16], [-95, 101, 6, -6], [73, 74, -1, -1], [0, -104, 0, 9], [-16, 10, -11, 22], [74, 0, -18, 0], [-78, 60, 42, -21], [2, 0, -54, 0], [50, 4, 58, -29], [65, 67, -2, -2], [0, -41, 0, -6], [-62, 8, -46, 23], [-191, 164, -27, 27], [0, -80, 0, -63], [122, 0, -12, 0], [-30, -6, -42, 21], [-87, -66, -21, -21], [-88, 136, -20, 40], [122, 0, 42, 0], [1, 38, -37, -37], [0, 49, 0, 0], [-8, -112, -64, 128], [-142, 0, -24, 0], [0, -266, 0, 51], [49, -4, 45, -45], [-135, -153, 18, 18], [0, 190, 0, -45], [-136, 310, 19, -38], [-126, 72, 18, -9], [1, -97, -96, 96], [-16, 154, 61, -122], [66, -21, 24, -12], [49, 26, 23, 23], [-136, 184, -44, 88], [98, 0, 126, 0], [265, -328, -63, 63], [137, 76, 61, 61], [0, 370, 0, -9], [130, -58, 14, -7], [-142, 0, 9, 0], [-382, 238, 94, -47], [-287, 299, 12, -12], [-23, -28, 5, 5], [-95, 218, 123, -123], [0, 67, 0, -114], [-318, 75, -168, 84], [73, 56, 129, -129], [-223, -257, 34, 34], [288, -540, 18, -36], [-47, 146, 99, -99], [0, -140, 0, -3], [194, 0, -93, 0], [-190, 10, -170, 85], [73, 110, -37, -37], [218, 0, 72, 0], [2, -11, -20, 10], [0, 211, 0, -114], [-174, 156, 138, -69], [-215, 38, -177, 177], [400, -844, -22, 44], [74, 0, 81, 0], [-95, -76, -171, 171], [-231, -255, 24, 24], [-120, 66, -87, 174], [-95, 113, 18, -18], [137, 148, -11, -11], [0, -374, 0, -57], [-160, 370, 25, -50], [81, -18, 99, 99], [-286, 0, -204, 0], [-254, 137, 20, -10], [0, -161, 0, 162], [-96, 240, 24, -48], [674, -332, 10, -5], [-56, 26, -43, 86], [602, 0, 36, 0], [266, 0, 168, 0], [-263, 155, -108, 108], [40, 92, 86, -172], [1122, -573, -24, 12], [-287, -151, -136, -136], [0, 67, 0, -258], [-118, 0, 9, 0], [-215, -61, -154, -154], [-368, 680, -28, 56], [-350, 242, 134, -67], [481, -283, 198, -198], [0, 616, 0, -87], [-118, 0, 108, 0], [-862, 421, -20, 10], [457, -547, -90, 90], [482, 0, -156, 0], [113, -17, 130, 130], [0, 577, 0, 0], [-288, 756, 90, -180], [994, -418, 158, -79], [193, 8, 185, 185], [0, -1058, 0, -45], [-455, 218, -237, 237], [505, 524, -19, -19], [-96, 78, -57, 114], [625, -706, -81, 81], [152, -464, -80, 160], [-23, -52, -75, 75], [144, 108, 198, -396], [-671, 536, -135, 135], [0, 118, 0, 315], [578, 0, 9, 0], [-30, 102, 174, -87], [0, 202, 0, -105], [-838, 0, -144, 0], [-1422, 684, -54, 27], [0, -662, 0, -129], [-312, 312, -156, 312], [0, -662, 0, 207], [-510, 330, 150, -75], [568, -1102, 17, -34], [-142, 0, 243, 0], [313, 374, -61, -61], [0, -266, 0, -21], [376, -568, 92, -184], [506, 0, 120, 0], [273, 204, 69, 69], [1177, -1168, 9, -9], [297, 252, 45, 45], [0, 517, 0, 12], [-176, 452, 50, -100], [0, -752, 0, -15], [464, -1064, -68, 136], [-574, 0, -177, 0], [770, -374, 22, -11], [265, -61, 204, -204], [33, 48, -15, -15], [-1294, 0, -126, 0], [-279, -180, -99, -99], [-382, 0, 348, 0], [130, -112, -94, 47], [-135, -261, 126, 126], [72, 342, 243, -486], [0, 94, 0, 387], [-216, -216, -324, 648], [610, -151, 308, -154], [-623, -598, -25, -25], [98, 0, -450, 0], [-887, 932, 45, -45]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_36_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_36_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_36_3_g_a(:prec:=4)
    chi := MakeCharacter_36_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_36_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_36_3_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_36_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;