
// Make newform 39.4.a.c in Magma, downloaded from the LMFDB on 03 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_39_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_39_4_a_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_39_4_a_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 := [-8, -16, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-10, -2, 1]];
    Rf_basisdens := [1, 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_39_a();"
function MakeCharacter_39_a()
    N := 39;
    order := 1;
    char_gens := [14, 28];
    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_39_a_Hecke( : Kf := Kf)
    return MakeCharacter_39_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]];
    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 := 4;
    raw_aps := [[1, -1, 0], [3, 0, 0], [2, 0, -2], [8, 6, 0], [-8, 2, 6], [13, 0, 0], [-46, -8, 0], [28, -6, 16], [-24, 32, -8], [-10, 20, 8], [120, -54, -4], [150, -48, -28], [150, -4, 34], [-68, -60, 4], [-12, 54, -42], [-186, -108, -12], [-624, 82, 2], [78, 24, -28], [36, 42, -76], [-276, -134, 14], [2, -240, 12], [-16, 48, -24], [-272, 30, -10], [430, 116, 30], [1098, -48, -4], [414, -340, -128], [-752, 84, 76], [-636, -304, 40], [54, 288, -64], [330, 232, 68], [-448, 108, 72], [364, -192, 152], [918, 268, 146], [-308, 552, -116], [810, -176, -94], [1392, -234, 20], [478, -480, 172], [332, 222, -288], [-356, 230, -234], [-1818, 4, 248], [148, 452, 300], [-1594, -456, 348], [1256, -92, -12], [-1742, -72, 280], [-3534, 176, -26], [584, -108, 480], [252, 264, 272], [840, 282, -316], [-776, -506, 490], [-2818, -216, 192], [-638, -552, 380], [468, -1086, -138], [4890, 96, -196], [-2532, -20, -580], [-1222, 1256, -124], [1296, -28, -188], [2902, 436, -512], [160, 918, -272], [-2050, 24, -552], [2822, -612, -14], [1156, -636, 40], [858, 40, 290], [3620, 102, -420], [792, 720, 0], [3466, -48, 64], [1754, -120, 166], [284, 2034, 420], [-3470, 1248, -272], [132, -512, -232], [-2610, 2184, 716], [1966, 788, -342], [5196, -1074, -738], [-4248, -552, -148], [-3186, -312, -1456], [2212, -942, -728], [-1684, 970, 210], [-2426, -156, 44], [62, -1224, 588], [-1082, -1396, -474], [-2190, 1704, 428], [-5828, 2576, 624], [-3970, -72, -1248], [-11364, -214, -446], [7722, -120, -148], [2432, -756, -1620], [-9004, 624, -296], [6406, 76, 718], [2, 1080, 1980], [4858, 952, 238], [7248, 126, -760], [-7788, -876, -444], [6492, 1526, 1018], [-3392, 282, -1100], [-324, 2948, 868], [-2636, 1362, 196], [2656, -3452, -428], [-566, 3560, -978], [6210, 1392, 2316], [5612, 2232, -516], [5206, 528, 2104], [-8652, 1728, -628], [-4398, -2944, 466], [3108, -212, -844], [14098, -1648, -1380], [2324, -4668, 912], [7874, -2832, 528], [-56, -2314, -1986], [-9578, 2316, 350], [11744, -3396, 148], [7530, 4800, -544], [-1144, 504, -348], [13350, 2976, -340], [18694, -1940, -950], [7372, -6954, -596], [-5200, 918, -2124], [4754, 2920, 1704], [-13852, 1602, -636], [9832, -376, 552], [-7290, -3076, 1240], [1804, 1648, -104], [-2762, -792, 364], [-20502, 2184, 500], [-5978, 3804, 428], [-216, -878, 1310], [-188, 2442, 700], [542, 5532, 676], [19854, 120, -952], [7296, -1952, -2464], [-2824, 6060, 2496], [-18938, 3624, -896], [-556, -1146, 492], [13068, -886, 670], [-15016, -2268, -2460], [1302, -1344, 548], [8678, 3964, -2670], [-3998, -4056, 2080], [9274, -1376, 2722], [-8364, -5466, -460], [12958, 772, 1876], [-30414, 1424, 388], [-3196, -870, 3984], [15578, -5104, 506], [-896, -996, -2228], [-6536, 2970, -34], [11678, 720, 636], [852, 2014, 554], [-13754, -2472, 1108], [1634, 1824, -1532], [17364, -2352, 704], [4012, 4290, 4658], [4334, -1800, 708], [8938, 3080, -3036], [2364, -2556, 4016], [18288, -6172, -524], [12052, 4476, -1208], [18152, 1596, -308], [8536, -1080, -3392], [14670, 724, -1030], [-21598, -2328, -1068], [19810, -7464, -2518], [-4248, -7342, -4070], [5202, -6704, 1340], [12800, -10950, 804], [15444, 2248, 2216], [-13434, 12236, -26], [36964, -6522, -1894], [840, -12216, 3044], [510, -3072, -4240]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_39_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_39_4_a_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_39_4_a_c(:prec:=3)
    chi := MakeCharacter_39_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_39_4_a_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_39_4_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_39_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![24, -15, -2, 1]>],Snew);
    return Vf;
end function;