
// Make newform 29.3.c.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_29_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_29_c_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_29_3_c_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_29_3_c_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 := [25, 0, 126, 0, 91, 0, 18, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [5, 6, 9, 0, 1, 0, 0, 0], [-5, 6, -9, 0, -1, 0, 0, 0], [0, -47, 0, -15, 0, -1, 0, 0], [0, -81, 0, -86, 0, -18, 0, -1], [35, 82, 15, 77, 1, 17, 0, 1], [-35, 82, -15, 77, -1, 17, 0, 1], [35, 0, 62, 0, 16, 0, 1, 0]];
    Rf_basisdens := [1, 6, 6, 6, 30, 12, 12, 6];
    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_29_c();"
function MakeCharacter_29_c()
    N := 29;
    order := 4;
    char_gens := [2];
    v := [1];
    // 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_29_c_Hecke();"
function MakeCharacter_29_c_Hecke(Kf)
    N := 29;
    order := 4;
    char_gens := [2];
    char_values := [[0, 0, 0, 0, 1, 0, 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, 0, 0, -1, 0], [0, 0, 1, 0, 0, 0, 1, 0], [0, 0, 0, -1, -1, 0, 0, 0], [0, 1, -1, 0, 0, -1, 1, 1], [-1, 0, -4, -1, -1, 0, -1, -1], [0, 2, 2, 0, 5, -1, -1, 0], [0, 0, 1, 0, 0, 0, -6, 0], [-1, 0, -1, -4, -1, 0, 4, -4], [0, -2, 2, 0, 0, 0, 0, 3], [15, -5, 2, 0, 6, 7, 3, 0], [1, 0, -7, 4, 1, 0, 9, 4], [-10, -6, 0, -8, 10, -2, 0, 8], [1, 7, 0, 5, -1, 6, 0, -5], [-25, 0, 6, 1, -25, 0, -5, 1], [10, 6, 0, 2, -10, -11, 0, -2], [35, -6, 6, 0, 0, -7, 7, -10], [-10, 4, -4, 0, 0, 10, -10, 17], [-24, 0, 9, 2, -24, 0, 8, 2], [0, -15, -15, 6, 40, 0, 0, 0], [0, 3, 3, -4, 62, -5, -5, 0], [-20, -6, 0, 4, 20, -14, 0, -4], [-46, 0, -14, -6, -46, 0, 7, -6], [30, 7, -7, 0, 0, 10, -10, -21], [-11, 0, 31, -8, -11, 0, -12, -8], [-5, -7, 0, -16, 5, -2, 0, 16], [-26, 0, -2, 7, -26, 0, -14, 7], [60, 5, -5, 0, 0, 17, -17, -12], [30, -10, 10, 0, 0, 3, -3, 15], [0, -6, -6, 9, 35, 6, 6, 0], [-15, 18, 0, -3, 15, 38, 0, 3], [-5, 0, 1, -8, -5, 0, -16, -8], [-1, -13, 0, 1, 1, 4, 0, -1], [-20, 0, 2, 13, -20, 0, 36, 13], [-48, -16, 16, 0, 0, 34, -34, -1], [0, -23, -23, 12, -15, 1, 1, 0], [0, 1, 1, -37, 8, -37, -37, 0], [-45, 69, 0, 15, 45, -24, 0, -15], [-60, -9, 0, 2, 60, 59, 0, -2], [0, 31, 31, 18, 120, -13, -13, 0], [0, -20, -20, -28, -40, -24, -24, 0], [0, 23, 23, -10, -100, -23, -23, 0], [15, 10, -10, 0, 0, -46, 46, 7], [-19, 0, -5, -34, -19, 0, 30, -34], [45, 0, -1, 39, 45, 0, 20, 39], [-20, -18, 18, 0, 0, -32, 32, -14], [-100, 29, -29, 0, 0, 45, -45, 1], [86, -27, 0, 4, -86, -65, 0, -4], [-10, -34, 34, 0, 0, -43, 43, 60], [-60, -14, 14, 0, 0, -11, 11, -12], [69, -39, 0, 15, -69, -54, 0, -15], [55, -41, 41, 0, 0, 10, -10, 10], [10, 45, -45, 0, 0, -26, 26, -65], [0, 52, 52, -12, 53, 52, 52, 0], [-66, 0, 3, -27, -66, 0, -65, -27], [-35, -12, 12, 0, 0, -11, 11, 49], [65, 0, -63, 38, 65, 0, 29, 38], [136, 69, 0, 14, -136, 58, 0, -14], [-51, -67, 0, 50, 51, -17, 0, -50], [100, 21, -21, 0, 0, -24, 24, -6], [-147, -15, 15, 0, 0, 108, -108, 18], [0, -36, -36, -51, -150, -6, -6, 0], [-65, 0, -16, 25, -65, 0, 44, 25], [-15, 0, 32, 18, -15, 0, -61, 18], [121, 0, 33, -2, 121, 0, 90, -2], [45, 29, -29, 0, 0, 55, -55, -13], [270, 1, 0, 24, -270, 14, 0, -24], [124, -43, 0, -7, -124, 43, 0, 7], [-180, 33, 0, 31, 180, -8, 0, -31], [0, 37, 37, -26, 40, 51, 51, 0], [215, -15, 15, 0, 0, -73, 73, -16], [0, -3, -3, -38, -110, -40, -40, 0], [-4, 0, 88, 92, -4, 0, 7, 92], [165, 0, -9, 35, 165, 0, 62, 35], [-185, -69, 69, 0, 0, -50, 50, -35], [-29, 0, -9, -28, -29, 0, -82, -28], [0, -32, -32, 11, 320, 19, 19, 0], [-6, -2, 0, -75, 6, 56, 0, 75], [65, 51, -51, 0, 0, -154, 154, 59], [5, 69, -69, 0, 0, 22, -22, -94], [19, 0, 126, 4, 19, 0, -56, 4], [0, 1, 1, -36, 438, 63, 63, 0], [-106, 41, 0, -57, 106, 32, 0, 57], [272, -19, 19, 0, 0, -40, 40, -67], [-20, 88, 0, 10, 20, 50, 0, -10], [0, 64, 64, 31, -172, 102, 102, 0], [35, -75, 0, -53, -35, 14, 0, 53], [-211, 0, -148, 47, -211, 0, -100, 47], [0, 91, 91, 16, 170, -24, -24, 0], [-349, -42, 0, 46, 349, 16, 0, -46], [0, -19, -19, 5, -170, 90, 90, 0], [-105, 0, -48, 63, -105, 0, -75, 63], [199, -43, 0, -19, -199, 71, 0, 19], [-300, 2, -2, 0, 0, 71, -71, -111], [-1, -8, 0, -47, 1, -229, 0, 47], [0, 21, 21, 37, -40, -148, -148, 0], [195, -33, 0, -33, -195, -115, 0, 33], [-273, -83, 83, 0, 0, 9, -9, -48], [0, -103, -103, 53, 275, 1, 1, 0], [-10, -19, 19, 0, 0, 56, -56, 54], [116, 0, -18, -23, 116, 0, -188, -23], [240, -16, 16, 0, 0, 1, -1, 35], [0, 81, 81, -18, -270, 96, 96, 0], [40, 79, 0, 83, -40, -119, 0, -83], [371, 10, 0, -23, -371, -24, 0, 23], [8, -20, 20, 0, 0, -48, 48, -82], [-240, 61, 0, -49, 240, 78, 0, 49], [340, 64, -64, 0, 0, -62, 62, -11], [0, -56, -56, -169, 455, 103, 103, 0], [466, 0, 62, 58, 466, 0, -13, 58], [-271, 0, 13, 26, -271, 0, 120, 26], [-185, -53, 0, -109, 185, 231, 0, 109], [0, -79, -79, 99, 5, -76, -76, 0], [-120, -92, 0, -99, 120, -44, 0, 99], [-204, 105, 0, -38, 204, -119, 0, 38], [0, -70, -70, 146, -170, -51, -51, 0], [-294, 0, 35, -60, -294, 0, -2, -60], [0, 42, 42, -99, 200, 129, 129, 0], [0, -116, -116, -53, -20, -86, -86, 0], [95, 121, 0, 28, -95, -288, 0, -28], [376, 0, -125, -77, 376, 0, -227, -77], [30, 94, -94, 0, 0, 20, -20, -122], [0, -19, -19, 42, -475, -10, -10, 0], [25, -120, 0, 113, -25, -132, 0, -113], [-390, 133, -133, 0, 0, -74, 74, -18], [-60, -35, 35, 0, 0, 92, -92, -86], [0, 30, 30, -134, -113, -43, -43, 0], [0, -115, -115, -72, -165, 59, 59, 0], [-490, 92, -92, 0, 0, 131, -131, -4], [-55, 0, 103, 174, -55, 0, 142, 174], [-50, 186, 0, 121, 50, -74, 0, -121], [-194, 0, 11, -56, -194, 0, 111, -56], [95, -57, 0, -83, -95, -96, 0, 83], [69, 159, 0, 81, -69, 234, 0, -81], [240, 0, -9, 55, 240, 0, 112, 55], [970, -31, 31, 0, 0, -64, 64, -92], [-336, -93, 0, -24, 336, 216, 0, 24], [-175, 0, -157, 50, -175, 0, -326, 50], [0, 92, 92, -2, 700, 43, 43, 0], [150, 0, 147, -30, 150, 0, -144, -30], [-134, 236, 0, -34, 134, -154, 0, 34], [0, -23, -23, -33, -80, -323, -323, 0], [0, -60, -60, 38, -715, -41, -41, 0], [-115, 0, -285, -108, -115, 0, 192, -108], [360, -9, 0, 129, -360, 45, 0, -129], [4, 0, 299, -45, 4, 0, -34, -45], [184, 74, 0, -43, -184, 41, 0, 43], [-395, 256, 0, -39, 395, 22, 0, 39], [-145, -106, 106, 0, 0, 133, -133, 197], [-11, 12, 0, -110, 11, -263, 0, 110], [0, 12, 12, 168, 450, 231, 231, 0], [435, 150, -150, 0, 0, 219, -219, 0], [211, 0, 16, 202, 211, 0, 184, 202], [0, -16, -16, 117, -330, 131, 131, 0], [155, 0, -17, -172, 155, 0, -221, -172], [-145, -31, 0, -3, 145, -130, 0, 3], [181, 219, 0, -130, -181, -23, 0, 130], [-170, 205, -205, 0, 0, 3, -3, 14], [-128, 35, -35, 0, 0, 136, -136, -284], [0, 68, 68, -256, -650, -30, -30, 0], [0, 110, 110, -105, 15, -246, -246, 0], [-275, 0, -50, -2, -275, 0, 135, -2], [-185, 17, -17, 0, 0, 218, -218, -26], [80, 182, 0, 112, -80, -303, 0, -112], [-189, 0, 89, -27, -189, 0, -170, -27], [285, 186, -186, 0, 0, 75, -75, -39], [-170, -144, 0, 113, 170, 131, 0, -113], [0, 194, 194, -48, -392, 167, 167, 0], [380, 0, 136, 152, 380, 0, 144, 152]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_29_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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