
// Make newform 21.28.c.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_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_21_28_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_21_28_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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-81, 162]];
    Rf_basisdens := [1, 1];
    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_21_c();"
function MakeCharacter_21_c()
    N := 21;
    order := 2;
    char_gens := [8, 10];
    v := [1, 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_21_c_Hecke();"
function MakeCharacter_21_c_Hecke(Kf)
    N := 21;
    order := 2;
    char_gens := [8, 10];
    char_values := [[-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 := 28;
    raw_aps := [[0, 0], [0, -19683], [0, 0], [233960191910, 746717689], [0, 0], [0, 4092891642436], [0, 0], [0, 2611259989628390], [0, 0], [0, 0], [0, 1704867049817312390], [-2356945203434359618190, 0], [0, 0], [-20439179162843765163320, 0], [0, 0], [0, 0], [0, 0], [0, -2852263265146360660740], [-7760223933324143843803120, 0], [0, 0], [0, -79325285992955677516056], [-12830481703087364691778156, 0], [0, 0], [0, 0], [0, 7544048879335117493735512], [0, 0], [0, 21175443929123129596457166], [0, 0], [3354161374879752380854791526, 0], [0, 0], [46938669937832428819652665820, 0], [0, 0], [0, 0], [0, -340601037019249615071980270], [0, 0], [498429282705362265975624595852, 0], [0, -6014630461071126747856551028], [-1071126783808149339710204878600, 0], [0, 0], [0, 0], [0, 0], [0, -29195400016296877353015643420], [0, 0], [13331470386476091350827148660350, 0], [0, 0], [0, -27921791789089212561326441870], [1903638847016969053670559398128, 0], [0, 122081380022369248102062899194], [0, 0], [0, 1027278380866911319351737777940], [0, 0], [0, 0], [0, -2043289303766610680095838438480], [0, 0], [0, 0], [0, 0], [0, 0], [0, -6806208784336159565448277715370], [1507692015169037504897919180071330, 0], [0, 0], [0, 14530752729893637151470585490526], [0, 0], [0, 52260336015132383440688219166678], [0, 0], [0, 51870840089405335810556172691664], [0, 0], [-14078608790408985392879934109606112, 0], [-13804499095482030084503705322836030, 0], [0, 0], [0, -177701466147260866772351614487660], [0, 0], [0, 0], [0, -568439421784587004514199387396918], [16317856895797499014175044618589150, 0], [-83084356022024958864761464278652856, 0], [0, 0], [0, 0], [0, 1346580041773023533550420268820788], [0, 0], [0, 41387363658979049746283009413480], [0, 0], [223691148386630519444510296838939858, 0], [0, 0], [0, -1502975238489066807798757004985976], [0, -4133419762902584135017267086108510], [0, 0], [0, 0], [-147169997220067975023515923842620390, 0], [0, 0], [-899450432373828049507662308262683620, 0], [0, 0], [0, 0], [2001178090687665034125038573259523900, 0], [0, 0], [2232715187353490311771065114732399136, 0], [0, 0], [0, 0], [0, 0], [0, -2465859603401722732711566947883294], [9900038965274962705327417783785667082, 0], [10019319847023731927179170915906277160, 0], [0, 0], [0, 0], [0, 0], [6752194341956980939463188758869762192, 0], [0, -268784265335935982707564190482075992], [0, 0], [0, 0], [0, 0], [0, 380485955744175725990119150273440200], [0, -152042634827345531058116026409269222], [-60874297431961556748194869725436744690, 0], [0, 0], [0, 694676695241122437324558808789778610], [64663653515752041376765130433918798212, 0], [0, 0], [0, 960138855778750627158287991871521654], [0, 0], [0, 0], [0, 0], [0, 1508548311160740310014081481586646540], [-158777676534299042771114370689172350450, 0], [0, 0], [0, 0], [0, -1701760767081316220527673361954848710], [0, 0], [533758561042729698568858644308786440174, 0], [0, 0], [0, 4905414305530006997242020560786425758], [0, -6372909737163196376481582242400001676], [1042268443669549123866356182208378222096, 0], [0, 0], [-975226818685261553400224612330589612548, 0], [973549236925671610996579601613057059230, 0], [0, 0], [0, 7308272891812752696584207523291957920], [0, 0], [0, 17684201614338817039767600881289782298], [0, 0], [0, 0], [0, 25878629916142990397572275593666152450], [0, 0], [-3225112079506207226892160890831682599580, 0], [0, 0], [0, -35453038807789696703424111520813758740], [0, 0], [0, 3601518607772664336911054530534927716], [0, 0], [0, -56198618630034295366185693349056370930], [0, 0], [-7721009846550781596891610292498608526650, 0], [0, 0], [-5693202939129625574357886186363351330680, 0], [0, 0], [-11663689070530980102908682064618551459560, 0], [0, 0], [-8710303386481169320111647727401781522916, 0], [0, 0], [0, 140345256767193039310354747489501339552], [0, 0], [0, 0], [0, 0], [-22204544549402915594618981220666035342980, 0], [0, 0], [0, 0], [0, 0], [49856928328721819478388893268718897255068, 0], [0, -176046693665854314522496771433084944188]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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