
// Make newform 138.4.a.d in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_138_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_138_4_a_d();".
// 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_138_4_a_d();".
// 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 := [-69, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [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_138_a();"
function MakeCharacter_138_a()
    N := 138;
    order := 1;
    char_gens := [47, 97];
    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_138_a_Hecke(Kf)
    return MakeCharacter_138_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 := [[-2, 0], [3, 0], [1, -1], [14, 0], [-1, 1], [24, 2], [-2, 2], [39, -1], [23, 0], [92, 10], [154, -2], [185, -3], [6, 24], [93, -7], [-264, -12], [-463, -5], [-396, 0], [-21, -37], [-217, 51], [312, -36], [-176, 46], [-254, 16], [497, 55], [0, -36], [-392, -26], [-496, 58], [408, -70], [617, -65], [-293, -41], [244, 8], [-504, -4], [-42, 126], [-54, 42], [-16, 36], [-691, 139], [-1830, 2], [-1655, -11], [2298, 110], [1792, -4], [-72, -138], [3806, -2], [-2501, 103], [-4462, -38], [2534, 168], [-1386, 240], [2510, -84], [150, -118], [290, -270], [-1113, -27], [-985, -237], [906, -96], [3804, -156], [-3636, -166], [-3979, -137], [166, -328], [2900, 256], [2998, 80], [-1324, 120], [-3120, -178], [-2148, -120], [3969, -175], [3637, -121], [4970, 54], [-1864, -80], [7596, -130], [-1828, -350], [458, -354], [6858, 260], [-478, -326], [-2278, -336], [-190, -260], [-3498, 414], [-3314, 196], [5717, -63], [-2169, -121], [-7044, 48], [-2313, -195], [11642, -12], [-9198, 54], [-2202, 320], [-7155, -21], [-2703, 785], [-6966, -354], [120, -1006], [-4588, -180], [8672, 100], [4622, -680], [-10900, -222], [-8286, 252], [8184, 488], [5005, -121], [5740, -772], [6672, 536], [1926, 102], [13710, 2], [-12102, -522], [18216, -90], [15350, -74], [-769, 651], [-326, -1352], [-6734, 70], [17827, -343], [-7361, -763], [-3490, 730], [10487, 171], [-9812, -146], [12806, 286], [8118, 744], [-848, 1400], [-6156, 14], [-10354, -522], [9107, -717], [3684, -600], [-6087, 1001], [-4408, -6], [-14914, -950], [-9267, 641], [-5848, -632], [17316, -426], [9753, 171], [-6209, -545], [-11064, 1034], [-8511, 855], [29142, -138], [-346, -174], [-14397, -483], [-14599, -183], [3960, -792], [10906, 820], [9837, 1157], [18336, 236], [-7824, 12], [7432, 1438], [979, 1087], [-7866, 840], [-2568, 794], [-1849, -1679], [14365, 337], [-16945, 13], [7278, -768], [9242, -546], [6756, -1554], [970, -1226], [-15387, -1137], [-8774, -608], [16320, 108], [1230, 1280], [-16038, 0], [36468, -352], [15616, 1784], [-3008, 934], [-14496, 228], [-28982, 502], [-22572, -888], [12249, -31], [-14086, 406], [-2770, 1332], [21210, -1332], [-13374, -2128], [-30915, -1149], [4098, 2298], [2110, -1318], [-22414, -498], [18079, 509], [35730, -6], [-12102, 1206], [5826, -82], [246, -268]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_138_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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