
// Make newform 1008.3.f.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1008_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1008_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_1008_3_f_g();".
// 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_f_g();".
// 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 := [4, 0, 2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 4, 0, 1], [1, 0, 1, 0], [0, 0, 0, -3]];
    Rf_basisdens := [1, 2, 1, 2];
    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_1008_f();"
function MakeCharacter_1008_f()
    N := 1008;
    order := 2;
    char_gens := [127, 757, 785, 577];
    v := [2, 2, 2, 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_1008_f_Hecke();"
function MakeCharacter_1008_f_Hecke(Kf)
    N := 1008;
    order := 2;
    char_gens := [127, 757, 785, 577];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [-1, 0, 0, 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 := 3;
    raw_aps := [[0, 0, 0, 0], [0, 0, 0, 0], [0, -1, 2, 0], [-2, -2, -1, 1], [-6, 0, 0, -1], [0, -2, 8, 0], [0, 11, 2, 0], [0, -8, 2, 0], [6, 0, 0, -3], [-30, 0, 0, 0], [0, 12, 12, 0], [-20, 0, 0, 12], [0, 7, -14, 0], [32, 0, 0, 10], [0, 4, 28, 0], [54, 0, 0, 4], [0, 20, -28, 0], [0, -4, 4, 0], [-44, 0, 0, 4], [-30, 0, 0, 19], [0, 26, 4, 0], [-32, 0, 0, -24], [0, 20, 32, 0], [0, 21, 54, 0], [0, 10, 44, 0], [0, -27, -42, 0], [0, 52, -28, 0], [-42, 0, 0, 1], [-2, 0, 0, 8], [90, 0, 0, 4], [-56, 0, 0, 8], [0, 12, 24, 0], [-30, 0, 0, 32], [0, -8, 116, 0], [-126, 0, 0, 20], [-4, 0, 0, -18], [0, -36, 84, 0], [124, 0, 0, 12], [0, -32, -68, 0], [0, -3, -18, 0], [-102, 0, 0, -21], [0, -38, -16, 0], [6, 0, 0, 25], [28, 0, 0, -12], [150, 0, 0, -8], [0, 68, -2, 0], [-64, 0, 0, -10], [0, -36, 18, 0], [0, -24, -132, 0], [0, -6, -72, 0], [-210, 0, 0, -36], [150, 0, 0, -31], [0, 2, 100, 0], [0, -32, 16, 0], [0, -21, 138, 0], [-246, 0, 0, -37], [0, -59, -194, 0], [0, -52, 52, 0], [104, 0, 0, -44], [66, 0, 0, 20], [0, -24, 198, 0], [0, -167, -74, 0], [0, 24, -54, 0], [0, 76, -56, 0], [0, -116, -136, 0], [18, 0, 0, -132], [-256, 0, 0, 18], [190, 0, 0, 24], [66, 0, 0, 91], [0, 20, -164, 0], [0, -139, 182, 0], [-102, 0, 0, -1], [0, -124, 10, 0], [-118, 0, 0, -72], [128, 0, 0, -54], [0, -172, 116, 0], [78, 0, 0, 72], [0, -48, 348, 0], [-222, 0, 0, 112], [0, -22, 28, 0], [0, -140, 28, 0], [32, 0, 0, -76], [318, 0, 0, -63], [0, 228, 96, 0], [0, -156, 6, 0], [-522, 0, 0, 69], [210, 0, 0, 92], [382, 0, 0, 24], [0, 139, -38, 0], [-152, 0, 0, 72], [0, 144, -168, 0], [0, -148, -112, 0], [164, 0, 0, 38], [198, 0, 0, 105], [-136, 0, 0, -146], [0, 92, 164, 0], [0, -143, -242, 0], [0, 169, 70, 0], [0, 272, 100, 0], [-376, 0, 0, 156], [436, 0, 0, 72], [-750, 0, 0, -20], [0, -84, 360, 0], [-690, 0, 0, 4], [316, 0, 0, -12], [0, -16, -248, 0], [0, 168, -168, 0], [0, 307, -110, 0], [-6, 0, 0, 223], [0, -264, 48, 0], [0, -268, 202, 0], [38, 0, 0, 56], [-54, 0, 0, -96], [0, -88, -92, 0], [160, 0, 0, 228], [-66, 0, 0, 224], [0, -144, 30, 0], [0, 384, 0, 0], [-138, 0, 0, 140], [654, 0, 0, -59], [0, 304, 236, 0], [292, 0, 0, -212], [0, 15, -678, 0], [-1026, 0, 0, 49], [0, 24, 84, 0], [234, 0, 0, 20], [-14, 0, 0, -48], [0, -244, -352, 0], [0, -60, 156, 0], [0, -186, 480, 0], [-1028, 0, 0, 72], [246, 0, 0, 241], [-244, 0, 0, -46], [-974, 0, 0, 72], [0, -89, 634, 0], [0, 260, 304, 0], [0, -37, 362, 0], [0, -88, 76, 0], [0, 279, 186, 0], [198, 0, 0, 20], [0, 216, 228, 0], [-882, 0, 0, -92], [280, 0, 0, -156], [810, 0, 0, -37], [0, 286, -280, 0], [0, -152, 580, 0], [0, -152, -316, 0], [0, 95, 170, 0], [0, -88, -14, 0], [-66, 0, 0, 101], [-658, 0, 0, -48], [0, -39, -138, 0], [1208, 0, 0, 66], [0, -32, 508, 0], [680, 0, 0, 30], [-150, 0, 0, -69], [-484, 0, 0, 142], [0, -445, 266, 0], [0, -112, 280, 0], [0, -29, -62, 0], [258, 0, 0, 39], [954, 0, 0, -96], [-872, 0, 0, -92], [0, 512, -100, 0], [-222, 0, 0, -372], [0, -472, 92, 0], [-916, 0, 0, 30], [0, -564, -324, 0], [-664, 0, 0, 276], [0, 197, -202, 0], [-1506, 0, 0, -51], [0, 48, -108, 0], [-450, 0, 0, 21], [752, 0, 0, -84], [0, -100, -170, 0], [0, 53, -562, 0], [-716, 0, 0, 0], [78, 0, 0, 44], [0, -76, -284, 0], [0, 12, 780, 0], [1408, 0, 0, -60], [0, -352, 344, 0], [1160, 0, 0, 12], [0, -449, -278, 0], [798, 0, 0, 37], [0, 85, 70, 0], [286, 0, 0, 208], [0, -464, 236, 0], [8, 0, 0, -100], [0, 316, 328, 0], [0, -74, -1060, 0], [210, 0, 0, -1], [416, 0, 0, 102], [0, 185, 350, 0], [294, 0, 0, -327], [0, -439, -298, 0], [100, 0, 0, 52], [-472, 0, 0, -228], [0, -561, -150, 0], [0, 44, -160, 0], [1206, 0, 0, -120], [0, -620, 254, 0], [0, -418, -104, 0], [0, 274, 20, 0], [0, -200, 520, 0], [0, 13, -146, 0], [0, 268, -514, 0], [750, 0, 0, -247], [1506, 0, 0, 40], [0, 328, -958, 0], [1118, 0, 0, 0], [0, 477, 318, 0], [-508, 0, 0, 258], [0, -616, 44, 0], [0, 212, -544, 0], [0, -64, -680, 0], [1124, 0, 0, -14], [0, 207, -558, 0], [546, 0, 0, -97], [-2130, 0, 0, 112], [514, 0, 0, 216], [0, 420, -420, 0], [810, 0, 0, 12], [-700, 0, 0, -218], [0, -4, -40, 0], [508, 0, 0, 36], [0, -247, 902, 0], [438, 0, 0, -3], [0, -116, -1372, 0], [1446, 0, 0, 185], [226, 0, 0, 136], [0, 384, 666, 0], [-820, 0, 0, 130], [654, 0, 0, 220], [0, 344, 682, 0], [0, -840, 276, 0], [0, 170, -188, 0], [930, 0, 0, 240], [1158, 0, 0, 109], [0, 692, 116, 0], [-2034, 0, 0, -63], [0, 656, -92, 0], [0, 76, 1226, 0], [-596, 0, 0, 340], [0, 393, -690, 0], [0, 736, 292, 0], [0, -124, 748, 0], [0, -728, -32, 0], [-548, 0, 0, 116], [-1590, 0, 0, 51], [478, 0, 0, -448], [0, 561, 582, 0], [966, 0, 0, -243], [0, 470, -788, 0], [0, 173, 278, 0], [906, 0, 0, -141], [-304, 0, 0, 396], [0, 336, -234, 0], [0, 579, -174, 0], [0, -24, 168, 0], [1264, 0, 0, -224], [1638, 0, 0, 97], [0, -1034, -208, 0], [0, 232, -4, 0], [0, -291, 342, 0], [0, 96, 804, 0], [-6, 0, 0, 40], [0, 321, -1074, 0], [-188, 0, 0, 656], [-450, 0, 0, 340], [0, -16, -284, 0], [-664, 0, 0, -250], [0, -390, 492, 0], [-1468, 0, 0, 42], [0, -582, 564, 0], [0, -236, -1228, 0], [-714, 0, 0, 421], [-1292, 0, 0, -364], [628, 0, 0, -364], [0, -224, 856, 0], [0, 208, 424, 0], [2440, 0, 0, 140], [0, 944, 200, 0], [0, 556, 884, 0], [0, 808, -364, 0], [42, 0, 0, -181], [862, 0, 0, 552], [1470, 0, 0, -160], [0, -668, 1220, 0], [0, -337, 578, 0], [-2610, 0, 0, 128], [0, -148, -1456, 0], [1914, 0, 0, -136], [0, 460, -1364, 0], [-2038, 0, 0, -152], [0, 41, 134, 0], [0, 356, -362, 0], [0, 343, -854, 0], [0, 592, -644, 0], [0, -40, -1772, 0], [0, 460, -88, 0], [2562, 0, 0, -120], [2536, 0, 0, 132]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1008_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1008_3_f_g();".
// 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_f_g(:prec:=4)
    chi := MakeCharacter_1008_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(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_f_g();".
// 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_f_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1008_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![36, 0, 36, 0, 1]>,<11,R![18, 12, 1]>],Snew);
    return Vf;
end function;