
// Make newform 63.6.a.f in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_63_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_63_6_a_f();".
// 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_63_6_a_f();".
// 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 := [-14, -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_63_a();"
function MakeCharacter_63_a()
    N := 63;
    order := 1;
    char_gens := [29, 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;

function MakeCharacter_63_a_Hecke(Kf)
    return MakeCharacter_63_a();
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 := 6;
    raw_aps := [[-4, -1], [0, 0], [14, -10], [49, 0], [-260, 124], [-238, 126], [-938, 76], [-1624, -18], [-760, -568], [-3222, -252], [-280, 540], [2846, 540], [2478, 1092], [884, -4788], [-3976, -3748], [-4838, 208], [20944, 2050], [-29974, -4806], [13364, -1944], [-50808, 4200], [11354, -5256], [18176, 14904], [-50904, -15750], [-53242, 22208], [5978, 8820], [91630, -27434], [30464, -10476], [76436, -21904], [-100330, 3348], [-35226, -23940], [-105040, 8064], [273224, -23278], [-217946, 51448], [168728, 46242], [2474, -33496], [38984, 2592], [257474, -81234], [-457684, -5508], [-120624, 48468], [-31234, 20906], [-96020, 71440], [-165382, 103950], [229232, 50504], [-49510, -158436], [6522, 83832], [-217168, 118476], [515300, 54936], [812336, -65016], [463624, -12398], [-186886, 284742], [-384682, 117872], [207408, 10920], [395402, 237924], [1615992, 13902], [-763042, 8432], [-164984, 91432], [-1915298, 25738], [169904, -440640], [-210874, 571104], [-168954, -351456], [-962416, 132462], [-1047522, 462462], [294056, 359982], [-23576, 542800], [-947422, -579492], [-2999518, 13208], [-824428, 418860], [-1308214, 337932], [462076, -191228], [1520498, -388458], [-2943458, -125840], [-1377688, -87760], [-661024, -259272], [-2821066, -313704], [2299796, -393372], [3722600, 91004], [-2009246, -610892], [2912546, 505494], [-165610, 385076], [-3702286, -194508], [92400, -47586], [2764022, -976752], [2327632, -796280], [2218202, -591444], [2333240, 782064], [-637228, -800584], [1543134, 1268400], [-1542334, 780732], [2402190, -67662], [-1879120, 734832], [996296, 606554], [-412328, 661828], [-432760, -1113768], [2113284, -1755096], [-2196316, -257256], [2435832, 1921752], [7195118, -1041526], [3041710, -1874300], [6175568, -879498], [7100510, -1550520], [-1785796, 2204244], [1072210, 799048], [7662928, -1286090], [5789486, 1296644], [-4339852, 817812], [-4014430, 415584], [-4086600, -1878954], [-373954, -1784296], [-12553976, -476240], [5229434, -3251808], [-2752288, -2468736], [-4030018, 1492668], [-9635586, 84], [4784864, -2256498], [-10271224, 859320], [-6019322, -1972700], [-9083200, -1494486], [-17863664, 280468], [6647882, 1566620], [-8726412, 2978556], [-1201102, 2807442], [-1517710, 2907576], [10551926, 707522], [-5738972, 4306888], [1746248, 1256958], [13224762, -750708], [-1749730, -1659636], [6002696, -2943580], [18698960, 33012], [12918122, -3672918], [-24555796, -79812], [-683832, 1063104], [-10469392, 4022712], [17946350, 896364], [9496718, 3792044], [2259530, -3671892], [-7082306, -5491298], [-15421672, 3121686], [-1781010, 1816794], [2094910, -5095148], [-1412656, 2319030], [5845178, -1048720], [-6722344, 1254528], [7251396, -4052496], [-10499062, 1136682], [10312512, 309372], [10630634, 4184838], [-15262898, 5899564], [2753576, -4137102], [-31247248, -1695016], [22767686, -3126276], [-1762530, 154056], [14697524, -1551312], [6522992, 1980668], [10979564, -1603152], [-2718144, -8821008], [11778680, 2356992], [-6726874, -925732], [-28148806, -2751336], [-7422842, 1426774], [23082356, 1064300], [-10610490, 9684528], [2057288, 203112], [-26226592, 5484434], [28487038, -8415080], [20543152, -9011780], [25179680, 2830032], [45264002, 813618]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_63_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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