
// Make newform 39.4.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// 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_b();".
// 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_b();".
// 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, 0, 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_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)
    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+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 := 4;
    raw_aps := [[1, 1], [-3, 0], [12, -2], [0, -2], [-22, -12], [-13, 0], [82, 4], [24, 2], [4, 48], [202, -24], [20, -26], [-50, 28], [100, 94], [-308, 52], [-162, 32], [-82, -120], [70, 40], [314, 136], [-236, -170], [214, -84], [-450, 76], [-216, -88], [-694, 64], [480, -190], [-266, -220], [-182, 28], [-280, 436], [984, 40], [-30, 280], [702, 232], [24, -340], [-736, -144], [164, 190], [460, -40], [-1080, 290], [-420, 114], [-322, -528], [-1976, 238], [310, -112], [-2862, 260], [1572, 496], [1286, -288], [1736, -208], [954, -392], [-4252, 102], [2680, -100], [-3852, 48], [4436, 174], [-306, -620], [-686, 632], [3118, -660], [1942, 876], [2766, 892], [5256, 312], [910, -572], [2628, -840], [-3606, -408], [856, -1378], [-1274, -144], [-2988, -290], [4236, 564], [-5016, -902], [316, 986], [2484, 896], [-1290, 816], [-4692, -378], [6540, 254], [5182, 1144], [5324, 1640], [382, -1084], [5252, -498], [-2490, -1920], [264, -2400], [178, 1200], [4240, 1770], [7450, 696], [7990, -420], [-9730, -612], [-2544, 1642], [1254, -348], [-1312, 544], [-8350, -1736], [-6470, 2024], [5990, 776], [-4032, -1836], [7264, -2952], [6984, -1838], [-5114, 1036], [2692, 1630], [-5680, 486], [-640, -2232], [-2630, 1820], [-12300, -1642], [5968, 568], [-11892, 254], [-340, -2384], [-5020, -1330], [11398, 8], [-5020, 56], [-2078, 1200], [-9036, 1240], [-6480, -2942], [5780, -3384], [590, 3780], [9652, -1380], [5522, -4296], [-1278, -96], [16920, -774], [-2388, 736], [1126, -3008], [-10696, 3480], [-12762, 3580], [10316, -3554], [19252, -1094], [10404, -1294], [-6974, -4588], [-3628, 502], [-5904, 1976], [-886, 3088], [-11748, -584], [30, -636], [-12246, -5392], [-1850, 208], [4858, -1380], [13084, -2914], [-9466, -1524], [-22518, 3616], [2252, 1656], [-14536, 1580], [8794, 5968], [-916, -4478], [-11498, -2628], [17056, -1428], [3594, -5400], [-7836, -5754], [-6150, -6000], [23512, -2678], [21020, 1298], [-8850, 8228], [-9114, 3928], [20072, -1910], [-26624, 4402], [14912, -3108], [-31294, 3636], [15522, 6056], [19754, 6452], [-14642, -1420], [10510, 4148], [-7692, -10136], [1162, -848], [-1042, -9548], [8566, 7172], [-13260, 356], [-1240, -8472], [-3932, 1452], [26104, -1704], [10144, 2632], [-3772, 494], [-21418, 2984], [9928, 298], [22766, 2496], [-11434, 524], [20140, -6802], [12268, 3208], [-24496, -6710], [-5222, 14060], [-32864, 6392], [1854, -4480]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_39_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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