
// Make newform 21.14.g.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_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_21_14_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_21_14_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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_21_g();"
function MakeCharacter_21_g()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 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_21_g_Hecke();"
function MakeCharacter_21_g_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [0, 1]];
    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 := 14;
    raw_aps := [[0, 0], [-729, -729], [0, 0], [52418, 281733], [0, 0], [235871, -471742], [0, 0], [-426993846, 213496923], [0, 0], [0, 0], [-4748529235, -4748529235], [0, -27377427169], [0, 0], [77218825315, 0], [0, 0], [0, 0], [0, 0], [-282783926872, 141391963436], [226871917261, -226871917261], [0, 0], [-1044098651241, -1044098651241], [0, -4084540747547], [0, 0], [0, 0], [6234825725848, -12469651451696], [0, 0], [-3770838895802, 1885419447901], [0, 0], [-6953348918863, 6953348918863], [0, 0], [-42848623824571, 0], [0, 0], [0, 0], [-70030018782347, 140060037564694], [0, 0], [-247674063414964, 247674063414964], [31944725060988, 31944725060988], [0, 403274921658328], [0, 0], [0, 0], [0, 0], [524999755818705, -1049999511637410], [0, 0], [-1315272831818425, 1315272831818425], [0, 0], [-1011175588463042, -1011175588463042], [2469658503574096, 0], [-2111542938060186, 4223085876120372], [0, 0], [5010956985303326, -2505478492651663], [0, 0], [0, 0], [-3440801151103696, -3440801151103696], [0, 0], [0, 0], [0, 0], [0, 0], [-11967424951285460, 5983712475642730], [2012457745273279, -2012457745273279], [0, 0], [-9123776253963989, -9123776253963989], [0, 0], [-10153572110457049, 20307144220914098], [0, 0], [-36151764772844454, 18075882386422227], [0, 0], [0, -16737279761802601], [-52653126739384085, 0], [0, 0], [10793969843988260, -21587939687976520], [0, 0], [0, 0], [40106072691390879, 40106072691390879], [0, -100790292758356643], [-64758455492388803, 0], [0, 0], [0, 0], [35320584688605238, -17660292344302619], [0, 0], [69616940844054217, 69616940844054217], [0, 0], [5771387292849341, 0], [0, 0], [144451674521756797, -288903349043513594], [-345007683218015964, 172503841609007982], [0, 0], [0, 0], [0, -322286938449813751], [0, 0], [367093983760207727, 0], [0, 0], [0, 0], [-588604345085975425, 588604345085975425], [0, 0], [0, 626614177500019757], [0, 0], [0, 0], [0, 0], [-352728056702195026, 176364028351097513], [0, -728484207979142189], [-871581422412923080, 0], [0, 0], [0, 0], [0, 0], [408474745301127991, -408474745301127991], [-384626444508070109, -384626444508070109], [0, 0], [0, 0], [0, 0], [-591425424691920001, 1182850849383840002], [-2790695378225100534, 1395347689112550267], [1255220249724146170, -1255220249724146170], [0, 0], [1232552842864466765, 1232552842864466765], [3060630403667076284, 0], [0, 0], [470352722527717819, -940705445055435638], [0, 0], [0, 0], [0, 0], [1880577599481060471, 1880577599481060471], [-3765747204311090507, 0], [0, 0], [0, 0], [-6569691703867852538, 3284845851933926269], [0, 0], [0, 4595371106599793498], [0, 0], [-2902820330977342763, 5805640661954685526], [1107989003737272322, -553994501868636161], [8646021860053130227, -8646021860053130227], [0, 0], [0, -9832689217929516079], [-1181423105669137994, 0], [0, 0], [-362077841817881295, 724155683635762590], [0, 0], [-5538108819046383218, -5538108819046383218], [0, 0], [0, 0], [6420149266351307766, -12840298532702615532], [0, 0], [14606224303640192548, -14606224303640192548], [0, 0], [-144620977055274687, -144620977055274687], [0, 0], [-6209359779122935317, 12418719558245870634], [0, 0], [18692617335787755340, -9346308667893877670], [0, 0], [0, 26827123639825755826], [0, 0], [18320378176566431737, 0], [0, 0], [-29318242254742867549, 29318242254742867549], [0, 0], [0, -7771824681877718281], [0, 0], [1374986998682610739, -2749973997365221478], [0, 0], [0, 0], [0, 0], [42227378839820282411, 0], [0, 0], [0, 0], [0, 0], [-23917461546394536103, 23917461546394536103], [-18738742285549488407, -18738742285549488407]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_14_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_21_14_g_a(:prec:=2)
    chi := MakeCharacter_21_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 14));
    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_21_14_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_21_14_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,14,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;