
// Make newform 138.4.a.f in Magma, downloaded from the LMFDB on 09 September 2025.
// 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_f();".
// 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_f();".
// 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 := [-84, -40, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 2, 0], [-27, -4, 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_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 := 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]];
    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, 0], [3, 0, 0], [7, 0, 1], [3, 1, -1], [6, -5, -2], [-2, 0, -4], [13, 9, 7], [-11, 0, -7], [-23, 0, 0], [-16, -18, 2], [10, 22, 18], [-34, -21, -12], [-154, 38, 4], [-103, -14, -35], [-230, -26, -10], [-119, 2, 51], [-422, -32, -22], [-58, 13, -44], [167, 6, 51], [-504, -6, 0], [234, -24, -8], [149, 47, -31], [-338, 29, 78], [-117, 57, 21], [700, 16, -6], [330, -8, -20], [629, -71, 1], [-26, -31, 6], [1532, -1, 50], [495, -31, 17], [406, 30, -130], [360, -6, 60], [1847, -1, -47], [316, 80, 88], [1723, -40, -51], [-980, -156, -148], [278, 215, 20], [-1390, 160, 94], [696, 38, 248], [1600, 142, 2], [-318, 22, -86], [700, -17, -30], [-414, 100, -38], [-1298, -334, -320], [1136, 212, 346], [-1199, -169, 229], [-1914, 20, 114], [-2616, 268, 152], [1580, -307, -92], [3804, -113, 26], [-3002, -128, 308], [1224, 252, 336], [1196, 400, 274], [-800, -145, -384], [-726, -406, -544], [3322, -334, -78], [-472, -104, -294], [2114, 174, -198], [3346, -366, -52], [1483, 397, 413], [87, 380, -45], [597, 218, -409], [-622, -420, 54], [2678, -564, -22], [-5506, -110, 100], [2620, -286, -522], [-5758, 336, 270], [2868, 170, 282], [2646, 142, -314], [654, -38, 260], [-9088, 474, 86], [-4098, -528, -258], [429, 195, -791], [4686, -629, -76], [485, -1184, -671], [2546, 170, 202], [6589, 784, 743], [222, 592, 776], [-3487, 1217, 331], [9698, 418, 52], [3750, 129, 6], [-3228, 65, -222], [4992, -630, 336], [-3442, 178, 868], [1300, 860, 700], [152, -732, -172], [-750, -2, -656], [10416, -416, 62], [-4296, 384, -606], [-8604, -412, -852], [1404, 803, -268], [-6516, 512, -124], [8128, 156, 416], [-11158, -262, -302], [4258, -24, -274], [9548, 122, -812], [-6552, -186, -366], [-7649, -253, -207], [-11637, 934, 431], [-3238, -514, -1432], [5134, -1460, -342], [13345, -624, 211], [5100, 989, 20], [-211, 501, -745], [-137, -442, 283], [-1158, -1194, -1868], [12264, -146, 340], [-4378, 1706, -524], [3336, -1600, -1480], [-16638, -250, -732], [9360, -56, -88], [-7010, -571, -788], [-11457, -195, 9], [-7969, -836, 571], [-14739, 301, 137], [-1011, -203, 343], [-3071, -1398, -931], [1446, -80, 2002], [2230, -56, -1240], [-9524, -593, 356], [-18414, 141, 1120], [-13110, -1834, -2388], [-10589, -1340, -1819], [20860, -398, -664], [1442, 1056, 1182], [-11397, -78, -795], [5272, -1471, -1010], [3990, 1092, 618], [-18777, -87, -941], [5976, -1579, -1074], [-9296, 1908, 1316], [13986, -702, -270], [12043, 259, 1239], [436, 1939, 42], [-3144, -1206, -90], [4646, -50, 1156], [-1081, 858, 17], [7105, -1658, -1539], [13261, 200, -297], [620, 2234, 418], [6842, 2472, 3390], [12254, 608, -2960], [22954, -674, -366], [-19164, 579, 612], [13258, -2150, -1800], [-13762, -670, -506], [-7250, 1536, 1808], [906, 192, -648], [5364, -2320, -2520], [30682, 1008, -650], [8614, -2438, 72], [-6965, -2171, 581], [-914, -80, -1086], [-1422, 3390, 2430], [-4269, 872, 1023], [-21676, 1878, 116], [6805, 1019, -1367], [-2578, 1610, 2260], [-33010, 28, -428], [5855, -1996, 145], [-21662, -1358, 1034], [-3071, -1563, 715], [11782, 3134, 1990], [-21480, -295, 1208], [-2317, 731, 397], [8172, 1242, 3540], [-7262, -3174, 818], [-15706, 1426, 1300]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_138_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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