
// Make newform 1008.3.cg.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1008_cg();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1008_cg_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_1008_3_cg_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_1008_3_cg_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_1008_cg();"
function MakeCharacter_1008_cg()
    N := 1008;
    order := 6;
    char_gens := [127, 757, 785, 577];
    v := [6, 6, 6, 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_1008_cg_Hecke();"
function MakeCharacter_1008_cg_Hecke(Kf)
    N := 1008;
    order := 6;
    char_gens := [127, 757, 785, 577];
    char_values := [[1, 0], [1, 0], [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, 0], [0, 0], [-6, 3], [-5, -3], [-15, 15], [8, -16], [-6, -6], [12, -6], [0, 0], [9, 0], [7, 7], [0, -10], [-6, 12], [74, 0], [0, 0], [33, -33], [9, 9], [104, -52], [-76, 76], [84, 0], [-36, -36], [0, -43], [69, -138], [84, -42], [-107, 214], [84, 84], [80, -40], [0, -93], [8, -8], [-42, 0], [-35, 0], [198, -99], [-96, 96], [-106, 212], [0, -186], [79, -79], [-12, -12], [0, -208], [144, -288], [264, -132], [-90, 90], [6, -12], [0, 312], [185, -185], [-330, 0], [-4, -4], [248, 0], [-111, 222], [-51, -51], [380, -190], [0, 270], [-228, 0], [257, 257], [3, -6], [-132, 66], [-186, 186], [195, 195], [-106, 53], [-380, 380], [-300, 0], [-118, -118], [315, -630], [100, -200], [-102, -102], [-246, 123], [0, 117], [0, 40], [91, 0], [-210, 210], [176, -352], [-228, -228], [0, -492], [189, 189], [0, -170], [-82, 0], [252, -126], [306, -306], [-296, 148], [0, 132], [-209, -209], [-372, 744], [752, 0], [-162, 162], [196, -392], [-450, 225], [0, 297], [492, 0], [0, 443], [240, -480], [82, 0], [312, -156], [198, 198], [-317, 317], [27, 0], [0, -446], [282, -564], [-114, 57], [522, 522], [808, -404], [0, -74], [934, 0], [843, -843], [549, 549], [0, 222], [440, -440], [379, 379], [609, -1218], [-816, 408], [-516, 516], [143, -286], [750, -375], [892, -892], [1224, 0], [232, 232], [-1115, 0], [-384, 384], [-4, 8], [516, 516], [0, 75], [642, 0], [-162, -162], [13, 0], [1458, -729], [969, -969], [116, -58], [-597, 0], [0, 830], [396, -198], [29, -58], [-212, 106], [-334, 334], [84, 0], [0, -359], [-80, 0], [936, -468], [447, -894], [420, 420], [824, 824], [351, -702], [-168, 168], [204, -408], [0, 285], [-274, 274], [429, 0], [-546, -546], [-150, 300], [-576, 1152], [-252, -252], [524, -262], [0, 390], [0, 544], [-108, 216], [-322, 0], [816, -408], [-1100, 1100], [900, 0], [0, -1718], [1716, -858], [553, -1106], [-207, -207], [0, -162], [954, 0], [751, 0], [-330, 165], [-18, 18], [-570, -570], [703, -703], [124, 124], [-343, 0], [-1866, 933], [-585, 585], [-162, 324], [0, -882], [-5, 5], [-773, -773], [288, -576], [70, 0], [-717, 717], [212, -424], [800, -400], [0, -481], [-411, 822], [-1264, 0], [1080, -540], [-1182, 1182], [813, 813], [-1774, 1774], [-192, -192], [0, -257], [690, 690], [-2058, 1029], [-39, 0], [0, 1666], [-2544, 1272], [-819, 819], [-654, -654], [2042, -2042], [0, 916], [438, -876], [-2088, 1044], [-999, 999], [212, -424], [248, -124], [-308, -308], [888, -1776], [705, 705], [640, -320], [0, 2055], [-1554, 0], [-98, -98], [0, -2186], [-51, 102], [749, 0], [-42, 21], [-30, -30], [-1976, 988], [-1241, 1241], [228, 228], [0, -2112], [390, 0], [0, 464], [-1292, 2584], [0, 540], [0, 457], [879, -1758], [-1172, 0], [0, 0], [2700, -2700], [-1614, 807], [0, 1434], [-64, 64], [-318, -318], [-859, 0], [258, -258], [1018, -2036], [1044, 1044], [1446, -723], [0, 1629], [-1266, 0], [-60, 120], [-1422, 1422], [1908, -954], [999, 999], [0, 1412], [-888, 1776], [-1548, 774], [-1451, 2902], [-216, -216], [800, -800], [990, 0], [148, 0], [0, 0], [1188, -1188], [-596, 1192], [-1119, -1119], [0, 1917], [1630, -1630], [-926, -926], [-513, 1026], [-1338, 669], [806, -806], [-2733, 0], [-1012, -1012], [-634, 1268], [384, 384], [-1064, 532], [-753, 0], [-1032, 2064], [-2366, 0], [-21, 21], [-312, 156], [3034, -3034], [-773, -773], [0, 13], [1221, -2442], [952, -476], [0, 2949], [-88, 88], [0, -1090], [3240, -1620], [-972, -972], [491, -491], [-204, 408], [-322, 644], [1504, -752], [0, 2658], [2195, -2195], [1527, 0], [-1141, -1141], [1908, -3816], [-2454, 2454], [-549, -549], [0, 1380], [-1668, 3336], [1784, 0], [-1461, -1461], [-296, 148], [1260, -2520], [3096, -1548], [1210, -2420], [3928, -1964], [0, 369], [-223, 223]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1008_cg_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1008_3_cg_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_1008_3_cg_a(:prec:=2)
    chi := MakeCharacter_1008_cg();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1008_3_cg_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_1008_3_cg_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1008_cg();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![27, 9, 1]>,<11,R![225, 15, 1]>,<13,R![192, 0, 1]>],Snew);
    return Vf;
end function;