
// Make newform 21.3.f.c in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_f_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_3_f_c();".
// 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_3_f_c();".
// 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_f();"
function MakeCharacter_21_f()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [6, 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_f_Hecke();"
function MakeCharacter_21_f_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 := 3;
    raw_aps := [[0, 2], [-1, -1], [-4, 2], [0, -7], [-10, 10], [-7, 14], [-4, -4], [38, -19], [0, -40], [16, 0], [3, 3], [0, -5], [-14, 28], [-19, 0], [-60, 30], [32, -32], [24, 24], [24, -12], [-59, 59], [-26, 0], [-11, -11], [0, -47], [14, -28], [136, -68], [28, -56], [-74, -74], [10, -5], [0, 212], [-17, 17], [142, 0], [-145, 0], [-172, 86], [116, -116], [-49, 98], [0, -124], [46, -46], [108, 108], [0, 58], [154, -308], [-144, 72], [-10, 10], [-189, 378], [0, 2], [235, -235], [100, 0], [-116, -116], [2, 0], [196, -392], [94, 94], [10, -5], [0, 170], [142, 0], [-88, -88], [168, -336], [-508, 254], [-136, 136], [-130, -130], [-424, 212], [-395, 395], [100, 0], [-207, -207], [-140, 280], [-105, 210], [318, 318], [234, -117], [0, -292], [0, -5], [-439, 0], [-220, 220], [196, -392], [178, 178], [0, -292], [311, 311], [0, 205], [-523, 0], [-88, 44], [74, -74], [374, -187], [0, 128], [171, 171], [238, -476], [107, 0], [-262, 262], [21, -42], [360, -180], [0, 212], [-782, 0], [0, -677], [-280, 560], [443, 0], [52, -26], [-32, -32], [67, -67], [-68, 0], [0, -509], [-378, 756], [1004, -502], [248, 248], [-1138, 569], [0, 121], [926, 0], [662, -662], [-186, -186], [0, 758], [865, -865], [619, 619], [-196, 392], [-284, 142], [284, -284], [343, -686], [10, -5], [-878, 878], [-194, 0], [353, 353], [-250, 0], [1124, -1124], [329, -658], [626, 626], [0, 1010], [-908, 0], [-417, -417], [-1027, 0], [-648, 324], [-976, 976], [654, -327], [352, 0], [0, 1150], [-1124, 562], [-119, 238], [1438, -719], [-311, 311], [394, 0], [0, 79], [-250, 0], [-1096, 548], [497, -994], [-130, -130], [-144, -144], [784, -1568], [1418, -1418], [-504, 1008], [0, -250], [-206, 206], [1234, 0], [199, 199], [-280, 560], [-161, 322], [-368, -368], [-704, 352], [0, -670], [0, 394], [-672, 1344], [737, 0], [-844, 422], [235, -235], [-740, 0], [0, -1517], [1284, -642], [483, -966], [-228, -228], [0, 338], [-1244, 0], [-1741, 0], [1480, -740], [-262, 262], [640, 640], [67, -67], [-571, -571]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_3_f_c();".
// 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_3_f_c(:prec:=2)
    chi := MakeCharacter_21_f();
    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_21_3_f_c();".
// 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_3_f_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![4, -2, 1]>],Snew);
    return Vf;
end function;